Richard Boegli's CnC_Generals_Zero_Hour Fork WIP
This is documentation of Richard Boegil's Zero Hour Fork
 
Loading...
Searching...
No Matches
matrix3.cpp
Go to the documentation of this file.
1/*
2** Command & Conquer Generals Zero Hour(tm)
3** Copyright 2025 Electronic Arts Inc.
4**
5** This program is free software: you can redistribute it and/or modify
6** it under the terms of the GNU General Public License as published by
7** the Free Software Foundation, either version 3 of the License, or
8** (at your option) any later version.
9**
10** This program is distributed in the hope that it will be useful,
11** but WITHOUT ANY WARRANTY; without even the implied warranty of
12** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13** GNU General Public License for more details.
14**
15** You should have received a copy of the GNU General Public License
16** along with this program. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19/***********************************************************************************************
20 *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
21 ***********************************************************************************************
22 * *
23 * Project Name : WWMath *
24 * *
25 * $Archive:: /Commando/Code/wwmath/matrix3.cpp $*
26 * *
27 * Org Author:: Greg_h *
28 * *
29 * Author : Kenny Mitchell *
30 * *
31 * $Modtime:: 06/26/02 4:04p $*
32 * *
33 * $Revision:: 17 $*
34 * *
35 * 06/26/02 KM Matrix name change to avoid MAX conflicts *
36 *---------------------------------------------------------------------------------------------*
37 * Functions: *
38 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
39
40
41#include "matrix3.h"
42#include "matrix3d.h"
43#include "matrix4.h"
44#include "quat.h"
45
46
47/*
48** Some pre-initialized Matrix3x3's
49*/
51(
52 1.0, 0.0, 0.0,
53 0.0, 1.0, 0.0,
54 0.0, 0.0, 1.0
55);
56
58(
59 1.0, 0.0, 0.0,
60 0.0, 0.0, -1.0,
61 0.0, 1.0, 0.0
62);
63
65(
66 1.0, 0.0, 0.0,
67 0.0, -1.0, 0.0,
68 0.0, 0.0, -1.0
69);
70
72(
73 1.0, 0.0, 0.0,
74 0.0, 0.0, 1.0,
75 0.0, -1.0, 0.0
76);
77
79(
80 0.0, 0.0, 1.0,
81 0.0, 1.0, 0.0,
82 -1.0, 0.0, 0.0
83);
84
86(
87 -1.0, 0.0, 0.0,
88 0.0, 1.0, 0.0,
89 0.0, 0.0, -1.0
90);
91
93(
94 0.0, 0.0, -1.0,
95 0.0, 1.0, 0.0,
96 1.0, 0.0, 0.0
97);
98
100(
101 0.0, -1.0, 0.0,
102 1.0, 0.0, 0.0,
103 0.0, 0.0, 1.0
104);
105
107(
108 -1.0, 0.0, 0.0,
109 0.0, -1.0, 0.0,
110 0.0, 0.0, 1.0
111);
112
114(
115 0.0, 1.0, 0.0,
116 -1.0, 0.0, 0.0,
117 0.0, 0.0, 1.0
118);
119
120
121
122/***********************************************************************************************
123 * Matrix3x3::Matrix3x3 -- Convert a Matrix3D (fake 4x4) to a Matrix3x3 *
124 * *
125 * INPUT: *
126 * *
127 * OUTPUT: *
128 * *
129 * WARNINGS: *
130 * *
131 * HISTORY: *
132 * 06/02/1997 GH : Created. *
133 *=============================================================================================*/
135{
136 Row[0].Set(m[0][0],m[0][1],m[0][2]);
137 Row[1].Set(m[1][0],m[1][1],m[1][2]);
138 Row[2].Set(m[2][0],m[2][1],m[2][2]);
139}
140
142{
143 Row[0].Set(m[0][0],m[0][1],m[0][2]);
144 Row[1].Set(m[1][0],m[1][1],m[1][2]);
145 Row[2].Set(m[2][0],m[2][1],m[2][2]);
146}
147
149{
150 Row[0].Set(m[0][0],m[0][1],m[0][2]);
151 Row[1].Set(m[1][0],m[1][1],m[1][2]);
152 Row[2].Set(m[2][0],m[2][1],m[2][2]);
153}
154
156{
157 Row[0].Set(m[0][0],m[0][1],m[0][2]);
158 Row[1].Set(m[1][0],m[1][1],m[1][2]);
159 Row[2].Set(m[2][0],m[2][1],m[2][2]);
160}
161
163{
164 Row[0][0] = (float)(1.0 - 2.0 * (q[1] * q[1] + q[2] * q[2]));
165 Row[0][1] = (float)(2.0 * (q[0] * q[1] - q[2] * q[3]));
166 Row[0][2] = (float)(2.0 * (q[2] * q[0] + q[1] * q[3]));
167
168 Row[1][0] = (float)(2.0 * (q[0] * q[1] + q[2] * q[3]));
169 Row[1][1] = (float)(1.0 - 2.0f * (q[2] * q[2] + q[0] * q[0]));
170 Row[1][2] = (float)(2.0 * (q[1] * q[2] - q[0] * q[3]));
171
172 Row[2][0] = (float)(2.0 * (q[2] * q[0] - q[1] * q[3]));
173 Row[2][1] = (float)(2.0 * (q[1] * q[2] + q[0] * q[3]));
174 Row[2][2] =(float)(1.0 - 2.0 * (q[1] * q[1] + q[0] * q[0]));
175}
176
177
179{
180 Row[0].Set(m[0][0],m[0][1],m[0][2]);
181 Row[1].Set(m[1][0],m[1][1],m[1][2]);
182 Row[2].Set(m[2][0],m[2][1],m[2][2]);
183 return *this;
184}
185
187{
188 Row[0].Set(m[0][0],m[0][1],m[0][2]);
189 Row[1].Set(m[1][0],m[1][1],m[1][2]);
190 Row[2].Set(m[2][0],m[2][1],m[2][2]);
191 return *this;
192}
193
194void Matrix3x3::Multiply(const Matrix3D & a, const Matrix3x3 & b,Matrix3x3 * res)
195{
196 #define ROWCOL(i,j) a[i][0]*b[0][j] + a[i][1]*b[1][j] + a[i][2]*b[2][j]
197
198 (*res)[0][0] = ROWCOL(0,0);
199 (*res)[0][1] = ROWCOL(0,1);
200 (*res)[0][2] = ROWCOL(0,2);
201
202 (*res)[1][0] = ROWCOL(1,0);
203 (*res)[1][1] = ROWCOL(1,1);
204 (*res)[1][2] = ROWCOL(1,2);
205
206 (*res)[2][0] = ROWCOL(2,0);
207 (*res)[2][1] = ROWCOL(2,1);
208 (*res)[2][2] = ROWCOL(2,2);
209
210 #undef ROWCOL
211}
212
213void Matrix3x3::Multiply(const Matrix3x3 & a, const Matrix3D & b,Matrix3x3 * res)
214{
215 #define ROWCOL(i,j) a[i][0]*b[0][j] + a[i][1]*b[1][j] + a[i][2]*b[2][j]
216
217 (*res)[0][0] = ROWCOL(0,0);
218 (*res)[0][1] = ROWCOL(0,1);
219 (*res)[0][2] = ROWCOL(0,2);
220
221 (*res)[1][0] = ROWCOL(1,0);
222 (*res)[1][1] = ROWCOL(1,1);
223 (*res)[1][2] = ROWCOL(1,2);
224
225 (*res)[2][0] = ROWCOL(2,0);
226 (*res)[2][1] = ROWCOL(2,1);
227 (*res)[2][2] = ROWCOL(2,2);
228
229 #undef ROWCOL
230}
231
233{
234 #define ROWCOL(i,j) a[i][0]*b[0][j] + a[i][1]*b[1][j] + a[i][2]*b[2][j]
235
236 return Matrix3x3(
237 Vector3(ROWCOL(0,0), ROWCOL(0,1), ROWCOL(0,2) ),
238 Vector3(ROWCOL(1,0), ROWCOL(1,1), ROWCOL(1,2) ),
239 Vector3(ROWCOL(2,0), ROWCOL(2,1), ROWCOL(2,2) )
240 );
241
242 #undef ROWCOL
243}
244
246{
247 #define ROWCOL(i,j) a[i][0]*b[0][j] + a[i][1]*b[1][j] + a[i][2]*b[2][j]
248
249 return Matrix3x3(
250 Vector3(ROWCOL(0,0), ROWCOL(0,1), ROWCOL(0,2) ),
251 Vector3(ROWCOL(1,0), ROWCOL(1,1), ROWCOL(1,2) ),
252 Vector3(ROWCOL(2,0), ROWCOL(2,1), ROWCOL(2,2) )
253 );
254
255 #undef ROWCOL
256}
257
258
259#if 0
260
261void Matrix3x3::Compute_Jacobi_Rotation(int i,int j,Matrix3x3 * r,Matrix3x3 * rinv)
262{
263
264}
265
266void Matrix3x3::Symmetric_Eigen_Solve(void)
267{
268 Matrix3x3 eigen_vals = *this;
269 Matrix3x3 eigen_vecs(1);
270
271 Matrix3x3 jr,jrinv;
272
273 while (!done) {
274 eigen_vals.Compute_Jacobi_Rotation(i,j,&jr,&jrinv);
275 eigen_vals = jr * (eigenvals) * jrinv;
276 eigen_vecs = eigen_vecs * jr;
277 }
278
279 /*
280 ** Done! Eigen values are the diagonals of
281 ** the eigen_vals matrix and the eigen vectors
282 ** are the columns of the eigen_vecs matrix
283 */
284
285}
286
287#endif
288
289
290void Matrix3x3::Multiply(const Matrix3x3 & A,const Matrix3x3 & B,Matrix3x3 * set_res)
291{
292 Matrix3x3 tmp;
293 Matrix3x3 * Aptr;
294 float tmp1,tmp2,tmp3;
295
296 // Check for aliased parameters, copy the 'A' matrix into a temporary if the
297 // result is going into 'A'. (in this case, this function is no better than
298 // the overloaded C++ operator...)
299 if (set_res == &A) {
300 tmp = A;
301 Aptr = &tmp;
302 } else {
303 Aptr = (Matrix3x3 *)&A;
304 }
305
306 tmp1 = B[0][0];
307 tmp2 = B[1][0];
308 tmp3 = B[2][0];
309
310 (*set_res)[0][0] = (float)((*Aptr)[0][0]*tmp1 + (*Aptr)[0][1]*tmp2 + (*Aptr)[0][2]*tmp3);
311 (*set_res)[1][0] = (float)((*Aptr)[1][0]*tmp1 + (*Aptr)[1][1]*tmp2 + (*Aptr)[1][2]*tmp3);
312 (*set_res)[2][0] = (float)((*Aptr)[2][0]*tmp1 + (*Aptr)[2][1]*tmp2 + (*Aptr)[2][2]*tmp3);
313
314 tmp1 = B[0][1];
315 tmp2 = B[1][1];
316 tmp3 = B[2][1];
317
318 (*set_res)[0][1] = (float)((*Aptr)[0][0]*tmp1 + (*Aptr)[0][1]*tmp2 + (*Aptr)[0][2]*tmp3);
319 (*set_res)[1][1] = (float)((*Aptr)[1][0]*tmp1 + (*Aptr)[1][1]*tmp2 + (*Aptr)[1][2]*tmp3);
320 (*set_res)[2][1] = (float)((*Aptr)[2][0]*tmp1 + (*Aptr)[2][1]*tmp2 + (*Aptr)[2][2]*tmp3);
321
322 tmp1 = B[0][2];
323 tmp2 = B[1][2];
324 tmp3 = B[2][2];
325
326 (*set_res)[0][2] = (float)((*Aptr)[0][0]*tmp1 + (*Aptr)[0][1]*tmp2 + (*Aptr)[0][2]*tmp3);
327 (*set_res)[1][2] = (float)((*Aptr)[1][0]*tmp1 + (*Aptr)[1][1]*tmp2 + (*Aptr)[1][2]*tmp3);
328 (*set_res)[2][2] = (float)((*Aptr)[2][0]*tmp1 + (*Aptr)[2][1]*tmp2 + (*Aptr)[2][2]*tmp3);
329}
330
332{
333 Vector3 x(Row[0].X,Row[0].Y,Row[0].Z);
334 Vector3 y(Row[1].X,Row[1].Y,Row[1].Z);
335 Vector3 z(Row[2].X,Row[2].Y,Row[2].Z);
336
337 if (Vector3::Dot_Product(x,y) > WWMATH_EPSILON) return 0;
338 if (Vector3::Dot_Product(y,z) > WWMATH_EPSILON) return 0;
339 if (Vector3::Dot_Product(z,x) > WWMATH_EPSILON) return 0;
340
341 if (WWMath::Fabs(x.Length() - 1.0f) > WWMATH_EPSILON) return 0;
342 if (WWMath::Fabs(y.Length() - 1.0f) > WWMATH_EPSILON) return 0;
343 if (WWMath::Fabs(z.Length() - 1.0f) > WWMATH_EPSILON) return 0;
344
345 return 1;
346}
347
349{
350 Vector3 x(Row[0][0],Row[0][1],Row[0][2]);
351 Vector3 y(Row[1][0],Row[1][1],Row[1][2]);
352 Vector3 z;
353
356
357 float len = x.Length();
358 if (len < WWMATH_EPSILON) {
360 return;
361 } else {
362 x /= len;
363 }
364
365 len = y.Length();
366 if (len < WWMATH_EPSILON) {
368 return;
369 } else {
370 y /= len;
371 }
372
373 len = z.Length();
374 if (len < WWMATH_EPSILON) {
376 return;
377 } else {
378 z /= len;
379 }
380
381 Row[0][0] = x.X;
382 Row[0][1] = x.Y;
383 Row[0][2] = x.Z;
384
385 Row[1][0] = y.X;
386 Row[1][1] = y.Y;
387 Row[1][2] = y.Z;
388
389 Row[2][0] = z.X;
390 Row[2][1] = z.Y;
391 Row[2][2] = z.Z;
392}
393
#define WWMATH_EPSILON
Definition wwmath.h:54
void Re_Orthogonalize(void)
Definition matrix3.cpp:348
static const Matrix3x3 RotateX270
Definition matrix3.h:247
Vector3 Row[3]
Definition matrix3.h:257
static const Matrix3x3 RotateZ270
Definition matrix3.h:253
static const Matrix3x3 RotateY180
Definition matrix3.h:249
static const Matrix3x3 RotateZ180
Definition matrix3.h:252
static const Matrix3x3 RotateZ90
Definition matrix3.h:251
static const Matrix3x3 RotateY270
Definition matrix3.h:250
WWINLINE Matrix3x3(void)
Definition matrix3.h:86
static const Matrix3x3 RotateX180
Definition matrix3.h:246
static const Matrix3x3 RotateY90
Definition matrix3.h:248
static void Multiply(const Matrix3x3 &a, const Matrix3x3 &b, Matrix3x3 *res)
Definition matrix3.cpp:290
static const Matrix3x3 Identity
Definition matrix3.h:244
WWINLINE void Make_Identity(void)
Definition matrix3.h:326
WWINLINE Matrix3x3 & operator=(const Matrix3x3 &m)
Definition matrix3.h:505
void Set(const Matrix3D &m)
Definition matrix3.cpp:148
int Is_Orthogonal(void) const
Definition matrix3.cpp:331
static const Matrix3x3 RotateX90
Definition matrix3.h:245
static WWINLINE float Dot_Product(const Vector3 &a, const Vector3 &b)
Definition vector3.h:293
float X
Definition vector3.h:90
float Z
Definition vector3.h:92
float Y
Definition vector3.h:91
WWINLINE float Length(void) const
Definition vector3.h:453
static WWINLINE void Cross_Product(const Vector3 &a, const Vector3 &b, Vector3 *result)
Definition vector3.h:374
static WWINLINE float Fabs(float val)
Definition wwmath.h:113
#define ROWCOL(i, j)
Matrix3x3 operator*(const Matrix3D &a, const Matrix3x3 &b)
Definition matrix3.cpp:232
#define ROWCOL(i, j)
int q
Definition test1.cpp:94