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 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /* libsegl - Sebas Extended GL Library
  2. * Collection of Opengl/3D-Math helpers
  3. *
  4. * Copyright (c) 2008 by Sebastian Lohff, seba@seba-geek.de
  5. * http://www.seba-geek.de
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Library General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Library General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Library General Public
  18. * License along with this library; if not, write to the
  19. * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
  20. * Boston, MA 02110-1301, USA.
  21. */
  22. #include "matrix.h"
  23. namespace segl {
  24. Matrix::Matrix(int _m, int _n) {
  25. m = _m;
  26. n = _n;
  27. allocate();
  28. }
  29. Matrix::Matrix(int _m, int _n, const float **src) {
  30. m = _m;
  31. n = _n;
  32. allocate();
  33. copyFromSource(src);
  34. }
  35. Matrix::Matrix(Punkt3D d) {
  36. m = 3;
  37. n = 1;
  38. allocate();
  39. c[0][0] = d.x;
  40. c[1][0] = d.y;
  41. c[2][0] = d.z;
  42. }
  43. Matrix::Matrix(const Matrix& mat) {
  44. m = mat.m;
  45. n = mat.n;
  46. allocate();
  47. copyFromSource(mat.getMatrix());
  48. }
  49. void Matrix::allocate() {
  50. c = new float*[m];
  51. for(int i=0; i<m; i++) {
  52. c[i] = new float[n];
  53. for(int t=0; t<n; t++)
  54. c[i][t] = 0;
  55. }
  56. }
  57. void Matrix::copyFromSource(const float** src) {
  58. for(int i=0; i<m; i++) {
  59. for(int t=0; t<n; t++)
  60. c[i][t] = src[i][t];
  61. }
  62. }
  63. const float **Matrix::getMatrix() const {
  64. return (const float**)c;
  65. }
  66. bool Matrix::set(float d, int _m, int _n) {
  67. if(_m>=m||_n>=n)
  68. return false;
  69. c[_m][_n] = d;
  70. return true;
  71. }
  72. float Matrix::get(int _m, int _n) {
  73. if(_m>=m||_n>=n)
  74. return 0.0f;
  75. return c[_m][_n];
  76. }
  77. Matrix Matrix::operator*(const Matrix &d) {
  78. if(n!=d.m)
  79. return Matrix(1,1);
  80. Matrix erg(m, d.n);
  81. for(int a=0; a<m; a++) {
  82. for(int b=0; b<d.n; b++) {
  83. float zerg = 0.0f;
  84. for(int _c=0; _c<n; _c++) {
  85. zerg += c[a][_c] * d.c[_c][b];
  86. }
  87. erg.c[a][b] = zerg;
  88. }
  89. }
  90. return erg;
  91. }
  92. Matrix& Matrix::operator=(const Matrix& mat) {
  93. if(mat.m<=m&&mat.n<=n) {
  94. copyFromSource(mat.getMatrix());
  95. }
  96. return *this;
  97. }
  98. Matrix Matrix::operator+(const float &f) {
  99. Matrix tmp(*this);
  100. for(int i=0; i<m; i++) {
  101. for(int j=0; j<n; j++) {
  102. tmp.c[i][j] += f;
  103. }
  104. }
  105. return tmp;
  106. }
  107. Matrix Matrix::operator-(const float &f) {
  108. Matrix tmp(*this);
  109. for(int i=0; i<m; i++) {
  110. for(int j=0; j<n; j++) {
  111. tmp.c[i][j] -= f;
  112. }
  113. }
  114. return tmp;
  115. }
  116. Matrix Matrix::operator*(const float &f) {
  117. Matrix tmp(*this);
  118. for(int i=0; i<m; i++) {
  119. for(int j=0; j<n; j++) {
  120. tmp.c[i][j] *= f;
  121. }
  122. }
  123. return tmp;
  124. }
  125. Matrix Matrix::operator/(const float &f) {
  126. Matrix tmp(*this);
  127. for(int i=0; i<m; i++) {
  128. for(int j=0; j<n; j++) {
  129. tmp.c[i][j] /= f;
  130. }
  131. }
  132. return tmp;
  133. }
  134. int Matrix::getM() const {
  135. return m;
  136. }
  137. int Matrix::getN() const {
  138. return n;
  139. }
  140. void Matrix::print(std::string s) const {
  141. std::cout << "Matrix " << s << "(" << m << "," << n << ")" << std::endl;
  142. for(int a=0; a<m; a++) {
  143. for(int b=0; b<n; b++) {
  144. std::cout << std::setw(15) << c[a][b];
  145. }
  146. std::cout << std::endl;
  147. }
  148. }
  149. Matrix::~Matrix() {
  150. for(int i=0; i<m; i++) {
  151. delete[](c[i]);
  152. }
  153. delete[](c);
  154. }
  155. } // namespace segl