m3math.h

Go to the documentation of this file.
00001 
00032 #ifndef LL_M3MATH_H
00033 #define LL_M3MATH_H
00034 
00035 #include "llerror.h"
00036 #include "stdtypes.h"
00037 
00038 class LLVector4;
00039 class LLVector3;
00040 class LLVector3d;
00041 class LLQuaternion;
00042 
00043 // NOTA BENE: Currently assuming a right-handed, z-up universe
00044 
00045 //                           ji 
00046 // LLMatrix3 = | 00 01 02 |
00047 //                         | 10 11 12 |
00048 //                         | 20 21 22 |
00049 
00050 // LLMatrix3 = | fx fy fz |     forward-axis
00051 //                         | lx ly lz | left-axis
00052 //                         | ux uy uz | up-axis
00053 
00054 // NOTE: The world of computer graphics uses column-vectors and matricies that 
00055 // "operate to the left". 
00056 
00057 
00058 static const U32 NUM_VALUES_IN_MAT3     = 3;
00059 class LLMatrix3
00060 {
00061         public:
00062                 F32     mMatrix[NUM_VALUES_IN_MAT3][NUM_VALUES_IN_MAT3];
00063 
00064                 LLMatrix3(void);                                                        // Initializes Matrix to identity matrix
00065                 explicit LLMatrix3(const F32 *mat);                                     // Initializes Matrix to values in mat
00066                 explicit LLMatrix3(const LLQuaternion &q);                      // Initializes Matrix with rotation q
00067 
00068                 LLMatrix3(const F32 angle, const F32 x, const F32 y, const F32 z);      // Initializes Matrix with axis angle
00069                 LLMatrix3(const F32 angle, const LLVector3 &vec);       // Initializes Matrix with axis angle
00070                 LLMatrix3(const F32 angle, const LLVector3d &vec);      // Initializes Matrix with axis angle
00071                 LLMatrix3(const F32 angle, const LLVector4 &vec);       // Initializes Matrix with axis angle
00072                 LLMatrix3(const F32 roll, const F32 pitch, const F32 yaw);      // Initializes Matrix with Euler angles
00073 
00075                 //
00076                 // Matrix initializers - these replace any existing values in the matrix
00077                 //
00078 
00079                 // various useful matrix functions
00080                 const LLMatrix3& setIdentity();                         // Load identity matrix
00081                 const LLMatrix3& clear();                                       // Clears Matrix to zero
00082                 const LLMatrix3& setZero();                                     // Clears Matrix to zero
00083 
00085                 //
00086                 // Matrix setters - set some properties without modifying others
00087                 //
00088 
00089                 // These functions take Rotation arguments
00090                 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)
00091                 const LLMatrix3& setRot(const F32 angle, const LLVector3 &vec); // Calculate rotation matrix for rotating angle radians about vec
00092                 const LLMatrix3& setRot(const F32 roll, const F32 pitch, const F32 yaw);        // Calculate rotation matrix from Euler angles
00093                 const LLMatrix3& setRot(const LLQuaternion &q);                 // Transform matrix by Euler angles and translating by pos
00094 
00095                 const LLMatrix3& setRows(const LLVector3 &x_axis, const LLVector3 &y_axis, const LLVector3 &z_axis);
00096                 const LLMatrix3& setRow( U32 rowIndex, const LLVector3& row );
00097                 const LLMatrix3& setCol( U32 colIndex, const LLVector3& col );
00098 
00099                 
00101                 //
00102                 // Get properties of a matrix
00103                 //
00104                 LLQuaternion quaternion() const;                // Returns quaternion from mat
00105                 void getEulerAngles(F32 *roll, F32 *pitch, F32 *yaw) const;     // Returns Euler angles, in radians
00106 
00107                 // Axis extraction routines
00108                 LLVector3 getFwdRow() const;
00109                 LLVector3 getLeftRow() const;
00110                 LLVector3 getUpRow() const;
00111                 F32      determinant() const;                   // Return determinant
00112 
00113 
00115                 //
00116                 // Operations on an existing matrix
00117                 //
00118                 const LLMatrix3& transpose();           // Transpose MAT4
00119                 const LLMatrix3& orthogonalize();       // Orthogonalizes X, then Y, then Z
00120                 void invert();                  // Invert MAT4
00121                 const LLMatrix3& adjointTranspose();// returns transpose of matrix adjoint, for multiplying normals
00122 
00123                 
00124                 // Rotate existing matrix  
00125                 // Note: the two lines below are equivalent:
00126                 //      foo.rotate(bar) 
00127                 //      foo = foo * bar
00128                 // That is, foo.rotate(bar) multiplies foo by bar FROM THE RIGHT
00129                 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)
00130                 const LLMatrix3& rotate(const F32 angle, const LLVector3 &vec);                                         // Rotate matrix by rotating angle radians about vec
00131                 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)
00132                 const LLMatrix3& rotate(const LLQuaternion &q);                 // Transform matrix by Euler angles and translating by pos
00133 
00134                 void add(const LLMatrix3& other_matrix);        // add other_matrix to this one
00135 
00136 // This operator is misleading as to operation direction
00137 //              friend LLVector3 operator*(const LLMatrix3 &a, const LLVector3 &b);                     // Apply rotation a to vector b
00138 
00139                 friend LLVector3 operator*(const LLVector3 &a, const LLMatrix3 &b);                     // Apply rotation b to vector a
00140                 friend LLVector3d operator*(const LLVector3d &a, const LLMatrix3 &b);                   // Apply rotation b to vector a
00141                 friend LLMatrix3 operator*(const LLMatrix3 &a, const LLMatrix3 &b);                     // Return a * b
00142 
00143                 friend bool operator==(const LLMatrix3 &a, const LLMatrix3 &b);                         // Return a == b
00144                 friend bool operator!=(const LLMatrix3 &a, const LLMatrix3 &b);                         // Return a != b
00145 
00146                 friend const LLMatrix3& operator*=(LLMatrix3 &a, const LLMatrix3 &b);                           // Return a * b
00147                 friend const LLMatrix3& operator*=(LLMatrix3 &a, F32 scalar );                                          // Return a * scalar
00148 
00149                 friend std::ostream&     operator<<(std::ostream& s, const LLMatrix3 &a);       // Stream a
00150 };
00151 
00152 inline LLMatrix3::LLMatrix3(void)
00153 {
00154         mMatrix[0][0] = 1.f;
00155         mMatrix[0][1] = 0.f;
00156         mMatrix[0][2] = 0.f;
00157 
00158         mMatrix[1][0] = 0.f;
00159         mMatrix[1][1] = 1.f;
00160         mMatrix[1][2] = 0.f;
00161 
00162         mMatrix[2][0] = 0.f;
00163         mMatrix[2][1] = 0.f;
00164         mMatrix[2][2] = 1.f;
00165 }
00166 
00167 inline LLMatrix3::LLMatrix3(const F32 *mat)
00168 {
00169         mMatrix[0][0] = mat[0];
00170         mMatrix[0][1] = mat[1];
00171         mMatrix[0][2] = mat[2];
00172 
00173         mMatrix[1][0] = mat[3];
00174         mMatrix[1][1] = mat[4];
00175         mMatrix[1][2] = mat[5];
00176 
00177         mMatrix[2][0] = mat[6];
00178         mMatrix[2][1] = mat[7];
00179         mMatrix[2][2] = mat[8];
00180 }
00181 
00182 
00183 #endif
00184 
00185 
00186 // Rotation matrix hints...
00187 
00188 // Inverse of Rotation Matrices
00189 // ----------------------------
00190 // If R is a rotation matrix that rotate vectors from Frame-A to Frame-B,
00191 // then the transpose of R will rotate vectors from Frame-B to Frame-A.
00192 
00193 
00194 // Creating Rotation Matricies From Object Axes
00195 // --------------------------------------------
00196 // Suppose you know the three axes of some object in some "absolute-frame".
00197 // If you take those three vectors and throw them into the rows of 
00198 // a rotation matrix what do you get?
00199 //
00200 // R = | X0  X1  X2 |
00201 //     | Y0  Y1  Y2 |
00202 //     | Z0  Z1  Z2 |
00203 //
00204 // Yeah, but what does it mean?
00205 //
00206 // Transpose the matrix and have it operate on a vector...
00207 //
00208 // V * R_transpose = [ V0  V1  V2 ] * | X0  Y0  Z0 | 
00209 //                                    | X1  Y1  Z1 |                       
00210 //                                    | X2  Y2  Z2 |
00211 // 
00212 //                 = [ V*X  V*Y  V*Z ] 
00213 //
00214 //                 = components of V that are parallel to the three object axes
00215 //
00216 //                 = transformation of V into object frame
00217 //
00218 // Since the transformation of a rotation matrix is its inverse, then
00219 // R must rotate vectors from the object-frame into the absolute-frame.
00220 
00221 
00222 

Generated on Fri May 16 08:32:18 2008 for SecondLife by  doxygen 1.5.5