4#ifndef OPENVDB_MATH_MAT3_H_HAS_BEEN_INCLUDED
5#define OPENVDB_MATH_MAT3_H_HAS_BEEN_INCLUDED
21template<
typename T>
class Vec3;
22template<
typename T>
class Mat4;
23template<
typename T>
class Quat;
37#if OPENVDB_ABI_VERSION_NUMBER >= 8
47 for (
int i=0; i<3; ++i) {
48 for (
int j=0; j<3; ++j) {
49 MyBase::mm[i*3 + j] = m[i][j];
67 template<
typename Source>
68 Mat3(Source a, Source b, Source c,
69 Source d, Source e, Source f,
70 Source g, Source h, Source i)
72 MyBase::mm[0] =
static_cast<T
>(a);
73 MyBase::mm[1] =
static_cast<T
>(b);
74 MyBase::mm[2] =
static_cast<T
>(c);
75 MyBase::mm[3] =
static_cast<T
>(d);
76 MyBase::mm[4] =
static_cast<T
>(e);
77 MyBase::mm[5] =
static_cast<T
>(f);
78 MyBase::mm[6] =
static_cast<T
>(g);
79 MyBase::mm[7] =
static_cast<T
>(h);
80 MyBase::mm[8] =
static_cast<T
>(i);
85 template<
typename Source>
89 this->setRows(v1, v2, v3);
91 this->setColumns(v1, v2, v3);
99 template<
typename Source>
102 MyBase::mm[0] =
static_cast<T
>(a[0]);
103 MyBase::mm[1] =
static_cast<T
>(a[1]);
104 MyBase::mm[2] =
static_cast<T
>(a[2]);
105 MyBase::mm[3] =
static_cast<T
>(a[3]);
106 MyBase::mm[4] =
static_cast<T
>(a[4]);
107 MyBase::mm[5] =
static_cast<T
>(a[5]);
108 MyBase::mm[6] =
static_cast<T
>(a[6]);
109 MyBase::mm[7] =
static_cast<T
>(a[7]);
110 MyBase::mm[8] =
static_cast<T
>(a[8]);
114 template<
typename Source>
117 for (
int i=0; i<3; ++i) {
118 for (
int j=0; j<3; ++j) {
119 MyBase::mm[i*3 + j] =
static_cast<T
>(m[i][j]);
127 for (
int i=0; i<3; ++i) {
128 for (
int j=0; j<3; ++j) {
129 MyBase::mm[i*3 + j] = m[i][j];
160 MyBase::mm[i3+0] = v[0];
161 MyBase::mm[i3+1] = v[1];
162 MyBase::mm[i3+2] = v[2];
169 return Vec3<T>((*
this)(i,0), (*
this)(i,1), (*
this)(i,2));
176 MyBase::mm[0+j] = v[0];
177 MyBase::mm[3+j] = v[1];
178 MyBase::mm[6+j] = v[2];
185 return Vec3<T>((*
this)(0,j), (*
this)(1,j), (*
this)(2,j));
195 return MyBase::mm[3*i+j];
205 return MyBase::mm[3*i+j];
211 MyBase::mm[0] = v1[0];
212 MyBase::mm[1] = v1[1];
213 MyBase::mm[2] = v1[2];
214 MyBase::mm[3] = v2[0];
215 MyBase::mm[4] = v2[1];
216 MyBase::mm[5] = v2[2];
217 MyBase::mm[6] = v3[0];
218 MyBase::mm[7] = v3[1];
219 MyBase::mm[8] = v3[2];
225 MyBase::mm[0] = v1[0];
226 MyBase::mm[1] = v2[0];
227 MyBase::mm[2] = v3[0];
228 MyBase::mm[3] = v1[1];
229 MyBase::mm[4] = v2[1];
230 MyBase::mm[5] = v3[1];
231 MyBase::mm[6] = v1[2];
232 MyBase::mm[7] = v2[2];
233 MyBase::mm[8] = v3[2];
239 MyBase::mm[0] = vdiag[0];
240 MyBase::mm[1] = vtri[0];
241 MyBase::mm[2] = vtri[1];
242 MyBase::mm[3] = vtri[0];
243 MyBase::mm[4] = vdiag[1];
244 MyBase::mm[5] = vtri[2];
245 MyBase::mm[6] = vtri[1];
246 MyBase::mm[7] = vtri[2];
247 MyBase::mm[8] = vdiag[2];
254 vdiag[0], vtri[0], vtri[1],
255 vtri[0], vdiag[1], vtri[2],
256 vtri[1], vtri[2], vdiag[2]
268 {*
this = rotation<Mat3<T> >(q);}
273 {*
this = rotation<Mat3<T> >(axis,
angle);}
304 template<
typename Source>
310 std::copy(src, (src + this->numElements()), MyBase::mm);
315 bool eq(
const Mat3 &m, T eps=1.0e-8)
const
332 -MyBase::mm[0], -MyBase::mm[1], -MyBase::mm[2],
333 -MyBase::mm[3], -MyBase::mm[4], -MyBase::mm[5],
334 -MyBase::mm[6], -MyBase::mm[7], -MyBase::mm[8]
344 template <
typename S>
347 MyBase::mm[0] *= scalar;
348 MyBase::mm[1] *= scalar;
349 MyBase::mm[2] *= scalar;
350 MyBase::mm[3] *= scalar;
351 MyBase::mm[4] *= scalar;
352 MyBase::mm[5] *= scalar;
353 MyBase::mm[6] *= scalar;
354 MyBase::mm[7] *= scalar;
355 MyBase::mm[8] *= scalar;
360 template <
typename S>
365 MyBase::mm[0] += s[0];
366 MyBase::mm[1] += s[1];
367 MyBase::mm[2] += s[2];
368 MyBase::mm[3] += s[3];
369 MyBase::mm[4] += s[4];
370 MyBase::mm[5] += s[5];
371 MyBase::mm[6] += s[6];
372 MyBase::mm[7] += s[7];
373 MyBase::mm[8] += s[8];
378 template <
typename S>
383 MyBase::mm[0] -= s[0];
384 MyBase::mm[1] -= s[1];
385 MyBase::mm[2] -= s[2];
386 MyBase::mm[3] -= s[3];
387 MyBase::mm[4] -= s[4];
388 MyBase::mm[5] -= s[5];
389 MyBase::mm[6] -= s[6];
390 MyBase::mm[7] -= s[7];
391 MyBase::mm[8] -= s[8];
396 template <
typename S>
403 MyBase::mm[0] =
static_cast<T
>(s0[0] * s1[0] +
406 MyBase::mm[1] =
static_cast<T
>(s0[0] * s1[1] +
409 MyBase::mm[2] =
static_cast<T
>(s0[0] * s1[2] +
413 MyBase::mm[3] =
static_cast<T
>(s0[3] * s1[0] +
416 MyBase::mm[4] =
static_cast<T
>(s0[3] * s1[1] +
419 MyBase::mm[5] =
static_cast<T
>(s0[3] * s1[2] +
423 MyBase::mm[6] =
static_cast<T
>(s0[6] * s1[0] +
426 MyBase::mm[7] =
static_cast<T
>(s0[6] * s1[1] +
429 MyBase::mm[8] =
static_cast<T
>(s0[6] * s1[2] +
440 MyBase::mm[4] * MyBase::mm[8] - MyBase::mm[5] * MyBase::mm[7],
441 MyBase::mm[5] * MyBase::mm[6] - MyBase::mm[3] * MyBase::mm[8],
442 MyBase::mm[3] * MyBase::mm[7] - MyBase::mm[4] * MyBase::mm[6],
443 MyBase::mm[2] * MyBase::mm[7] - MyBase::mm[1] * MyBase::mm[8],
444 MyBase::mm[0] * MyBase::mm[8] - MyBase::mm[2] * MyBase::mm[6],
445 MyBase::mm[1] * MyBase::mm[6] - MyBase::mm[0] * MyBase::mm[7],
446 MyBase::mm[1] * MyBase::mm[5] - MyBase::mm[2] * MyBase::mm[4],
447 MyBase::mm[2] * MyBase::mm[3] - MyBase::mm[0] * MyBase::mm[5],
448 MyBase::mm[0] * MyBase::mm[4] - MyBase::mm[1] * MyBase::mm[3]);
455 MyBase::mm[4] * MyBase::mm[8] - MyBase::mm[5] * MyBase::mm[7],
456 MyBase::mm[2] * MyBase::mm[7] - MyBase::mm[1] * MyBase::mm[8],
457 MyBase::mm[1] * MyBase::mm[5] - MyBase::mm[2] * MyBase::mm[4],
458 MyBase::mm[5] * MyBase::mm[6] - MyBase::mm[3] * MyBase::mm[8],
459 MyBase::mm[0] * MyBase::mm[8] - MyBase::mm[2] * MyBase::mm[6],
460 MyBase::mm[2] * MyBase::mm[3] - MyBase::mm[0] * MyBase::mm[5],
461 MyBase::mm[3] * MyBase::mm[7] - MyBase::mm[4] * MyBase::mm[6],
462 MyBase::mm[1] * MyBase::mm[6] - MyBase::mm[0] * MyBase::mm[7],
463 MyBase::mm[0] * MyBase::mm[4] - MyBase::mm[1] * MyBase::mm[3]);
471 MyBase::mm[0], MyBase::mm[3], MyBase::mm[6],
472 MyBase::mm[1], MyBase::mm[4], MyBase::mm[7],
473 MyBase::mm[2], MyBase::mm[5], MyBase::mm[8]);
483 const T det = inv.
mm[0]*MyBase::mm[0] + inv.
mm[1]*MyBase::mm[3] + inv.
mm[2]*MyBase::mm[6];
489 return inv * (T(1)/det);
495 const T co00 = MyBase::mm[4]*MyBase::mm[8] - MyBase::mm[5]*MyBase::mm[7];
496 const T co10 = MyBase::mm[5]*MyBase::mm[6] - MyBase::mm[3]*MyBase::mm[8];
497 const T co20 = MyBase::mm[3]*MyBase::mm[7] - MyBase::mm[4]*MyBase::mm[6];
498 return MyBase::mm[0]*co00 + MyBase::mm[1]*co10 + MyBase::mm[2]*co20;
504 return MyBase::mm[0]+MyBase::mm[4]+MyBase::mm[8];
518 template<
typename T0>
521 return static_cast< Vec3<T0> >(v * *
this);
526 template<
typename T0>
529 return static_cast< Vec3<T0> >(*
this * v);
539 ret.
mm[0] *= diag(0);
540 ret.
mm[1] *= diag(1);
541 ret.
mm[2] *= diag(2);
542 ret.
mm[3] *= diag(0);
543 ret.
mm[4] *= diag(1);
544 ret.
mm[5] *= diag(2);
545 ret.
mm[6] *= diag(0);
546 ret.
mm[7] *= diag(1);
547 ret.
mm[8] *= diag(2);
555template <
typename T0,
typename T1>
561 for (
int i=0; i<9; ++i) {
569template <
typename T0,
typename T1>
574template <
typename S,
typename T>
580template <
typename S,
typename T>
590template <
typename T0,
typename T1>
600template <
typename T0,
typename T1>
610template <
typename T0,
typename T1>
620template<
typename T,
typename MT>
626 _v[0]*m[0] + _v[1]*m[1] + _v[2]*m[2],
627 _v[0]*m[3] + _v[1]*m[4] + _v[2]*m[5],
628 _v[0]*m[6] + _v[1]*m[7] + _v[2]*m[8]);
633template<
typename T,
typename MT>
639 _v[0]*m[0] + _v[1]*m[3] + _v[2]*m[6],
640 _v[0]*m[1] + _v[1]*m[4] + _v[2]*m[7],
641 _v[0]*m[2] + _v[1]*m[5] + _v[2]*m[8]);
646template<
typename T,
typename MT>
659 return Mat3<T>(v1[0]*v2[0], v1[0]*v2[1], v1[0]*v2[2],
660 v1[1]*v2[0], v1[1]*v2[1], v1[1]*v2[2],
661 v1[2]*v2[0], v1[2]*v2[1], v1[2]*v2[2]);
668template<
typename T,
typename T0>
678namespace mat3_internal {
687 double cotan_of_2_theta;
689 double cosin_of_theta;
695 double Sjj_minus_Sii = D[j] - D[i];
698 tan_of_theta = Sij / Sjj_minus_Sii;
701 cotan_of_2_theta = 0.5*Sjj_minus_Sii / Sij ;
703 if (cotan_of_2_theta < 0.) {
705 -1./(sqrt(1. + cotan_of_2_theta*cotan_of_2_theta) - cotan_of_2_theta);
708 1./(sqrt(1. + cotan_of_2_theta*cotan_of_2_theta) + cotan_of_2_theta);
712 cosin_of_theta = 1./sqrt( 1. + tan_of_theta * tan_of_theta);
713 sin_of_theta = cosin_of_theta * tan_of_theta;
714 z = tan_of_theta * Sij;
718 for (
int k = 0; k < i; ++k) {
720 S(k,i) = cosin_of_theta * temp - sin_of_theta * S(k,j);
721 S(k,j)= sin_of_theta * temp + cosin_of_theta * S(k,j);
723 for (
int k = i+1; k < j; ++k) {
725 S(i,k) = cosin_of_theta * temp - sin_of_theta * S(k,j);
726 S(k,j) = sin_of_theta * temp + cosin_of_theta * S(k,j);
728 for (
int k = j+1; k < n; ++k) {
730 S(i,k) = cosin_of_theta * temp - sin_of_theta * S(j,k);
731 S(j,k) = sin_of_theta * temp + cosin_of_theta * S(j,k);
733 for (
int k = 0; k < n; ++k)
736 Q(k,i) = cosin_of_theta * temp - sin_of_theta*Q(k,j);
737 Q(k,j) = sin_of_theta * temp + cosin_of_theta*Q(k,j);
752 unsigned int MAX_ITERATIONS=250)
762 for (
int i = 0; i < n; ++i) {
766 unsigned int iterations(0);
773 for (
int i = 0; i < n; ++i) {
774 for (
int j = i+1; j < n; ++j) {
787 for (
int i = 0; i < n; ++i) {
788 for (
int j = i+1; j < n; ++j){
794 if (fabs(S(i,j)) > max_element) {
795 max_element = fabs(S(i,j));
802 }
while (iterations < MAX_ITERATIONS);
814 for (
unsigned i = 0; i < 9; ++i, ++op, ++ip) *op =
math::Abs(*ip);
818template<
typename Type1,
typename Type2>
825 for (
unsigned i = 0; i < 9; ++i, ++op, ++ip) {
837 return cwiseLessThan<3, T>(m0, m1);
844 return cwiseGreaterThan<3, T>(m0, m1);
851#if OPENVDB_ABI_VERSION_NUMBER >= 8
859template<>
inline math::Mat3s zeroVal<math::Mat3s>() {
return math::Mat3s::zero(); }
860template<>
inline math::Mat3d zeroVal<math::Mat3d>() {
return math::Mat3d::zero(); }
#define OPENVDB_IS_POD(Type)
Definition: Math.h:55
Definition: Exceptions.h:56
3x3 matrix class.
Definition: Mat3.h:29
Mat3(Source *a)
Definition: Mat3.h:100
Mat3(const Mat4< T > &m)
Conversion from Mat4 (copies top left)
Definition: Mat3.h:125
const Mat3< T > & operator-=(const Mat3< S > &m1)
Subtract each element of the given matrix from the corresponding element of this matrix.
Definition: Mat3.h:379
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:209
Mat3< typename promote< S, T >::type > operator*(S scalar, const Mat3< T > &m)
Multiply each element of the given matrix by scalar and return the result.
Definition: Mat3.h:575
bool operator!=(const Mat3< T0 > &m0, const Mat3< T1 > &m1)
Inequality operator, does exact floating point comparisons.
Definition: Mat3.h:570
Mat3()=default
Trivial constructor, the matrix is NOT initialized.
Mat3< typename promote< T0, T1 >::type > operator+(const Mat3< T0 > &m0, const Mat3< T1 > &m1)
Add corresponding elements of m0 and m1 and return the result.
Definition: Mat3.h:591
Mat3< typename promote< S, T >::type > operator*(const Mat3< T > &m, S scalar)
Multiply each element of the given matrix by scalar and return the result.
Definition: Mat3.h:581
void setIdentity()
Set this matrix to identity.
Definition: Mat3.h:290
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:68
Mat3(const Mat3< Source > &m)
Conversion constructor.
Definition: Mat3.h:115
void setToRotation(const Vec3< T > &axis, T angle)
Set this matrix to the rotation specified by axis and angle.
Definition: Mat3.h:272
void setZero()
Set this matrix to zero.
Definition: Mat3.h:276
const Mat3 & operator=(const Mat3< Source > &m)
Assignment operator.
Definition: Mat3.h:305
Mat3(const Quat< T > &q)
Definition: Mat3.h:57
const Mat3< T > & operator+=(const Mat3< S > &m1)
Add each element of the given matrix to the corresponding element of this matrix.
Definition: Mat3.h:361
Vec3< T0 > pretransform(const Vec3< T0 > &v) const
Definition: Mat3.h:527
bool eq(const Mat3 &m, T eps=1.0e-8) const
Return true if this matrix is equivalent to m within a tolerance of eps.
Definition: Mat3.h:315
void setToRotation(const Quat< T > &q)
Set this matrix to the rotation matrix specified by the quaternion.
Definition: Mat3.h:267
Mat3 inverse(T tolerance=0) const
Definition: Mat3.h:479
T trace() const
Trace of matrix.
Definition: Mat3.h:502
Mat3< T > operator-() const
Negation operator, for e.g. m1 = -m2;.
Definition: Mat3.h:329
void setCol(int j, const Vec3< T > &v)
Set jth column to vector v.
Definition: Mat3.h:173
Mat3 adjoint() const
Return the adjoint of this matrix, i.e., the transpose of its cofactor.
Definition: Mat3.h:452
void setSkew(const Vec3< T > &v)
Set the matrix as cross product of the given vector.
Definition: Mat3.h:261
const Mat3< T > & operator*=(S scalar)
Multiplication operator, e.g. M = scalar * M;.
Definition: Mat3.h:345
Vec3< typename promote< T, MT >::type > operator*(const Mat3< MT > &_m, const Vec3< T > &_v)
Multiply _m by _v and return the resulting vector.
Definition: Mat3.h:622
Mat3 snapBasis(Axis axis, const Vec3< T > &direction)
Definition: Mat3.h:511
Mat3 transpose() const
returns transpose of this
Definition: Mat3.h:468
Mat3 timesDiagonal(const Vec3< T > &diag) const
Treat diag as a diagonal matrix and return the product of this matrix with diag (from the right).
Definition: Mat3.h:535
T det() const
Determinant of matrix.
Definition: Mat3.h:493
static const Mat3< T > & zero()
Predefined constant for zero matrix.
Definition: Mat3.h:145
Vec3< T > col(int j) const
Get jth column, e.g. Vec3d v = m.col(0);.
Definition: Mat3.h:182
T & operator()(int i, int j)
Definition: Mat3.h:191
static Mat3 symmetric(const Vec3< T > &vdiag, const Vec3< T > &vtri)
Return a matrix with the prescribed diagonal and symmetric triangular components.
Definition: Mat3.h:251
Vec3< T0 > transform(const Vec3< T0 > &v) const
Definition: Mat3.h:519
static const Mat3< T > & identity()
Predefined constant for identity matrix.
Definition: Mat3.h:135
T operator()(int i, int j) const
Definition: Mat3.h:201
bool operator==(const Mat3< T0 > &m0, const Mat3< T1 > &m1)
Equality operator, does exact floating point comparisons.
Definition: Mat3.h:556
const Mat3< T > & operator*=(const Mat3< S > &m1)
Multiply this matrix by the given matrix.
Definition: Mat3.h:397
Vec3< T > row(int i) const
Get ith row, e.g. Vec3d v = m.row(1);.
Definition: Mat3.h:166
void setRow(int i, const Vec3< T > &v)
Set ith row to vector v.
Definition: Mat3.h:155
Vec3< typename promote< T, MT >::type > operator*(const Vec3< T > &_v, const Mat3< MT > &_m)
Multiply _v by _m and return the resulting vector.
Definition: Mat3.h:635
T ValueType
Definition: Mat3.h:33
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:223
Mat3 cofactor() const
Return the cofactor matrix of this matrix.
Definition: Mat3.h:437
Mat3< typename promote< T0, T1 >::type > operator-(const Mat3< T0 > &m0, const Mat3< T1 > &m1)
Subtract corresponding elements of m0 and m1 and return the result.
Definition: Mat3.h:601
Mat3(const Vec3< Source > &v1, const Vec3< Source > &v2, const Vec3< Source > &v3, bool rows=true)
Definition: Mat3.h:86
void setSymmetric(const Vec3< T > &vdiag, const Vec3< T > &vtri)
Set diagonal and symmetric triangular components.
Definition: Mat3.h:237
T value_type
Data type held by the matrix.
Definition: Mat3.h:32
4x4 -matrix class.
Definition: Mat4.h:24
T mm[SIZE *SIZE]
Definition: Mat.h:182
T * asPointer()
Direct access to the internal data.
Definition: Mat.h:123
void pivot(int i, int j, Mat3< T > &S, Vec3< T > &D, Mat3< T > &Q)
Definition: Mat3.h:682
void powSolve(const MatType &aA, MatType &aB, double aPower, double aTol=0.01)
Definition: Mat.h:844
Mat3< double > Mat3d
Definition: Mat3.h:848
MatType skew(const Vec3< typename MatType::value_type > &skew)
Return a matrix as the cross product of the given vector.
Definition: Mat.h:730
bool isExactlyEqual(const T0 &a, const T1 &b)
Return true if a is exactly equal to b.
Definition: Math.h:444
bool cwiseLessThan(const Mat3< T > &m0, const Mat3< T > &m1)
Definition: Mat3.h:835
Mat3< T > powLerp(const Mat3< T0 > &m1, const Mat3< T0 > &m2, T t)
Definition: Mat3.h:669
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:751
T angle(const Vec2< T > &v1, const Vec2< T > &v2)
Definition: Vec2.h:450
Mat3< T > Abs(const Mat3< T > &m)
Definition: Mat3.h:809
Mat3< T > outerProduct(const Vec3< T > &v1, const Vec3< T > &v2)
Definition: Mat3.h:657
Mat3< typename promote< T0, T1 >::type > operator*(const Mat3< T0 > &m0, const Mat3< T1 > &m1)
Multiply m0 by m1 and return the resulting matrix.
Definition: Mat3.h:611
Mat3< Type1 > cwiseAdd(const Mat3< Type1 > &m, const Type2 s)
Definition: Mat3.h:820
bool isApproxEqual(const Type &a, const Type &b, const Type &tolerance)
Return true if a is equal to b to within the given tolerance.
Definition: Math.h:407
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:773
Axis
Definition: Math.h:904
bool cwiseGreaterThan(const Mat3< T > &m0, const Mat3< T > &m1)
Definition: Mat3.h:842
Definition: Exceptions.h:13
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:74
Tolerance for floating-point comparison.
Definition: Math.h:147
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h.in:116
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h.in:202