Momentum Scripting v1
Loading...
Searching...
No Matches
Matrix4x4.h
1/*
2Copyright 2007-2025. Algoryx Simulation AB.
3
4All AGX source code, intellectual property, documentation, sample code,
5tutorials, scene files and technical white papers, are copyrighted, proprietary
6and confidential material of Algoryx Simulation AB. You may not download, read,
7store, distribute, publish, copy or otherwise disseminate, use or expose this
8material without having a written signed agreement with Algoryx Simulation AB.
9
10Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
11from using this software, unless otherwise stated in written agreements with
12Algoryx Simulation AB.
13*/
14#ifndef MOMENTUM_MATRIX4X4_H
15#define MOMENTUM_MATRIX4X4_H
16
17#include "momentum_export.h"
18#include <momentum_namespace.h>
19
20#include "macros.h"
21#include <math.h>
22#include "Quat.h"
23#include "Vec3.h"
24#include "Vec4.h"
25
26#include <agx/Prefetch.h>
27
28namespace MOMENTUM_NAMESPACE
29{
30 class EulerAngles;
31
32#define MOMENTUM_MATRIX4X4_SET_ROW(row, v1, v2, v3, v4 ) \
33 m_data[(row)][0] = (v1); \
34 m_data[(row)][1] = (v2); \
35 m_data[(row)][2] = (v3); \
36 m_data[(row)][3] = (v4);
37
38#define MOMENTUM_MATRIX4X4_INNER_PRODUCT(a,b,r,c) \
39 ((a).m_data[r][0] * (b).m_data[0][c]) \
40 +((a).m_data[r][1] * (b).m_data[1][c]) \
41 +((a).m_data[r][2] * (b).m_data[2][c]) \
42 +((a).m_data[r][3] * (b).m_data[3][c])
43
50 class MOMENTUM_EXPORT Matrix4x4
51 {
52 public:
53
54 /*=====================================================
55 Constructors
56 =======================================================*/
57
61 Matrix4x4();
62
64 Matrix4x4( const Matrix4x4& mat );
65
67 Matrix4x4( double const * const ptr );
68
69
71 Matrix4x4(const Quat& rotation, const Vec3& translation = Vec3());
72
74 Matrix4x4( const EulerAngles& rotation, const Vec3& translation = Vec3());
75
76
78 Matrix4x4( double a00, double a01, double a02, double a03,
79 double a10, double a11, double a12, double a13,
80 double a20, double a21, double a22, double a23,
81 double a30, double a31, double a32, double a33 );
82
83
85 ~Matrix4x4();
86
87 /*=====================================================
88 Accessors
89 =======================================================*/
90
94 bool valid() const;
95
99 bool isNaN() const;
100
104 bool isFinite() const;
105
109 bool isIdentity() const;
110
114 bool isRigidTransformation() const;
115
120 Matrix4x4 inverse() const;
121
125 Matrix4x4 transpose() const;
126
127 /*=====================================================
128 Mutators
129 =======================================================*/
130
142 Matrix4x4 set(double const * const ptr );
143 Matrix4x4 set(double a00, double a01, double a02, double a03,
144 double a10, double a11, double a12, double a13,
145 double a20, double a21, double a22, double a23,
146 double a30, double a31, double a32, double a33 );
147
148 Matrix4x4 set( const Quat& q );
150
151
157 void setRow(size_t row, const Vec4& vec ) {
158 m_data[row][0] = vec[0];
159 m_data[row][1] = vec[1];
160 m_data[row][2] = vec[2];
161 m_data[row][3] = vec[3];
162 }
163
164 void setCol(size_t col, const Vec4& vec ) {
165 m_data[0][col] = vec[0];
166 m_data[1][col] = vec[1];
167 m_data[2][col] = vec[2];
168 m_data[3][col] = vec[3];
169 }
170
171 Vec4 getRow(size_t row) const
172 {
173 return Vec4(
174 m_data[row][0],
175 m_data[row][1],
176 m_data[row][2],
177 m_data[row][3]
178 );
179 }
180
181 Vec4 getCol(size_t col) const
182 {
183 return Vec4(
184 m_data[0][col],
185 m_data[1][col],
186 m_data[2][col],
187 m_data[3][col]
188 );
189 }
190
191
194 Matrix4x4 setRotate( const Vec3& from, const Vec3& to );
195 Matrix4x4 setRotate( double angle, const Vec3& axis );
196 Matrix4x4 setRotate( double angle, double x, double y, double z );
197 Matrix4x4 setRotate( double angle1, const Vec3& axis1,
198 double angle2, const Vec3& axis2,
199 double angle3, const Vec3& axis3 );
200
201 Matrix4x4 setTranslate( const Vec3& t );
202 Matrix4x4 setTranslate(double tx, double ty, double tz);
203
204 Matrix4x4 addTranslate( const Vec3& t );
205 Matrix4x4 addTranslate(double tx, double ty, double tz);
206
207 Matrix4x4 setIdentity();
208
209 /*=====================================================
210 Accessors
211 =======================================================*/
212 Vec3 getTranslate() const;
213
214 Quat getRotate() const;
215
221
222
223 /* Get the internal row-major buffer */
224 double *ptr();
225 const double *ptr() const;
226
227 /*=====================================================
228 Static methods
229 =======================================================*/
230
234 static Matrix4x4 crossMatrix(const Vec3& vec);
235 static Matrix4x4 translate( const Vec3& dv );
236 static Matrix4x4 translate( double x, double y, double z );
237 static Matrix4x4 rotate( const Vec3& from, const Vec3& to );
238 static Matrix4x4 rotate( double angle, double x, double y, double z );
239 static Matrix4x4 rotate( double angle, const Vec3& axis );
240 static Matrix4x4 rotate( double angle1, const Vec3& axis1,
241 double angle2, const Vec3& axis2,
242 double angle3, const Vec3& axis3 );
243
244 /*=====================================================
245 Operators
246 =======================================================*/
247 Vec3 operator* ( const Vec3& v ) const;
248 Vec4 operator* ( const Vec4& v ) const;
249
250 void operator*= ( const Matrix4x4& other );
251 Matrix4x4 operator* ( const Matrix4x4 &m ) const;
252 bool operator== ( const Matrix4x4& m ) const;
253 bool operator!= ( const Matrix4x4& m ) const;
254#ifndef SWIG
255 double& operator()(size_t row, size_t col);
256 Matrix4x4 operator= ( const Matrix4x4& rhs );
257#endif
258 double operator()(size_t row, size_t col) const;
259
260 Vec3 transform3x3(const Vec3& vIn) const;
261
262 Vec3 transform3x3Inv( const Vec3& vIn) const;
263
264 Vec3 preMult(const Vec3& v) const;
265
266 Vec3 postMult(const Vec3& v) const;
267
268 Vec4 preMult(const Vec4& v) const;
269
270 Vec4 postMult(const Vec4& v) const;
271
272 void mult( const Matrix4x4&, const Matrix4x4& );
273 void preMult( const Matrix4x4& );
274 void postMult( const Matrix4x4& );
275 Matrix4x4 preMultTranslate( const Vec3& v );
276 Matrix4x4 postMultTranslate( const Vec3& v );
277
282 std::string __str__() const;
283
284 protected:
285
286 bool invert( const Matrix4x4& rhs );
287
288
289 double m_data[4][4];
291
292 };
293}
294
295
296#ifndef SWIG
297/* ****************************************** */
298/* * IMPLEMENTATION ** */
299/* ****************************************** */
300#include "EulerAngles.h"
301
302namespace MOMENTUM_NAMESPACE
303{
304
305 /*=====================================================
306 Constructors
307 =======================================================*/
308
310 {
311 this->setIdentity();
312 }
313
314 inline Matrix4x4::Matrix4x4( const Matrix4x4& mat )
315 {
316 m_data[0][0] = mat(0, 0);
317 m_data[0][1] = mat(0, 1);
318 m_data[0][2] = mat(0, 2);
319 m_data[0][3] = mat(0, 3);
320
321 m_data[1][0] = mat(1, 0);
322 m_data[1][1] = mat(1, 1);
323 m_data[1][2] = mat(1, 2);
324 m_data[1][3] = mat(1, 3);
325
326 m_data[2][0] = mat(2, 0);
327 m_data[2][1] = mat(2, 1);
328 m_data[2][2] = mat(2, 2);
329 m_data[2][3] = mat(2, 3);
330
331 m_data[3][0] = mat(3, 0);
332 m_data[3][1] = mat(3, 1);
333 m_data[3][2] = mat(3, 2);
334 m_data[3][3] = mat(3, 3);
335 }
336
337 inline Matrix4x4::Matrix4x4( double a00, double a01, double a02, double a03,
338 double a10, double a11, double a12, double a13,
339 double a20, double a21, double a22, double a23,
340 double a30, double a31, double a32, double a33 )
341 {
342 m_data[0][0] = a00;
343 m_data[0][1] = a01;
344 m_data[0][2] = a02;
345 m_data[0][3] = a03;
346
347 m_data[1][0] = a10;
348 m_data[1][1] = a11;
349 m_data[1][2] = a12;
350 m_data[1][3] = a13;
351
352 m_data[2][0] = a20;
353 m_data[2][1] = a21;
354 m_data[2][2] = a22;
355 m_data[2][3] = a23;
356
357 m_data[3][0] = a30;
358 m_data[3][1] = a31;
359 m_data[3][2] = a32;
360 m_data[3][3] = a33;
361
362 }
363
364 inline Matrix4x4::Matrix4x4( double const * const ptr )
365 {
366 set( ptr );
367 }
368
369
370 inline Matrix4x4::Matrix4x4(const Quat& quat, const Vec3& translation)
371 {
372 this->setIdentity();
373 this->set( quat );
374 this->setTranslate(translation);
375 }
376
378 inline Matrix4x4::Matrix4x4(const EulerAngles& rotation, const Vec3& translation)
379 {
380 this->setIdentity();
381 this->set(rotation);
382 this->setTranslate(translation);
383 }
384
386 {}
387
388
389 /*=====================================================
390 Queries
391 =======================================================*/
392
393 inline bool Matrix4x4::valid() const
394 {
395 return !isNaN();
396 }
397
398
399
400 inline bool Matrix4x4::isNaN() const
401 {
402 return std::isnan( m_data[0][0] ) || std::isnan( m_data[0][1] )
403 || std::isnan( m_data[0][2] ) || std::isnan( m_data[0][3] )
404 || std::isnan( m_data[1][0] ) || std::isnan( m_data[1][1] )
405 || std::isnan( m_data[1][2] ) || std::isnan( m_data[1][3] )
406 || std::isnan( m_data[2][0] ) || std::isnan( m_data[2][1] )
407 || std::isnan( m_data[2][2] ) || std::isnan( m_data[2][3] )
408 || std::isnan( m_data[3][0] ) || std::isnan( m_data[3][1] )
409 || std::isnan( m_data[3][2] ) || std::isnan( m_data[3][3] );
410 }
411
412
413
414 inline bool Matrix4x4::isFinite() const
415 {
416 return std::isfinite( m_data[0][0] ) && std::isfinite( m_data[0][1] )
417 && std::isfinite( m_data[0][2] ) && std::isfinite( m_data[0][3] )
418 && std::isfinite( m_data[1][0] ) && std::isfinite( m_data[1][1] )
419 && std::isfinite( m_data[1][2] ) && std::isfinite( m_data[1][3] )
420 && std::isfinite( m_data[2][0] ) && std::isfinite( m_data[2][1] )
421 && std::isfinite( m_data[2][2] ) && std::isfinite( m_data[2][3] )
422 && std::isfinite( m_data[3][0] ) && std::isfinite( m_data[3][1] )
423 && std::isfinite( m_data[3][2] ) && std::isfinite( m_data[3][3] );
424 }
425
426
427 inline bool Matrix4x4::isIdentity() const
428 {
429 return *this == Matrix4x4();
430 }
431
432
434 {
435 bool isRigid = true;
436 // rows have norm 1
437 isRigid &= equivalent(m_data[0][0]*m_data[0][0] + m_data[0][1]*m_data[0][1] + m_data[0][2]*m_data[0][2], double(1), double(1.0e-3));
438 isRigid &= equivalent(m_data[1][0]*m_data[1][0] + m_data[1][1]*m_data[1][1] + m_data[1][2]*m_data[1][2], double(1), double(1.0e-3));
439 isRigid &= equivalent(m_data[2][0]*m_data[2][0] + m_data[2][1]*m_data[2][1] + m_data[2][2]*m_data[2][2], double(1), double(1.0e-3));
440 // columns have norm 1
441 isRigid &= equivalent(m_data[0][0]*m_data[0][0] + m_data[1][0]*m_data[1][0] + m_data[2][0]*m_data[2][0], double(1), double(1.0e-3));
442 isRigid &= equivalent(m_data[0][1]*m_data[0][1] + m_data[1][1]*m_data[1][1] + m_data[2][1]*m_data[2][1], double(1), double(1.0e-3));
443 isRigid &= equivalent(m_data[0][2]*m_data[0][2] + m_data[1][2]*m_data[1][2] + m_data[2][2]*m_data[2][2], double(1), double(1.0e-3));
444 // determinant 1
445 isRigid &= equivalent(m_data[0][0]*m_data[1][1]*m_data[2][2] + m_data[0][1]*m_data[1][2]*m_data[2][0] + m_data[0][2]*m_data[1][0]*m_data[2][1]
446 - m_data[2][0]*m_data[1][1]*m_data[0][2] - m_data[1][0]*m_data[0][1]*m_data[2][2] - m_data[0][0]*m_data[2][1]*m_data[1][2],
447 double(1), double(1.0e-1));
448 // no row scaling
449 isRigid &= (m_data[0][3] == double(0));
450 isRigid &= (m_data[1][3] == double(0));
451 isRigid &= (m_data[2][3] == double(0));
452 // no transform scaling
453 isRigid &= (m_data[3][3] == double(1));
454 return isRigid;
455 }
456
457
458
459 /*=====================================================
460 Setters
461 =======================================================*/
462
463 inline Matrix4x4 Matrix4x4::set( double const * const ptr )
464 {
465 memcpy(m_data, ptr, sizeof(double)*16);
466 return *this;
467 }
468
469
470
471 inline Matrix4x4 Matrix4x4::set( double a00, double a01, double a02, double a03,
472 double a10, double a11, double a12, double a13,
473 double a20, double a21, double a22, double a23,
474 double a30, double a31, double a32, double a33 )
475 {
476 m_data[0][0] = a00;
477 m_data[0][1] = a01;
478 m_data[0][2] = a02;
479 m_data[0][3] = a03;
480
481 m_data[1][0] = a10;
482 m_data[1][1] = a11;
483 m_data[1][2] = a12;
484 m_data[1][3] = a13;
485
486 m_data[2][0] = a20;
487 m_data[2][1] = a21;
488 m_data[2][2] = a22;
489 m_data[2][3] = a23;
490
491 m_data[3][0] = a30;
492 m_data[3][1] = a31;
493 m_data[3][2] = a32;
494 m_data[3][3] = a33;
495
496 return *this;
497 }
498
499
501 {
502 this->setRotate(q);
503 this->setTranslate(0.0, 0.0, 0.0);
504
505 return *this;
506 }
507
508
509 inline Matrix4x4 Matrix4x4::setRotate( const Vec3& from, const Vec3& to )
510 {
511 Quat quat;
512 quat.setRotate(from,to);
513 this->setRotate(quat);
514 return *this;
515 }
516
517
518 inline Matrix4x4 Matrix4x4::setRotate( double angle, const Vec3& axis )
519 {
520 Quat quat;
521 quat.setRotate( angle, axis);
522 this->setRotate(quat);
523 return *this;
524 }
525
526
527 inline Matrix4x4 Matrix4x4::setRotate( double angle, double x, double y, double z )
528 {
529 Quat quat;
530 quat.setRotate( angle, x, y, z);
531 this->setRotate(quat);
532 return *this;
533 }
534
535
536 inline Matrix4x4 Matrix4x4::setRotate( double angle1, const Vec3& axis1,
537 double angle2, const Vec3& axis2,
538 double angle3, const Vec3& axis3 )
539 {
540 Quat quat;
541 quat.setRotate(angle1, axis1,
542 angle2, axis2,
543 angle3, axis3);
544 this->setRotate(quat);
545 return *this;
546 }
547
548
550 {
551 m_data[3][0] = v[0];
552 m_data[3][1] = v[1];
553 m_data[3][2] = v[2];
554
555 return *this;
556 }
557
558
559 inline Matrix4x4 Matrix4x4::setTranslate(double tx, double ty, double tz)
560 {
561 m_data[3][0] = tx;
562 m_data[3][1] = ty;
563 m_data[3][2] = tz;
564
565 return *this;
566 }
567
568
570 {
571 m_data[3][0] += v[0];
572 m_data[3][1] += v[1];
573 m_data[3][2] += v[2];
574
575 return *this;
576 }
577
578
579 inline Matrix4x4 Matrix4x4::addTranslate(double tx, double ty, double tz)
580 {
581 m_data[3][0] += tx;
582 m_data[3][1] += ty;
583 m_data[3][2] += tz;
584
585 return *this;
586 }
587
588
589
591 {
592 this->set(1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1);
593 return *this;
594 }
595
596 /*=====================================================
597 Getters
598 =======================================================*/
599
600
602 {
603 return Vec3( m_data[3][0], m_data[3][1], m_data[3][2] );
604 }
605
606
607
609 {
610 Quat q;
611 q = getAsQuat();
612 return q;
613 }
614
615 inline double *Matrix4x4::ptr()
616 {
617 return ( double* )m_data;
618 }
619
620
621 inline const double *Matrix4x4::ptr() const
622 {
623 return ( const double * )m_data;
624 }
625
626 /*=====================================================
627 Serialization methods
628 =======================================================*/
629
630 /*=====================================================
631 Static methods
632 =======================================================*/
633
634
636 {
637 return Matrix4x4( 0, -v.z(), v.y(), 0,
638 v.z(), 0, -v.x(), 0,
639 -v.y(), v.x(), 0, 0,
640 0, 0, 0, 1 );
641 }
642
643
644
646 {
647 Matrix4x4 m;
648 m.setTranslate(dv);
649 return m;
650 }
651
652
653 inline Matrix4x4 Matrix4x4::translate( double x, double y, double z )
654 {
655 Matrix4x4 m;
656 m.setTranslate(x, y, z);
657 return m;
658 }
659
660
661 inline Matrix4x4 Matrix4x4::rotate( const Vec3& from, const Vec3& to )
662 {
663 Matrix4x4 m;
664 m.setRotate(from, to);
665 return m;
666 }
667
668
669 inline Matrix4x4 Matrix4x4::rotate( double angle, double x, double y, double z )
670 {
671 Matrix4x4 m;
672 m.setRotate(angle, x, y, z);
673 return m;
674 }
675
676
677 inline Matrix4x4 Matrix4x4::rotate( double angle, const Vec3& axis )
678 {
679 Matrix4x4 m;
680 m.setRotate(angle, axis);
681 return m;
682 }
683
684
685 inline Matrix4x4 Matrix4x4::rotate( double angle1, const Vec3& axis1,
686 double angle2, const Vec3& axis2,
687 double angle3, const Vec3& axis3 )
688 {
689 Matrix4x4 m;
690 m.setRotate(angle1, axis1, angle2, axis2, angle3, axis3);
691 return m;
692 }
693
694
695 /*=====================================================
696 Operators
697 =======================================================*/
698
699
700 inline void Matrix4x4::mult( const Matrix4x4& lhs, const Matrix4x4& rhs )
701 {
702 agx::prefetch<agx::L1>( lhs.m_data );
703 agx::prefetch<agx::L1>( rhs.m_data );
704
705 if (&lhs==this)
706 {
707 postMult(rhs);
708 return;
709 }
710 if (&rhs==this)
711 {
712 preMult(lhs);
713 return;
714 }
715
716 m_data[0][0] = MOMENTUM_MATRIX4X4_INNER_PRODUCT(lhs, rhs, 0, 0);
717 m_data[0][1] = MOMENTUM_MATRIX4X4_INNER_PRODUCT(lhs, rhs, 0, 1);
718 m_data[0][2] = MOMENTUM_MATRIX4X4_INNER_PRODUCT(lhs, rhs, 0, 2);
719 m_data[0][3] = MOMENTUM_MATRIX4X4_INNER_PRODUCT(lhs, rhs, 0, 3);
720
721 m_data[1][0] = MOMENTUM_MATRIX4X4_INNER_PRODUCT(lhs, rhs, 1, 0);
722 m_data[1][1] = MOMENTUM_MATRIX4X4_INNER_PRODUCT(lhs, rhs, 1, 1);
723 m_data[1][2] = MOMENTUM_MATRIX4X4_INNER_PRODUCT(lhs, rhs, 1, 2);
724 m_data[1][3] = MOMENTUM_MATRIX4X4_INNER_PRODUCT(lhs, rhs, 1, 3);
725
726 m_data[2][0] = MOMENTUM_MATRIX4X4_INNER_PRODUCT(lhs, rhs, 2, 0);
727 m_data[2][1] = MOMENTUM_MATRIX4X4_INNER_PRODUCT(lhs, rhs, 2, 1);
728 m_data[2][2] = MOMENTUM_MATRIX4X4_INNER_PRODUCT(lhs, rhs, 2, 2);
729 m_data[2][3] = MOMENTUM_MATRIX4X4_INNER_PRODUCT(lhs, rhs, 2, 3);
730
731 m_data[3][0] = MOMENTUM_MATRIX4X4_INNER_PRODUCT(lhs, rhs, 3, 0);
732 m_data[3][1] = MOMENTUM_MATRIX4X4_INNER_PRODUCT(lhs, rhs, 3, 1);
733 m_data[3][2] = MOMENTUM_MATRIX4X4_INNER_PRODUCT(lhs, rhs, 3, 2);
734 m_data[3][3] = MOMENTUM_MATRIX4X4_INNER_PRODUCT(lhs, rhs, 3, 3);
735
736 }
737
738
739 inline void Matrix4x4::preMult( const Matrix4x4& other )
740 {
741
742 // more efficient method just use a T[4] for temporary storage.
743 double t[4];
744 for(size_t col=0; col<4; ++col) {
745 t[0] = MOMENTUM_MATRIX4X4_INNER_PRODUCT( other, *this, 0, col );
746 t[1] = MOMENTUM_MATRIX4X4_INNER_PRODUCT( other, *this, 1, col );
747 t[2] = MOMENTUM_MATRIX4X4_INNER_PRODUCT( other, *this, 2, col );
748 t[3] = MOMENTUM_MATRIX4X4_INNER_PRODUCT( other, *this, 3, col );
749 m_data[0][col] = t[0];
750 m_data[1][col] = t[1];
751 m_data[2][col] = t[2];
752 m_data[3][col] = t[3];
753 }
754 }
755
756
757 inline void Matrix4x4::postMult( const Matrix4x4& other )
758 {
759
760 // more efficient method just use a T[3] for temporary storage.
761 double t[4];
762 for(size_t row=0; row<4; ++row)
763 {
764 t[0] = MOMENTUM_MATRIX4X4_INNER_PRODUCT( *this, other, row, 0 );
765 t[1] = MOMENTUM_MATRIX4X4_INNER_PRODUCT( *this, other, row, 1 );
766 t[2] = MOMENTUM_MATRIX4X4_INNER_PRODUCT( *this, other, row, 2 );
767 t[3] = MOMENTUM_MATRIX4X4_INNER_PRODUCT( *this, other, row, 3 );
768
769 MOMENTUM_MATRIX4X4_SET_ROW(row, t[0], t[1], t[2], t[3] )
770 }
771 }
772
773
775 {
776 for (unsigned i = 0; i < 3; ++i)
777 {
778 m_data[3][0] += v[i]*m_data[i][0];
779 m_data[3][1] += v[i]*m_data[i][1];
780 m_data[3][2] += v[i]*m_data[i][2];
781 m_data[3][3] += v[i]*m_data[i][3];
782 }
783 return *this;
784 }
785
786
788 {
789 for (unsigned i = 0; i < 3; ++i)
790 {
791 m_data[0][i] += v[i]*m_data[0][3];
792 m_data[1][i] += v[i]*m_data[1][3];
793 m_data[2][i] += v[i]*m_data[2][3];
794 m_data[3][i] += v[i]*m_data[3][3];
795 }
796 return *this;
797 }
798
799
800 inline bool Matrix4x4::operator== ( const Matrix4x4& m ) const
801 {
802 const int bitCompare = memcmp(m_data, m.m_data, sizeof(m_data));
803 return (bitCompare == 0);
804 }
805
806
807 inline bool Matrix4x4::operator!= ( const Matrix4x4& m ) const
808 {
809 const int bitCompare = memcmp(m_data, m.m_data, sizeof(m_data));
810 return (bitCompare != 0);
811 }
812
813#ifndef SWIG
814 inline double& Matrix4x4::operator()( size_t row, size_t col )
815 {
816 return m_data[row][col];
817 }
818#endif
819
820
821 inline double Matrix4x4::operator()( size_t row, size_t col ) const
822 {
823 return m_data[row][col];
824 }
825
826
828 {
829 if ( this != &rhs )
830 this->set( rhs.ptr() );
831
832 return *this;
833 }
834
835
836 inline void Matrix4x4::operator*= ( const Matrix4x4& other )
837 {
838 if ( this == &other )
839 {
840 Matrix4x4 temp( other );
841 postMult( temp );
842 }
843 else postMult( other );
844 }
845
846
848 {
849 Matrix4x4 r;
850 r.mult( *this, m );
851 return r;
852 }
853
854
855 inline Vec3 Matrix4x4::operator* ( const Vec3& v ) const
856 {
857 return postMult( v );
858 }
859
860
861 inline Vec4 Matrix4x4::operator* ( const Vec4& v ) const
862 {
863 return postMult( v );
864 }
865
866
867 inline Vec3 operator* ( const Vec3& v, const Matrix4x4& m )
868 {
869 return m.preMult( v );
870 }
871
872 inline Vec4 operator* ( const Vec4& v, const Matrix4x4& m )
873 {
874 return m.preMult( v );
875 }
876
877 std::ostream& operator<< (std::ostream& os, const Matrix4x4& m);
878
879 /*=====================================================
880 Internal
881 =======================================================*/
882
883 inline Vec3 Matrix4x4::transform3x3( const Vec3& vIn ) const
884 {
885 Vec3 vOut;
886 vOut.set(
887 (m_data[0][0] * vIn.x() + m_data[1][0] * vIn.y() + m_data[2][0] * vIn.z()),
888 (m_data[0][1] * vIn.x() + m_data[1][1] * vIn.y() + m_data[2][1] * vIn.z()),
889 (m_data[0][2] * vIn.x() + m_data[1][2] * vIn.y() + m_data[2][2] * vIn.z()));
890 return vOut;
891 }
892
893
894 inline Vec3 Matrix4x4::transform3x3Inv( const Vec3& vIn ) const
895 {
896 Vec3 vOut;
897 vOut.set(
898 (m_data[0][0] * vIn.x() + m_data[0][1] * vIn.y() + m_data[0][2] * vIn.z()),
899 (m_data[1][0] * vIn.x() + m_data[1][1] * vIn.y() + m_data[1][2] * vIn.z()),
900 (m_data[2][0] * vIn.x() + m_data[2][1] * vIn.y() + m_data[2][2] * vIn.z()));
901 return vOut;
902 }
903
904
905 inline Vec3 Matrix4x4::postMult( const Vec3& v ) const
906 {
907 double d = double(1.0) / ( m_data[3][0] * v.x() + m_data[3][1] * v.y() + m_data[3][2] * v.z() + double(1.0) ) ;
908 return Vec3( ( m_data[0][0]*v.x() + m_data[0][1]*v.y() + m_data[0][2]*v.z() ) *d,
909 ( m_data[1][0]*v.x() + m_data[1][1]*v.y() + m_data[1][2]*v.z() ) *d,
910 ( m_data[2][0]*v.x() + m_data[2][1]*v.y() + m_data[2][2]*v.z() ) *d ) ;
911 }
912
913
914 inline Vec3 Matrix4x4::preMult( const Vec3& v ) const
915 {
916 return Vec3(
917 ( m_data[0][0]*v.x() + m_data[1][0]*v.y() + m_data[2][0]*v.z() + m_data[3][0] ),
918 ( m_data[0][1]*v.x() + m_data[1][1]*v.y() + m_data[2][1]*v.z() + m_data[3][1] ),
919 ( m_data[0][2]*v.x() + m_data[1][2]*v.y() + m_data[2][2]*v.z() + m_data[3][2] ) );
920 }
921
922
923 inline Vec4 Matrix4x4::postMult( const Vec4& v ) const
924 {
925 return Vec4(
926 ( m_data[0][0]*v.x() + m_data[0][1]*v.y() + m_data[0][2]*v.z() + m_data[0][3]*v.w() ),
927 ( m_data[1][0]*v.x() + m_data[1][1]*v.y() + m_data[1][2]*v.z() + m_data[1][3]*v.w() ),
928 ( m_data[2][0]*v.x() + m_data[2][1]*v.y() + m_data[2][2]*v.z() + m_data[2][3]*v.w() ),
929 ( m_data[3][0]*v.x() + m_data[3][1]*v.y() + m_data[3][2]*v.z() + m_data[3][3]*v.w() ) ) ;
930 }
931
932
933 inline Vec4 Matrix4x4::preMult( const Vec4& v ) const
934 {
935 return Vec4(
936 ( m_data[0][0]*v.x() + m_data[1][0]*v.y() + m_data[2][0]*v.z() + m_data[3][0]*v.w() ),
937 ( m_data[0][1]*v.x() + m_data[1][1]*v.y() + m_data[2][1]*v.z() + m_data[3][1]*v.w() ),
938 ( m_data[0][2]*v.x() + m_data[1][2]*v.y() + m_data[2][2]*v.z() + m_data[3][2]*v.w() ),
939 ( m_data[0][3]*v.x() + m_data[1][3]*v.y() + m_data[2][3]*v.z() + m_data[3][3]*v.w() ) );
940 }
941
942
943
945 {
946 return Matrix4x4( m_data[0][0], m_data[1][0], m_data[2][0], m_data[3][0],
947 m_data[0][1], m_data[1][1], m_data[2][1], m_data[3][1],
948 m_data[0][2], m_data[1][2], m_data[2][2], m_data[3][2],
949 m_data[0][3], m_data[1][3], m_data[2][3], m_data[3][3]);
950 }
951
952
953
955 {
956 Matrix4x4 m;
957 m.invert( *this );
958 return m;
959 }
960
961
962 inline bool equivalent( const Matrix4x4& a, const Matrix4x4& b, double epsilon = 1e-6 )
963 {
964 return
965 agx::equivalent(a(0, 0), b(0, 0), epsilon) &&
966 agx::equivalent(a(0, 1), b(0, 1), epsilon) &&
967 agx::equivalent(a(0, 2), b(0, 2), epsilon) &&
968 agx::equivalent(a(0, 3), b(0, 3), epsilon) &&
969 agx::equivalent(a(1, 0), b(1, 0), epsilon) &&
970 agx::equivalent(a(1, 1), b(1, 1), epsilon) &&
971 agx::equivalent(a(1, 2), b(1, 2), epsilon) &&
972 agx::equivalent(a(1, 3), b(1, 3), epsilon) &&
973 agx::equivalent(a(2, 0), b(2, 0), epsilon) &&
974 agx::equivalent(a(2, 1), b(2, 1), epsilon) &&
975 agx::equivalent(a(2, 2), b(2, 2), epsilon) &&
976 agx::equivalent(a(2, 3), b(2, 3), epsilon) &&
977 agx::equivalent(a(3, 0), b(3, 0), epsilon) &&
978 agx::equivalent(a(3, 1), b(3, 1), epsilon) &&
979 agx::equivalent(a(3, 2), b(3, 2), epsilon) &&
980 agx::equivalent(a(3, 3), b(3, 3), epsilon);
981 }
982
983}
984
985#endif //"#ifndef SWIG"
986
987#endif
This class provides conversion services between Euler angles in any of the 24 conventions and corresp...
Definition: EulerAngles.h:64
Matrix class for affine transformations.
Definition: Matrix4x4.h:51
Matrix4x4 operator=(const Matrix4x4 &rhs)
Definition: Matrix4x4.h:827
static Matrix4x4 crossMatrix(const Vec3 &vec)
Generates a new matrix of a specific type.
Definition: Matrix4x4.h:635
Matrix4x4 set(double const *const ptr)
Definition: Matrix4x4.h:463
double * ptr()
Definition: Matrix4x4.h:615
bool isRigidTransformation() const
Definition: Matrix4x4.h:433
void mult(const Matrix4x4 &, const Matrix4x4 &)
Definition: Matrix4x4.h:700
~Matrix4x4()
Destructor.
Definition: Matrix4x4.h:385
Matrix4x4 addTranslate(const Vec3 &t)
Definition: Matrix4x4.h:569
void setCol(size_t col, const Vec4 &vec)
Definition: Matrix4x4.h:164
Matrix4x4 setIdentity()
Definition: Matrix4x4.h:590
double & operator()(size_t row, size_t col)
Definition: Matrix4x4.h:814
void setRow(size_t row, const Vec4 &vec)
Set the row of the matrix,.
Definition: Matrix4x4.h:157
Matrix4x4 transpose() const
Definition: Matrix4x4.h:944
Vec4 getCol(size_t col) const
Definition: Matrix4x4.h:181
void operator*=(const Matrix4x4 &other)
Definition: Matrix4x4.h:836
Vec3 transform3x3Inv(const Vec3 &vIn) const
Definition: Matrix4x4.h:894
static Matrix4x4 rotate(const Vec3 &from, const Vec3 &to)
Definition: Matrix4x4.h:661
Matrix4x4 setRotate(const EulerAngles &euler)
Vec3 transform3x3(const Vec3 &vIn) const
Definition: Matrix4x4.h:883
static Matrix4x4 translate(const Vec3 &dv)
Definition: Matrix4x4.h:645
bool isFinite() const
Definition: Matrix4x4.h:414
Matrix4x4 preMultTranslate(const Vec3 &v)
Definition: Matrix4x4.h:774
Matrix4x4()
Creates a new matrix, initialized to be an identity matrix.
Definition: Matrix4x4.h:309
bool valid() const
Definition: Matrix4x4.h:393
bool operator!=(const Matrix4x4 &m) const
Definition: Matrix4x4.h:807
bool isIdentity() const
Definition: Matrix4x4.h:427
Matrix4x4 setTranslate(const Vec3 &t)
Definition: Matrix4x4.h:549
Vec4 getRow(size_t row) const
Definition: Matrix4x4.h:171
Matrix4x4 set(const EulerAngles &e)
bool operator==(const Matrix4x4 &m) const
Definition: Matrix4x4.h:800
bool isNaN() const
Definition: Matrix4x4.h:400
Vec3 preMult(const Vec3 &v) const
Definition: Matrix4x4.h:914
Vec3 operator*(const Vec3 &v) const
Definition: Matrix4x4.h:855
Vec3 postMult(const Vec3 &v) const
Definition: Matrix4x4.h:905
Matrix4x4 postMultTranslate(const Vec3 &v)
Definition: Matrix4x4.h:787
Matrix4x4 inverse() const
Matrix inverse.
Definition: Matrix4x4.h:954
Quat getRotate() const
Definition: Matrix4x4.h:608
Matrix4x4 setRotate(const Quat &q)
Vec3 getTranslate() const
Definition: Matrix4x4.h:601
EulerAngles getAsEulerAngles() const
The object holding quaternions and providing operations on these.
Definition: Quat.h:55
void setRotate(double angle, double x, double y, double z)
Set the rotation of the Quaternion as a rotation angle radians around the vector (x,...
A 3 dimensional vector which can be used to define a point or a vector and contains basic arithmetic.
Definition: Vec3.h:40
double y() const
Definition: Vec3.h:497
void set(double x, double y, double z)
Set the value of this vector with the specified elements.
Definition: Vec3.h:457
double z() const
Definition: Vec3.h:503
double x() const
Definition: Vec3.h:491
A 4 dimensional vector and contains basic arithmetic.
Definition: Vec4.h:33
double z() const
Definition: Vec4.h:466
double w() const
Definition: Vec4.h:472
double y() const
Definition: Vec4.h:461
double x() const
Definition: Vec4.h:455
Namespace for Momentum Scripting API.
Definition: AffineMatrix4x4.h:29
std::ostream & operator<<(std::ostream &os, const EulerAngles &e)
Definition: EulerAngles.h:358
Vec3 operator*(const Vec3 &v, const Matrix3x3 &m)
Definition: Matrix3x3.h:746
bool equivalent(const Matrix3x3 &a, const Matrix3x3 &b, double epsilon=1e-6)
Definition: Matrix3x3.h:905