Richard Boegli's CnC_Generals_Zero_Hour Fork WIP
This is documentation of Richard Boegil's Zero Hour Fork
 
Loading...
Searching...
No Matches
matrix3.h
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/* $Header: /Commando/Code/wwmath/matrix3.h 36 5/11/01 7:11p Jani_p $ */
20/***********************************************************************************************
21 *** Confidential - Westwood Studios ***
22 ***********************************************************************************************
23 * *
24 * Project Name : WW3D *
25 * *
26 * File Name : MATRIX3.H *
27 * *
28 * Org Programmer : Greg Hjelstrom *
29 * *
30 * Author : Kenny Mitchell *
31 * *
32 * Start Date : 06/02/97 *
33 * *
34 * Last Update : June 6, 2002 [KM] *
35 * *
36 * 06/26/02 KM Matrix name change to avoid MAX conflicts *
37 *---------------------------------------------------------------------------------------------*
38 * Functions: *
39 * Matrix3x3::Matrix3x3 -- Constructor, optionally initialize to Identitiy matrix *
40 * Matrix3x3::Matrix3x3 -- Copy Constructor *
41 * Matrix3x3::Matrix3x3 -- Convert a Matrix3D (fake 4x4) to a Matrix3x3 *
42 * Matrix3x3::Matrix3x3 -- Constructor *
43 * Matrix3x3::Transpose -- Returns transpose of the matrix *
44 * Matrix3x3::Inverse -- returns the inverse of the matrix *
45 * Matrix3x3::Determinant -- returns the determinant of the matrix *
46 * Matrix3x3::operator = -- assignment operator *
47 * Matrix3x3::operator += -- "plus equals" operator *
48 * Matrix3x3::operator-= -- "minus equals" operator *
49 * Matrix3x3::operator *= -- "times equals" operator *
50 * Matrix3x3::operator /= -- "divide equals" operator *
51 * Create_X_Rotation_Matrix3 -- creates a matrix3 which is a rotation about X *
52 * Create_Y_Rotation_Matrix3 -- Creates a Matrix3x3 which is a rotation about Y *
53 * Create_Z_Rotation_Matrix3 -- Creates a matrix3 which is a rotation about Z *
54 * Matrix3x3::Rotate_X -- Post-mutiplies an x rotation onto the current matrix *
55 * Matrix3x3::Rotate_Y -- Post-multiplies the matrix with a rotation about Y *
56 * Matrix3x3::Rotate_Z -- Post-multiplies the matrix with a rotation about Z *
57 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
58
59
60#if defined(_MSC_VER)
61#pragma once
62#endif
63
64#ifndef Matrix3_H
65#define Matrix3_H
66
67#include "always.h"
68#include "vector3.h"
69#include "wwdebug.h"
70#ifdef _UNIX
71#include "osdep.h"
72#endif
73
74
75class Matrix3D;
76class Matrix4x4;
77class Quaternion;
78
80{
81public:
82
83 /*
84 ** Constructors
85 */
87 WWINLINE Matrix3x3(const Matrix3x3 & m);
88
89 WWINLINE explicit Matrix3x3(bool identity);
90 WWINLINE explicit Matrix3x3(const Vector3 & v0, const Vector3 & v1, const Vector3 & v2);
91 explicit Matrix3x3(const Matrix3D & m);
92 explicit Matrix3x3(const Matrix4x4 & m);
93 WWINLINE explicit Matrix3x3(
94 float m11,float m12,float m13,
95 float m21,float m22,float m23,
96 float m31,float m32,float m33
97 );
98 WWINLINE explicit Matrix3x3(const Vector3 & axis,float angle);
99 WWINLINE explicit Matrix3x3(const Vector3 & axis,float s_angle,float c_angle);
100 WWINLINE explicit Matrix3x3(const Quaternion & q);
101
102 /*
103 ** 'Set' functions
104 */
105 void Set(const Matrix3D & m);
106 void Set(const Matrix4x4 & m);
107 WWINLINE void Set(const Vector3 & v0, const Vector3 & v1, const Vector3 & v2);
108 WWINLINE void Set(
109 float m11,float m12,float m13,
110 float m21,float m22,float m23,
111 float m31,float m32,float m33
112 );
113 WWINLINE void Set(const Vector3 & axis,float angle);
114 WWINLINE void Set(const Vector3 & axis,float s_angle,float c_angle);
115 void Set(const Quaternion & q);
116
117 /*
118 ** Access operators
119 */
120 WWINLINE Vector3 & operator [] (int i) { return Row[i]; }
121 WWINLINE const Vector3 & operator [] (int i) const { return Row[i]; }
122
123 /*
124 ** Transpose and Inverse
125 */
126 WWINLINE Matrix3x3 Transpose (void) const;
127 WWINLINE Matrix3x3 Inverse (void) const;
128 WWINLINE float Determinant (void) const;
129
130 /*
131 ** Assignment operators
132 */
134 Matrix3x3 & operator = (const Matrix3D & m);
135 Matrix3x3 & operator = (const Matrix4x4 & m);
136
141
142 WWINLINE void Make_Identity(void);
143
144 /*
145 ** Automatically concatenate a rotation onto the current matrix
146 */
147 WWINLINE void Rotate_X(float theta);
148 WWINLINE void Rotate_X(float s,float c);
149
150 WWINLINE void Rotate_Y(float theta);
151 WWINLINE void Rotate_Y(float s,float c);
152
153 WWINLINE void Rotate_Z(float theta);
154 WWINLINE void Rotate_Z(float s,float c);
155
156 /*
157 ** These functions will give you the approximate amount that the
158 ** matrix has been rotated about a given axis. These functions
159 ** cannot be used to re-build a matrx. Use the EulerAnglesClass
160 ** to convert a matrix into a set of three Euler angles.
161 */
162 float Get_X_Rotation(void) const;
163 float Get_Y_Rotation(void) const;
164 float Get_Z_Rotation(void) const;
165
166 /*
167 ** These functions return a vector representing the direction an
168 ** axis is pointing.
169 */
170 WWINLINE Vector3 Get_X_Vector(void) const;
171 WWINLINE Vector3 Get_Y_Vector(void) const;
172 WWINLINE Vector3 Get_Z_Vector(void) const;
173 WWINLINE void Get_X_Vector(Vector3 * set_x) const;
174 WWINLINE void Get_Y_Vector(Vector3 * set_y) const;
175 WWINLINE void Get_Z_Vector(Vector3 * set_z) const;
176
177 /*
178 ** Negation
179 */
180 WWINLINE friend Matrix3x3 operator - (const Matrix3x3& a);
181
182 /*
183 ** Scalar multiplication and division
184 */
185 WWINLINE friend Matrix3x3 operator * (const Matrix3x3& a,float d);
186 WWINLINE friend Matrix3x3 operator * (float d,const Matrix3x3& a);
187 WWINLINE friend Matrix3x3 operator / (const Matrix3x3& a,float d);
188
189 /*
190 ** matrix addition
191 */
192 WWINLINE friend Matrix3x3 operator + (const Matrix3x3& a, const Matrix3x3& b);
193 static WWINLINE void Add(const Matrix3x3 & a, const Matrix3x3 & b,Matrix3x3 * res);
194
195 /*
196 ** matrix subtraction
197 */
198 WWINLINE friend Matrix3x3 operator - (const Matrix3x3 & a, const Matrix3x3 & b);
199 static WWINLINE void Subtract(const Matrix3x3 & a, const Matrix3x3 & b,Matrix3x3 * res);
200
201 /*
202 ** matrix multiplication
203 */
204 WWINLINE friend Matrix3x3 operator * (const Matrix3x3 & a, const Matrix3x3 & b);
205 WWINLINE friend Matrix3x3 operator * (const Matrix3D & a, const Matrix3x3 & b);
206 WWINLINE friend Matrix3x3 operator * (const Matrix3x3 & a, const Matrix3D & b);
207
208 static void Multiply(const Matrix3x3 & a, const Matrix3x3 & b,Matrix3x3 * res);
209 static void Multiply(const Matrix3D & a, const Matrix3x3 & b,Matrix3x3 * res);
210 static void Multiply(const Matrix3x3 & a, const Matrix3D & b,Matrix3x3 * res);
211
212 /*
213 ** Matrix-Vector multiplication
214 */
215 WWINLINE friend Vector3 operator * (const Matrix3x3 & a, const Vector3 & v);
216 static WWINLINE void Rotate_Vector(const Matrix3x3 & tm,const Vector3 & in,Vector3 * out);
217 static WWINLINE void Transpose_Rotate_Vector(const Matrix3x3 & tm,const Vector3 & in,Vector3 * out);
218
219 /*
220 ** Comparison operators
221 */
222 WWINLINE friend int operator == (const Matrix3x3 & a, const Matrix3x3 & b);
223 WWINLINE friend int operator != (const Matrix3x3 & a, const Matrix3x3 & b);
224
225 /*
226 ** Swap two matrices in place
227 */
228 WWINLINE friend void Swap(Matrix3x3 & a,Matrix3x3 & b);
229
230 /*
231 ** Check whether a matrix is orthogonal, make it orthogonal
232 */
233 int Is_Orthogonal(void) const;
234 void Re_Orthogonalize(void);
235
236 /*
237 ** Miscellaneous
238 */
239 WWINLINE void Rotate_AABox_Extent(const Vector3 & extent,Vector3 * new_extent);
240
241 /*
242 ** Some useful pre-initialized Matrix3x3's
243 */
244 static const Matrix3x3 Identity;
245 static const Matrix3x3 RotateX90;
246 static const Matrix3x3 RotateX180;
247 static const Matrix3x3 RotateX270;
248 static const Matrix3x3 RotateY90;
249 static const Matrix3x3 RotateY180;
250 static const Matrix3x3 RotateY270;
251 static const Matrix3x3 RotateZ90;
252 static const Matrix3x3 RotateZ180;
253 static const Matrix3x3 RotateZ270;
254
255protected:
256
258
259};
260
261
262/***********************************************************************************************
263 * Matrix3x3::Matrix3x3 -- Constructor, optionally initialize to Identitiy matrix *
264 * *
265 * INPUT: *
266 * *
267 * OUTPUT: *
268 * *
269 * WARNINGS: *
270 * *
271 * HISTORY: *
272 * 06/02/1997 GH : Created. *
273 *=============================================================================================*/
275{
276 if (identity) {
277 Row[0].Set(1.0,0.0,0.0);
278 Row[1].Set(0.0,1.0,0.0);
279 Row[2].Set(0.0,0.0,1.0);
280 }
281}
282
283/***********************************************************************************************
284 * Matrix3x3::Matrix3x3 -- Copy Constructor *
285 * *
286 * INPUT: *
287 * *
288 * OUTPUT: *
289 * *
290 * WARNINGS: *
291 * *
292 * HISTORY: *
293 * 06/02/1997 GH : Created. *
294 *=============================================================================================*/
296{
297 Row[0] = m.Row[0]; Row[1] = m.Row[1]; Row[2] = m.Row[2];
298}
299
300/***********************************************************************************************
301 * Matrix3x3::Matrix3x3 -- Constructor *
302 * *
303 * INPUT: *
304 * *
305 * OUTPUT: *
306 * *
307 * WARNINGS: *
308 * *
309 * HISTORY: *
310 * 06/02/1997 GH : Created. *
311 *=============================================================================================*/
312WWINLINE Matrix3x3::Matrix3x3(const Vector3 & r0, const Vector3 & r1, const Vector3 & r2)
313{
314 Row[0] = r0;
315 Row[1] = r1;
316 Row[2] = r2;
317}
318
319WWINLINE void Matrix3x3::Set(const Vector3 & r0, const Vector3 & r1, const Vector3 & r2)
320{
321 Row[0] = r0;
322 Row[1] = r1;
323 Row[2] = r2;
324}
325
327{
328 Row[0].Set(1.0f,0.0f,0.0f);
329 Row[1].Set(0.0f,1.0f,0.0f);
330 Row[2].Set(0.0f,0.0f,1.0f);
331}
332
333
335(
336 float m11,float m12,float m13,
337 float m21,float m22,float m23,
338 float m31,float m32,float m33
339)
340{
341 Row[0].Set(m11,m12,m13);
342 Row[1].Set(m21,m22,m23);
343 Row[2].Set(m31,m32,m33);
344}
345
347(
348 float m11,float m12,float m13,
349 float m21,float m22,float m23,
350 float m31,float m32,float m33
351)
352{
353 Row[0].Set(m11,m12,m13);
354 Row[1].Set(m21,m22,m23);
355 Row[2].Set(m31,m32,m33);
356}
357
358WWINLINE Matrix3x3::Matrix3x3(const Vector3 & axis,float angle)
359{
360 Set(axis,angle);
361}
362
363WWINLINE Matrix3x3::Matrix3x3(const Vector3 & axis,float s_angle,float c_angle)
364{
365 Set(axis,s_angle,c_angle);
366}
367
368WWINLINE void Matrix3x3::Set(const Vector3 & axis,float angle)
369{
370 Set(axis,sinf(angle),cosf(angle));
371}
372
373WWINLINE void Matrix3x3::Set(const Vector3 & axis,float s,float c)
374{
375 WWASSERT(WWMath::Fabs(axis.Length2() - 1.0f) < 0.001f);
376
377 Row[0].Set(
378 (float)(axis[0]*axis[0] + c*(1.0f - axis[0]*axis[0])),
379 (float)(axis[0]*axis[1]*(1.0f - c) - axis[2]*s),
380 (float)(axis[2]*axis[0]*(1.0f - c) + axis[1]*s)
381 );
382
383 Row[1].Set(
384 (float)(axis[0]*axis[1]*(1.0f - c) + axis[2]*s),
385 (float)(axis[1]*axis[1] + c*(1.0f - axis[1]*axis[1])),
386 (float)(axis[1]*axis[2]*(1.0f - c) - axis[0]*s)
387 );
388
389 Row[2].Set(
390 (float)(axis[2]*axis[0]*(1.0f - c) - axis[1]*s),
391 (float)(axis[1]*axis[2]*(1.0f - c) + axis[0]*s),
392 (float)(axis[2]*axis[2] + c*(1 - axis[2]*axis[2]))
393 );
394}
395
397{
398 this->Set(q);
399}
400
401/***********************************************************************************************
402 * Matrix3x3::Transpose -- Returns transpose of the matrix *
403 * *
404 * INPUT: *
405 * *
406 * OUTPUT: *
407 * *
408 * WARNINGS: *
409 * *
410 * HISTORY: *
411 * 06/02/1997 GH : Created. *
412 *=============================================================================================*/
414{
415 return Matrix3x3(
416 Vector3(Row[0][0], Row[1][0], Row[2][0]),
417 Vector3(Row[0][1], Row[1][1], Row[2][1]),
418 Vector3(Row[0][2], Row[1][2], Row[2][2])
419 );
420}
421
422/***********************************************************************************************
423 * Matrix3x3::Inverse -- returns the inverse of the matrix *
424 * *
425 * INPUT: *
426 * *
427 * OUTPUT: *
428 * *
429 * WARNINGS: *
430 * *
431 * HISTORY: *
432 * 06/02/1997 GH : Created. *
433 *=============================================================================================*/
434WWINLINE Matrix3x3 Matrix3x3::Inverse() const // Gauss-Jordan elimination with partial pivoting
435{
436 Matrix3x3 a(*this); // As a evolves from original mat into identity
437 Matrix3x3 b(true); // b evolves from identity into inverse(a)
438 int i, j, i1;
439
440 // Loop over cols of a from left to right, eliminating above and below diagonal
441 for (j=0; j<3; j++) {
442
443 // Find largest pivot in column j among rows j..3
444 i1 = j;
445 for (i=j+1; i<3; i++) {
446 if (WWMath::Fabs(a[i][j]) > WWMath::Fabs(a[i1][j])) {
447 i1 = i;
448 }
449 }
450
451 // Swap rows i1 and j in a and b to put pivot on diagonal
452 Swap(a.Row[i1], a.Row[j]);
453 Swap(b.Row[i1], b.Row[j]);
454
455 // Scale row j to have a unit diagonal
456 if (a[j][j]==0.) {
457 //Matrix3x3::inverse: singular matrix; can't invert
458 }
459 b.Row[j] /= a.Row[j][j];
460 a.Row[j] /= a.Row[j][j];
461
462 // Eliminate off-diagonal elems in col j of a, doing identical ops to b
463 for (i=0; i<3; i++) {
464 if (i != j) {
465 b.Row[i] -= a[i][j] * b.Row[j];
466 a.Row[i] -= a[i][j] * a.Row[j];
467 }
468 }
469 }
470 return b;
471}
472
473
474/***********************************************************************************************
475 * Matrix3x3::Determinant -- returns the determinant of the matrix *
476 * *
477 * INPUT: *
478 * *
479 * OUTPUT: *
480 * *
481 * WARNINGS: *
482 * *
483 * HISTORY: *
484 * 1/7/20 DRM : Created. *
485 *=============================================================================================*/
487{
488 return Row[0][0] * (Row[1][1] * Row[2][2] - Row[1][2] * Row[2][1])
489 - Row[0][1] * (Row[1][0] * Row[2][2] - Row[1][2] * Row[2][0])
490 - Row[0][2] * (Row[1][0] * Row[2][1] - Row[1][1] * Row[2][0]);
491}
492
493/***********************************************************************************************
494 * Matrix3x3::operator = -- assignment operator *
495 * *
496 * INPUT: *
497 * *
498 * OUTPUT: *
499 * *
500 * WARNINGS: *
501 * *
502 * HISTORY: *
503 * 06/02/1997 GH : Created. *
504 *=============================================================================================*/
506{
507 Row[0] = m.Row[0]; Row[1] = m.Row[1]; Row[2] = m.Row[2];
508 return *this;
509}
510
511/***********************************************************************************************
512 * Matrix3x3::operator += -- "plus equals" operator *
513 * *
514 * INPUT: *
515 * *
516 * OUTPUT: *
517 * *
518 * WARNINGS: *
519 * *
520 * HISTORY: *
521 * 06/02/1997 GH : Created. *
522 *=============================================================================================*/
524{
525 Row[0] += m.Row[0]; Row[1] += m.Row[1]; Row[2] += m.Row[2];
526 return *this;
527}
528
529/***********************************************************************************************
530 * Matrix3x3::operator-= -- "minus equals" operator *
531 * *
532 * INPUT: *
533 * *
534 * OUTPUT: *
535 * *
536 * WARNINGS: *
537 * *
538 * HISTORY: *
539 * 06/02/1997 GH : Created. *
540 *=============================================================================================*/
542{
543 Row[0] -= m.Row[0]; Row[1] -= m.Row[1]; Row[2] -= m.Row[2];
544 return *this;
545}
546
547/***********************************************************************************************
548 * Matrix3x3::operator *= -- "times equals" operator *
549 * *
550 * INPUT: *
551 * *
552 * OUTPUT: *
553 * *
554 * WARNINGS: *
555 * *
556 * HISTORY: *
557 * 06/02/1997 GH : Created. *
558 *=============================================================================================*/
560{
561 Row[0] *= d; Row[1] *= d; Row[2] *= d;
562 return *this;
563}
564
565/***********************************************************************************************
566 * Matrix3x3::operator /= -- "divide equals" operator *
567 * *
568 * INPUT: *
569 * *
570 * OUTPUT: *
571 * *
572 * WARNINGS: *
573 * *
574 * HISTORY: *
575 * 06/02/1997 GH : Created. *
576 *=============================================================================================*/
578{
579 Row[0] /= d; Row[1] /= d; Row[2] /= d;
580 return *this;
581}
582
583/***********************************************************************************************
584 * Matrix3x3::Get_X_Rotation -- approximates the rotation about the X axis *
585 * *
586 * INPUT: *
587 * *
588 * OUTPUT: *
589 * *
590 * WARNINGS: *
591 * *
592 * HISTORY: *
593 * 08/11/1997 GH : Created. *
594 *=============================================================================================*/
596{
597 Vector3 v = (*this) * Vector3(0.0,1.0,0.0);
598 return WWMath::Atan2(v[2], v[1]);
599}
600
601/***********************************************************************************************
602 * Matrix3x3::Get_Y_Rotation -- approximates the rotation about the Y axis *
603 * *
604 * INPUT: *
605 * *
606 * OUTPUT: *
607 * *
608 * WARNINGS: *
609 * *
610 * HISTORY: *
611 * 08/11/1997 GH : Created. *
612 *=============================================================================================*/
614{
615 Vector3 v = (*this) * Vector3(0.0,0.0,1.0);
616 return WWMath::Atan2(v[0],v[2]);
617}
618
619/***********************************************************************************************
620 * Matrix3x3::Get_Z_Rotation -- approximates the rotation about the Z axis *
621 * *
622 * INPUT: *
623 * *
624 * OUTPUT: *
625 * *
626 * WARNINGS: *
627 * *
628 * HISTORY: *
629 * 08/11/1997 GH : Created. *
630 *=============================================================================================*/
632{
633 Vector3 v = (*this) * Vector3(1.0,0.0,0.0);
634 return WWMath::Atan2(v[1],v[0]);
635}
636
638{
639 return Vector3(Row[0][0], Row[1][0], Row[2][0]);
640}
641
643{
644 return Vector3(Row[0][1], Row[1][1], Row[2][1]);
645}
646
648{
649 return Vector3(Row[0][2], Row[1][2], Row[2][2]);
650}
651
653{
654 set->Set(Row[0][0], Row[1][0], Row[2][0]);
655}
656
658{
659 set->Set(Row[0][1], Row[1][1], Row[2][1]);
660}
661
663{
664 set->Set(Row[0][2], Row[1][2], Row[2][2]);
665}
666
668{
669 return Matrix3x3(-a.Row[0], -a.Row[1], -a.Row[2]);
670}
671
673{
674 return Matrix3x3(a.Row[0] * d, a.Row[1] * d, a.Row[2] * d);
675}
676
678{
679 return a*d;
680}
681
683{
684 float ood = 1.0f / d;
685 return Matrix3x3(a.Row[0] * ood, a.Row[1] * ood, a.Row[2] * ood);
686}
687
688/*
689** matrix addition
690*/
692{
693 return Matrix3x3(
694 a.Row[0] + b.Row[0],
695 a.Row[1] + b.Row[1],
696 a.Row[2] + b.Row[2]
697 );
698}
699
701{
702 WWASSERT(c);
703 Vector3::Add(a.Row[0],b.Row[0],&(c->Row[0]));
704 Vector3::Add(a.Row[1],b.Row[1],&(c->Row[1]));
705 Vector3::Add(a.Row[2],b.Row[2],&(c->Row[2]));
706}
707
708/*
709** matrix subtraction
710*/
712{
713 return Matrix3x3(
714 a.Row[0] - b.Row[0],
715 a.Row[1] - b.Row[1],
716 a.Row[2] - b.Row[2]
717 );
718}
719
721{
722 WWASSERT(c);
723 Vector3::Subtract(a.Row[0],b.Row[0],&(c->Row[0]));
724 Vector3::Subtract(a.Row[1],b.Row[1],&(c->Row[1]));
725 Vector3::Subtract(a.Row[2],b.Row[2],&(c->Row[2]));
726}
727
728/*
729** matrix multiplication
730*/
732{
733 #define ROWCOL(i,j) a[i][0]*b[0][j] + a[i][1]*b[1][j] + a[i][2]*b[2][j]
734
735 return Matrix3x3(
736 Vector3(ROWCOL(0,0), ROWCOL(0,1), ROWCOL(0,2) ),
737 Vector3(ROWCOL(1,0), ROWCOL(1,1), ROWCOL(1,2) ),
738 Vector3(ROWCOL(2,0), ROWCOL(2,1), ROWCOL(2,2) )
739 );
740
741 #undef ROWCOL
742}
743
744
745/*
746** Multiply a Matrix3x3 by a Vector3. Yeilds a Vector3 result
747*/
749{
750 return Vector3(
751 a[0][0] * v[0] + a[0][1] * v[1] + a[0][2] * v[2],
752 a[1][0] * v[0] + a[1][1] * v[1] + a[1][2] * v[2],
753 a[2][0] * v[0] + a[2][1] * v[1] + a[2][2] * v[2]
754 );
755}
756
757
758WWINLINE int operator == (const Matrix3x3 & a, const Matrix3x3 & b)
759{
760 return ((a [0] == b [0]) && (a [1] == b [1]) && (a [2] == b [2]));
761}
762
763
764WWINLINE int operator != (const Matrix3x3 & a, const Matrix3x3 & b)
765{
766 return (!(a == b));
767}
768
769
770/***********************************************************************************************
771 * Matrix3x3::Rotate_X -- Post-mutiplies an x rotation onto the current matrix *
772 * *
773 * INPUT: *
774 * *
775 * OUTPUT: *
776 * *
777 * WARNINGS: *
778 * *
779 * HISTORY: *
780 * 08/26/1997 GH : Created. *
781 *=============================================================================================*/
783{
784 Rotate_X(sinf(theta),cosf(theta));
785}
786
787WWINLINE void Matrix3x3::Rotate_X(float s,float c)
788{
789 float tmp1,tmp2;
790
791 tmp1 = Row[0][1]; tmp2 = Row[0][2];
792 Row[0][1] = (float)( c*tmp1 + s*tmp2);
793 Row[0][2] = (float)(-s*tmp1 + c*tmp2);
794
795 tmp1 = Row[1][1]; tmp2 = Row[1][2];
796 Row[1][1] = (float)( c*tmp1 + s*tmp2);
797 Row[1][2] = (float)(-s*tmp1 + c*tmp2);
798
799 tmp1 = Row[2][1]; tmp2 = Row[2][2];
800 Row[2][1] = (float)( c*tmp1 + s*tmp2);
801 Row[2][2] = (float)(-s*tmp1 + c*tmp2);
802}
803
804/***********************************************************************************************
805 * Matrix3x3::Rotate_Y -- Post-multiplies the matrix with a rotation about Y *
806 * *
807 * INPUT: *
808 * *
809 * OUTPUT: *
810 * *
811 * WARNINGS: *
812 * *
813 * HISTORY: *
814 * 08/26/1997 GH : Created. *
815 *=============================================================================================*/
817{
818 Rotate_Y(sinf(theta),cosf(theta));
819}
820
821WWINLINE void Matrix3x3::Rotate_Y(float s,float c)
822{
823 float tmp1,tmp2;
824
825 tmp1 = Row[0][0]; tmp2 = Row[0][2];
826 Row[0][0] = (float)(c*tmp1 - s*tmp2);
827 Row[0][2] = (float)(s*tmp1 + c*tmp2);
828
829 tmp1 = Row[1][0]; tmp2 = Row[1][2];
830 Row[1][0] = (float)(c*tmp1 - s*tmp2);
831 Row[1][2] = (float)(s*tmp1 + c*tmp2);
832
833 tmp1 = Row[2][0]; tmp2 = Row[2][2];
834 Row[2][0] = (float)(c*tmp1 - s*tmp2);
835 Row[2][2] = (float)(s*tmp1 + c*tmp2);
836}
837
838
839/***********************************************************************************************
840 * Matrix3x3::Rotate_Z -- Post-multiplies the matrix with a rotation about Z *
841 * *
842 * INPUT: *
843 * *
844 * OUTPUT: *
845 * *
846 * WARNINGS: *
847 * *
848 * HISTORY: *
849 * 08/26/1997 GH : Created. *
850 *=============================================================================================*/
852{
853 Rotate_Z(sinf(theta),cosf(theta));
854}
855
856WWINLINE void Matrix3x3::Rotate_Z(float s,float c)
857{
858 float tmp1,tmp2;
859
860 tmp1 = Row[0][0]; tmp2 = Row[0][1];
861 Row[0][0] = (float)( c*tmp1 + s*tmp2);
862 Row[0][1] = (float)(-s*tmp1 + c*tmp2);
863
864 tmp1 = Row[1][0]; tmp2 = Row[1][1];
865 Row[1][0] = (float)( c*tmp1 + s*tmp2);
866 Row[1][1] = (float)(-s*tmp1 + c*tmp2);
867
868 tmp1 = Row[2][0]; tmp2 = Row[2][1];
869 Row[2][0] = (float)( c*tmp1 + s*tmp2);
870 Row[2][1] = (float)(-s*tmp1 + c*tmp2);
871}
872
873
874/***********************************************************************************************
875 * Create_X_Rotation_Matrix3 -- creates a matrix3 which is a rotation about X *
876 * *
877 * INPUT: *
878 * *
879 * OUTPUT: *
880 * *
881 * WARNINGS: *
882 * *
883 * HISTORY: *
884 * 08/26/1997 GH : Created. *
885 *=============================================================================================*/
887{
888 Matrix3x3 mat;
889
890 mat[0][0] = 1.0f;
891 mat[0][1] = 0.0f;
892 mat[0][2] = 0.0f;
893
894 mat[1][0] = 0.0f;
895 mat[1][1] = c;
896 mat[1][2] = -s;
897
898 mat[2][0] = 0.0f;
899 mat[2][1] = s;
900 mat[2][2] = c;
901
902 return mat;
903}
904
906{
907 return Create_X_Rotation_Matrix3(sinf(rad),cosf(rad));
908}
909
910/***********************************************************************************************
911 * Create_Y_Rotation_Matrix3 -- Creates a Matrix3x3 which is a rotation about Y *
912 * *
913 * INPUT: *
914 * *
915 * OUTPUT: *
916 * *
917 * WARNINGS: *
918 * *
919 * HISTORY: *
920 * 08/26/1997 GH : Created. *
921 *=============================================================================================*/
923{
924 Matrix3x3 mat;
925
926 mat[0][0] = c;
927 mat[0][1] = 0.0f;
928 mat[0][2] = s;
929
930 mat[1][0] = 0.0f;
931 mat[1][1] = 1.0f;
932 mat[1][2] = 0.0f;
933
934 mat[2][0] = -s;
935 mat[2][1] = 0.0f;
936 mat[2][2] = c;
937
938 return mat;
939}
940
942{
943 return Create_Y_Rotation_Matrix3(sinf(rad),cosf(rad));
944}
945
946/***********************************************************************************************
947 * Create_Z_Rotation_Matrix3 -- Creates a matrix3 which is a rotation about Z *
948 * *
949 * INPUT: *
950 * *
951 * OUTPUT: *
952 * *
953 * WARNINGS: *
954 * *
955 * HISTORY: *
956 * 08/26/1997 GH : Created. *
957 *=============================================================================================*/
959{
960 Matrix3x3 mat;
961
962 mat[0][0] = c;
963 mat[0][1] = -s;
964 mat[0][2] = 0.0f;
965
966 mat[1][0] = s;
967 mat[1][1] = c;
968 mat[1][2] = 0.0f;
969
970 mat[2][0] = 0.0f;
971 mat[2][1] = 0.0f;
972 mat[2][2] = 1.0f;
973
974 return mat;
975}
976
978{
979 return Create_Z_Rotation_Matrix3(sinf(rad),cosf(rad));
980}
981
983{
984 Vector3 tmp;
985 Vector3 * v;
986
987 // check for aliased parameters
988 if (out == &in) {
989 tmp = in;
990 v = &tmp;
991 } else {
992 v = (Vector3 *)&in; // whats the right way to do this...
993 }
994
995 out->X = (A[0][0] * v->X + A[0][1] * v->Y + A[0][2] * v->Z);
996 out->Y = (A[1][0] * v->X + A[1][1] * v->Y + A[1][2] * v->Z);
997 out->Z = (A[2][0] * v->X + A[2][1] * v->Y + A[2][2] * v->Z);
998}
999
1001{
1002 Vector3 tmp;
1003 Vector3 * v;
1004
1005 // check for aliased parameters
1006 if (out == &in) {
1007 tmp = in;
1008 v = &tmp;
1009 } else {
1010 v = (Vector3 *)&in;
1011 }
1012 out->X = (A[0][0] * v->X + A[1][0] * v->Y + A[2][0] * v->Z);
1013 out->Y = (A[0][1] * v->X + A[1][1] * v->Y + A[2][1] * v->Z);
1014 out->Z = (A[0][2] * v->X + A[1][2] * v->Y + A[2][2] * v->Z);
1015}
1016
1018{
1019 // push each extent out to the projections of the original extents
1020 for (int i=0; i<3; i++) {
1021
1022 // start the center out at the translation portion of the matrix
1023 // and the extent at zero
1024 (*set_extent)[i] = 0.0f;
1025
1026 for (int j=0; j<3; j++) {
1027 (*set_extent)[i] += WWMath::Fabs(Row[i][j] * extent[j]);
1028 }
1029 }
1030}
1031
1032#endif /*Matrix3_H*/
#define WWASSERT
#define WWINLINE
Definition always.h:172
WWINLINE Matrix3x3 & operator/=(float d)
Definition matrix3.h:577
void Re_Orthogonalize(void)
Definition matrix3.cpp:348
WWINLINE void Rotate_X(float theta)
Definition matrix3.h:782
WWINLINE friend Matrix3x3 operator*(const Matrix3x3 &a, float d)
Definition matrix3.h:672
static const Matrix3x3 RotateX270
Definition matrix3.h:247
WWINLINE Matrix3x3 Inverse(void) const
Definition matrix3.h:434
Vector3 Row[3]
Definition matrix3.h:257
WWINLINE float Determinant(void) const
Definition matrix3.h:486
WWINLINE Matrix3x3 & operator+=(const Matrix3x3 &m)
Definition matrix3.h:523
WWINLINE Matrix3x3 & operator-=(const Matrix3x3 &m)
Definition matrix3.h:541
float Get_Y_Rotation(void) const
Definition matrix3.h:613
static const Matrix3x3 RotateZ270
Definition matrix3.h:253
static const Matrix3x3 RotateY180
Definition matrix3.h:249
static const Matrix3x3 RotateZ180
Definition matrix3.h:252
WWINLINE friend int operator==(const Matrix3x3 &a, const Matrix3x3 &b)
Definition matrix3.h:758
WWINLINE Vector3 Get_Y_Vector(void) const
Definition matrix3.h:642
static const Matrix3x3 RotateZ90
Definition matrix3.h:251
static const Matrix3x3 RotateY270
Definition matrix3.h:250
WWINLINE Matrix3x3(void)
Definition matrix3.h:86
WWINLINE void Rotate_Y(float theta)
Definition matrix3.h:816
float Get_Z_Rotation(void) const
Definition matrix3.h:631
WWINLINE Vector3 Get_Z_Vector(void) const
Definition matrix3.h:647
WWINLINE friend Matrix3x3 operator/(const Matrix3x3 &a, float d)
Definition matrix3.h:682
static WWINLINE void Rotate_Vector(const Matrix3x3 &tm, const Vector3 &in, Vector3 *out)
Definition matrix3.h:982
WWINLINE friend Matrix3x3 operator+(const Matrix3x3 &a, const Matrix3x3 &b)
Definition matrix3.h:691
static const Matrix3x3 RotateX180
Definition matrix3.h:246
static WWINLINE void Add(const Matrix3x3 &a, const Matrix3x3 &b, Matrix3x3 *res)
Definition matrix3.h:700
static const Matrix3x3 RotateY90
Definition matrix3.h:248
WWINLINE void Rotate_AABox_Extent(const Vector3 &extent, Vector3 *new_extent)
Definition matrix3.h:1017
WWINLINE friend void Swap(Matrix3x3 &a, Matrix3x3 &b)
static void Multiply(const Matrix3x3 &a, const Matrix3x3 &b, Matrix3x3 *res)
Definition matrix3.cpp:290
WWINLINE Vector3 & operator[](int i)
Definition matrix3.h:120
static const Matrix3x3 Identity
Definition matrix3.h:244
WWINLINE friend int operator!=(const Matrix3x3 &a, const Matrix3x3 &b)
Definition matrix3.h:764
WWINLINE Matrix3x3 & operator*=(float d)
Definition matrix3.h:559
WWINLINE Matrix3x3 Transpose(void) const
Definition matrix3.h:413
static WWINLINE void Subtract(const Matrix3x3 &a, const Matrix3x3 &b, Matrix3x3 *res)
Definition matrix3.h:720
static WWINLINE void Transpose_Rotate_Vector(const Matrix3x3 &tm, const Vector3 &in, Vector3 *out)
Definition matrix3.h:1000
WWINLINE void Make_Identity(void)
Definition matrix3.h:326
WWINLINE Matrix3x3 & operator=(const Matrix3x3 &m)
Definition matrix3.h:505
float Get_X_Rotation(void) const
Definition matrix3.h:595
WWINLINE friend Matrix3x3 operator-(const Matrix3x3 &a)
Definition matrix3.h:667
void Set(const Matrix3D &m)
Definition matrix3.cpp:148
WWINLINE void Rotate_Z(float theta)
Definition matrix3.h:851
WWINLINE Vector3 Get_X_Vector(void) const
Definition matrix3.h:637
int Is_Orthogonal(void) const
Definition matrix3.cpp:331
static const Matrix3x3 RotateX90
Definition matrix3.h:245
float X
Definition vector3.h:90
WWINLINE float Length2(void) const
Definition vector3.h:469
float Z
Definition vector3.h:92
float Y
Definition vector3.h:91
static WWINLINE void Subtract(const Vector3 &a, const Vector3 &b, Vector3 *c)
Definition vector3.h:576
static WWINLINE void Add(const Vector3 &a, const Vector3 &b, Vector3 *c)
Definition vector3.h:555
WWINLINE void Set(float x, float y, float z)
Definition vector3.h:103
static float Atan2(float y, float x)
Definition wwmath.h:150
static WWINLINE float Fabs(float val)
Definition wwmath.h:113
WWINLINE Matrix3x3 operator-(const Matrix3x3 &a)
Definition matrix3.h:667
WWINLINE Matrix3x3 operator+(const Matrix3x3 &a, const Matrix3x3 &b)
Definition matrix3.h:691
WWINLINE Matrix3x3 operator/(const Matrix3x3 &a, float d)
Definition matrix3.h:682
WWINLINE int operator==(const Matrix3x3 &a, const Matrix3x3 &b)
Definition matrix3.h:758
WWINLINE int operator!=(const Matrix3x3 &a, const Matrix3x3 &b)
Definition matrix3.h:764
WWINLINE Matrix3x3 Create_Y_Rotation_Matrix3(float s, float c)
Definition matrix3.h:922
WWINLINE Matrix3x3 operator*(const Matrix3x3 &a, float d)
Definition matrix3.h:672
#define ROWCOL(i, j)
WWINLINE Matrix3x3 Create_X_Rotation_Matrix3(float s, float c)
Definition matrix3.h:886
WWINLINE Matrix3x3 Create_Z_Rotation_Matrix3(float s, float c)
Definition matrix3.h:958
int q
Definition test1.cpp:94