Matrix.hpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. Copyright (c) 2009 Christopher A. Taylor. All rights reserved.
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions are met:
  5. * Redistributions of source code must retain the above copyright notice,
  6. this list of conditions and the following disclaimer.
  7. * Redistributions in binary form must reproduce the above copyright notice,
  8. this list of conditions and the following disclaimer in the documentation
  9. and/or other materials provided with the distribution.
  10. * Neither the name of LibCat nor the names of its contributors may be used
  11. to endorse or promote products derived from this software without
  12. specific prior written permission.
  13. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  14. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  16. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  17. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  18. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  19. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  21. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  22. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  23. POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #ifndef CAT_MATRIX_HPP
  26. #define CAT_MATRIX_HPP
  27. #include <cat/gfx/Vector.hpp>
  28. namespace cat {
  29. #define FOR_EACH_ELEMENT(index) for (int index = 0; index < ELEMENTS; ++index)
  30. /*
  31. 4x4 matrix elements arranged column major (row, column):
  32. m[0] m[4] m[8] m[12]
  33. m[1] m[5] m[9] m[13]
  34. m[2] m[6] m[10] m[14]
  35. m[3] m[7] m[11] m[15]
  36. Matrix element order corresponds to OpenGL matrix ordering s.t. in a
  37. 4x4 matrix the elements define the following new coordinate system:
  38. new x-axis vector: { m[0] m[1] m[2] }
  39. new y-axis vector: { m[4] m[5] m[6] }
  40. new z-axis vector: { m[7] m[9] m[10] }
  41. new origin: { m[12] m[13] m[14] }
  42. */
  43. template<int ROWS, int COLS, class Scalar> class Matrix
  44. {
  45. protected:
  46. static const int ELEMENTS = ROWS * COLS;
  47. Scalar _elements[ROWS * COLS];
  48. public:
  49. // Short-hand for the current matrix type
  50. typedef Matrix<ROWS, COLS, Scalar> mytype;
  51. // Default constructor does not initialize elements
  52. Matrix()
  53. {
  54. }
  55. // Copy constructor
  56. Matrix(const mytype &u)
  57. {
  58. memcpy(_elements, u._elements, sizeof(_elements));
  59. }
  60. // Assignment operator
  61. mytype &operator=(const mytype &u)
  62. {
  63. memcpy(_elements, u._elements, sizeof(_elements));
  64. }
  65. // Load zero matrix
  66. void loadZero()
  67. {
  68. OBJCLR(_elements);
  69. }
  70. // Load identity matrix
  71. void loadIdentity()
  72. {
  73. OBJCLR(_elements);
  74. // Write a 1 along the diagonal
  75. for (int ii = 0; ii < ROWS && ii < COLS; ++ii)
  76. {
  77. _elements[ii * ROWS + ii] = static_cast<Scalar>( 1 );
  78. }
  79. }
  80. // Addition in-place
  81. mytype &operator+=(const mytype &u)
  82. {
  83. FOR_EACH_ELEMENT(ii) _elements[ii] += u._elements[ii];
  84. }
  85. // Subtraction in-place
  86. mytype &operator-=(const mytype &u)
  87. {
  88. FOR_EACH_ELEMENT(ii) _elements[ii] -= u._elements[ii];
  89. }
  90. // Multiplication by scalar in-place
  91. mytype &operator*=(Scalar u)
  92. {
  93. FOR_EACH_ELEMENT(ii) _elements[ii] *= u;
  94. }
  95. // Division by scalar in-place
  96. mytype &operator/=(Scalar u)
  97. {
  98. FOR_EACH_ELEMENT(ii) _elements[ii] /= u;
  99. }
  100. // Matrix multiplication
  101. template<int OTHER_COLS>
  102. Matrix<ROWS, OTHER_COLS, Scalar> operator*(const Matrix<COLS, OTHER_COLS, Scalar> &u)
  103. {
  104. Matrix<ROWS, OTHER_COLS, Scalar> result;
  105. // For each row of the matrix product,
  106. for (int r = 0; r < ROWS; ++r)
  107. {
  108. // For each column of the matrix product,
  109. for (int c = 0; c < OTHER_COLS; ++c)
  110. {
  111. Scalar x = static_cast<Scalar>( 0 );
  112. // For each row of the right operand (u),
  113. for (int ii = 0; ii < COLS; ++ii)
  114. {
  115. // Accumulate sum of products
  116. x += (*this)(r, ii) * u(ii, c);
  117. }
  118. // Write the sum
  119. result(r, c) = x;
  120. }
  121. }
  122. return result;
  123. }
  124. // Accessors
  125. inline Scalar &operator()(int ii) { return _elements[ii]; }
  126. inline const Scalar &operator()(int ii) const { return _elements[ii]; }
  127. inline Scalar &operator()(int row, int col) { return _elements[col * ROWS + row]; }
  128. inline const Scalar &operator()(int row, int col) const { return _elements[col * ROWS + row]; }
  129. };
  130. // Short-hand for common usages:
  131. typedef Matrix<2, 2, u32> Matrix2x2u;
  132. typedef Matrix<3, 3, u32> Matrix3x3u;
  133. typedef Matrix<4, 4, u32> Matrix4x4u;
  134. typedef Matrix<2, 2, s32> Matrix2x2i;
  135. typedef Matrix<3, 3, s32> Matrix3x3i;
  136. typedef Matrix<4, 4, s32> Matrix4x4i;
  137. typedef Matrix<2, 2, f32> Matrix2x2f;
  138. typedef Matrix<3, 3, f32> Matrix3x3f;
  139. typedef Matrix<4, 4, f32> Matrix4x4f;
  140. typedef Matrix<2, 2, f64> Matrix2x2d;
  141. typedef Matrix<3, 3, f64> Matrix3x3d;
  142. typedef Matrix<4, 4, f64> Matrix4x4d;
  143. #undef FOR_EACH_ELEMENT
  144. } // namespace cat
  145. #endif // CAT_MATRIX_HPP
粤ICP备19079148号