1 | #ifndef GFXMATH_MAT4_INCLUDED // -*- C++ -*-
|
---|
2 | #define GFXMATH_MAT4_INCLUDED
|
---|
3 |
|
---|
4 | /************************************************************************
|
---|
5 |
|
---|
6 | 4x4 Matrix class
|
---|
7 |
|
---|
8 | $Id: Mat4.h,v 1.14 1997/06/18 15:55:22 garland Exp $
|
---|
9 |
|
---|
10 | ************************************************************************/
|
---|
11 |
|
---|
12 | #include <gfx/math/Vec3.h>
|
---|
13 | #include <gfx/math/Vec4.h>
|
---|
14 |
|
---|
15 | namespace simplif
|
---|
16 | {
|
---|
17 | class Mat4
|
---|
18 | {
|
---|
19 | private:
|
---|
20 | Vec4 row[4];
|
---|
21 |
|
---|
22 | protected:
|
---|
23 |
|
---|
24 | inline void copy(const Mat4& m);
|
---|
25 | inline Vec4 col(int i) const
|
---|
26 | { return Vec4(row[0][i],row[1][i],row[2][i],row[3][i]); }
|
---|
27 |
|
---|
28 | public:
|
---|
29 | // Standard matrices
|
---|
30 | static Mat4 identity;
|
---|
31 | static Mat4 zero;
|
---|
32 | static Mat4 unit;
|
---|
33 |
|
---|
34 | static Mat4 trans(real,real,real);
|
---|
35 | static Mat4 scale(real,real,real);
|
---|
36 | static Mat4 xrot(real); //
|
---|
37 | static Mat4 yrot(real); // Arguments are in radians
|
---|
38 | static Mat4 zrot(real); //
|
---|
39 |
|
---|
40 |
|
---|
41 | // Standard constructors
|
---|
42 | Mat4() { copy(zero); }
|
---|
43 | Mat4(const Vec4& r0,const Vec4& r1,const Vec4& r2,const Vec4& r3)
|
---|
44 | { row[0]=r0; row[1]=r1; row[2]=r2; row[3]=r3; }
|
---|
45 | Mat4(const Mat4& m) { copy(m); }
|
---|
46 |
|
---|
47 | // Access methods
|
---|
48 | // M(i, j) == row i;col j
|
---|
49 | real& operator()(int i, int j) { return row[i][j]; }
|
---|
50 | real operator()(int i, int j) const { return row[i][j]; }
|
---|
51 | const Vec4& operator[](int i) const { return row[i]; }
|
---|
52 |
|
---|
53 | // Comparison methods
|
---|
54 | inline int operator==(const Mat4&);
|
---|
55 |
|
---|
56 | // Assignment methods
|
---|
57 | inline Mat4& operator=(const Mat4& m) { copy(m); return *this; }
|
---|
58 | inline Mat4& operator+=(const Mat4& m);
|
---|
59 | inline Mat4& operator-=(const Mat4& m);
|
---|
60 |
|
---|
61 | inline Mat4& operator*=(real s);
|
---|
62 | inline Mat4& operator/=(real s);
|
---|
63 |
|
---|
64 |
|
---|
65 | // Arithmetic methods
|
---|
66 | inline Mat4 operator+(const Mat4& m) const;
|
---|
67 | inline Mat4 operator-(const Mat4& m) const;
|
---|
68 | inline Mat4 operator-() const;
|
---|
69 |
|
---|
70 | inline Mat4 operator*(real s) const;
|
---|
71 | inline Mat4 operator/(real s) const;
|
---|
72 | Mat4 operator*(const Mat4& m) const;
|
---|
73 |
|
---|
74 | inline Vec4 operator*(const Vec4& v) const; // [x y z w]
|
---|
75 | inline Vec3 operator*(const Vec3& v) const; // [x y z w]
|
---|
76 |
|
---|
77 | // Matrix operations
|
---|
78 | real det() const;
|
---|
79 | Mat4 transpose() const;
|
---|
80 | Mat4 adjoint() const;
|
---|
81 | real inverse(Mat4&) const;
|
---|
82 | real cramerInverse(Mat4&) const;
|
---|
83 |
|
---|
84 | // Input/Output methods
|
---|
85 | // friend ostream& operator<<(ostream&, const Mat4&);
|
---|
86 | // friend istream& operator>>(istream&, Mat4&);
|
---|
87 | };
|
---|
88 |
|
---|
89 |
|
---|
90 | inline void Mat4::copy(const Mat4& m)
|
---|
91 | {
|
---|
92 | row[0] = m.row[0]; row[1] = m.row[1];
|
---|
93 | row[2] = m.row[2]; row[3] = m.row[3];
|
---|
94 | }
|
---|
95 |
|
---|
96 | inline int Mat4::operator==(const Mat4& m)
|
---|
97 | {
|
---|
98 | return row[0]==m.row[0] &&
|
---|
99 | row[1]==m.row[1] &&
|
---|
100 | row[2]==m.row[2] &&
|
---|
101 | row[3]==m.row[3] ;
|
---|
102 | }
|
---|
103 |
|
---|
104 | inline Mat4& Mat4::operator+=(const Mat4& m)
|
---|
105 | {
|
---|
106 | row[0] += m.row[0]; row[1] += m.row[1];
|
---|
107 | row[2] += m.row[2]; row[3] += m.row[3];
|
---|
108 | return *this;
|
---|
109 | }
|
---|
110 |
|
---|
111 | inline Mat4& Mat4::operator-=(const Mat4& m)
|
---|
112 | {
|
---|
113 | row[0] -= m.row[0]; row[1] -= m.row[1];
|
---|
114 | row[2] -= m.row[2]; row[3] -= m.row[3];
|
---|
115 | return *this;
|
---|
116 | }
|
---|
117 |
|
---|
118 | inline Mat4& Mat4::operator*=(real s)
|
---|
119 | {
|
---|
120 | row[0] *= s; row[1] *= s; row[2] *= s; row[3] *= s;
|
---|
121 | return *this;
|
---|
122 | }
|
---|
123 |
|
---|
124 | inline Mat4& Mat4::operator/=(real s)
|
---|
125 | {
|
---|
126 | row[0] /= s; row[1] /= s; row[2] /= s; row[3] /= s;
|
---|
127 | return *this;
|
---|
128 | }
|
---|
129 |
|
---|
130 | inline Mat4 Mat4::operator+(const Mat4& m) const
|
---|
131 | {
|
---|
132 | return Mat4(row[0]+m.row[0],
|
---|
133 | row[1]+m.row[1],
|
---|
134 | row[2]+m.row[2],
|
---|
135 | row[3]+m.row[3]);
|
---|
136 | }
|
---|
137 |
|
---|
138 | inline Mat4 Mat4::operator-(const Mat4& m) const
|
---|
139 | {
|
---|
140 | return Mat4(row[0]-m.row[0],
|
---|
141 | row[1]-m.row[1],
|
---|
142 | row[2]-m.row[2],
|
---|
143 | row[3]-m.row[3]);
|
---|
144 | }
|
---|
145 |
|
---|
146 | inline Mat4 Mat4::operator-() const
|
---|
147 | {
|
---|
148 | return Mat4(-row[0], -row[1], -row[2], -row[3]);
|
---|
149 | }
|
---|
150 |
|
---|
151 | inline Mat4 Mat4::operator*(real s) const
|
---|
152 | {
|
---|
153 | return Mat4(row[0]*s, row[1]*s, row[2]*s, row[3]*s);
|
---|
154 | }
|
---|
155 |
|
---|
156 | inline Mat4 Mat4::operator/(real s) const
|
---|
157 | {
|
---|
158 | return Mat4(row[0]/s, row[1]/s, row[2]/s, row[3]/s);
|
---|
159 | }
|
---|
160 |
|
---|
161 | inline Vec4 Mat4::operator*(const Vec4& v) const
|
---|
162 | {
|
---|
163 | return Vec4(row[0]*v, row[1]*v, row[2]*v, row[3]*v);
|
---|
164 | }
|
---|
165 |
|
---|
166 | //
|
---|
167 | // Transform a homogeneous 3-vector and reproject into normal 3-space
|
---|
168 | //
|
---|
169 | inline Vec3 Mat4::operator*(const Vec3& v) const
|
---|
170 | {
|
---|
171 | Vec4 u=Vec4(v,1);
|
---|
172 | real w=row[3]*u;
|
---|
173 |
|
---|
174 | if(w==0.0)
|
---|
175 | return Vec3(row[0]*u, row[1]*u, row[2]*u);
|
---|
176 | else
|
---|
177 | return Vec3(row[0]*u/w, row[1]*u/w, row[2]*u/w);
|
---|
178 | }
|
---|
179 |
|
---|
180 | /* inline ostream& operator<<(ostream& out, const Mat4& M)
|
---|
181 | {
|
---|
182 | return out<<M.row[0]<<endl<<M.row[1]<<endl<<M.row[2]<<endl<<M.row[3];
|
---|
183 | }
|
---|
184 |
|
---|
185 | inline istream& operator>>(istream& in, Mat4& M)
|
---|
186 | {
|
---|
187 | return in >> M.row[0] >> M.row[1] >> M.row[2] >> M.row[3];
|
---|
188 | }
|
---|
189 | */
|
---|
190 | extern bool cholesky(Mat4&, Vec4&);
|
---|
191 | extern bool jacobi(const Mat4& m, Vec4& vals, Vec4 vecs[4]);
|
---|
192 | }
|
---|
193 |
|
---|
194 | // GFXMATH_MAT4_INCLUDED
|
---|
195 | #endif
|
---|