diff options
Diffstat (limited to 'linden/indra/llmath/m3math.h')
-rw-r--r-- | linden/indra/llmath/m3math.h | 217 |
1 files changed, 217 insertions, 0 deletions
diff --git a/linden/indra/llmath/m3math.h b/linden/indra/llmath/m3math.h new file mode 100644 index 0000000..74c5203 --- /dev/null +++ b/linden/indra/llmath/m3math.h | |||
@@ -0,0 +1,217 @@ | |||
1 | /** | ||
2 | * @file m3math.h | ||
3 | * @brief LLMatrix3 class header file. | ||
4 | * | ||
5 | * Copyright (c) 2000-2007, Linden Research, Inc. | ||
6 | * | ||
7 | * The source code in this file ("Source Code") is provided by Linden Lab | ||
8 | * to you under the terms of the GNU General Public License, version 2.0 | ||
9 | * ("GPL"), unless you have obtained a separate licensing agreement | ||
10 | * ("Other License"), formally executed by you and Linden Lab. Terms of | ||
11 | * the GPL can be found in doc/GPL-license.txt in this distribution, or | ||
12 | * online at http://secondlife.com/developers/opensource/gplv2 | ||
13 | * | ||
14 | * There are special exceptions to the terms and conditions of the GPL as | ||
15 | * it is applied to this Source Code. View the full text of the exception | ||
16 | * in the file doc/FLOSS-exception.txt in this software distribution, or | ||
17 | * online at http://secondlife.com/developers/opensource/flossexception | ||
18 | * | ||
19 | * By copying, modifying or distributing this software, you acknowledge | ||
20 | * that you have read and understood your obligations described above, | ||
21 | * and agree to abide by those obligations. | ||
22 | * | ||
23 | * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO | ||
24 | * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY, | ||
25 | * COMPLETENESS OR PERFORMANCE. | ||
26 | */ | ||
27 | |||
28 | #ifndef LL_M3MATH_H | ||
29 | #define LL_M3MATH_H | ||
30 | |||
31 | #include "llerror.h" | ||
32 | |||
33 | class LLVector4; | ||
34 | class LLVector3; | ||
35 | class LLVector3d; | ||
36 | class 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 | |||
53 | static const U32 NUM_VALUES_IN_MAT3 = 3; | ||
54 | #if LL_WINDOWS | ||
55 | __declspec( align(16) ) | ||
56 | #endif | ||
57 | class LLMatrix3 | ||
58 | { | ||
59 | public: | ||
60 | F32 mMatrix[NUM_VALUES_IN_MAT3][NUM_VALUES_IN_MAT3]; | ||
61 | |||
62 | LLMatrix3(void); // Initializes Matrix to identity matrix | ||
63 | explicit LLMatrix3(const F32 *mat); // Initializes Matrix to values in mat | ||
64 | explicit LLMatrix3(const LLQuaternion &q); // Initializes Matrix with rotation q | ||
65 | |||
66 | LLMatrix3(const F32 angle, const F32 x, const F32 y, const F32 z); // Initializes Matrix with axis angle | ||
67 | LLMatrix3(const F32 angle, const LLVector3 &vec); // Initializes Matrix with axis angle | ||
68 | LLMatrix3(const F32 angle, const LLVector3d &vec); // Initializes Matrix with axis angle | ||
69 | LLMatrix3(const F32 angle, const LLVector4 &vec); // Initializes Matrix with axis angle | ||
70 | LLMatrix3(const F32 roll, const F32 pitch, const F32 yaw); // Initializes Matrix with Euler angles | ||
71 | |||
72 | ////////////////////////////// | ||
73 | // | ||
74 | // Matrix initializers - these replace any existing values in the matrix | ||
75 | // | ||
76 | |||
77 | // various useful matrix functions | ||
78 | const LLMatrix3& identity(); // Load identity matrix | ||
79 | const LLMatrix3& zero(); // Clears Matrix to zero | ||
80 | |||
81 | /////////////////////////// | ||
82 | // | ||
83 | // Matrix setters - set some properties without modifying others | ||
84 | // | ||
85 | |||
86 | // These functions take Rotation arguments | ||
87 | 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) | ||
88 | const LLMatrix3& setRot(const F32 angle, const LLVector3 &vec); // Calculate rotation matrix for rotating angle radians about vec | ||
89 | const LLMatrix3& setRot(const F32 roll, const F32 pitch, const F32 yaw); // Calculate rotation matrix from Euler angles | ||
90 | const LLMatrix3& setRot(const LLQuaternion &q); // Transform matrix by Euler angles and translating by pos | ||
91 | |||
92 | const LLMatrix3& setRows(const LLVector3 &x_axis, const LLVector3 &y_axis, const LLVector3 &z_axis); | ||
93 | |||
94 | /////////////////////////// | ||
95 | // | ||
96 | // Get properties of a matrix | ||
97 | // | ||
98 | LLQuaternion quaternion() const; // Returns quaternion from mat | ||
99 | void getEulerAngles(F32 *roll, F32 *pitch, F32 *yaw) const; // Returns Euler angles, in radians | ||
100 | |||
101 | // Axis extraction routines | ||
102 | LLVector3 getFwdRow() const; | ||
103 | LLVector3 getLeftRow() const; | ||
104 | LLVector3 getUpRow() const; | ||
105 | F32 determinant() const; // Return determinant | ||
106 | |||
107 | |||
108 | /////////////////////////// | ||
109 | // | ||
110 | // Operations on an existing matrix | ||
111 | // | ||
112 | const LLMatrix3& transpose(); // Transpose MAT4 | ||
113 | const LLMatrix3& invert(); // Invert MAT4 | ||
114 | const LLMatrix3& orthogonalize(); // Orthogonalizes X, then Y, then Z | ||
115 | const LLMatrix3& adjointTranspose(); // returns transpose of matrix adjoint, for multiplying normals | ||
116 | |||
117 | |||
118 | // Rotate existing matrix | ||
119 | // Note: the two lines below are equivalent: | ||
120 | // foo.rotate(bar) | ||
121 | // foo = foo * bar | ||
122 | // That is, foo.rotMat3(bar) multiplies foo by bar FROM THE RIGHT | ||
123 | 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) | ||
124 | const LLMatrix3& rotate(const F32 angle, const LLVector3 &vec); // Rotate matrix by rotating angle radians about vec | ||
125 | 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) | ||
126 | const LLMatrix3& rotate(const LLQuaternion &q); // Transform matrix by Euler angles and translating by pos | ||
127 | |||
128 | // This operator is misleading as to operation direction | ||
129 | // friend LLVector3 operator*(const LLMatrix3 &a, const LLVector3 &b); // Apply rotation a to vector b | ||
130 | |||
131 | friend LLVector3 operator*(const LLVector3 &a, const LLMatrix3 &b); // Apply rotation b to vector a | ||
132 | friend LLVector3d operator*(const LLVector3d &a, const LLMatrix3 &b); // Apply rotation b to vector a | ||
133 | friend LLMatrix3 operator*(const LLMatrix3 &a, const LLMatrix3 &b); // Return a * b | ||
134 | |||
135 | friend bool operator==(const LLMatrix3 &a, const LLMatrix3 &b); // Return a == b | ||
136 | friend bool operator!=(const LLMatrix3 &a, const LLMatrix3 &b); // Return a != b | ||
137 | |||
138 | friend const LLMatrix3& operator*=(LLMatrix3 &a, const LLMatrix3 &b); // Return a * b | ||
139 | |||
140 | friend std::ostream& operator<<(std::ostream& s, const LLMatrix3 &a); // Stream a | ||
141 | } | ||
142 | #if LL_DARWIN | ||
143 | __attribute__ ((aligned (16))) | ||
144 | #endif | ||
145 | ; | ||
146 | |||
147 | inline 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 | |||
162 | inline 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 | |||