PageRenderTime 29ms CodeModel.GetById 14ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 1ms

/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
  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
 27#ifndef LL_M3MATH_H
 28#define LL_M3MATH_H
 29
 30#include "llerror.h"
 31#include "stdtypes.h"
 32
 33class LLVector4;
 34class LLVector3;
 35class LLVector3d;
 36class LLQuaternion;
 37
 38// NOTA BENE: Currently assuming a right-handed, z-up universe
 39
 40//			     ji	
 41// LLMatrix3 = | 00 01 02 |
 42//			   | 10 11 12 |
 43//			   | 20 21 22 |
 44
 45// LLMatrix3 = | fx fy fz |	forward-axis
 46//			   | lx ly lz |	left-axis
 47//			   | ux uy uz |	up-axis
 48
 49// NOTE: The world of computer graphics uses column-vectors and matricies that 
 50// "operate to the left". 
 51
 52
 53static const U32 NUM_VALUES_IN_MAT3	= 3;
 54class LLMatrix3
 55{
 56	public:
 57		F32	mMatrix[NUM_VALUES_IN_MAT3][NUM_VALUES_IN_MAT3];
 58
 59		LLMatrix3(void);							// Initializes Matrix to identity matrix
 60		explicit LLMatrix3(const F32 *mat);					// Initializes Matrix to values in mat
 61		explicit LLMatrix3(const LLQuaternion &q);			// Initializes Matrix with rotation q
 62
 63		LLMatrix3(const F32 angle, const F32 x, const F32 y, const F32 z);	// Initializes Matrix with axis angle
 64		LLMatrix3(const F32 angle, const LLVector3 &vec);	// Initializes Matrix with axis angle
 65		LLMatrix3(const F32 angle, const LLVector3d &vec);	// Initializes Matrix with axis angle
 66		LLMatrix3(const F32 angle, const LLVector4 &vec);	// Initializes Matrix with axis angle
 67		LLMatrix3(const F32 roll, const F32 pitch, const F32 yaw);	// Initializes Matrix with Euler angles
 68
 69		//////////////////////////////
 70		//
 71		// Matrix initializers - these replace any existing values in the matrix
 72		//
 73
 74		// various useful matrix functions
 75		const LLMatrix3& setIdentity();				// Load identity matrix
 76		const LLMatrix3& clear();					// Clears Matrix to zero
 77		const LLMatrix3& setZero();					// Clears Matrix to zero
 78
 79		///////////////////////////
 80		//
 81		// Matrix setters - set some properties without modifying others
 82		//
 83
 84		// These functions take Rotation arguments
 85		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)
 86		const LLMatrix3& setRot(const F32 angle, const LLVector3 &vec);	// Calculate rotation matrix for rotating angle radians about vec
 87		const LLMatrix3& setRot(const F32 roll, const F32 pitch, const F32 yaw);	// Calculate rotation matrix from Euler angles
 88		const LLMatrix3& setRot(const LLQuaternion &q);			// Transform matrix by Euler angles and translating by pos
 89
 90		const LLMatrix3& setRows(const LLVector3 &x_axis, const LLVector3 &y_axis, const LLVector3 &z_axis);
 91		const LLMatrix3& setRow( U32 rowIndex, const LLVector3& row );
 92		const LLMatrix3& setCol( U32 colIndex, const LLVector3& col );
 93
 94		
 95		///////////////////////////
 96		//
 97		// Get properties of a matrix
 98		//
 99		LLQuaternion quaternion() const;		// Returns quaternion from mat
100		void getEulerAngles(F32 *roll, F32 *pitch, F32 *yaw) const;	// Returns Euler angles, in radians
101
102		// Axis extraction routines
103		LLVector3 getFwdRow() const;
104		LLVector3 getLeftRow() const;
105		LLVector3 getUpRow() const;
106		F32	 determinant() const;			// Return determinant
107
108
109		///////////////////////////
110		//
111		// Operations on an existing matrix
112		//
113		const LLMatrix3& transpose();		// Transpose MAT4
114		const LLMatrix3& orthogonalize();	// Orthogonalizes X, then Y, then Z
115		void invert();			// Invert MAT4
116		const LLMatrix3& adjointTranspose();// returns transpose of matrix adjoint, for multiplying normals
117
118		
119		// Rotate existing matrix  
120		// Note: the two lines below are equivalent:
121		//	foo.rotate(bar) 
122		//	foo = foo * bar
123		// That is, foo.rotate(bar) multiplies foo by bar FROM THE RIGHT
124		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)
125		const LLMatrix3& rotate(const F32 angle, const LLVector3 &vec);						// Rotate matrix by rotating angle radians about vec
126		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)
127		const LLMatrix3& rotate(const LLQuaternion &q);			// Transform matrix by Euler angles and translating by pos
128
129		void add(const LLMatrix3& other_matrix);	// add other_matrix to this one
130
131// This operator is misleading as to operation direction
132//		friend LLVector3 operator*(const LLMatrix3 &a, const LLVector3 &b);			// Apply rotation a to vector b
133
134		friend LLVector3 operator*(const LLVector3 &a, const LLMatrix3 &b);			// Apply rotation b to vector a
135		friend LLVector3d operator*(const LLVector3d &a, const LLMatrix3 &b);			// Apply rotation b to vector a
136		friend LLMatrix3 operator*(const LLMatrix3 &a, const LLMatrix3 &b);			// Return a * b
137
138		friend bool operator==(const LLMatrix3 &a, const LLMatrix3 &b);				// Return a == b
139		friend bool operator!=(const LLMatrix3 &a, const LLMatrix3 &b);				// Return a != b
140
141		friend const LLMatrix3& operator*=(LLMatrix3 &a, const LLMatrix3 &b);				// Return a * b
142		friend const LLMatrix3& operator*=(LLMatrix3 &a, F32 scalar );						// Return a * scalar
143
144		friend std::ostream&	 operator<<(std::ostream& s, const LLMatrix3 &a);	// Stream a
145};
146
147inline LLMatrix3::LLMatrix3(void)
148{
149	mMatrix[0][0] = 1.f;
150	mMatrix[0][1] = 0.f;
151	mMatrix[0][2] = 0.f;
152
153	mMatrix[1][0] = 0.f;
154	mMatrix[1][1] = 1.f;
155	mMatrix[1][2] = 0.f;
156
157	mMatrix[2][0] = 0.f;
158	mMatrix[2][1] = 0.f;
159	mMatrix[2][2] = 1.f;
160}
161
162inline LLMatrix3::LLMatrix3(const F32 *mat)
163{
164	mMatrix[0][0] = mat[0];
165	mMatrix[0][1] = mat[1];
166	mMatrix[0][2] = mat[2];
167
168	mMatrix[1][0] = mat[3];
169	mMatrix[1][1] = mat[4];
170	mMatrix[1][2] = mat[5];
171
172	mMatrix[2][0] = mat[6];
173	mMatrix[2][1] = mat[7];
174	mMatrix[2][2] = mat[8];
175}
176
177
178#endif
179
180
181// Rotation matrix hints...
182
183// Inverse of Rotation Matrices
184// ----------------------------
185// If R is a rotation matrix that rotate vectors from Frame-A to Frame-B,
186// then the transpose of R will rotate vectors from Frame-B to Frame-A.
187
188
189// Creating Rotation Matricies From Object Axes
190// --------------------------------------------
191// Suppose you know the three axes of some object in some "absolute-frame".
192// If you take those three vectors and throw them into the rows of 
193// a rotation matrix what do you get?
194//
195// R = | X0  X1  X2 |
196//     | Y0  Y1  Y2 |
197//     | Z0  Z1  Z2 |
198//
199// Yeah, but what does it mean?
200//
201// Transpose the matrix and have it operate on a vector...
202//
203// V * R_transpose = [ V0  V1  V2 ] * | X0  Y0  Z0 | 
204//                                    | X1  Y1  Z1 |                       
205//                                    | X2  Y2  Z2 |
206// 
207//                 = [ V*X  V*Y  V*Z ] 
208//
209//                 = components of V that are parallel to the three object axes
210//
211//                 = transformation of V into object frame
212//
213// Since the transformation of a rotation matrix is its inverse, then
214// R must rotate vectors from the object-frame into the absolute-frame.
215
216
217