PageRenderTime 178ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/llmath/m3math.h

https://bitbucket.org/lindenlab/viewer-beta/
C++ Header | 217 lines | 80 code | 45 blank | 92 comment | 0 complexity | 202aa7f4e6d7c2cc7016ad4a298dd8f7 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file m3math.h
  3. * @brief LLMatrix3 class header file.
  4. *
  5. * $LicenseInfo:firstyear=2000&license=viewerlgpl$
  6. * Second Life Viewer Source Code
  7. * Copyright (C) 2010, Linden Research, Inc.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License only.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  24. * $/LicenseInfo$
  25. */
  26. #ifndef LL_M3MATH_H
  27. #define LL_M3MATH_H
  28. #include "llerror.h"
  29. #include "stdtypes.h"
  30. class LLVector4;
  31. class LLVector3;
  32. class LLVector3d;
  33. class LLQuaternion;
  34. // NOTA BENE: Currently assuming a right-handed, z-up universe
  35. // ji
  36. // LLMatrix3 = | 00 01 02 |
  37. // | 10 11 12 |
  38. // | 20 21 22 |
  39. // LLMatrix3 = | fx fy fz | forward-axis
  40. // | lx ly lz | left-axis
  41. // | ux uy uz | up-axis
  42. // NOTE: The world of computer graphics uses column-vectors and matricies that
  43. // "operate to the left".
  44. static const U32 NUM_VALUES_IN_MAT3 = 3;
  45. class LLMatrix3
  46. {
  47. public:
  48. F32 mMatrix[NUM_VALUES_IN_MAT3][NUM_VALUES_IN_MAT3];
  49. LLMatrix3(void); // Initializes Matrix to identity matrix
  50. explicit LLMatrix3(const F32 *mat); // Initializes Matrix to values in mat
  51. explicit LLMatrix3(const LLQuaternion &q); // Initializes Matrix with rotation q
  52. LLMatrix3(const F32 angle, const F32 x, const F32 y, const F32 z); // Initializes Matrix with axis angle
  53. LLMatrix3(const F32 angle, const LLVector3 &vec); // Initializes Matrix with axis angle
  54. LLMatrix3(const F32 angle, const LLVector3d &vec); // Initializes Matrix with axis angle
  55. LLMatrix3(const F32 angle, const LLVector4 &vec); // Initializes Matrix with axis angle
  56. LLMatrix3(const F32 roll, const F32 pitch, const F32 yaw); // Initializes Matrix with Euler angles
  57. //////////////////////////////
  58. //
  59. // Matrix initializers - these replace any existing values in the matrix
  60. //
  61. // various useful matrix functions
  62. const LLMatrix3& setIdentity(); // Load identity matrix
  63. const LLMatrix3& clear(); // Clears Matrix to zero
  64. const LLMatrix3& setZero(); // Clears Matrix to zero
  65. ///////////////////////////
  66. //
  67. // Matrix setters - set some properties without modifying others
  68. //
  69. // These functions take Rotation arguments
  70. const LLMatrix3& setRot(const F32 angle, const F32 x, const F32 y, const F32 z); // Calculate rotation matrix for rotating angle radians about (x, y, z)
  71. const LLMatrix3& setRot(const F32 angle, const LLVector3 &vec); // Calculate rotation matrix for rotating angle radians about vec
  72. const LLMatrix3& setRot(const F32 roll, const F32 pitch, const F32 yaw); // Calculate rotation matrix from Euler angles
  73. const LLMatrix3& setRot(const LLQuaternion &q); // Transform matrix by Euler angles and translating by pos
  74. const LLMatrix3& setRows(const LLVector3 &x_axis, const LLVector3 &y_axis, const LLVector3 &z_axis);
  75. const LLMatrix3& setRow( U32 rowIndex, const LLVector3& row );
  76. const LLMatrix3& setCol( U32 colIndex, const LLVector3& col );
  77. ///////////////////////////
  78. //
  79. // Get properties of a matrix
  80. //
  81. LLQuaternion quaternion() const; // Returns quaternion from mat
  82. void getEulerAngles(F32 *roll, F32 *pitch, F32 *yaw) const; // Returns Euler angles, in radians
  83. // Axis extraction routines
  84. LLVector3 getFwdRow() const;
  85. LLVector3 getLeftRow() const;
  86. LLVector3 getUpRow() const;
  87. F32 determinant() const; // Return determinant
  88. ///////////////////////////
  89. //
  90. // Operations on an existing matrix
  91. //
  92. const LLMatrix3& transpose(); // Transpose MAT4
  93. const LLMatrix3& orthogonalize(); // Orthogonalizes X, then Y, then Z
  94. void invert(); // Invert MAT4
  95. const LLMatrix3& adjointTranspose();// returns transpose of matrix adjoint, for multiplying normals
  96. // Rotate existing matrix
  97. // Note: the two lines below are equivalent:
  98. // foo.rotate(bar)
  99. // foo = foo * bar
  100. // That is, foo.rotate(bar) multiplies foo by bar FROM THE RIGHT
  101. const LLMatrix3& rotate(const F32 angle, const F32 x, const F32 y, const F32 z); // Rotate matrix by rotating angle radians about (x, y, z)
  102. const LLMatrix3& rotate(const F32 angle, const LLVector3 &vec); // Rotate matrix by rotating angle radians about vec
  103. const LLMatrix3& rotate(const F32 roll, const F32 pitch, const F32 yaw); // Rotate matrix by roll (about x), pitch (about y), and yaw (about z)
  104. const LLMatrix3& rotate(const LLQuaternion &q); // Transform matrix by Euler angles and translating by pos
  105. void add(const LLMatrix3& other_matrix); // add other_matrix to this one
  106. // This operator is misleading as to operation direction
  107. // friend LLVector3 operator*(const LLMatrix3 &a, const LLVector3 &b); // Apply rotation a to vector b
  108. friend LLVector3 operator*(const LLVector3 &a, const LLMatrix3 &b); // Apply rotation b to vector a
  109. friend LLVector3d operator*(const LLVector3d &a, const LLMatrix3 &b); // Apply rotation b to vector a
  110. friend LLMatrix3 operator*(const LLMatrix3 &a, const LLMatrix3 &b); // Return a * b
  111. friend bool operator==(const LLMatrix3 &a, const LLMatrix3 &b); // Return a == b
  112. friend bool operator!=(const LLMatrix3 &a, const LLMatrix3 &b); // Return a != b
  113. friend const LLMatrix3& operator*=(LLMatrix3 &a, const LLMatrix3 &b); // Return a * b
  114. friend const LLMatrix3& operator*=(LLMatrix3 &a, F32 scalar ); // Return a * scalar
  115. friend std::ostream& operator<<(std::ostream& s, const LLMatrix3 &a); // Stream a
  116. };
  117. inline LLMatrix3::LLMatrix3(void)
  118. {
  119. mMatrix[0][0] = 1.f;
  120. mMatrix[0][1] = 0.f;
  121. mMatrix[0][2] = 0.f;
  122. mMatrix[1][0] = 0.f;
  123. mMatrix[1][1] = 1.f;
  124. mMatrix[1][2] = 0.f;
  125. mMatrix[2][0] = 0.f;
  126. mMatrix[2][1] = 0.f;
  127. mMatrix[2][2] = 1.f;
  128. }
  129. inline LLMatrix3::LLMatrix3(const F32 *mat)
  130. {
  131. mMatrix[0][0] = mat[0];
  132. mMatrix[0][1] = mat[1];
  133. mMatrix[0][2] = mat[2];
  134. mMatrix[1][0] = mat[3];
  135. mMatrix[1][1] = mat[4];
  136. mMatrix[1][2] = mat[5];
  137. mMatrix[2][0] = mat[6];
  138. mMatrix[2][1] = mat[7];
  139. mMatrix[2][2] = mat[8];
  140. }
  141. #endif
  142. // Rotation matrix hints...
  143. // Inverse of Rotation Matrices
  144. // ----------------------------
  145. // If R is a rotation matrix that rotate vectors from Frame-A to Frame-B,
  146. // then the transpose of R will rotate vectors from Frame-B to Frame-A.
  147. // Creating Rotation Matricies From Object Axes
  148. // --------------------------------------------
  149. // Suppose you know the three axes of some object in some "absolute-frame".
  150. // If you take those three vectors and throw them into the rows of
  151. // a rotation matrix what do you get?
  152. //
  153. // R = | X0 X1 X2 |
  154. // | Y0 Y1 Y2 |
  155. // | Z0 Z1 Z2 |
  156. //
  157. // Yeah, but what does it mean?
  158. //
  159. // Transpose the matrix and have it operate on a vector...
  160. //
  161. // V * R_transpose = [ V0 V1 V2 ] * | X0 Y0 Z0 |
  162. // | X1 Y1 Z1 |
  163. // | X2 Y2 Z2 |
  164. //
  165. // = [ V*X V*Y V*Z ]
  166. //
  167. // = components of V that are parallel to the three object axes
  168. //
  169. // = transformation of V into object frame
  170. //
  171. // Since the transformation of a rotation matrix is its inverse, then
  172. // R must rotate vectors from the object-frame into the absolute-frame.