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);
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];
179 MyBase::mm[i3+0] = v[0];
180 MyBase::mm[i3+1] = v[1];
181 MyBase::mm[i3+2] = v[2];
188 return Vec3<T>((*this)(
i,0), (*
this)(
i,1), (*
this)(
i,2));
195 MyBase::mm[0+j] = v[0];
196 MyBase::mm[3+j] = v[1];
197 MyBase::mm[6+j] = v[2];
204 return Vec3<T>((*this)(0,j), (*
this)(1,j), (*
this)(2,j));
212 T* operator[](
int i) {
return &(MyBase::mm[
i*3]); }
228 return MyBase::mm[3*
i+j];
238 return MyBase::mm[3*
i+j];
244 MyBase::mm[0] = v1[0];
245 MyBase::mm[1] = v1[1];
246 MyBase::mm[2] = v1[2];
247 MyBase::mm[3] = v2[0];
248 MyBase::mm[4] = v2[1];
249 MyBase::mm[5] = v2[2];
250 MyBase::mm[6] = v3[0];
251 MyBase::mm[7] = v3[1];
252 MyBase::mm[8] = v3[2];
258 MyBase::mm[0] = v1[0];
259 MyBase::mm[1] = v2[0];
260 MyBase::mm[2] = v3[0];
261 MyBase::mm[3] = v1[1];
262 MyBase::mm[4] = v2[1];
263 MyBase::mm[5] = v3[1];
264 MyBase::mm[6] = v1[2];
265 MyBase::mm[7] = v2[2];
266 MyBase::mm[8] = v3[2];
272 this->setRows(v1, v2, v3);
278 MyBase::mm[0] = vdiag[0];
279 MyBase::mm[1] = vtri[0];
280 MyBase::mm[2] = vtri[1];
281 MyBase::mm[3] = vtri[0];
282 MyBase::mm[4] = vdiag[1];
283 MyBase::mm[5] = vtri[2];
284 MyBase::mm[6] = vtri[1];
285 MyBase::mm[7] = vtri[2];
286 MyBase::mm[8] = vdiag[2];
294 vdiag[0], vtri[0], vtri[1],
295 vtri[0], vdiag[1], vtri[2],
296 vtri[1], vtri[2], vdiag[2]
308 {*
this = rotation<Mat3<T> >(q);}
313 {*
this = rotation<Mat3<T> >(axis,
angle);}
344 template<
typename Source>
350 std::copy(src, (src + this->numElements()), MyBase::mm);
355 bool eq(
const Mat3 &m, T eps=1.0e-8)
const 372 -MyBase::mm[0], -MyBase::mm[1], -MyBase::mm[2],
373 -MyBase::mm[3], -MyBase::mm[4], -MyBase::mm[5],
374 -MyBase::mm[6], -MyBase::mm[7], -MyBase::mm[8]
384 template <
typename S>
387 MyBase::mm[0] *= scalar;
388 MyBase::mm[1] *= scalar;
389 MyBase::mm[2] *= scalar;
390 MyBase::mm[3] *= scalar;
391 MyBase::mm[4] *= scalar;
392 MyBase::mm[5] *= scalar;
393 MyBase::mm[6] *= scalar;
394 MyBase::mm[7] *= scalar;
395 MyBase::mm[8] *= scalar;
400 template <
typename S>
405 MyBase::mm[0] += s[0];
406 MyBase::mm[1] += s[1];
407 MyBase::mm[2] += s[2];
408 MyBase::mm[3] += s[3];
409 MyBase::mm[4] += s[4];
410 MyBase::mm[5] += s[5];
411 MyBase::mm[6] += s[6];
412 MyBase::mm[7] += s[7];
413 MyBase::mm[8] += s[8];
418 template <
typename S>
423 MyBase::mm[0] -= s[0];
424 MyBase::mm[1] -= s[1];
425 MyBase::mm[2] -= s[2];
426 MyBase::mm[3] -= s[3];
427 MyBase::mm[4] -= s[4];
428 MyBase::mm[5] -= s[5];
429 MyBase::mm[6] -= s[6];
430 MyBase::mm[7] -= s[7];
431 MyBase::mm[8] -= s[8];
436 template <
typename S>
443 MyBase::mm[0] =
static_cast<T
>(s0[0] * s1[0] +
446 MyBase::mm[1] =
static_cast<T
>(s0[0] * s1[1] +
449 MyBase::mm[2] =
static_cast<T
>(s0[0] * s1[2] +
453 MyBase::mm[3] =
static_cast<T
>(s0[3] * s1[0] +
456 MyBase::mm[4] =
static_cast<T
>(s0[3] * s1[1] +
459 MyBase::mm[5] =
static_cast<T
>(s0[3] * s1[2] +
463 MyBase::mm[6] =
static_cast<T
>(s0[6] * s1[0] +
466 MyBase::mm[7] =
static_cast<T
>(s0[6] * s1[1] +
469 MyBase::mm[8] =
static_cast<T
>(s0[6] * s1[2] +
480 MyBase::mm[4] * MyBase::mm[8] - MyBase::mm[5] * MyBase::mm[7],
481 MyBase::mm[5] * MyBase::mm[6] - MyBase::mm[3] * MyBase::mm[8],
482 MyBase::mm[3] * MyBase::mm[7] - MyBase::mm[4] * MyBase::mm[6],
483 MyBase::mm[2] * MyBase::mm[7] - MyBase::mm[1] * MyBase::mm[8],
484 MyBase::mm[0] * MyBase::mm[8] - MyBase::mm[2] * MyBase::mm[6],
485 MyBase::mm[1] * MyBase::mm[6] - MyBase::mm[0] * MyBase::mm[7],
486 MyBase::mm[1] * MyBase::mm[5] - MyBase::mm[2] * MyBase::mm[4],
487 MyBase::mm[2] * MyBase::mm[3] - MyBase::mm[0] * MyBase::mm[5],
488 MyBase::mm[0] * MyBase::mm[4] - MyBase::mm[1] * MyBase::mm[3]);
495 MyBase::mm[4] * MyBase::mm[8] - MyBase::mm[5] * MyBase::mm[7],
496 MyBase::mm[2] * MyBase::mm[7] - MyBase::mm[1] * MyBase::mm[8],
497 MyBase::mm[1] * MyBase::mm[5] - MyBase::mm[2] * MyBase::mm[4],
498 MyBase::mm[5] * MyBase::mm[6] - MyBase::mm[3] * MyBase::mm[8],
499 MyBase::mm[0] * MyBase::mm[8] - MyBase::mm[2] * MyBase::mm[6],
500 MyBase::mm[2] * MyBase::mm[3] - MyBase::mm[0] * MyBase::mm[5],
501 MyBase::mm[3] * MyBase::mm[7] - MyBase::mm[4] * MyBase::mm[6],
502 MyBase::mm[1] * MyBase::mm[6] - MyBase::mm[0] * MyBase::mm[7],
503 MyBase::mm[0] * MyBase::mm[4] - MyBase::mm[1] * MyBase::mm[3]);
511 MyBase::mm[0], MyBase::mm[3], MyBase::mm[6],
512 MyBase::mm[1], MyBase::mm[4], MyBase::mm[7],
513 MyBase::mm[2], MyBase::mm[5], MyBase::mm[8]);
523 const T det = inv.
mm[0]*MyBase::mm[0] + inv.
mm[1]*MyBase::mm[3] + inv.
mm[2]*MyBase::mm[6];
529 return inv * (T(1)/det);
535 const T co00 = MyBase::mm[4]*MyBase::mm[8] - MyBase::mm[5]*MyBase::mm[7];
536 const T co10 = MyBase::mm[5]*MyBase::mm[6] - MyBase::mm[3]*MyBase::mm[8];
537 const T co20 = MyBase::mm[3]*MyBase::mm[7] - MyBase::mm[4]*MyBase::mm[6];
538 return MyBase::mm[0]*co00 + MyBase::mm[1]*co10 + MyBase::mm[2]*co20;
544 return MyBase::mm[0]+MyBase::mm[4]+MyBase::mm[8];
558 template<
typename T0>
561 return static_cast< Vec3<T0> >(v * *
this);
566 template<
typename T0>
569 return static_cast< Vec3<T0> >(*
this * v);
579 ret.
mm[0] *= diag(0);
580 ret.
mm[1] *= diag(1);
581 ret.
mm[2] *= diag(2);
582 ret.
mm[3] *= diag(0);
583 ret.
mm[4] *= diag(1);
584 ret.
mm[5] *= diag(2);
585 ret.
mm[6] *= diag(0);
586 ret.
mm[7] *= diag(1);
587 ret.
mm[8] *= diag(2);
595 template <
typename T0,
typename T1>
601 for (
int i=0;
i<9; ++
i) {
609 template <
typename T0,
typename T1>
614 template <
typename S,
typename T>
620 template <
typename S,
typename T>
630 template <
typename T0,
typename T1>
640 template <
typename T0,
typename T1>
653 template <
typename T0,
typename T1>
663 template<
typename T,
typename MT>
669 _v[0]*m[0] + _v[1]*m[1] + _v[2]*m[2],
670 _v[0]*m[3] + _v[1]*m[4] + _v[2]*m[5],
671 _v[0]*m[6] + _v[1]*m[7] + _v[2]*m[8]);
676 template<
typename T,
typename MT>
682 _v[0]*m[0] + _v[1]*m[3] + _v[2]*m[6],
683 _v[0]*m[1] + _v[1]*m[4] + _v[2]*m[7],
684 _v[0]*m[2] + _v[1]*m[5] + _v[2]*m[8]);
689 template<
typename T,
typename MT>
699 template <
typename T>
702 return Mat3<T>(v1[0]*v2[0], v1[0]*v2[1], v1[0]*v2[2],
703 v1[1]*v2[0], v1[1]*v2[1], v1[1]*v2[2],
704 v1[2]*v2[0], v1[2]*v2[1], v1[2]*v2[2]);
715 template<
typename T,
typename T0>
727 void pivot(
int i,
int j, Mat3<T>& S,
Vec3<T>& D, Mat3<T>& Q)
732 double cotan_of_2_theta;
734 double cosin_of_theta;
740 double Sjj_minus_Sii = D[j] - D[
i];
743 tan_of_theta = Sij / Sjj_minus_Sii;
746 cotan_of_2_theta = 0.5*Sjj_minus_Sii / Sij ;
748 if (cotan_of_2_theta < 0.) {
750 -1./(sqrt(1. + cotan_of_2_theta*cotan_of_2_theta) - cotan_of_2_theta);
753 1./(sqrt(1. + cotan_of_2_theta*cotan_of_2_theta) + cotan_of_2_theta);
757 cosin_of_theta = 1./sqrt( 1. + tan_of_theta * tan_of_theta);
758 sin_of_theta = cosin_of_theta * tan_of_theta;
759 z = tan_of_theta * Sij;
763 for (
int k = 0; k <
i; ++k) {
765 S(k,
i) = cosin_of_theta * temp - sin_of_theta * S(k,j);
766 S(k,j)= sin_of_theta * temp + cosin_of_theta * S(k,j);
768 for (
int k =
i+1; k < j; ++k) {
770 S(
i,k) = cosin_of_theta * temp - sin_of_theta * S(k,j);
771 S(k,j) = sin_of_theta * temp + cosin_of_theta * S(k,j);
773 for (
int k = j+1; k < n; ++k) {
775 S(
i,k) = cosin_of_theta * temp - sin_of_theta * S(j,k);
776 S(j,k) = sin_of_theta * temp + cosin_of_theta * S(j,k);
778 for (
int k = 0; k < n; ++k)
781 Q(k,
i) = cosin_of_theta * temp - sin_of_theta*Q(k,j);
782 Q(k,j) = sin_of_theta * temp + cosin_of_theta*Q(k,j);
795 unsigned int MAX_ITERATIONS=250)
805 for (
int i = 0;
i < n; ++
i) {
809 unsigned int iterations(0);
816 for (
int i = 0;
i < n; ++
i) {
817 for (
int j =
i+1; j < n; ++j) {
830 for (
int i = 0;
i < n; ++
i) {
831 for (
int j =
i+1; j < n; ++j){
837 if (fabs(S(
i,j)) > max_element) {
838 max_element = fabs(S(
i,j));
844 pivot(ip, jp, S, D, Q);
845 }
while (iterations < MAX_ITERATIONS);
854 #endif // OPENVDB_MATH_MAT3_H_HAS_BEEN_INCLUDED Mat3 transpose() const
returns transpose of this
Definition: Mat3.h:508
Mat3< T > powLerp(const Mat3< T0 > &m1, const Mat3< T0 > &m2, T t)
Definition: Mat3.h:716
const Mat3< T > & operator-=(const Mat3< S > &m1)
Returns m0, where for .
Definition: Mat3.h:419
4x4 -matrix class.
Definition: Mat3.h:48
Vec3< typename promote< T, MT >::type > operator*(const Vec3< T > &_v, const Mat3< MT > &_m)
Returns v, where for .
Definition: Mat3.h:678
void setCol(int j, const Vec3< T > &v)
Set jth column to vector v.
Definition: Mat3.h:192
Mat3< typename promote< S, T >::type > operator*(const Mat3< T > &m, S scalar)
Returns M, where for .
Definition: Mat3.h:621
void setSkew(const Vec3< T > &v)
Set the matrix as cross product of the given vector.
Definition: Mat3.h:301
Mat3d Mat3f
Definition: Mat3.h:709
Tolerance for floating-point comparison.
Definition: Math.h:117
Mat3 timesDiagonal(const Vec3< T > &diag) const
Definition: Mat3.h:575
Vec3< T0 > pretransform(const Vec3< T0 > &v) const
Definition: Mat3.h:567
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:270
T angle(const Vec2< T > &v1, const Vec2< T > &v2)
Definition: Vec2.h:471
const Mat3< T > & operator*=(const Mat3< S > &m1)
Returns m0, where for .
Definition: Mat3.h:437
void setRow(int i, const Vec3< T > &v)
Set ith row to vector v.
Definition: Mat3.h:174
const Mat3< T > & operator*=(S scalar)
Multiplication operator, e.g. M = scalar * M;.
Definition: Mat3.h:385
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:242
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:256
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:101
Vec3< T > row(int i) const
Get ith row, e.g. Vec3d v = m.row(1);.
Definition: Mat3.h:185
Mat3< double > Mat3d
Definition: Mat3.h:708
bool isExactlyEqual(const T0 &a, const T1 &b)
Return true if a is exactly equal to b.
Definition: Math.h:391
tbb::atomic< Index32 > i
Definition: LeafBuffer.h:71
Definition: Exceptions.h:82
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:354
void setSymmetric(const Vec3< T > &vdiag, const Vec3< T > &vtri)
Set diagonal and symmetric triangular components.
Definition: Mat3.h:276
3x3 matrix class.
Definition: Mat3.h:54
Mat3()
Trivial constructor, the matrix is NOT initialized.
Definition: Mat3.h:62
Mat3< typename promote< T0, T1 >::type > operator-(const Mat3< T0 > &m0, const Mat3< T1 > &m1)
Returns M, where for .
Definition: Mat3.h:641
const Mat3< T > & operator+=(const Mat3< S > &m1)
Returns m0, where for .
Definition: Mat3.h:401
const T * asPointer() const
Definition: Mat3.h:219
Mat3 inverse(T tolerance=0) const
Definition: Mat3.h:519
Mat3(const Mat< 3, T > &m)
Copy constructor.
Definition: Mat3.h:123
T & operator()(int i, int j)
Definition: Mat3.h:224
Mat< 3, T > MyBase
Definition: Mat3.h:60
T trace() const
Trace of matrix.
Definition: Mat3.h:542
void setIdentity()
Set "this" matrix to identity.
Definition: Mat3.h:330
Vec3< T > col(int j) const
Get jth column, e.g. Vec3d v = m.col(0);.
Definition: Mat3.h:201
void powSolve(const MatType &aA, MatType &aB, double aPower, double aTol=0.01)
Definition: Mat.h:850
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:779
#define OPENVDB_VERSION_NAME
Definition: version.h:43
T mm[SIZE *SIZE]
Definition: Mat.h:190
static const Mat3< T > & zero()
Predefined constant for zero matrix.
Definition: Mat3.h:164
T * asPointer()
Definition: Mat3.h:218
T operator()(int i, int j) const
Definition: Mat3.h:234
Mat3 snapBasis(Axis axis, const Vec3< T > &direction)
Definition: Mat3.h:551
Mat3(const Vec3< Source > &v1, const Vec3< Source > &v2, const Vec3< Source > &v3, bool rows=true)
Definition: Mat3.h:95
Mat3< T > outerProduct(const Vec3< T > &v1, const Vec3< T > &v2)
Definition: Mat3.h:700
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:794
T det() const
Determinant of matrix.
Definition: Mat3.h:533
Mat3(const Mat4< T > &m)
Conversion from Mat4 (copies top left)
Definition: Mat3.h:144
Mat3(Source *a)
Definition: Mat3.h:109
Mat3< typename promote< S, T >::type > operator*(S scalar, const Mat3< T > &m)
Returns M, where for .
Definition: Mat3.h:615
Definition: Exceptions.h:39
void setToRotation(const Vec3< T > &axis, T angle)
Set this matrix to the rotation specified by axis and angle.
Definition: Mat3.h:312
Mat3 adjoint() const
returns adjoint of "this", i.e. the transpose of the cofactor of "this"
Definition: Mat3.h:492
Vec3< typename promote< T, MT >::type > operator*(const Mat3< MT > &_m, const Vec3< T > &_v)
Returns v, where for .
Definition: Mat3.h:665
static const Mat3< T > & identity()
Predefined constant for identity matrix.
Definition: Mat3.h:154
T ValueType
Definition: Mat3.h:59
const T * operator[](int i) const
Definition: Mat3.h:215
MatType skew(const Vec3< typename MatType::value_type > &skew)
Return a matrix as the cross product of the given vector.
Definition: Mat.h:736
Mat3(const Quat< T > &q)
Definition: Mat3.h:66
void setZero()
Set this matrix to zero.
Definition: Mat3.h:316
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:355
Mat3< typename promote< T0, T1 >::type > operator+(const Mat3< T0 > &m0, const Mat3< T1 > &m1)
Returns M, where for .
Definition: Mat3.h:631
T value_type
Data type held by the matrix.
Definition: Mat3.h:58
static Mat3 symmetric(const Vec3< T > &vdiag, const Vec3< T > &vtri)
Definition: Mat3.h:291
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 setToRotation(const Quat< T > &q)
Set this matrix to the rotation matrix specified by the quaternion.
Definition: Mat3.h:307
bool operator==(const Mat3< T0 > &m0, const Mat3< T1 > &m1)
Equality operator, does exact floating point comparisons.
Definition: Mat3.h:596
Mat3< T > operator-() const
Negation operator, for e.g. m1 = -m2;.
Definition: Mat3.h:369
Mat3< typename promote< T0, T1 >::type > operator*(const Mat3< T0 > &m0, const Mat3< T1 > &m1)
Matrix multiplication.
Definition: Mat3.h:654
Mat3 cofactor() const
Return the cofactor matrix of "this".
Definition: Mat3.h:477
Vec3< T0 > transform(const Vec3< T0 > &v) const
Definition: Mat3.h:559
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:71
Axis
Definition: Math.h:852
Mat3< float > Mat3s
Definition: Mat3.h:707
Mat3(const Mat3< Source > &m)
Conversion constructor.
Definition: Mat3.h:134
static constexpr size_t size
The size of a LeafBuffer when LeafBuffer::mOutOfCore is atomic.
Definition: LeafBuffer.h:85
const Mat3 & operator=(const Mat3< Source > &m)
Assignment operator.
Definition: Mat3.h:345
T ValueType
Definition: Mat.h:56
bool operator!=(const Mat3< T0 > &m0, const Mat3< T1 > &m1)
Inequality operator, does exact floating point comparisons.
Definition: Mat3.h:610