31 #ifndef OPENVDB_MATH_MAT3_H_HAS_BEEN_INCLUDED 32 #define OPENVDB_MATH_MAT3_H_HAS_BEEN_INCLUDED 47 template<
typename T>
class Vec3;
48 template<
typename T>
class Mat4;
49 template<
typename T>
class Quat;
76 template<
typename Source>
77 Mat3(Source a, Source b, Source c,
78 Source d, Source e, Source f,
79 Source g, Source h, Source i)
81 MyBase::mm[0] =
static_cast<ValueType
>(a);
82 MyBase::mm[1] =
static_cast<ValueType
>(b);
83 MyBase::mm[2] =
static_cast<ValueType
>(c);
84 MyBase::mm[3] =
static_cast<ValueType
>(d);
85 MyBase::mm[4] =
static_cast<ValueType
>(e);
86 MyBase::mm[5] =
static_cast<ValueType
>(f);
87 MyBase::mm[6] =
static_cast<ValueType
>(g);
88 MyBase::mm[7] =
static_cast<ValueType
>(h);
89 MyBase::mm[8] =
static_cast<ValueType
>(i);
94 template<
typename Source>
98 this->setRows(v1, v2, v3);
100 this->setColumns(v1, v2, v3);
108 template<
typename Source>
111 MyBase::mm[0] = a[0];
112 MyBase::mm[1] = a[1];
113 MyBase::mm[2] = a[2];
114 MyBase::mm[3] = a[3];
115 MyBase::mm[4] = a[4];
116 MyBase::mm[5] = a[5];
117 MyBase::mm[6] = a[6];
118 MyBase::mm[7] = a[7];
119 MyBase::mm[8] = a[8];
125 for (
int i=0; i<3; ++i) {
126 for (
int j=0; j<3; ++j) {
127 MyBase::mm[i*3 + j] = m[i][j];
133 template<
typename Source>
136 for (
int i=0; i<3; ++i) {
137 for (
int j=0; j<3; ++j) {
138 MyBase::mm[i*3 + j] =
static_cast<T
>(m[i][j]);
146 for (
int i=0; i<3; ++i) {
147 for (
int j=0; j<3; ++j) {
148 MyBase::mm[i*3 + j] = m[i][j];
169 MyBase::mm[i3+0] = v[0];
170 MyBase::mm[i3+1] = v[1];
171 MyBase::mm[i3+2] = v[2];
178 return Vec3<T>((*this)(i,0), (*
this)(i,1), (*
this)(i,2));
185 MyBase::mm[0+j] = v[0];
186 MyBase::mm[3+j] = v[1];
187 MyBase::mm[6+j] = v[2];
194 return Vec3<T>((*this)(0,j), (*
this)(1,j), (*
this)(2,j));
202 T* operator[](
int i) {
return &(MyBase::mm[i*3]); }
205 const T*
operator[](
int i)
const {
return &(MyBase::mm[i*3]); }
218 return MyBase::mm[3*i+j];
228 return MyBase::mm[3*i+j];
234 MyBase::mm[0] = v1[0];
235 MyBase::mm[1] = v1[1];
236 MyBase::mm[2] = v1[2];
237 MyBase::mm[3] = v2[0];
238 MyBase::mm[4] = v2[1];
239 MyBase::mm[5] = v2[2];
240 MyBase::mm[6] = v3[0];
241 MyBase::mm[7] = v3[1];
242 MyBase::mm[8] = v3[2];
248 MyBase::mm[0] = v1[0];
249 MyBase::mm[1] = v2[0];
250 MyBase::mm[2] = v3[0];
251 MyBase::mm[3] = v1[1];
252 MyBase::mm[4] = v2[1];
253 MyBase::mm[5] = v3[1];
254 MyBase::mm[6] = v1[2];
255 MyBase::mm[7] = v2[2];
256 MyBase::mm[8] = v3[2];
262 this->setRows(v1, v2, v3);
268 MyBase::mm[0] = vdiag[0];
269 MyBase::mm[1] = vtri[0];
270 MyBase::mm[2] = vtri[1];
271 MyBase::mm[3] = vtri[0];
272 MyBase::mm[4] = vdiag[1];
273 MyBase::mm[5] = vtri[2];
274 MyBase::mm[6] = vtri[1];
275 MyBase::mm[7] = vtri[2];
276 MyBase::mm[8] = vdiag[2];
284 vdiag[0], vtri[0], vtri[1],
285 vtri[0], vdiag[1], vtri[2],
286 vtri[1], vtri[2], vdiag[2]
298 {*
this = rotation<Mat3<T> >(q);}
303 {*
this = rotation<Mat3<T> >(axis,
angle);}
334 template<
typename Source>
340 std::copy(src, (src + this->numElements()), MyBase::mm);
345 bool eq(
const Mat3 &m, T eps=1.0e-8)
const 362 -MyBase::mm[0], -MyBase::mm[1], -MyBase::mm[2],
363 -MyBase::mm[3], -MyBase::mm[4], -MyBase::mm[5],
364 -MyBase::mm[6], -MyBase::mm[7], -MyBase::mm[8]
374 template <
typename S>
377 MyBase::mm[0] *= scalar;
378 MyBase::mm[1] *= scalar;
379 MyBase::mm[2] *= scalar;
380 MyBase::mm[3] *= scalar;
381 MyBase::mm[4] *= scalar;
382 MyBase::mm[5] *= scalar;
383 MyBase::mm[6] *= scalar;
384 MyBase::mm[7] *= scalar;
385 MyBase::mm[8] *= scalar;
390 template <
typename S>
395 MyBase::mm[0] += s[0];
396 MyBase::mm[1] += s[1];
397 MyBase::mm[2] += s[2];
398 MyBase::mm[3] += s[3];
399 MyBase::mm[4] += s[4];
400 MyBase::mm[5] += s[5];
401 MyBase::mm[6] += s[6];
402 MyBase::mm[7] += s[7];
403 MyBase::mm[8] += s[8];
408 template <
typename S>
413 MyBase::mm[0] -= s[0];
414 MyBase::mm[1] -= s[1];
415 MyBase::mm[2] -= s[2];
416 MyBase::mm[3] -= s[3];
417 MyBase::mm[4] -= s[4];
418 MyBase::mm[5] -= s[5];
419 MyBase::mm[6] -= s[6];
420 MyBase::mm[7] -= s[7];
421 MyBase::mm[8] -= s[8];
426 template <
typename S>
433 MyBase::mm[0] =
static_cast<T
>(s0[0] * s1[0] +
436 MyBase::mm[1] =
static_cast<T
>(s0[0] * s1[1] +
439 MyBase::mm[2] =
static_cast<T
>(s0[0] * s1[2] +
443 MyBase::mm[3] =
static_cast<T
>(s0[3] * s1[0] +
446 MyBase::mm[4] =
static_cast<T
>(s0[3] * s1[1] +
449 MyBase::mm[5] =
static_cast<T
>(s0[3] * s1[2] +
453 MyBase::mm[6] =
static_cast<T
>(s0[6] * s1[0] +
456 MyBase::mm[7] =
static_cast<T
>(s0[6] * s1[1] +
459 MyBase::mm[8] =
static_cast<T
>(s0[6] * s1[2] +
470 MyBase::mm[4] * MyBase::mm[8] - MyBase::mm[5] * MyBase::mm[7],
471 MyBase::mm[5] * MyBase::mm[6] - MyBase::mm[3] * MyBase::mm[8],
472 MyBase::mm[3] * MyBase::mm[7] - MyBase::mm[4] * MyBase::mm[6],
473 MyBase::mm[2] * MyBase::mm[7] - MyBase::mm[1] * MyBase::mm[8],
474 MyBase::mm[0] * MyBase::mm[8] - MyBase::mm[2] * MyBase::mm[6],
475 MyBase::mm[1] * MyBase::mm[6] - MyBase::mm[0] * MyBase::mm[7],
476 MyBase::mm[1] * MyBase::mm[5] - MyBase::mm[2] * MyBase::mm[4],
477 MyBase::mm[2] * MyBase::mm[3] - MyBase::mm[0] * MyBase::mm[5],
478 MyBase::mm[0] * MyBase::mm[4] - MyBase::mm[1] * MyBase::mm[3]);
485 MyBase::mm[4] * MyBase::mm[8] - MyBase::mm[5] * MyBase::mm[7],
486 MyBase::mm[2] * MyBase::mm[7] - MyBase::mm[1] * MyBase::mm[8],
487 MyBase::mm[1] * MyBase::mm[5] - MyBase::mm[2] * MyBase::mm[4],
488 MyBase::mm[5] * MyBase::mm[6] - MyBase::mm[3] * MyBase::mm[8],
489 MyBase::mm[0] * MyBase::mm[8] - MyBase::mm[2] * MyBase::mm[6],
490 MyBase::mm[2] * MyBase::mm[3] - MyBase::mm[0] * MyBase::mm[5],
491 MyBase::mm[3] * MyBase::mm[7] - MyBase::mm[4] * MyBase::mm[6],
492 MyBase::mm[1] * MyBase::mm[6] - MyBase::mm[0] * MyBase::mm[7],
493 MyBase::mm[0] * MyBase::mm[4] - MyBase::mm[1] * MyBase::mm[3]);
501 MyBase::mm[0], MyBase::mm[3], MyBase::mm[6],
502 MyBase::mm[1], MyBase::mm[4], MyBase::mm[7],
503 MyBase::mm[2], MyBase::mm[5], MyBase::mm[8]);
513 const T det = inv.
mm[0]*MyBase::mm[0] + inv.
mm[1]*MyBase::mm[3] + inv.
mm[2]*MyBase::mm[6];
519 return inv * (T(1)/det);
525 const T co00 = MyBase::mm[4]*MyBase::mm[8] - MyBase::mm[5]*MyBase::mm[7];
526 const T co10 = MyBase::mm[5]*MyBase::mm[6] - MyBase::mm[3]*MyBase::mm[8];
527 const T co20 = MyBase::mm[3]*MyBase::mm[7] - MyBase::mm[4]*MyBase::mm[6];
528 return MyBase::mm[0]*co00 + MyBase::mm[1]*co10 + MyBase::mm[2]*co20;
534 return MyBase::mm[0]+MyBase::mm[4]+MyBase::mm[8];
548 template<
typename T0>
551 return static_cast< Vec3<T0> >(v * *
this);
556 template<
typename T0>
559 return static_cast< Vec3<T0> >(*
this * v);
569 ret.
mm[0] *= diag(0);
570 ret.
mm[1] *= diag(1);
571 ret.
mm[2] *= diag(2);
572 ret.
mm[3] *= diag(0);
573 ret.
mm[4] *= diag(1);
574 ret.
mm[5] *= diag(2);
575 ret.
mm[6] *= diag(0);
576 ret.
mm[7] *= diag(1);
577 ret.
mm[8] *= diag(2);
582 static const Mat3<T> sIdentity;
587 template <
typename T>
592 template <
typename T>
599 template <
typename T0,
typename T1>
605 for (
int i=0; i<9; ++i) {
613 template <
typename T0,
typename T1>
618 template <
typename S,
typename T>
624 template <
typename S,
typename T>
634 template <
typename T0,
typename T1>
644 template <
typename T0,
typename T1>
657 template <
typename T0,
typename T1>
667 template<
typename T,
typename MT>
673 _v[0]*m[0] + _v[1]*m[1] + _v[2]*m[2],
674 _v[0]*m[3] + _v[1]*m[4] + _v[2]*m[5],
675 _v[0]*m[6] + _v[1]*m[7] + _v[2]*m[8]);
680 template<
typename T,
typename MT>
686 _v[0]*m[0] + _v[1]*m[3] + _v[2]*m[6],
687 _v[0]*m[1] + _v[1]*m[4] + _v[2]*m[7],
688 _v[0]*m[2] + _v[1]*m[5] + _v[2]*m[8]);
693 template<
typename T,
typename MT>
703 template <
typename T>
706 return Mat3<T>(v1[0]*v2[0], v1[0]*v2[1], v1[0]*v2[2],
707 v1[1]*v2[0], v1[1]*v2[1], v1[1]*v2[2],
708 v1[2]*v2[0], v1[2]*v2[1], v1[2]*v2[2]);
714 #if DWREAL_IS_DOUBLE == 1 718 #endif // DWREAL_IS_DOUBLE 724 template<
typename T,
typename T0>
741 double cotan_of_2_theta;
743 double cosin_of_theta;
749 double Sjj_minus_Sii = D[j] - D[i];
752 tan_of_theta = Sij / Sjj_minus_Sii;
755 cotan_of_2_theta = 0.5*Sjj_minus_Sii / Sij ;
757 if (cotan_of_2_theta < 0.) {
759 -1./(sqrt(1. + cotan_of_2_theta*cotan_of_2_theta) - cotan_of_2_theta);
762 1./(sqrt(1. + cotan_of_2_theta*cotan_of_2_theta) + cotan_of_2_theta);
766 cosin_of_theta = 1./sqrt( 1. + tan_of_theta * tan_of_theta);
767 sin_of_theta = cosin_of_theta * tan_of_theta;
768 z = tan_of_theta * Sij;
772 for (
int k = 0; k < i; ++k) {
774 S(k,i) = cosin_of_theta * temp - sin_of_theta * S(k,j);
775 S(k,j)= sin_of_theta * temp + cosin_of_theta * S(k,j);
777 for (
int k = i+1; k < j; ++k) {
779 S(i,k) = cosin_of_theta * temp - sin_of_theta * S(k,j);
780 S(k,j) = sin_of_theta * temp + cosin_of_theta * S(k,j);
782 for (
int k = j+1; k < n; ++k) {
784 S(i,k) = cosin_of_theta * temp - sin_of_theta * S(j,k);
785 S(j,k) = sin_of_theta * temp + cosin_of_theta * S(j,k);
787 for (
int k = 0; k < n; ++k)
790 Q(k,i) = cosin_of_theta * temp - sin_of_theta*Q(k,j);
791 Q(k,j) = sin_of_theta * temp + cosin_of_theta*Q(k,j);
804 unsigned int MAX_ITERATIONS=250)
814 for (
int i = 0; i < n; ++i) {
818 unsigned int iterations(0);
825 for (
int i = 0; i < n; ++i) {
826 for (
int j = i+1; j < n; ++j) {
839 for (
int i = 0; i < n; ++i) {
840 for (
int j = i+1; j < n; ++j){
846 if (fabs(S(i,j)) > max_element) {
847 max_element = fabs(S(i,j));
853 pivot(ip, jp, S, D, Q);
854 }
while (iterations < MAX_ITERATIONS);
863 #endif // OPENVDB_MATH_MAT3_H_HAS_BEEN_INCLUDED Mat3(const Quat< T > &q)
Definition: Mat3.h:66
Vec3< T0 > pretransform(const Vec3< T0 > &v) const
Definition: Mat3.h:557
const Mat3 & operator=(const Mat3< Source > &m)
Assignment operator.
Definition: Mat3.h:335
static Mat3 symmetric(const Vec3< T > &vdiag, const Vec3< T > &vtri)
Definition: Mat3.h:281
bool operator!=(const Mat3< T0 > &m0, const Mat3< T1 > &m1)
Inequality operator, does exact floating point comparisons.
Definition: Mat3.h:614
bool isExactlyEqual(const T0 &a, const T1 &b)
Return true if a is exactly equal to b.
Definition: Math.h:407
Mat3< float > Mat3s
Definition: Mat3.h:711
Mat3 cofactor() const
Return the cofactor matrix of "this".
Definition: Mat3.h:467
Mat3< T > operator-() const
Negation operator, for e.g. m1 = -m2;.
Definition: Mat3.h:359
static const Mat3< T > & zero()
Predefined constant for zero matrix.
Definition: Mat3.h:159
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:97
T mm[SIZE *SIZE]
Definition: Mat.h:141
const T * asPointer() const
Definition: Mat3.h:209
Definition: Exceptions.h:78
T & operator()(int i, int j)
Definition: Mat3.h:214
Mat3(Source a, Source b, Source c, Source d, Source e, Source f, Source g, Source h, Source i)
Constructor given array of elements, the ordering is in row major form:
Definition: Mat3.h:77
void setRows(const Vec3< T > &v1, const Vec3< T > &v2, const Vec3< T > &v3)
Set the rows of "this" matrix to the vectors v1, v2, v3.
Definition: Mat3.h:232
Mat3< typename promote< T0, T1 >::type > operator*(const Mat3< T0 > &m0, const Mat3< T1 > &m1)
Matrix multiplication.
Definition: Mat3.h:658
bool operator==(const Mat3< T0 > &m0, const Mat3< T1 > &m1)
Equality operator, does exact floating point comparisons.
Definition: Mat3.h:600
Mat3(const Mat4< T > &m)
Conversion from Mat4 (copies top left)
Definition: Mat3.h:144
Vec3< T > col(int j) const
Get jth column, e.g. Vec3d v = m.col(0);.
Definition: Mat3.h:191
Mat3 transpose() const
returns transpose of this
Definition: Mat3.h:498
Vec3< T0 > transform(const Vec3< T0 > &v) const
Definition: Mat3.h:549
Mat3< typename promote< S, T >::type > operator*(const Mat3< T > &m, S scalar)
Returns M, where for .
Definition: Mat3.h:625
4x4 -matrix class.
Definition: Mat3.h:48
Mat3< typename promote< T0, T1 >::type > operator-(const Mat3< T0 > &m0, const Mat3< T1 > &m1)
Returns M, where for .
Definition: Mat3.h:645
Mat3< T > powLerp(const Mat3< T0 > &m1, const Mat3< T0 > &m2, T t)
Definition: Mat3.h:725
bool isApproxEqual(const Type &a, const Type &b)
Return true if a is equal to b to within the default floating-point comparison tolerance.
Definition: Math.h:370
void setZero()
Set this matrix to zero.
Definition: Mat3.h:306
Mat3(Source *a)
Definition: Mat3.h:109
#define OPENVDB_VERSION_NAME
Definition: version.h:43
Mat3< typename promote< S, T >::type > operator*(S scalar, const Mat3< T > &m)
Returns M, where for .
Definition: Mat3.h:619
Vec3< T > row(int i) const
Get ith row, e.g. Vec3d v = m.row(1);.
Definition: Mat3.h:175
void setColumns(const Vec3< T > &v1, const Vec3< T > &v2, const Vec3< T > &v3)
Set the columns of "this" matrix to the vectors v1, v2, v3.
Definition: Mat3.h:246
void setToRotation(const Vec3< T > &axis, T angle)
Set this matrix to the rotation specified by axis and angle.
Definition: Mat3.h:302
Vec3< typename promote< T, MT >::type > operator*(const Vec3< T > &_v, const Mat3< MT > &_m)
Returns v, where for .
Definition: Mat3.h:682
Mat3< double > Mat3d
Definition: Mat3.h:712
T ValueType
Definition: Mat3.h:59
void setSymmetric(const Vec3< T > &vdiag, const Vec3< T > &vtri)
Set diagonal and symmetric triangular components.
Definition: Mat3.h:266
const Mat3< T > & operator-=(const Mat3< S > &m1)
Returns m0, where for .
Definition: Mat3.h:409
void setSkew(const Vec3< T > &v)
Set the matrix as cross product of the given vector.
Definition: Mat3.h:291
Definition: Exceptions.h:39
Tolerance for floating-point comparison.
Definition: Math.h:125
3x3 matrix class.
Definition: Mat3.h:54
bool eq(const Mat3 &m, T eps=1.0e-8) const
Test if "this" is equivalent to m with tolerance of eps value.
Definition: Mat3.h:345
Mat3 inverse(T tolerance=0) const
Definition: Mat3.h:509
T angle(const Vec2< T > &v1, const Vec2< T > &v2)
Definition: Vec2.h:446
const Mat3< T > & operator*=(S scalar)
Multiplication operator, e.g. M = scalar * M;.
Definition: Mat3.h:375
bool diagonalizeSymmetricMatrix(const Mat3< T > &input, Mat3< T > &Q, Vec3< T > &D, unsigned int MAX_ITERATIONS=250)
Use Jacobi iterations to decompose a symmetric 3x3 matrix (diagonalize and compute eigenvectors) ...
Definition: Mat3.h:803
OPENVDB_DEPRECATED void setBasis(const Vec3< T > &v1, const Vec3< T > &v2, const Vec3< T > &v3)
Set the rows of "this" matrix to the vectors v1, v2, v3.
Definition: Mat3.h:260
Mat3(const Mat3< Source > &m)
Conversion constructor.
Definition: Mat3.h:134
Mat3< T > outerProduct(const Vec3< T > &v1, const Vec3< T > &v2)
Definition: Mat3.h:704
Mat3(const Mat< 3, T > &m)
Copy constructor.
Definition: Mat3.h:123
void setIdentity()
Set "this" matrix to identity.
Definition: Mat3.h:320
Mat< 3, T > MyBase
Definition: Mat3.h:60
void setRow(int i, const Vec3< T > &v)
Set ith row to vector v.
Definition: Mat3.h:164
static const Mat3< T > & identity()
Predefined constant for identity matrix.
Definition: Mat3.h:154
Vec3< typename promote< T, MT >::type > operator*(const Mat3< MT > &_m, const Vec3< T > &_v)
Returns v, where for .
Definition: Mat3.h:669
void setCol(int j, const Vec3< T > &v)
Set jth column to vector v.
Definition: Mat3.h:182
Mat3 timesDiagonal(const Vec3< T > &diag) const
Definition: Mat3.h:565
Mat3()
Trivial constructor, the matrix is NOT initialized.
Definition: Mat3.h:62
MatType snapMatBasis(const MatType &source, Axis axis, const Vec3< typename MatType::value_type > &direction)
This function snaps a specific axis to a specific direction, preserving scaling.
Definition: Mat.h:730
T value_type
Data type held by the matrix.
Definition: Mat3.h:58
Mat3s Mat3f
Definition: Mat3.h:717
Axis
Definition: Math.h:856
MatType skew(const Vec3< typename MatType::value_type > &skew)
Return a matrix as the cross product of the given vector.
Definition: Mat.h:687
T det() const
Determinant of matrix.
Definition: Mat3.h:523
void powSolve(const MatType &aA, MatType &aB, double aPower, double aTol=0.01)
Definition: Mat.h:810
const Mat3< T > & operator*=(const Mat3< S > &m1)
Returns m0, where for .
Definition: Mat3.h:427
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:71
T trace() const
Trace of matrix.
Definition: Mat3.h:532
const T * operator[](int i) const
Definition: Mat3.h:205
Mat3 adjoint() const
returns adjoint of "this", i.e. the transpose of the cofactor of "this"
Definition: Mat3.h:482
void setToRotation(const Quat< T > &q)
Set this matrix to the rotation matrix specified by the quaternion.
Definition: Mat3.h:297
Mat3(const Vec3< Source > &v1, const Vec3< Source > &v2, const Vec3< Source > &v3, bool rows=true)
Definition: Mat3.h:95
Mat3 snapBasis(Axis axis, const Vec3< T > &direction)
Definition: Mat3.h:541
T operator()(int i, int j) const
Definition: Mat3.h:224
const Mat3< T > & operator+=(const Mat3< S > &m1)
Returns m0, where for .
Definition: Mat3.h:391
T * asPointer()
Definition: Mat3.h:208
Mat3< typename promote< T0, T1 >::type > operator+(const Mat3< T0 > &m0, const Mat3< T1 > &m1)
Returns M, where for .
Definition: Mat3.h:635