gpuray_glsl

view vmath/matrix.cc @ 3:297dbc5080c4

cone intersection
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 09 Nov 2014 20:13:33 +0200
parents
children
line source
1 #include <cstdio>
2 #include <cmath>
3 #include "matrix.h"
4 #include "vector.h"
5 #include "quat.h"
7 using namespace std;
9 // ----------- Matrix3x3 --------------
11 Matrix3x3 Matrix3x3::identity = Matrix3x3(1, 0, 0, 0, 1, 0, 0, 0, 1);
13 Matrix3x3::Matrix3x3()
14 {
15 *this = identity;
16 }
18 Matrix3x3::Matrix3x3( scalar_t m11, scalar_t m12, scalar_t m13,
19 scalar_t m21, scalar_t m22, scalar_t m23,
20 scalar_t m31, scalar_t m32, scalar_t m33)
21 {
22 m[0][0] = m11; m[0][1] = m12; m[0][2] = m13;
23 m[1][0] = m21; m[1][1] = m22; m[1][2] = m23;
24 m[2][0] = m31; m[2][1] = m32; m[2][2] = m33;
25 }
27 Matrix3x3::Matrix3x3(const Vector3 &ivec, const Vector3 &jvec, const Vector3 &kvec)
28 {
29 set_row_vector(ivec, 0);
30 set_row_vector(jvec, 1);
31 set_row_vector(kvec, 2);
32 }
34 Matrix3x3::Matrix3x3(const mat3_t cmat)
35 {
36 memcpy(m, cmat, sizeof(mat3_t));
37 }
39 Matrix3x3::Matrix3x3(const Matrix4x4 &mat4x4)
40 {
41 for(int i=0; i<3; i++) {
42 for(int j=0; j<3; j++) {
43 m[i][j] = mat4x4[i][j];
44 }
45 }
46 }
48 Matrix3x3 operator +(const Matrix3x3 &m1, const Matrix3x3 &m2)
49 {
50 Matrix3x3 res;
51 const scalar_t *op1 = m1.m[0], *op2 = m2.m[0];
52 scalar_t *dest = res.m[0];
54 for(int i=0; i<9; i++) {
55 *dest++ = *op1++ + *op2++;
56 }
57 return res;
58 }
60 Matrix3x3 operator -(const Matrix3x3 &m1, const Matrix3x3 &m2)
61 {
62 Matrix3x3 res;
63 const scalar_t *op1 = m1.m[0], *op2 = m2.m[0];
64 scalar_t *dest = res.m[0];
66 for(int i=0; i<9; i++) {
67 *dest++ = *op1++ - *op2++;
68 }
69 return res;
70 }
72 Matrix3x3 operator *(const Matrix3x3 &m1, const Matrix3x3 &m2)
73 {
74 Matrix3x3 res;
75 for(int i=0; i<3; i++) {
76 for(int j=0; j<3; j++) {
77 res.m[i][j] = m1.m[i][0] * m2.m[0][j] + m1.m[i][1] * m2.m[1][j] + m1.m[i][2] * m2.m[2][j];
78 }
79 }
80 return res;
81 }
83 void operator +=(Matrix3x3 &m1, const Matrix3x3 &m2)
84 {
85 scalar_t *op1 = m1.m[0];
86 const scalar_t *op2 = m2.m[0];
88 for(int i=0; i<9; i++) {
89 *op1++ += *op2++;
90 }
91 }
93 void operator -=(Matrix3x3 &m1, const Matrix3x3 &m2)
94 {
95 scalar_t *op1 = m1.m[0];
96 const scalar_t *op2 = m2.m[0];
98 for(int i=0; i<9; i++) {
99 *op1++ -= *op2++;
100 }
101 }
103 void operator *=(Matrix3x3 &m1, const Matrix3x3 &m2)
104 {
105 Matrix3x3 res;
106 for(int i=0; i<3; i++) {
107 for(int j=0; j<3; j++) {
108 res.m[i][j] = m1.m[i][0] * m2.m[0][j] + m1.m[i][1] * m2.m[1][j] + m1.m[i][2] * m2.m[2][j];
109 }
110 }
111 memcpy(m1.m, res.m, 9 * sizeof(scalar_t));
112 }
114 Matrix3x3 operator *(const Matrix3x3 &mat, scalar_t scalar)
115 {
116 Matrix3x3 res;
117 const scalar_t *mptr = mat.m[0];
118 scalar_t *dptr = res.m[0];
120 for(int i=0; i<9; i++) {
121 *dptr++ = *mptr++ * scalar;
122 }
123 return res;
124 }
126 Matrix3x3 operator *(scalar_t scalar, const Matrix3x3 &mat)
127 {
128 Matrix3x3 res;
129 const scalar_t *mptr = mat.m[0];
130 scalar_t *dptr = res.m[0];
132 for(int i=0; i<9; i++) {
133 *dptr++ = *mptr++ * scalar;
134 }
135 return res;
136 }
138 void operator *=(Matrix3x3 &mat, scalar_t scalar)
139 {
140 scalar_t *mptr = mat.m[0];
142 for(int i=0; i<9; i++) {
143 *mptr++ *= scalar;
144 }
145 }
147 void Matrix3x3::translate(const Vector2 &trans)
148 {
149 Matrix3x3 tmat(1, 0, trans.x, 0, 1, trans.y, 0, 0, 1);
150 *this *= tmat;
151 }
153 void Matrix3x3::set_translation(const Vector2 &trans)
154 {
155 *this = Matrix3x3(1, 0, trans.x, 0, 1, trans.y, 0, 0, 1);
156 }
158 void Matrix3x3::rotate(scalar_t angle)
159 {
160 scalar_t cos_a = cos(angle);
161 scalar_t sin_a = sin(angle);
162 Matrix3x3 rmat( cos_a, -sin_a, 0,
163 sin_a, cos_a, 0,
164 0, 0, 1);
165 *this *= rmat;
166 }
168 void Matrix3x3::set_rotation(scalar_t angle)
169 {
170 scalar_t cos_a = cos(angle);
171 scalar_t sin_a = sin(angle);
172 *this = Matrix3x3(cos_a, -sin_a, 0, sin_a, cos_a, 0, 0, 0, 1);
173 }
175 void Matrix3x3::rotate(const Vector3 &euler_angles)
176 {
177 Matrix3x3 xrot, yrot, zrot;
179 xrot = Matrix3x3( 1, 0, 0,
180 0, cos(euler_angles.x), -sin(euler_angles.x),
181 0, sin(euler_angles.x), cos(euler_angles.x));
183 yrot = Matrix3x3( cos(euler_angles.y), 0, sin(euler_angles.y),
184 0, 1, 0,
185 -sin(euler_angles.y), 0, cos(euler_angles.y));
187 zrot = Matrix3x3( cos(euler_angles.z), -sin(euler_angles.z), 0,
188 sin(euler_angles.z), cos(euler_angles.z), 0,
189 0, 0, 1);
191 *this *= xrot * yrot * zrot;
192 }
194 void Matrix3x3::set_rotation(const Vector3 &euler_angles)
195 {
196 Matrix3x3 xrot, yrot, zrot;
198 xrot = Matrix3x3( 1, 0, 0,
199 0, cos(euler_angles.x), -sin(euler_angles.x),
200 0, sin(euler_angles.x), cos(euler_angles.x));
202 yrot = Matrix3x3( cos(euler_angles.y), 0, sin(euler_angles.y),
203 0, 1, 0,
204 -sin(euler_angles.y), 0, cos(euler_angles.y));
206 zrot = Matrix3x3( cos(euler_angles.z), -sin(euler_angles.z), 0,
207 sin(euler_angles.z), cos(euler_angles.z), 0,
208 0, 0, 1);
210 *this = xrot * yrot * zrot;
211 }
213 void Matrix3x3::rotate(const Vector3 &axis, scalar_t angle)
214 {
215 scalar_t sina = (scalar_t)sin(angle);
216 scalar_t cosa = (scalar_t)cos(angle);
217 scalar_t invcosa = 1-cosa;
218 scalar_t nxsq = axis.x * axis.x;
219 scalar_t nysq = axis.y * axis.y;
220 scalar_t nzsq = axis.z * axis.z;
222 Matrix3x3 xform;
223 xform.m[0][0] = nxsq + (1-nxsq) * cosa;
224 xform.m[0][1] = axis.x * axis.y * invcosa - axis.z * sina;
225 xform.m[0][2] = axis.x * axis.z * invcosa + axis.y * sina;
227 xform.m[1][0] = axis.x * axis.y * invcosa + axis.z * sina;
228 xform.m[1][1] = nysq + (1-nysq) * cosa;
229 xform.m[1][2] = axis.y * axis.z * invcosa - axis.x * sina;
231 xform.m[2][0] = axis.x * axis.z * invcosa - axis.y * sina;
232 xform.m[2][1] = axis.y * axis.z * invcosa + axis.x * sina;
233 xform.m[2][2] = nzsq + (1-nzsq) * cosa;
235 *this *= xform;
236 }
238 void Matrix3x3::set_rotation(const Vector3 &axis, scalar_t angle)
239 {
240 scalar_t sina = (scalar_t)sin(angle);
241 scalar_t cosa = (scalar_t)cos(angle);
242 scalar_t invcosa = 1-cosa;
243 scalar_t nxsq = axis.x * axis.x;
244 scalar_t nysq = axis.y * axis.y;
245 scalar_t nzsq = axis.z * axis.z;
247 reset_identity();
248 m[0][0] = nxsq + (1-nxsq) * cosa;
249 m[0][1] = axis.x * axis.y * invcosa - axis.z * sina;
250 m[0][2] = axis.x * axis.z * invcosa + axis.y * sina;
251 m[1][0] = axis.x * axis.y * invcosa + axis.z * sina;
252 m[1][1] = nysq + (1-nysq) * cosa;
253 m[1][2] = axis.y * axis.z * invcosa - axis.x * sina;
254 m[2][0] = axis.x * axis.z * invcosa - axis.y * sina;
255 m[2][1] = axis.y * axis.z * invcosa + axis.x * sina;
256 m[2][2] = nzsq + (1-nzsq) * cosa;
257 }
259 // Algorithm in Ken Shoemake's article in 1987 SIGGRAPH course notes
260 // article "Quaternion Calculus and Fast Animation".
261 // adapted from: http://www.geometrictools.com/LibMathematics/Algebra/Wm5Quaternion.inl
262 Quaternion Matrix3x3::get_rotation_quat() const
263 {
264 static const int next[3] = {1, 2, 0};
266 float quat[4];
268 scalar_t trace = m[0][0] + m[1][1] + m[2][2];
269 scalar_t root;
271 if(trace > 0.0f) {
272 // |w| > 1/2
273 root = sqrt(trace + 1.0f); // 2w
274 quat[0] = 0.5f * root;
275 root = 0.5f / root; // 1 / 4w
276 quat[1] = (m[2][1] - m[1][2]) * root;
277 quat[2] = (m[0][2] - m[2][0]) * root;
278 quat[3] = (m[1][0] - m[0][1]) * root;
279 } else {
280 // |w| <= 1/2
281 int i = 0;
282 if(m[1][1] > m[0][0]) {
283 i = 1;
284 }
285 if(m[2][2] > m[i][i]) {
286 i = 2;
287 }
288 int j = next[i];
289 int k = next[j];
291 root = sqrt(m[i][i] - m[j][j] - m[k][k] + 1.0f);
292 quat[i + 1] = 0.5f * root;
293 root = 0.5f / root;
294 quat[0] = (m[k][j] - m[j][k]) * root;
295 quat[j + 1] = (m[j][i] - m[i][j]) * root;
296 quat[k + 1] = (m[k][i] - m[i][k]) * root;
297 }
298 return Quaternion(quat[0], quat[1], quat[2], quat[3]);
299 }
301 void Matrix3x3::scale(const Vector3 &scale_vec)
302 {
303 Matrix3x3 smat( scale_vec.x, 0, 0,
304 0, scale_vec.y, 0,
305 0, 0, scale_vec.z);
306 *this *= smat;
307 }
309 void Matrix3x3::set_scaling(const Vector3 &scale_vec)
310 {
311 *this = Matrix3x3( scale_vec.x, 0, 0,
312 0, scale_vec.y, 0,
313 0, 0, scale_vec.z);
314 }
316 void Matrix3x3::set_column_vector(const Vector3 &vec, unsigned int col_index)
317 {
318 m[0][col_index] = vec.x;
319 m[1][col_index] = vec.y;
320 m[2][col_index] = vec.z;
321 }
323 void Matrix3x3::set_row_vector(const Vector3 &vec, unsigned int row_index)
324 {
325 m[row_index][0] = vec.x;
326 m[row_index][1] = vec.y;
327 m[row_index][2] = vec.z;
328 }
330 Vector3 Matrix3x3::get_column_vector(unsigned int col_index) const
331 {
332 return Vector3(m[0][col_index], m[1][col_index], m[2][col_index]);
333 }
335 Vector3 Matrix3x3::get_row_vector(unsigned int row_index) const
336 {
337 return Vector3(m[row_index][0], m[row_index][1], m[row_index][2]);
338 }
340 void Matrix3x3::transpose()
341 {
342 Matrix3x3 tmp = *this;
343 for(int i=0; i<3; i++) {
344 for(int j=0; j<3; j++) {
345 m[i][j] = tmp[j][i];
346 }
347 }
348 }
350 Matrix3x3 Matrix3x3::transposed() const
351 {
352 Matrix3x3 res;
353 for(int i=0; i<3; i++) {
354 for(int j=0; j<3; j++) {
355 res[i][j] = m[j][i];
356 }
357 }
358 return res;
359 }
361 scalar_t Matrix3x3::determinant() const
362 {
363 return m[0][0] * (m[1][1]*m[2][2] - m[1][2]*m[2][1]) -
364 m[0][1] * (m[1][0]*m[2][2] - m[1][2]*m[2][0]) +
365 m[0][2] * (m[1][0]*m[2][1] - m[1][1]*m[2][0]);
366 }
368 Matrix3x3 Matrix3x3::inverse() const
369 {
370 // TODO: implement 3x3 inverse
371 return *this;
372 }
374 ostream &operator <<(ostream &out, const Matrix3x3 &mat)
375 {
376 for(int i=0; i<3; i++) {
377 char str[100];
378 sprintf(str, "[ %12.5f %12.5f %12.5f ]\n", (float)mat.m[i][0], (float)mat.m[i][1], (float)mat.m[i][2]);
379 out << str;
380 }
381 return out;
382 }
386 /* ----------------- Matrix4x4 implementation --------------- */
388 Matrix4x4 Matrix4x4::identity = Matrix4x4(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1);
390 Matrix4x4::Matrix4x4()
391 {
392 *this = identity;
393 }
395 Matrix4x4::Matrix4x4( scalar_t m11, scalar_t m12, scalar_t m13, scalar_t m14,
396 scalar_t m21, scalar_t m22, scalar_t m23, scalar_t m24,
397 scalar_t m31, scalar_t m32, scalar_t m33, scalar_t m34,
398 scalar_t m41, scalar_t m42, scalar_t m43, scalar_t m44)
399 {
400 m[0][0] = m11; m[0][1] = m12; m[0][2] = m13; m[0][3] = m14;
401 m[1][0] = m21; m[1][1] = m22; m[1][2] = m23; m[1][3] = m24;
402 m[2][0] = m31; m[2][1] = m32; m[2][2] = m33; m[2][3] = m34;
403 m[3][0] = m41; m[3][1] = m42; m[3][2] = m43; m[3][3] = m44;
404 }
406 Matrix4x4::Matrix4x4(const mat4_t cmat)
407 {
408 memcpy(m, cmat, sizeof(mat4_t));
409 }
411 Matrix4x4::Matrix4x4(const Matrix3x3 &mat3x3)
412 {
413 reset_identity();
414 for(int i=0; i<3; i++) {
415 for(int j=0; j<3; j++) {
416 m[i][j] = mat3x3[i][j];
417 }
418 }
419 }
421 Matrix4x4 operator +(const Matrix4x4 &m1, const Matrix4x4 &m2)
422 {
423 Matrix4x4 res;
424 const scalar_t *op1 = m1.m[0], *op2 = m2.m[0];
425 scalar_t *dest = res.m[0];
427 for(int i=0; i<16; i++) {
428 *dest++ = *op1++ + *op2++;
429 }
430 return res;
431 }
433 Matrix4x4 operator -(const Matrix4x4 &m1, const Matrix4x4 &m2)
434 {
435 Matrix4x4 res;
436 const scalar_t *op1 = m1.m[0], *op2 = m2.m[0];
437 scalar_t *dest = res.m[0];
439 for(int i=0; i<16; i++) {
440 *dest++ = *op1++ - *op2++;
441 }
442 return res;
443 }
445 void operator +=(Matrix4x4 &m1, const Matrix4x4 &m2)
446 {
447 scalar_t *op1 = m1.m[0];
448 const scalar_t *op2 = m2.m[0];
450 for(int i=0; i<16; i++) {
451 *op1++ += *op2++;
452 }
453 }
455 void operator -=(Matrix4x4 &m1, const Matrix4x4 &m2)
456 {
457 scalar_t *op1 = m1.m[0];
458 const scalar_t *op2 = m2.m[0];
460 for(int i=0; i<16; i++) {
461 *op1++ -= *op2++;
462 }
463 }
465 Matrix4x4 operator *(const Matrix4x4 &mat, scalar_t scalar)
466 {
467 Matrix4x4 res;
468 const scalar_t *mptr = mat.m[0];
469 scalar_t *dptr = res.m[0];
471 for(int i=0; i<16; i++) {
472 *dptr++ = *mptr++ * scalar;
473 }
474 return res;
475 }
477 Matrix4x4 operator *(scalar_t scalar, const Matrix4x4 &mat)
478 {
479 Matrix4x4 res;
480 const scalar_t *mptr = mat.m[0];
481 scalar_t *dptr = res.m[0];
483 for(int i=0; i<16; i++) {
484 *dptr++ = *mptr++ * scalar;
485 }
486 return res;
487 }
489 void operator *=(Matrix4x4 &mat, scalar_t scalar)
490 {
491 scalar_t *mptr = mat.m[0];
493 for(int i=0; i<16; i++) {
494 *mptr++ *= scalar;
495 }
496 }
498 void Matrix4x4::translate(const Vector3 &trans)
499 {
500 Matrix4x4 tmat(1, 0, 0, trans.x, 0, 1, 0, trans.y, 0, 0, 1, trans.z, 0, 0, 0, 1);
501 *this *= tmat;
502 }
504 void Matrix4x4::set_translation(const Vector3 &trans)
505 {
506 *this = Matrix4x4(1, 0, 0, trans.x, 0, 1, 0, trans.y, 0, 0, 1, trans.z, 0, 0, 0, 1);
507 }
509 Vector3 Matrix4x4::get_translation() const
510 {
511 return Vector3(m[0][3], m[1][3], m[2][3]);
512 }
514 void Matrix4x4::rotate(const Vector3 &euler_angles)
515 {
516 Matrix3x3 xrot, yrot, zrot;
518 xrot = Matrix3x3( 1, 0, 0,
519 0, cos(euler_angles.x), -sin(euler_angles.x),
520 0, sin(euler_angles.x), cos(euler_angles.x));
522 yrot = Matrix3x3( cos(euler_angles.y), 0, sin(euler_angles.y),
523 0, 1, 0,
524 -sin(euler_angles.y), 0, cos(euler_angles.y));
526 zrot = Matrix3x3( cos(euler_angles.z), -sin(euler_angles.z), 0,
527 sin(euler_angles.z), cos(euler_angles.z), 0,
528 0, 0, 1);
530 *this *= Matrix4x4(xrot * yrot * zrot);
531 }
533 void Matrix4x4::set_rotation(const Vector3 &euler_angles)
534 {
535 Matrix3x3 xrot, yrot, zrot;
537 xrot = Matrix3x3( 1, 0, 0,
538 0, cos(euler_angles.x), -sin(euler_angles.x),
539 0, sin(euler_angles.x), cos(euler_angles.x));
541 yrot = Matrix3x3( cos(euler_angles.y), 0, sin(euler_angles.y),
542 0, 1, 0,
543 -sin(euler_angles.y), 0, cos(euler_angles.y));
545 zrot = Matrix3x3( cos(euler_angles.z), -sin(euler_angles.z), 0,
546 sin(euler_angles.z), cos(euler_angles.z), 0,
547 0, 0, 1);
549 *this = Matrix4x4(xrot * yrot * zrot);
550 }
552 void Matrix4x4::rotate(const Vector3 &axis, scalar_t angle)
553 {
554 scalar_t sina = (scalar_t)sin(angle);
555 scalar_t cosa = (scalar_t)cos(angle);
556 scalar_t invcosa = 1-cosa;
557 scalar_t nxsq = axis.x * axis.x;
558 scalar_t nysq = axis.y * axis.y;
559 scalar_t nzsq = axis.z * axis.z;
561 Matrix4x4 xform;
562 xform[0][0] = nxsq + (1-nxsq) * cosa;
563 xform[0][1] = axis.x * axis.y * invcosa - axis.z * sina;
564 xform[0][2] = axis.x * axis.z * invcosa + axis.y * sina;
565 xform[1][0] = axis.x * axis.y * invcosa + axis.z * sina;
566 xform[1][1] = nysq + (1-nysq) * cosa;
567 xform[1][2] = axis.y * axis.z * invcosa - axis.x * sina;
568 xform[2][0] = axis.x * axis.z * invcosa - axis.y * sina;
569 xform[2][1] = axis.y * axis.z * invcosa + axis.x * sina;
570 xform[2][2] = nzsq + (1-nzsq) * cosa;
572 *this *= xform;
573 }
575 void Matrix4x4::set_rotation(const Vector3 &axis, scalar_t angle)
576 {
577 scalar_t sina = (scalar_t)sin(angle);
578 scalar_t cosa = (scalar_t)cos(angle);
579 scalar_t invcosa = 1-cosa;
580 scalar_t nxsq = axis.x * axis.x;
581 scalar_t nysq = axis.y * axis.y;
582 scalar_t nzsq = axis.z * axis.z;
584 reset_identity();
585 m[0][0] = nxsq + (1-nxsq) * cosa;
586 m[0][1] = axis.x * axis.y * invcosa - axis.z * sina;
587 m[0][2] = axis.x * axis.z * invcosa + axis.y * sina;
588 m[1][0] = axis.x * axis.y * invcosa + axis.z * sina;
589 m[1][1] = nysq + (1-nysq) * cosa;
590 m[1][2] = axis.y * axis.z * invcosa - axis.x * sina;
591 m[2][0] = axis.x * axis.z * invcosa - axis.y * sina;
592 m[2][1] = axis.y * axis.z * invcosa + axis.x * sina;
593 m[2][2] = nzsq + (1-nzsq) * cosa;
594 }
596 void Matrix4x4::rotate(const Quaternion &quat)
597 {
598 *this *= quat.get_rotation_matrix();
599 }
601 void Matrix4x4::set_rotation(const Quaternion &quat)
602 {
603 *this = quat.get_rotation_matrix();
604 }
606 Quaternion Matrix4x4::get_rotation_quat() const
607 {
608 Matrix3x3 mat3 = *this;
609 return mat3.get_rotation_quat();
610 }
612 void Matrix4x4::scale(const Vector4 &scale_vec)
613 {
614 Matrix4x4 smat( scale_vec.x, 0, 0, 0,
615 0, scale_vec.y, 0, 0,
616 0, 0, scale_vec.z, 0,
617 0, 0, 0, scale_vec.w);
618 *this *= smat;
619 }
621 void Matrix4x4::set_scaling(const Vector4 &scale_vec)
622 {
623 *this = Matrix4x4( scale_vec.x, 0, 0, 0,
624 0, scale_vec.y, 0, 0,
625 0, 0, scale_vec.z, 0,
626 0, 0, 0, scale_vec.w);
627 }
629 Vector3 Matrix4x4::get_scaling() const
630 {
631 Vector3 vi = get_row_vector(0);
632 Vector3 vj = get_row_vector(1);
633 Vector3 vk = get_row_vector(2);
635 return Vector3(vi.length(), vj.length(), vk.length());
636 }
638 void Matrix4x4::set_column_vector(const Vector4 &vec, unsigned int col_index)
639 {
640 m[0][col_index] = vec.x;
641 m[1][col_index] = vec.y;
642 m[2][col_index] = vec.z;
643 m[3][col_index] = vec.w;
644 }
646 void Matrix4x4::set_row_vector(const Vector4 &vec, unsigned int row_index)
647 {
648 m[row_index][0] = vec.x;
649 m[row_index][1] = vec.y;
650 m[row_index][2] = vec.z;
651 m[row_index][3] = vec.w;
652 }
654 Vector4 Matrix4x4::get_column_vector(unsigned int col_index) const
655 {
656 return Vector4(m[0][col_index], m[1][col_index], m[2][col_index], m[3][col_index]);
657 }
659 Vector4 Matrix4x4::get_row_vector(unsigned int row_index) const
660 {
661 return Vector4(m[row_index][0], m[row_index][1], m[row_index][2], m[row_index][3]);
662 }
664 void Matrix4x4::transpose()
665 {
666 Matrix4x4 tmp = *this;
667 for(int i=0; i<4; i++) {
668 for(int j=0; j<4; j++) {
669 m[i][j] = tmp[j][i];
670 }
671 }
672 }
674 Matrix4x4 Matrix4x4::transposed() const
675 {
676 Matrix4x4 res;
677 for(int i=0; i<4; i++) {
678 for(int j=0; j<4; j++) {
679 res[i][j] = m[j][i];
680 }
681 }
682 return res;
683 }
685 scalar_t Matrix4x4::determinant() const
686 {
687 scalar_t det11 = (m[1][1] * (m[2][2] * m[3][3] - m[3][2] * m[2][3])) -
688 (m[1][2] * (m[2][1] * m[3][3] - m[3][1] * m[2][3])) +
689 (m[1][3] * (m[2][1] * m[3][2] - m[3][1] * m[2][2]));
691 scalar_t det12 = (m[1][0] * (m[2][2] * m[3][3] - m[3][2] * m[2][3])) -
692 (m[1][2] * (m[2][0] * m[3][3] - m[3][0] * m[2][3])) +
693 (m[1][3] * (m[2][0] * m[3][2] - m[3][0] * m[2][2]));
695 scalar_t det13 = (m[1][0] * (m[2][1] * m[3][3] - m[3][1] * m[2][3])) -
696 (m[1][1] * (m[2][0] * m[3][3] - m[3][0] * m[2][3])) +
697 (m[1][3] * (m[2][0] * m[3][1] - m[3][0] * m[2][1]));
699 scalar_t det14 = (m[1][0] * (m[2][1] * m[3][2] - m[3][1] * m[2][2])) -
700 (m[1][1] * (m[2][0] * m[3][2] - m[3][0] * m[2][2])) +
701 (m[1][2] * (m[2][0] * m[3][1] - m[3][0] * m[2][1]));
703 return m[0][0] * det11 - m[0][1] * det12 + m[0][2] * det13 - m[0][3] * det14;
704 }
707 Matrix4x4 Matrix4x4::adjoint() const
708 {
709 Matrix4x4 coef;
711 coef.m[0][0] = (m[1][1] * (m[2][2] * m[3][3] - m[3][2] * m[2][3])) -
712 (m[1][2] * (m[2][1] * m[3][3] - m[3][1] * m[2][3])) +
713 (m[1][3] * (m[2][1] * m[3][2] - m[3][1] * m[2][2]));
714 coef.m[0][1] = (m[1][0] * (m[2][2] * m[3][3] - m[3][2] * m[2][3])) -
715 (m[1][2] * (m[2][0] * m[3][3] - m[3][0] * m[2][3])) +
716 (m[1][3] * (m[2][0] * m[3][2] - m[3][0] * m[2][2]));
717 coef.m[0][2] = (m[1][0] * (m[2][1] * m[3][3] - m[3][1] * m[2][3])) -
718 (m[1][1] * (m[2][0] * m[3][3] - m[3][0] * m[2][3])) +
719 (m[1][3] * (m[2][0] * m[3][1] - m[3][0] * m[2][1]));
720 coef.m[0][3] = (m[1][0] * (m[2][1] * m[3][2] - m[3][1] * m[2][2])) -
721 (m[1][1] * (m[2][0] * m[3][2] - m[3][0] * m[2][2])) +
722 (m[1][2] * (m[2][0] * m[3][1] - m[3][0] * m[2][1]));
724 coef.m[1][0] = (m[0][1] * (m[2][2] * m[3][3] - m[3][2] * m[2][3])) -
725 (m[0][2] * (m[2][1] * m[3][3] - m[3][1] * m[2][3])) +
726 (m[0][3] * (m[2][1] * m[3][2] - m[3][1] * m[2][2]));
727 coef.m[1][1] = (m[0][0] * (m[2][2] * m[3][3] - m[3][2] * m[2][3])) -
728 (m[0][2] * (m[2][0] * m[3][3] - m[3][0] * m[2][3])) +
729 (m[0][3] * (m[2][0] * m[3][2] - m[3][0] * m[2][2]));
730 coef.m[1][2] = (m[0][0] * (m[2][1] * m[3][3] - m[3][1] * m[2][3])) -
731 (m[0][1] * (m[2][0] * m[3][3] - m[3][0] * m[2][3])) +
732 (m[0][3] * (m[2][0] * m[3][1] - m[3][0] * m[2][1]));
733 coef.m[1][3] = (m[0][0] * (m[2][1] * m[3][2] - m[3][1] * m[2][2])) -
734 (m[0][1] * (m[2][0] * m[3][2] - m[3][0] * m[2][2])) +
735 (m[0][2] * (m[2][0] * m[3][1] - m[3][0] * m[2][1]));
737 coef.m[2][0] = (m[0][1] * (m[1][2] * m[3][3] - m[3][2] * m[1][3])) -
738 (m[0][2] * (m[1][1] * m[3][3] - m[3][1] * m[1][3])) +
739 (m[0][3] * (m[1][1] * m[3][2] - m[3][1] * m[1][2]));
740 coef.m[2][1] = (m[0][0] * (m[1][2] * m[3][3] - m[3][2] * m[1][3])) -
741 (m[0][2] * (m[1][0] * m[3][3] - m[3][0] * m[1][3])) +
742 (m[0][3] * (m[1][0] * m[3][2] - m[3][0] * m[1][2]));
743 coef.m[2][2] = (m[0][0] * (m[1][1] * m[3][3] - m[3][1] * m[1][3])) -
744 (m[0][1] * (m[1][0] * m[3][3] - m[3][0] * m[1][3])) +
745 (m[0][3] * (m[1][0] * m[3][1] - m[3][0] * m[1][1]));
746 coef.m[2][3] = (m[0][0] * (m[1][1] * m[3][2] - m[3][1] * m[1][2])) -
747 (m[0][1] * (m[1][0] * m[3][2] - m[3][0] * m[1][2])) +
748 (m[0][2] * (m[1][0] * m[3][1] - m[3][0] * m[1][1]));
750 coef.m[3][0] = (m[0][1] * (m[1][2] * m[2][3] - m[2][2] * m[1][3])) -
751 (m[0][2] * (m[1][1] * m[2][3] - m[2][1] * m[1][3])) +
752 (m[0][3] * (m[1][1] * m[2][2] - m[2][1] * m[1][2]));
753 coef.m[3][1] = (m[0][0] * (m[1][2] * m[2][3] - m[2][2] * m[1][3])) -
754 (m[0][2] * (m[1][0] * m[2][3] - m[2][0] * m[1][3])) +
755 (m[0][3] * (m[1][0] * m[2][2] - m[2][0] * m[1][2]));
756 coef.m[3][2] = (m[0][0] * (m[1][1] * m[2][3] - m[2][1] * m[1][3])) -
757 (m[0][1] * (m[1][0] * m[2][3] - m[2][0] * m[1][3])) +
758 (m[0][3] * (m[1][0] * m[2][1] - m[2][0] * m[1][1]));
759 coef.m[3][3] = (m[0][0] * (m[1][1] * m[2][2] - m[2][1] * m[1][2])) -
760 (m[0][1] * (m[1][0] * m[2][2] - m[2][0] * m[1][2])) +
761 (m[0][2] * (m[1][0] * m[2][1] - m[2][0] * m[1][1]));
763 coef.transpose();
765 for(int i=0; i<4; i++) {
766 for(int j=0; j<4; j++) {
767 coef.m[i][j] = j%2 ? -coef.m[i][j] : coef.m[i][j];
768 if(i%2) coef.m[i][j] = -coef.m[i][j];
769 }
770 }
772 return coef;
773 }
775 Matrix4x4 Matrix4x4::inverse() const
776 {
777 Matrix4x4 adj = adjoint();
779 return adj * (1.0f / determinant());
780 }
782 void Matrix4x4::keep_upper_left()
783 {
784 m[3][0] = m[3][1] = m[3][2] = m[0][3] = m[1][3] = m[2][3] = 0.0f;
785 m[3][3] = 1.0f;
786 }
788 void Matrix4x4::clear_upper_left()
789 {
790 m[0][0] = m[1][1] = m[2][2] = 1.0f;
791 m[0][1] = m[0][2] = m[1][2] = m[1][0] = m[2][0] = m[2][1] = 0.0f;
792 }
794 ostream &operator <<(ostream &out, const Matrix4x4 &mat)
795 {
796 for(int i=0; i<4; i++) {
797 char str[100];
798 sprintf(str, "[ %12.5f %12.5f %12.5f %12.5f ]\n", (float)mat.m[i][0], (float)mat.m[i][1], (float)mat.m[i][2], (float)mat.m[i][3]);
799 out << str;
800 }
801 return out;
802 }