Small OpenGL based c++ rendering library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

matrix.cpp 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #include "matrix.h"
  2. namespace segl {
  3. Matrix::Matrix(int _m, int _n) {
  4. m = _m;
  5. n = _n;
  6. allocate();
  7. }
  8. Matrix::Matrix(int _m, int _n, const float **src) {
  9. m = _m;
  10. n = _n;
  11. allocate();
  12. copyFromSource(src);
  13. }
  14. Matrix::Matrix(Punkt3D d) {
  15. m = 3;
  16. n = 1;
  17. allocate();
  18. c[0][0] = d.x;
  19. c[1][0] = d.y;
  20. c[2][0] = d.z;
  21. }
  22. Matrix::Matrix(const Matrix& mat) {
  23. m = mat.m;
  24. n = mat.n;
  25. allocate();
  26. copyFromSource(mat.getMatrix());
  27. }
  28. void Matrix::allocate() {
  29. c = new float*[m];
  30. for(int i=0; i<m; i++) {
  31. c[i] = new float[n];
  32. for(int t=0; t<n; t++)
  33. c[i][t] = 0;
  34. }
  35. }
  36. void Matrix::copyFromSource(const float** src) {
  37. for(int i=0; i<m; i++) {
  38. for(int t=0; t<n; t++)
  39. c[i][t] = src[i][t];
  40. }
  41. }
  42. const float **Matrix::getMatrix() const {
  43. return (const float**)c;
  44. }
  45. bool Matrix::set(float d, int _m, int _n) {
  46. if(_m>=m||_n>=n)
  47. return false;
  48. c[_m][_n] = d;
  49. return true;
  50. }
  51. float Matrix::get(int _m, int _n) {
  52. if(_m>=m||_n>=n)
  53. return 0.0f;
  54. return c[_m][_n];
  55. }
  56. Matrix Matrix::operator*(const Matrix &d) {
  57. if(n!=d.m)
  58. return Matrix(1,1);
  59. Matrix erg(m, d.n);
  60. for(int a=0; a<m; a++) {
  61. for(int b=0; b<d.n; b++) {
  62. float zerg = 0.0f;
  63. for(int _c=0; _c<n; _c++) {
  64. zerg += c[a][_c] * d.c[_c][b];
  65. }
  66. erg.c[a][b] = zerg;
  67. }
  68. }
  69. return erg;
  70. }
  71. Matrix& Matrix::operator=(const Matrix& mat) {
  72. if(mat.m<=m&&mat.n<=n) {
  73. copyFromSource(mat.getMatrix());
  74. }
  75. return *this;
  76. }
  77. Matrix Matrix::operator+(const float &f) {
  78. Matrix tmp(*this);
  79. for(int i=0; i<m; i++) {
  80. for(int j=0; j<n; j++) {
  81. tmp.c[i][j] += f;
  82. }
  83. }
  84. return tmp;
  85. }
  86. Matrix Matrix::operator-(const float &f) {
  87. Matrix tmp(*this);
  88. for(int i=0; i<m; i++) {
  89. for(int j=0; j<n; j++) {
  90. tmp.c[i][j] -= f;
  91. }
  92. }
  93. return tmp;
  94. }
  95. Matrix Matrix::operator*(const float &f) {
  96. Matrix tmp(*this);
  97. for(int i=0; i<m; i++) {
  98. for(int j=0; j<n; j++) {
  99. tmp.c[i][j] *= f;
  100. }
  101. }
  102. return tmp;
  103. }
  104. Matrix Matrix::operator/(const float &f) {
  105. Matrix tmp(*this);
  106. for(int i=0; i<m; i++) {
  107. for(int j=0; j<n; j++) {
  108. tmp.c[i][j] /= f;
  109. }
  110. }
  111. return tmp;
  112. }
  113. int Matrix::getM() const {
  114. return m;
  115. }
  116. int Matrix::getN() const {
  117. return n;
  118. }
  119. void Matrix::print(std::string s) const {
  120. std::cout << "Matrix " << s << "(" << m << "," << n << ")" << std::endl;
  121. for(int a=0; a<m; a++) {
  122. for(int b=0; b<n; b++) {
  123. std::cout << std::setw(15) << c[a][b];
  124. }
  125. std::cout << std::endl;
  126. }
  127. }
  128. Matrix::~Matrix() {
  129. for(int i=0; i<m; i++) {
  130. delete[](c[i]);
  131. }
  132. delete[](c);
  133. }
  134. } // namespace segl