3DCoat Core API
The 3DCoat API documentation.
Loading...
Searching...
No Matches
cMat4.h
1#pragma once
2
6class APICALL cMat4 {
7public:
8 enum EZeroCtor { ZeroCtor };
9 enum EIdentityCtor { IdentityCtor };
10 enum ERowsCtor { RowsCtor };
11 enum EColsCtor { ColsCtor };
12
13 cMat4();
14 cMat4(const EZeroCtor);
15 cMat4(const EIdentityCtor);
16 cMat4(const ERowsCtor, const cVec4 &Row0, const cVec4 &Row1, const cVec4 &Row2, const cVec4 &Row3);
17 cMat4(const EColsCtor, const cVec4 &Col0, const cVec4 &Col1, const cVec4 &Col2, const cVec4 &Col3);
18 cMat4(const cMat3 &Rotation, const cVec3 &Translation);
19 cMat4(const cVec3 &Scaling, const cVec3 &Translation);
20 cMat4(const float _00, const float _01, const float _02, const float _03,
21 const float _10, const float _11, const float _12, const float _13,
22 const float _20, const float _21, const float _22, const float _23,
23 const float _30, const float _31, const float _32, const float _33);
24 cMat4(const cMat4& v) { memcpy(this, &v, sizeof(v)); }
25
26 void Copy(const float *Float16);
27 void CopyTransposed(const float *Float16);
28
29 void SetZero();
30 void SetIdentity();
31
32 const cVec4 & GetRow(const int Index) const;
33 const cVec4 & GetRow0() const;
34 const cVec4 & GetRow1() const;
35 const cVec4 & GetRow2() const;
36 const cVec4 & GetRow3() const;
37
38 cVec4 & Row(const int Index);
39 cVec4 & Row0();
40 cVec4 & Row1();
41 cVec4 & Row2();
42 cVec4 & Row3();
43
44 void SetRow(const int Index, const cVec4 &);
45 void SetRow0(const cVec4 &);
46 void SetRow1(const cVec4 &);
47 void SetRow2(const cVec4 &);
48 void SetRow3(const cVec4 &);
49
50 void SetRow(const int Index, const float X, const float Y, const float Z, const float W);
51 void SetRow0(const float X, const float Y, const float Z, const float W);
52 void SetRow1(const float X, const float Y, const float Z, const float W);
53 void SetRow2(const float X, const float Y, const float Z, const float W);
54 void SetRow3(const float X, const float Y, const float Z, const float W);
55
56 const cVec4 GetCol(const int Index) const;
57 const cVec4 GetCol0() const;
58 const cVec4 GetCol1() const;
59 const cVec4 GetCol2() const;
60 const cVec4 GetCol3() const;
61
62 void SetCol(const int Index, const cVec4 &);
63 void SetCol0(const cVec4 &);
64 void SetCol1(const cVec4 &);
65 void SetCol2(const cVec4 &);
66 void SetCol3(const cVec4 &);
67
68 void SetCol(const int Index, const float X, const float Y, const float Z, const float W);
69 void SetCol0(const float X, const float Y, const float Z, const float W);
70 void SetCol1(const float X, const float Y, const float Z, const float W);
71 void SetCol2(const float X, const float Y, const float Z, const float W);
72 void SetCol3(const float X, const float Y, const float Z, const float W);
73
74 void SetElem(int Row, int Col, float Value);
75 float GetElem(const int Row, const int Col) const;
76 float & Elem(const int Row, const int Col);
77
78 const cVec4 & operator [] (const int Row) const;
79 cVec4 & operator [] (const int Row);
80
81 float operator () (const int Row, const int Col) const;
82 float & operator () (const int Row, const int Col);
83
84 float Trace() const;
85 float Determinant() const;
86
87 bool operator == (const cMat4 &) const;
88 static bool Equals(const cMat4 &, const cMat4 &, const float Eps = cMath::MatrixEpsilon);
89
90 bool IsZero(float Eps = cMath::MatrixEpsilon) const;
91 bool IsIdentity(float Eps = cMath::MatrixEpsilon) const;
92 bool IsSymmetric( float Eps = cMath::MatrixEpsilon ) const;
93 bool IsOrthonormal( float Eps = cMath::MatrixEpsilon ) const;
94
95 const cMat4 operator - () const;
96
97 cMat4& operator += (const cMat4 &R);
98 cMat4& operator -= (const cMat4 &R);
99 cMat4& operator *= (const cMat4 &R);
100 cMat4& operator *= (const float);
101 cMat4& operator /= (const float);
102
103 const cMat4 operator + (const cMat4 &R) const;
104 const cMat4 operator - (const cMat4 &R) const;
105 const cMat4 operator * (const cMat4 &R) const;
106 const cMat4 operator * (const float) const;
107 friend const cMat4 operator * (const float, const cMat4 &);
108 const cMat4 operator / (const float) const;
109
110 void Add(const cMat4 &R);
111 void Sub(const cMat4 &R);
112 void Mul(const cMat4 &R);
113 void Mul(const float s);
114
115 static const cMat4 Add(const cMat4 &L, const cMat4 &R);
116 static const cMat4 Sub(const cMat4 &L, const cMat4 &R);
117 static const cMat4 Mul(const cMat4 &L, const cMat4 &R);
118 static const cMat4 Mul(const cMat4 &L, const float s);
119
120 const float * ToFloatPtr() const;
121 float * ToFloatPtr();
122
123 const cStr ToString(const int Prec = 2) const;
124
125 const cMat3 ToMat3() const;
126 const cMat3 ToNormalMatrix() const;
127 const cQuat ToQuat() const;
128
129 const cVec3 GetTranslation() const;
130 void SetTranslation( const cVec3 & );
131
132 const cVec3 GetScaling() const;
133 void SetScaling( const cVec3 & );
134
135 cAngles GetRotation() const;
136 void SetRotation( const cAngles & );
137
138 static const cMat4 Transpose(const cMat4 &);
139 void Transpose();
140
141 static bool Invert(const cMat4 &Fm, cMat4 *To);
142 bool Invert() { return Invert(*this, this); }
143
144 static const cMat4 Zero;
145 static const cMat4 Identity;
146
147 static const cMat4 Translation(const float X, const float Y);
148 static const cMat4 Translation(const float X, const float Y, const float Z);
149 static const cMat4 Translation(const cVec2 &XY);
150 static const cMat4 Translation(const cVec3 &XYZ);
151
152 static const cMat4 Rotation(const cVec3 &Axis, const float Angle);
153 static const cMat4 RotationX(const float Angle);
154 static const cMat4 RotationY(const float Angle);
155 static const cMat4 RotationZ(const float Angle);
156 static const cMat4 RotationXYZ(const float Pitch, const float Yaw, const float Roll);
157 static const cMat4 EulerZYX(const float eulerX, const float eulerY, const float eulerZ);
158
159 static const cMat4 RotationAt(const cVec2 &Orig, const float Angle);
160 static const cMat4 RotationAt(const cVec3 &Orig, const cVec3 &Axis, const float Angle);
161
162 static const cMat4 Scaling(const float XYZ);
163 static const cMat4 Scaling(const float X, const float Y);
164 static const cMat4 Scaling(const float X, const float Y, const float Z);
165 static const cMat4 Scaling(const cVec2 &XY);
166 static const cMat4 Scaling(const cVec3 &XYZ);
167
168 static const cMat4 ScalingAt(const float OrigX, const float OrigY, const float ScaleXY);
169 static const cMat4 ScalingAt(const float OrigX, const float OrigY, const float ScaleX, const float ScaleY);
170
171 static const cMat4 ScalingAt(const cVec2 &Orig, const float ScaleXY);
172 static const cMat4 ScalingAt(const cVec2 &Orig, const float ScaleX, const float ScaleY);
173 static const cMat4 ScalingAt(const cVec2 &Orig, const cVec2 &Scale);
174
175 static const cMat4 ScalingAt(const cVec3 &Orig, const float ScaleXYZ);
176 static const cMat4 ScalingAt(const cVec3 &Orig, const float ScaleX, const float ScaleY, const float ScaleZ);
177 static const cMat4 ScalingAt(const cVec3 &Orig, const cVec3 &Scale);
178 static const cMat4 ScalingAt(const cVec3 &Orig, const cVec3 &Dir, float Scale);
179
180 static const cMat4 Perspective(float YFov, float AspectWtoH, float Znear, float Zfar);
181 static const cMat4 PerspectiveInf(const float YFov, const float AspectWtoH, const float Znear);
182
183 static const cMat4 Ortho(const float Width, const float Height, const float Znear, const float Zfar);
184 static const cMat4 Ortho(const float Left, const float Right, const float Bottom, const float Top, const float Znear, const float Zfar);
185 static const cMat4 Ortho(const cBounds &B);
186
187 //---------------
188 // Side Axis
189 //---------------
190 // 0 +X
191 // 1 -X
192 // 2 +Y
193 // 3 -Y
194 // 4 +Z
195 // 5 -Z
196 //---------------
197 static const cMat4 CubeViewProjection(const cVec3 &Pos, const int Side, const float Radius, const bool GL);
198 static const cMat4 LookAtViewProjection(const cVec3 &LookFrom, const cVec3 &LookAt, const float FovY, const float AspectYtoH, const float Znear, const float Zfar);
199
200 std::tuple<float, float, float, float, float, float, float, float, float, float, float, float, float, float, float, float> __getstate__();
201 void __setstate__(const std::tuple<float, float, float, float, float, float, float, float, float, float, float, float, float, float, float, float>& state);
202 const std::string __repr__() const;
203private:
204 cVec4 m_Rows[4];
205};
206
207// cMat4.ctor : ()
208inline cMat4::cMat4() {
209}
210
211// cMat4.ctor : (const ERowsCtor, const cVec4 &, const cVec4 &, const cVec4 &, const cVec4 &)
212inline cMat4::cMat4(const ERowsCtor, const cVec4 &Row0, const cVec4 &Row1, const cVec4 &Row2, const cVec4 &Row3) {
213 m_Rows[0] = Row0;
214 m_Rows[1] = Row1;
215 m_Rows[2] = Row2;
216 m_Rows[3] = Row3;
217}
218
219// cMat4.ctor : (const EColsCtor, const cVec4 &, const cVec4 &, const cVec4 &, const cVec4 &)
220inline cMat4::cMat4(const EColsCtor, const cVec4 &Col0, const cVec4 &Col1, const cVec4 &Col2, const cVec4 &Col3) {
221 m_Rows[0].Set(Col0.x, Col1.x, Col2.x, Col3.x);
222 m_Rows[1].Set(Col0.y, Col1.y, Col2.y, Col3.y);
223 m_Rows[2].Set(Col0.z, Col1.z, Col2.z, Col3.z);
224 m_Rows[3].Set(Col0.w, Col1.w, Col2.w, Col3.w);
225}
226
227// cMat4.ctor : (const EZeroCtor)
228inline cMat4::cMat4(const EZeroCtor) {
229 m_Rows[0].SetZero();
230 m_Rows[1].SetZero();
231 m_Rows[2].SetZero();
232 m_Rows[3].SetZero();
233}
234
235// cMat4.ctor : (const EIdentityCtor)
236inline cMat4::cMat4(const EIdentityCtor) {
237 m_Rows[0].Set(1.0f, 0.0f, 0.0f, 0.0f);
238 m_Rows[1].Set(0.0f, 1.0f, 0.0f, 0.0f);
239 m_Rows[2].Set(0.0f, 0.0f, 1.0f, 0.0f);
240 m_Rows[3].Set(0.0f, 0.0f, 0.0f, 1.0f);
241}
242
243// cMat4.ctor : (const cMat3 &, const cVec3 &)
244inline cMat4::cMat4(const cMat3 &Rotation, const cVec3 &Translation) {
245 m_Rows[0].Set(Rotation.GetRow0(), 0.0f);
246 m_Rows[1].Set(Rotation.GetRow1(), 0.0f);
247 m_Rows[2].Set(Rotation.GetRow2(), 0.0f);
248 m_Rows[3].Set(Translation, 1.0f);
249}
250
251// cMat4.ctor : (const cVec3 &, const cVec3 &)
252inline cMat4::cMat4(const cVec3 &Scaling, const cVec3 &Translation) {
253 m_Rows[0].Set(Scaling[0], 0.0f, 0.0f, 0.0f);
254 m_Rows[1].Set(0.0f, Scaling[1], 0.0f, 0.0f);
255 m_Rows[2].Set(0.0f, 0.0f, Scaling[2], 0.0f);
256 m_Rows[3].Set(Translation, 1.0f);
257}
258
259// cMat4.ctor
260inline cMat4::cMat4(const float _00, const float _01, const float _02, const float _03,
261 const float _10, const float _11, const float _12, const float _13,
262 const float _20, const float _21, const float _22, const float _23,
263 const float _30, const float _31, const float _32, const float _33) {
264 m_Rows[0].Set(_00, _01, _02, _03);
265 m_Rows[1].Set(_10, _11, _12, _13);
266 m_Rows[2].Set(_20, _21, _22, _23);
267 m_Rows[3].Set(_30, _31, _32, _33);
268}
269
270// cMat4::Copy
271inline void cMat4::Copy(const float *Float16) {
272 m_Rows[0].Set(Float16[0], Float16[1], Float16[2], Float16[3]);
273 m_Rows[1].Set(Float16[4], Float16[5], Float16[6], Float16[7]);
274 m_Rows[2].Set(Float16[8], Float16[9], Float16[10], Float16[11]);
275 m_Rows[3].Set(Float16[12], Float16[13], Float16[14], Float16[15]);
276}
277
278// cMat4::CopyTransposed
279inline void cMat4::CopyTransposed(const float *Float16) {
280 m_Rows[0].Set(Float16[0], Float16[4], Float16[8], Float16[12]);
281 m_Rows[1].Set(Float16[1], Float16[5], Float16[9], Float16[13]);
282 m_Rows[2].Set(Float16[2], Float16[6], Float16[10], Float16[14]);
283 m_Rows[3].Set(Float16[3], Float16[7], Float16[11], Float16[15]);
284}
285
286// cMat4::SetZero
287inline void cMat4::SetZero() {
288 m_Rows[0].SetZero();
289 m_Rows[1].SetZero();
290 m_Rows[2].SetZero();
291 m_Rows[3].SetZero();
292}
293
294// cMat4::SetIdentity
295inline void cMat4::SetIdentity() {
296 m_Rows[0].Set(1.0f, 0.0f, 0.0f, 0.0f);
297 m_Rows[1].Set(0.0f, 1.0f, 0.0f, 0.0f);
298 m_Rows[2].Set(0.0f, 0.0f, 1.0f, 0.0f);
299 m_Rows[3].Set(0.0f, 0.0f, 0.0f, 1.0f);
300}
301
302// cMat4::GetRow : const cVec4 & (const int) const
303inline const cVec4 & cMat4::GetRow(const int Index) const {
304 cAssert(Index >= 0 && Index < 4);
305 return m_Rows[Index];
306}
307
308// cMat4::GetRow0 : const cVec4 & () const
309inline const cVec4 & cMat4::GetRow0() const {
310 return m_Rows[0];
311}
312
313// cMat4::GetRow1 : const cVec4 & () const
314inline const cVec4 & cMat4::GetRow1() const {
315 return m_Rows[1];
316}
317
318// cMat4::GetRow2 : const cVec4 & () const
319inline const cVec4 & cMat4::GetRow2() const {
320 return m_Rows[2];
321}
322
323// cMat4::GetRow3 : const cVec4 & () const
324inline const cVec4 & cMat4::GetRow3() const {
325 return m_Rows[3];
326}
327
328// cMat4::Row : cVec4 & (const int)
329inline cVec4 & cMat4::Row(const int Index) {
330 cAssert(Index >= 0 && Index < 4);
331 return m_Rows[Index];
332}
333
334// cMat4::Row0 : cVec4 & ()
335inline cVec4 & cMat4::Row0() {
336 return m_Rows[0];
337}
338
339// cMat4::Row1 : cVec4 & ()
340inline cVec4 & cMat4::Row1() {
341 return m_Rows[1];
342}
343
344// cMat4::Row2 : cVec4 & ()
345inline cVec4 & cMat4::Row2() {
346 return m_Rows[2];
347}
348
349// cMat4::Row3 : cVec4 & ()
350inline cVec4 & cMat4::Row3() {
351 return m_Rows[3];
352}
353
354// cMat4::SetRow : void (const int, const cVec4 &)
355inline void cMat4::SetRow(const int Index, const cVec4 &r) {
356 cAssert(Index >= 0 && Index < 4);
357 m_Rows[Index] = r;
358}
359
360// cMat4::SetRow0 : void (const cVec4 &)
361inline void cMat4::SetRow0(const cVec4 &r) {
362 m_Rows[0] = r;
363}
364
365// cMat4::SetRow1 : void (const cVec4 &)
366inline void cMat4::SetRow1(const cVec4 &r) {
367 m_Rows[1] = r;
368}
369
370// cMat4::SetRow2 : void (const cVec4 &)
371inline void cMat4::SetRow2(const cVec4 &r) {
372 m_Rows[2] = r;
373}
374
375// cMat4::SetRow3 : void (const cVec4 &)
376inline void cMat4::SetRow3(const cVec4 &r) {
377 m_Rows[3] = r;
378}
379
380// cMat4::SetRow : void (const int, const float, const float, const float, const float)
381inline void cMat4::SetRow(const int Index, const float X, const float Y, const float Z, const float W) {
382 cAssert(Index >= 0 && Index < 4);
383 m_Rows[Index].Set(X, Y, Z, W);
384}
385
386// cMat4::SetRow0 : void (const float, const float, const float, const float)
387inline void cMat4::SetRow0(const float X, const float Y, const float Z, const float W) {
388 m_Rows[0].Set(X, Y, Z, W);
389}
390
391// cMat4::SetRow1 : void (const float, const float, const float, const float)
392inline void cMat4::SetRow1(const float X, const float Y, const float Z, const float W) {
393 m_Rows[1].Set(X, Y, Z, W);
394}
395
396// cMat4::SetRow2 : void (const float, const float, const float, const float)
397inline void cMat4::SetRow2(const float X, const float Y, const float Z, const float W) {
398 m_Rows[2].Set(X, Y, Z, W);
399}
400
401// cMat4::SetRow3 : void (const float, const float, const float, const float)
402inline void cMat4::SetRow3(const float X, const float Y, const float Z, const float W) {
403 m_Rows[3].Set(X, Y, Z, W);
404}
405
406// cMat4::GetCol
407inline const cVec4 cMat4::GetCol(const int Index) const {
408 cAssert(Index >= 0 && Index < 4);
409 return cVec4(m_Rows[0][Index], m_Rows[1][Index], m_Rows[2][Index], m_Rows[3][Index]);
410}
411
412// cMat4::GetCol0
413inline const cVec4 cMat4::GetCol0() const {
414 return cVec4(m_Rows[0].x, m_Rows[1].x, m_Rows[2].x, m_Rows[3].x);
415}
416
417// cMat4::GetCol1
418inline const cVec4 cMat4::GetCol1() const {
419 return cVec4(m_Rows[0].y, m_Rows[1].y, m_Rows[2].y, m_Rows[3].y);
420}
421
422// cMat4::GetCol2
423inline const cVec4 cMat4::GetCol2() const {
424 return cVec4(m_Rows[0].z, m_Rows[1].z, m_Rows[2].z, m_Rows[3].z);
425}
426
427// cMat4::GetCol3
428inline const cVec4 cMat4::GetCol3() const {
429 return cVec4(m_Rows[0].w, m_Rows[1].w, m_Rows[2].w, m_Rows[3].w);
430}
431
432// cMat4::SetCol : void (const int, const cVec4 &)
433inline void cMat4::SetCol(const int Index, const cVec4 &u) {
434 cAssert(Index >= 0 && Index < 4);
435 m_Rows[0][Index] = u.x;
436 m_Rows[1][Index] = u.y;
437 m_Rows[2][Index] = u.z;
438 m_Rows[3][Index] = u.w;
439}
440
441// cMat4::SetCol0 : void (const cVec4 &)
442inline void cMat4::SetCol0(const cVec4 &u) {
443 m_Rows[0].x = u.x;
444 m_Rows[1].x = u.y;
445 m_Rows[2].x = u.z;
446 m_Rows[3].x = u.w;
447}
448
449// cMat4::SetCol1 : void (const cVec4 &)
450inline void cMat4::SetCol1(const cVec4 &u) {
451 m_Rows[0].y = u.x;
452 m_Rows[1].y = u.y;
453 m_Rows[2].y = u.z;
454 m_Rows[3].y = u.w;
455}
456
457// cMat4::SetCol2 : void (const cVec4 &)
458inline void cMat4::SetCol2(const cVec4 &u) {
459 m_Rows[0].z = u.x;
460 m_Rows[1].z = u.y;
461 m_Rows[2].z = u.z;
462 m_Rows[3].z = u.w;
463}
464
465// cMat4::SetCol3 : void (const cVec4 &)
466inline void cMat4::SetCol3(const cVec4 &u) {
467 m_Rows[0].w = u.x;
468 m_Rows[1].w = u.y;
469 m_Rows[2].w = u.z;
470 m_Rows[3].w = u.w;
471}
472
473// cMat4::SetCol : void (const int, const float, const float, const float, const float)
474inline void cMat4::SetCol(const int Index, const float X, const float Y, const float Z, const float W) {
475 cAssert(Index >= 0 && Index < 4);
476 m_Rows[0][Index] = X;
477 m_Rows[1][Index] = Y;
478 m_Rows[2][Index] = Z;
479 m_Rows[3][Index] = W;
480}
481
482// cMat4::SetCol0 : void (const float, const float, const float, const float)
483inline void cMat4::SetCol0(const float X, const float Y, const float Z, const float W) {
484 m_Rows[0].x = X;
485 m_Rows[1].x = Y;
486 m_Rows[2].x = Z;
487 m_Rows[3].x = W;
488}
489
490// cMat4::SetCol1 : void (const float, const float, const float, const float)
491inline void cMat4::SetCol1(const float X, const float Y, const float Z, const float W) {
492 m_Rows[0].y = X;
493 m_Rows[1].y = Y;
494 m_Rows[2].y = Z;
495 m_Rows[3].y = W;
496}
497
498// cMat4::SetCol2 : void (const float, const float, const float, const float)
499inline void cMat4::SetCol2(const float X, const float Y, const float Z, const float W) {
500 m_Rows[0].z = X;
501 m_Rows[1].z = Y;
502 m_Rows[2].z = Z;
503 m_Rows[3].z = W;
504}
505
506// cMat4::SetCol3 : void (const float, const float, const float, const float)
507inline void cMat4::SetCol3(const float X, const float Y, const float Z, const float W) {
508 m_Rows[0].w = X;
509 m_Rows[1].w = Y;
510 m_Rows[2].w = Z;
511 m_Rows[3].w = W;
512}
513
514// cMat4::SetElem : float (int, int, float)
515inline void cMat4::SetElem(int Row, int Col, float Value) {
516 cAssert(Row >= 0 && Row < 4);
517 cAssert(Col >= 0 && Col < 4);
518 m_Rows[Row][Col] = Value;
519}
520
521// cMat4::GetElem : float (const int, const int) const
522inline float cMat4::GetElem(const int Row, const int Col) const {
523 cAssert(Row >= 0 && Row < 4);
524 cAssert(Col >= 0 && Col < 4);
525 return m_Rows[Row][Col];
526}
527
528// cMat4::Elem : float & (const int, const int)
529inline float & cMat4::Elem(const int Row, const int Col) {
530 cAssert(Row >= 0 && Row < 4);
531 cAssert(Col >= 0 && Col < 4);
532 return m_Rows[Row][Col];
533}
534
535// cMat4::operator [] : cVec4 & (const int)
536inline cVec4 & cMat4::operator [] (const int Row) {
537 cAssert(Row >= 0 && Row < 4);
538 return m_Rows[Row];
539}
540
541// cMat4::operator [] : const cVec4 & (const int) const
542inline const cVec4 & cMat4::operator [] (const int Row) const {
543 cAssert(Row >= 0 && Row < 4);
544 return m_Rows[Row];
545}
546
547// cMat4::operator () : float & (const int, const int)
548inline float & cMat4::operator () (const int Row, const int Col) {
549 cAssert(Row >= 0 && Row < 4 && Col >= 0 && Col < 4);
550 return m_Rows[Row][Col];
551}
552
553// cMat4::operator () : float(const int, const int) const
554inline float cMat4::operator () (const int Row, const int Col) const {
555 cAssert(Row >= 0 && Row < 4 && Col >= 0 && Col < 4);
556 return m_Rows[Row][Col];
557}
558
559// cMat4::operator - : const cMat4 () const
560inline const cMat4 cMat4::operator - () const {
561 return Mul(*this, -1.0f);
562}
563
564// cMat4::Add : void (const cMat4 &)
565inline void cMat4::Add(const cMat4 &R) {
566 float *l = (float *)this;
567 const float *r = (const float *)&R;
568
569 for(int i = 0; i < 16; i++) {
570 *l++ += *r++;
571 }
572}
573
574// cMat4::operator +=
575inline cMat4& cMat4::operator += (const cMat4 &R) {
576 Add(R);
577 return *this;
578}
579
580// cMat4::Add : const cMat4 (const cMat4 &, const cMat4 &)
581inline const cMat4 cMat4::Add(const cMat4 &L, const cMat4 &R) {
582 const float *l = (const float *)&L;
583 const float *r = (const float *)&R;
584
585 cMat4 S;
586 float *s = (float *)&S;
587
588 for(int i = 0; i < 16; i++) {
589 *s++ = *l++ + *r++;
590 }
591
592 return S;
593}
594
595// cMat4::operator +
596inline const cMat4 cMat4::operator + (const cMat4 &R) const {
597 return Add(*this, R);
598}
599
600// cMat4::Sub : void (const cMat4 &)
601inline void cMat4::Sub(const cMat4 &R) {
602 float *l = (float *)this;
603 const float *r = (const float *)&R;
604
605 for(int i = 0; i < 16; i++) {
606 *l++ -= *r++;
607 }
608}
609
610// cMat4::operator -=
611inline cMat4& cMat4::operator -= (const cMat4 &R) {
612 Sub(R);
613 return *this;
614}
615
616// cMat4::Sub : const cMat4 (const cMat4 &, const cMat4 &)
617inline const cMat4 cMat4::Sub(const cMat4 &L, const cMat4 &R) {
618 const float *l = (const float *)&L;
619 const float *r = (const float *)&R;
620
621 cMat4 D;
622 float *d = (float *)&D;
623
624 for(int i = 0; i < 16; i++) {
625 *d++ = *l++ - *r++;
626 }
627 return D;
628}
629
630// cMat4::operator -
631inline const cMat4 cMat4::operator - (const cMat4 &R) const {
632 return Sub(*this, R);
633}
634
635// cMat4::Mul : void (const cMat4 &)
636inline void cMat4::Mul(const cMat4 &R) {
637 float *l = reinterpret_cast<float *>(this);
638 const float *r = reinterpret_cast<const float *>(&R);
639
640 float t[4];
641 for(int i = 0; i < 4; i++) {
642 for(int j = 0; j < 4; j++) {
643 t[j] = l[0] * r[0 * 4 + j] + l[1] * r[1 * 4 + j] + l[2] * r[2 * 4 + j] + l[3] * r[3 * 4 + j];
644 }
645 l[0] = t[0];
646 l[1] = t[1];
647 l[2] = t[2];
648 l[3] = t[3];
649 l += 4;
650 }
651}
652
653// cMat4::operator *=
654inline cMat4& cMat4::operator *= (const cMat4 &R) {
655 Mul(R);
656 return *this;
657}
658
659// cMat4::Mul : void (const float)
660inline void cMat4::Mul(const float s) {
661 float *l = (float *)this;
662 for(int i = 0; i < 16; i++) {
663 *l++ *= s;
664 }
665}
666
667// cMat4::operator *= : void (const float)
668inline cMat4& cMat4::operator *= (const float s) {
669 Mul(s);
670 return *this;
671}
672
673// cMat4::operator /= : void (const float)
674inline cMat4& cMat4::operator /= (const float s) {
675 float is = 1.0f / s;
676 Mul(is);
677 return *this;
678}
679
680//-----------------------------------------------------------------------------
681// cMat4::Mul : const cMat4 (const cMat4 &, const cMat4 &)
682//-----------------------------------------------------------------------------
683inline const cMat4 cMat4::Mul(const cMat4 &L, const cMat4 &R) {
684 const float *l = reinterpret_cast<const float *>(&L);
685 const float *r = reinterpret_cast<const float *>(&R);
686 cMat4 M;
687 float *m = reinterpret_cast<float *>(&M);
688
689 for(int i = 0; i < 4; i++) {
690 for(int j = 0; j < 4; j++) {
691 *m = l[0] * r[0 * 4 + j] + l[1] * r[1 * 4 + j] + l[2] * r[2 * 4 + j] + l[3] * r[3 * 4 + j];
692 m++;
693 }
694 l += 4;
695 }
696 return M;
697} // cMat4::Mul
698
699// cMat4::operator * : const cMat4 (const cMat4 &) const
700inline const cMat4 cMat4::operator * (const cMat4 &R) const {
701 return Mul(*this, R);
702}
703
704// cMat4::Mul : const cMat4 (const cMat4 &, const float)
705inline const cMat4 cMat4::Mul(const cMat4 &L, const float s) {
706 cMat4 M;
707 float *m = (float *)&M;
708 const float *l = (const float *)&L;
709
710 for(int i = 0; i < 16; i++) {
711 *m++ = *l++ * s;
712 }
713 return M;
714}
715
716// cMat4::operator * : const cMat4 (const float) const
717inline const cMat4 cMat4::operator * (const float s) const {
718 return Mul(*this, s);
719}
720
721// operator * : const cMat4 (const float, const cMat4 &)
722inline const cMat4 operator * (const float s, const cMat4 &R) {
723 return cMat4::Mul(R, s);
724}
725
726// cMat4::operator / : const cMat4 (const float) const
727inline const cMat4 cMat4::operator / (const float s) const {
728 float is = 1.0f / s;
729 return Mul(*this, is);
730}
731
732// cMat4::Trace
733inline float cMat4::Trace() const {
734 return m_Rows[0][0] + m_Rows[1][1] + m_Rows[2][2] + m_Rows[3][3];
735}
736
737// cMat4::operator == : bool(const cMat4 &) const
738inline bool cMat4::operator == (const cMat4 &u) const {
739 return cMat4::Equals( *this, u );
740}
741
742// cMat4::Equals
743inline bool cMat4::Equals(const cMat4 &P, const cMat4 &Q, const float Eps) {
744 if(cVec4::Equals(P[0], Q[0], Eps) && cVec4::Equals(P[1], Q[1], Eps) && cVec4::Equals(P[2], Q[2], Eps) && cVec4::Equals(P[3], Q[3], Eps)) {
745 return true;
746 }
747 return false;
748}
749
750// cMat4::IsZero
751inline bool cMat4::IsZero(float Eps) const {
752 return Equals(*this, Zero, Eps);
753}
754
755// cMat4::IsIdentity
756inline bool cMat4::IsIdentity(float Eps) const {
757 return Equals(*this, Identity, Eps);
758}
759
760// cMat4::IsSymmetric
761inline bool cMat4::IsSymmetric( float Eps ) const {
762 if ( !cMath::Equals( m_Rows[ 0 ][ 1 ], m_Rows[ 1 ][ 0 ], Eps ) ) {
763 return false;
764 }
765 if ( !cMath::Equals( m_Rows[ 0 ][ 2 ], m_Rows[ 2 ][ 0 ], Eps ) ) {
766 return false;
767 }
768 if ( !cMath::Equals( m_Rows[ 1 ][ 2 ], m_Rows[ 2 ][ 1 ], Eps ) ) {
769 return false;
770 }
771 if ( !cMath::Equals( m_Rows[ 0 ][ 3 ], m_Rows[ 3 ][ 0 ], Eps ) ) {
772 return false;
773 }
774 if ( !cMath::Equals( m_Rows[ 1 ][ 3 ], m_Rows[ 3 ][ 1 ], Eps ) ) {
775 return false;
776 }
777 if ( !cMath::Equals( m_Rows[ 2 ][ 3 ], m_Rows[ 3 ][ 2 ], Eps ) ) {
778 return false;
779 }
780 return true;
781}
782
783// cMat4::IsOrthonormal
784inline bool cMat4::IsOrthonormal( float Eps ) const {
785 return cMath::IsOne( Determinant(), Eps );
786}
787
788// cMat4::ToFloatPtr : const float * () const
789inline const float * cMat4::ToFloatPtr() const {
790 return m_Rows[0].ToFloatPtr();
791}
792
793// cMat4::ToFloatPtr : float * ()
794inline float * cMat4::ToFloatPtr() {
795 return m_Rows[0].ToFloatPtr();
796}
797
798// cMat4::GetTranslation
799inline const cVec3 cMat4::GetTranslation() const {
800 return m_Rows[3].ToVec3();
801}
802
803// cMat4::SetTranslation
804inline void cMat4::SetTranslation( const cVec3& c ) {
805 m_Rows[ 3 ].x = c.x;
806 m_Rows[ 3 ].y = c.y;
807 m_Rows[ 3 ].z = c.z;
808}
809
810// cMat4::GetScaling
811inline const cVec3 cMat4::GetScaling() const {
812 return cVec3(m_Rows[0].ToVec3().Length(), m_Rows[1].ToVec3().Length(), m_Rows[2].ToVec3().Length());
813}
814
815// cMat4::SetScaling
816inline void cMat4::SetScaling( const cVec3& s ) {
817 m_Rows[ 0 ].x = s.x;
818 m_Rows[ 1 ].y = s.y;
819 m_Rows[ 2 ].z = s.z;
820}
821
822// cMat4::Transpose : const cMat4 (const cMat4 &)
823inline const cMat4 cMat4::Transpose(const cMat4 &M) {
824 cMat4 T;
825 for(int i = 0; i < 4; i++) {
826 T.SetRow(i, M.GetCol(i));
827 }
828 return T;
829}
830
831// cMat4::Transpose : void ()
832inline void cMat4::Transpose() {
833 cMath::Swap(m_Rows[0][1], m_Rows[1][0]);
834 cMath::Swap(m_Rows[0][2], m_Rows[2][0]);
835 cMath::Swap(m_Rows[1][2], m_Rows[2][1]);
836 cMath::Swap(m_Rows[0][3], m_Rows[3][0]);
837 cMath::Swap(m_Rows[1][3], m_Rows[3][1]);
838 cMath::Swap(m_Rows[2][3], m_Rows[3][2]);
839}
840
841//*****************************************************************************
842// cVec2 & cMat4
843//*****************************************************************************
844
845// cVec2::Transform : const cVec4 (const cVec2 &, const cMat4 &)
846inline const cVec4 cVec2::Transform(const cVec2 &u, const cMat4 &T) {
847 cVec4 r;
848 r.x = u.x * T(0, 0) + u.y * T(1, 0) + T(3, 0);
849 r.y = u.x * T(0, 1) + u.y * T(1, 1) + T(3, 1);
850 r.z = u.x * T(0, 2) + u.y * T(1, 2) + T(3, 2);
851 r.w = u.x * T(0, 3) + u.y * T(1, 3) + T(3, 3);
852 return r;
853}
854
855// cVec2::TransformCoordinate : const cVec2 (const cVec2 &, const cMat4 &)
856inline const cVec2 cVec2::TransformCoordinate(const cVec2 &u, const cMat4 &T) {
857 cVec2 r;
858 r.x = u.x * T(0, 0) + u.y * T(1, 0) + T(3, 0);
859 r.y = u.x * T(0, 1) + u.y * T(1, 1) + T(3, 1);
860 float w = u.x * T(0, 3) + u.y * T(1, 3) + T(3, 3);
861 if(w != 1.0f) {
862 float iw = 1.0f / w;
863 r.x *= iw;
864 r.y *= iw;
865 }
866 return r;
867}
868
869// cVec2::TransformCoordinate : void (const cMat4 &)
870inline void cVec2::TransformCoordinate(const cMat4 &T) {
871 *this = TransformCoordinate(*this, T);
872}
873
874// cVec2::TransformNormal : const cVec2 (const cVec2 &, const cMat4 &)
875inline const cVec2 cVec2::TransformNormal(const cVec2 &u, const cMat4 &T) {
876 cVec2 r;
877 r.x = u.x * T(0, 0) + u.y * T(1, 0);
878 r.y = u.x * T(0, 1) + u.y * T(1, 1);
879 return r;
880}
881
882// cVec2::TransformNormal : void (const cMat4 &)
883inline void cVec2::TransformNormal(const cMat4 &T) {
884 *this = TransformNormal(*this, T);
885}
886
887//*****************************************************************************
888// cVec3 & cMat4
889//*****************************************************************************
890
891// cVec3::Transform
892inline const cVec4 cVec3::Transform(const cVec3 &u, const cMat4 &M) {
893 cVec4 t;
894 t.x = u.x * M[0][0] + u.y * M[1][0] + u.z * M[2][0] + M[3][0];
895 t.y = u.x * M[0][1] + u.y * M[1][1] + u.z * M[2][1] + M[3][1];
896 t.z = u.x * M[0][2] + u.y * M[1][2] + u.z * M[2][2] + M[3][2];
897 t.w = u.x * M[0][3] + u.y * M[1][3] + u.z * M[2][3] + M[3][3];
898 return t;
899}
900
901// cVec3::TransformCoordinate : const cVec3 (const cVec3 &, const cMat4 &)
902inline const cVec3 cVec3::TransformCoordinate(const cVec3 &u, const cMat4 &T) {
903 cVec3 r;
904 r.x = u.x * T(0, 0) + u.y * T(1, 0) + u.z * T(2, 0) + T(3, 0);
905 r.y = u.x * T(0, 1) + u.y * T(1, 1) + u.z * T(2, 1) + T(3, 1);
906 r.z = u.x * T(0, 2) + u.y * T(1, 2) + u.z * T(2, 2) + T(3, 2);
907 float w = u.x * T(0, 3) + u.y * T(1, 3) + u.z * T(2, 3) + T(3, 3);
908 if(w != 1.0f) {
909 float iw = 1.0f / w;
910 r.x *= iw;
911 r.y *= iw;
912 r.z *= iw;
913 }
914 return r;
915}
916
917// cVec3::TransformCoordinate : void (const cMat4 &)
918inline void cVec3::TransformCoordinate(const cMat4 &T) {
919 *this = TransformCoordinate(*this, T);
920}
921
922// cVec3::TransformNormal
923inline const cVec3 cVec3::TransformNormal(const cVec3 &u, const cMat4 &T) {
924 cVec3 r;
925 r.x = u.x * T(0, 0) + u.y * T(1, 0) + u.z * T(2, 0);
926 r.y = u.x * T(0, 1) + u.y * T(1, 1) + u.z * T(2, 1);
927 r.z = u.x * T(0, 2) + u.y * T(1, 2) + u.z * T(2, 2);
928 return r;
929}
930
931// cVec3::TransformNormal
932inline void cVec3::TransformNormal(const cMat4 &T) {
933 *this = TransformNormal(*this, T);
934}
935
936// cVec3::TransformNormalTransposed
937inline void cVec3::TransformNormalTransposed(const cMat4 &T) {
938 cVec3 u = *this;
939 x = u.x * T(0, 0) + u.y * T(0, 1) + u.z * T(0, 2);
940 y = u.x * T(1, 0) + u.y * T(1, 1) + u.z * T(1, 2);
941 z = u.x * T(2, 0) + u.y * T(2, 1) + u.z * T(2, 2);
942}
943
944//*****************************************************************************
945// cVec4 & cMat4
946//*****************************************************************************
947
948// cVec4::Transform : const cVec4 (const cVec4 &, const cMat4 &)
949inline const cVec4 cVec4::Transform(const cVec4 &u, const cMat4 &T) {
950 cVec4 r;
951 r.x = u.x * T(0, 0) + u.y * T(1, 0) + u.z * T(2, 0) + u.w * T(3, 0);
952 r.y = u.x * T(0, 1) + u.y * T(1, 1) + u.z * T(2, 1) + u.w * T(3, 1);
953 r.z = u.x * T(0, 2) + u.y * T(1, 2) + u.z * T(2, 2) + u.w * T(3, 2);
954 r.w = u.x * T(0, 3) + u.y * T(1, 3) + u.z * T(2, 3) + u.w * T(3, 3);
955 return r;
956}
957
958// cVec4::Transform : void (const cMat4 &)
959inline void cVec4::Transform(const cMat4 &T) {
960 *this = Transform(*this, T);
961}
962
963// cVec4::operator *= : void (const cMat4 &)
964inline void cVec4::operator *= (const cMat4 &T) {
965 *this = Transform(*this, T);
966}
967
968// cVec4::operator * : const cVec4 (const cMat4 &) const
969inline const cVec4 cVec4::operator * (const cMat4 &T) const {
970 return Transform(*this, T);
971}
972
973//*****************************************************************************
974// cMat3 & cMat4
975//*****************************************************************************
976
977// cMat3::ToMat4
978inline const cMat4 cMat3::ToMat4() const {
979 return cMat4(*this, cVec3::Zero);
980}
981
982// cMat4::ToMat3
983inline const cMat3 cMat4::ToMat3() const {
984 return cMat3(cMat3::RowsCtor, m_Rows[0].ToVec3(), m_Rows[1].ToVec3(), m_Rows[2].ToVec3());
985}
986
987
988
989
990::std::ostream& operator<<( ::std::ostream&, const cMat4& );
The 3D-matrix, refer it as coat::mat3 in the Core API.
Definition cMat3.h:6
The 4D-matrix, refer it as coat::mat4 in the Core API.
Definition cMat4.h:6
Definition cStr.h:6
The 2D-vector, refer it as coat::vec2 in the Core API.
Definition cVec2.h:9
The 3D-vector, refer it as coat::vec3 in the Core API.
Definition cVec3.h:10
The 4D-vector, refer it as coat::vec4 in the Core API.
Definition cVec4.h:9