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

Generated on Thu Jul 1 06:09:54 2010 for Second Life Viewer by  doxygen 1.4.7