source: golgotha/src/maxtool/sdk_inc/matrix3.h @ 80

Last change on this file since 80 was 80, checked in by Sam Hocevar, 12 years ago
  • Adding the Golgotha source code. Not sure what's going to be interesting in there, but since it's all public domain, there's certainly stuff to pick up.
  • Property svn:keywords set to Id
File size: 5.7 KB
Line 
1/**********************************************************************
2 *<
3        FILE: matrix3.h
4
5        DESCRIPTION: Class definitions for Matrix3
6
7        CREATED BY: Dan Silva
8
9        HISTORY:
10
11 *>     Copyright (c) 1994, All Rights Reserved.
12 **********************************************************************/
13#ifndef _MATRIX3_H
14
15#define _MATRIX3_H
16
17#include "ioapi.h"
18#include "point3.h"
19
20//Flags
21#define POS_IDENT  1
22#define ROT_IDENT  2
23#define SCL_IDENT  4
24#define MAT_IDENT (POS_IDENT|ROT_IDENT|SCL_IDENT)
25
26typedef float MRow[3];
27
28struct Point4 {
29        float r[4];
30        };
31
32class Quat;
33
34class Matrix3 {
35        friend Matrix3 DllExport RotateXMatrix(float angle);   
36        friend Matrix3 DllExport RotateYMatrix(float angle);
37        friend Matrix3 DllExport RotateZMatrix(float angle);
38        friend Matrix3 DllExport TransMatrix(const Point3& p);
39        friend Matrix3 DllExport ScaleMatrix(const Point3& s);
40        friend Matrix3 DllExport RotateYPRMatrix(float Yaw, float Pitch, float Roll);
41        friend Matrix3 DllExport RotAngleAxisMatrix(Point3& axis, float angle);
42        friend Matrix3 DllExport Inverse(const Matrix3& M);
43        friend Point3 DllExport operator*(const Matrix3& A, const Point3& V);
44        friend Point3 DllExport operator*(const Point3& V, const Matrix3& A);
45        friend Point3 DllExport VectorTransform(const Matrix3& M, const Point3& V);
46        friend Matrix3 DllExport XFormMat(const Matrix3& xm, const Matrix3& m);
47
48        friend class Quat;
49        float m[4][3];
50        // Access i-th row as Point3 for read or assignment:
51        Point3& operator[](int i) { return((Point3&)(*m[i]));  }
52        Point3& operator[](int i) const { return((Point3&)(*m[i])); }
53        DWORD flags;
54
55public:
56        // if change any components directly via GetAddr, must call this
57        void SetNotIdent() { flags &= ~MAT_IDENT; }
58        void SetIdentFlags(DWORD f) { flags &= ~MAT_IDENT; flags |= f; }
59        DWORD GetIdentFlags() const { return flags; }
60        void ClearIdentFlag(DWORD f) { flags &= ~f; }
61        BOOL IsIdentity() { return ((flags&MAT_IDENT)==MAT_IDENT); }
62
63        // CAUTION: if you change the matrix via this pointer, you MUST clear the
64        // proper IDENT flags !!!
65        MRow* GetAddr() const { return (MRow *)(m); }
66
67        // Constructors
68        Matrix3(){ flags = 0; }  // NO INITIALIZATION done in this constructor!!                                 
69        Matrix3(BOOL init) {flags=0; IdentityMatrix();} // RB: An option to initialize
70        DllExport Matrix3(float (*fp)[3]);
71
72        // Assignment operators
73        DllExport Matrix3& operator-=( const Matrix3& M);
74        DllExport Matrix3& operator+=( const Matrix3& M);
75        DllExport Matrix3& operator*=( const Matrix3& M);       // Matrix multiplication
76
77        // Operations on matrix
78        DllExport void IdentityMatrix();                // Make into the Identity Matrix
79        DllExport void Zero();          // set all elements to 0
80       
81        Point3 GetRow(int i) const { return (*this)[i]; }       
82        DllExport void SetRow(int i, Point3 p);
83
84
85        DllExport Point4 Matrix3::GetColumn(int i);
86        DllExport void Matrix3::SetColumn(int i,  Point4 col);
87
88        // zero the translation part;
89        DllExport void NoTrans();
90        // null out the rotation part;
91        DllExport void NoRot();
92        // null out the scale part;
93        DllExport void NoScale();
94       
95        // This is an "unbiased" orthogonalization
96        // It seems to take a maximum of 4 iterations to converge.
97        DllExport void Orthogonalize();
98
99        // Access the translation row
100        void SetTrans(const Point3 p) { (*this)[3] = p;  flags &= ~POS_IDENT; }
101        void SetTrans(int i, float v) { (*this)[3][i] = v; flags &= ~POS_IDENT; }
102        Point3 GetTrans() { return (*this)[3]; }
103   
104        void SetScale(const Point3 p);
105
106        // Apply Incremental transformations to this matrix
107        // Equivalent to multiplying on the RIGHT by transform
108        DllExport void Translate(const Point3& p);
109        DllExport void RotateX(float angle); 
110        DllExport void RotateY(float angle);
111        DllExport void RotateZ(float angle);
112        // if trans = FALSE the translation component is unaffected:
113        DllExport void Scale(const Point3& s, BOOL trans = FALSE);
114
115        // Apply Incremental transformations to this matrix
116        // Equivalent to multiplying on the LEFT by transform
117        DllExport void PreTranslate(const Point3& p);
118        DllExport void PreRotateX(float angle); 
119        DllExport void PreRotateY(float angle);
120        DllExport void PreRotateZ(float angle);
121        // if trans = FALSE the translation component is unaffected:
122        DllExport void PreScale(const Point3& s, BOOL trans = FALSE);
123               
124        DllExport Matrix3 operator*(const Matrix3&) const;
125        DllExport Matrix3 operator+(const Matrix3&) const;
126        DllExport Matrix3 operator-(const Matrix3&) const;
127
128        DllExport IOResult Save(ISave* isave);
129        DllExport IOResult Load(ILoad* iload);
130        };
131
132
133// Build new matrices for transformations
134Matrix3 DllExport RotateXMatrix(float angle);   
135Matrix3 DllExport RotateYMatrix(float angle);
136Matrix3 DllExport RotateZMatrix(float angle);
137Matrix3 DllExport TransMatrix(const Point3& p);
138Matrix3 DllExport ScaleMatrix(const Point3& s);
139Matrix3 DllExport RotateYPRMatrix(float Yaw, float Pitch, float Roll);
140Matrix3 DllExport RotAngleAxisMatrix(Point3& axis, float angle);
141 
142Matrix3 DllExport Inverse(const Matrix3& M);  // return Inverse of matrix
143
144// These two versions of transforming a point with a matrix do the same thing,
145// regardless of the order of operands (linear algebra rules notwithstanding).
146Point3 DllExport operator*(const Matrix3& A, const Point3& V); // Transform Point with matrix
147Point3 DllExport operator*(const Point3& V, const Matrix3& A); // Transform Point with matrix
148
149
150Point3 DllExport VectorTransform(const Matrix3& M, const Point3& V);
151
152// transformats matrix m so it is applied in the space of matrix xm:
153//  returns xm*m*Inverse(xm)
154Matrix3 DllExport XFormMat(const Matrix3& xm, const Matrix3& m);
155
156#endif _MATRIX3_H
Note: See TracBrowser for help on using the repository browser.