Go to the documentation of this file.
4 #ifndef OPENVDB_MATH_MAT3_H_HAS_BEEN_INCLUDED
5 #define OPENVDB_MATH_MAT3_H_HAS_BEEN_INCLUDED
21 template<
typename T>
class Vec3;
22 template<
typename T>
class Mat4;
23 template<
typename T>
class Quat;
50 template<
typename Source>
51 Mat3(Source a, Source b, Source c,
52 Source d, Source e, Source f,
53 Source g, Source h, Source i)
55 MyBase::mm[0] =
static_cast<T
>(a);
56 MyBase::mm[1] =
static_cast<T
>(b);
57 MyBase::mm[2] =
static_cast<T
>(c);
58 MyBase::mm[3] =
static_cast<T
>(d);
59 MyBase::mm[4] =
static_cast<T
>(e);
60 MyBase::mm[5] =
static_cast<T
>(f);
61 MyBase::mm[6] =
static_cast<T
>(g);
62 MyBase::mm[7] =
static_cast<T
>(h);
63 MyBase::mm[8] =
static_cast<T
>(i);
68 template<
typename Source>
72 this->setRows(v1, v2, v3);
74 this->setColumns(v1, v2, v3);
82 template<
typename Source>
85 MyBase::mm[0] =
static_cast<T
>(a[0]);
86 MyBase::mm[1] =
static_cast<T
>(a[1]);
87 MyBase::mm[2] =
static_cast<T
>(a[2]);
88 MyBase::mm[3] =
static_cast<T
>(a[3]);
89 MyBase::mm[4] =
static_cast<T
>(a[4]);
90 MyBase::mm[5] =
static_cast<T
>(a[5]);
91 MyBase::mm[6] =
static_cast<T
>(a[6]);
92 MyBase::mm[7] =
static_cast<T
>(a[7]);
93 MyBase::mm[8] =
static_cast<T
>(a[8]);
99 for (
int i=0; i<3; ++i) {
100 for (
int j=0; j<3; ++j) {
101 MyBase::mm[i*3 + j] = m[i][j];
107 template<
typename Source>
110 for (
int i=0; i<3; ++i) {
111 for (
int j=0; j<3; ++j) {
112 MyBase::mm[i*3 + j] =
static_cast<T
>(m[i][j]);
120 for (
int i=0; i<3; ++i) {
121 for (
int j=0; j<3; ++j) {
122 MyBase::mm[i*3 + j] = m[i][j];
153 MyBase::mm[i3+0] = v[0];
154 MyBase::mm[i3+1] = v[1];
155 MyBase::mm[i3+2] = v[2];
162 return Vec3<T>((*
this)(i,0), (*
this)(i,1), (*
this)(i,2));
169 MyBase::mm[0+j] = v[0];
170 MyBase::mm[3+j] = v[1];
171 MyBase::mm[6+j] = v[2];
178 return Vec3<T>((*
this)(0,j), (*
this)(1,j), (*
this)(2,j));
186 T* operator[](
int i) {
return &(MyBase::mm[i*3]); }
189 const T*
operator[](
int i)
const {
return &(MyBase::mm[i*3]); }
202 return MyBase::mm[3*i+j];
212 return MyBase::mm[3*i+j];
218 MyBase::mm[0] = v1[0];
219 MyBase::mm[1] = v1[1];
220 MyBase::mm[2] = v1[2];
221 MyBase::mm[3] = v2[0];
222 MyBase::mm[4] = v2[1];
223 MyBase::mm[5] = v2[2];
224 MyBase::mm[6] = v3[0];
225 MyBase::mm[7] = v3[1];
226 MyBase::mm[8] = v3[2];
232 MyBase::mm[0] = v1[0];
233 MyBase::mm[1] = v2[0];
234 MyBase::mm[2] = v3[0];
235 MyBase::mm[3] = v1[1];
236 MyBase::mm[4] = v2[1];
237 MyBase::mm[5] = v3[1];
238 MyBase::mm[6] = v1[2];
239 MyBase::mm[7] = v2[2];
240 MyBase::mm[8] = v3[2];
246 MyBase::mm[0] = vdiag[0];
247 MyBase::mm[1] = vtri[0];
248 MyBase::mm[2] = vtri[1];
249 MyBase::mm[3] = vtri[0];
250 MyBase::mm[4] = vdiag[1];
251 MyBase::mm[5] = vtri[2];
252 MyBase::mm[6] = vtri[1];
253 MyBase::mm[7] = vtri[2];
254 MyBase::mm[8] = vdiag[2];
261 vdiag[0], vtri[0], vtri[1],
262 vtri[0], vdiag[1], vtri[2],
263 vtri[1], vtri[2], vdiag[2]
275 {*
this = rotation<Mat3<T> >(q);}
280 {*
this = rotation<Mat3<T> >(axis,
angle);}
311 template<
typename Source>
317 std::copy(src, (src + this->numElements()), MyBase::mm);
322 bool eq(
const Mat3 &m, T eps=1.0e-8)
const
339 -MyBase::mm[0], -MyBase::mm[1], -MyBase::mm[2],
340 -MyBase::mm[3], -MyBase::mm[4], -MyBase::mm[5],
341 -MyBase::mm[6], -MyBase::mm[7], -MyBase::mm[8]
351 template <
typename S>
354 MyBase::mm[0] *= scalar;
355 MyBase::mm[1] *= scalar;
356 MyBase::mm[2] *= scalar;
357 MyBase::mm[3] *= scalar;
358 MyBase::mm[4] *= scalar;
359 MyBase::mm[5] *= scalar;
360 MyBase::mm[6] *= scalar;
361 MyBase::mm[7] *= scalar;
362 MyBase::mm[8] *= scalar;
367 template <
typename S>
372 MyBase::mm[0] += s[0];
373 MyBase::mm[1] += s[1];
374 MyBase::mm[2] += s[2];
375 MyBase::mm[3] += s[3];
376 MyBase::mm[4] += s[4];
377 MyBase::mm[5] += s[5];
378 MyBase::mm[6] += s[6];
379 MyBase::mm[7] += s[7];
380 MyBase::mm[8] += s[8];
385 template <
typename S>
390 MyBase::mm[0] -= s[0];
391 MyBase::mm[1] -= s[1];
392 MyBase::mm[2] -= s[2];
393 MyBase::mm[3] -= s[3];
394 MyBase::mm[4] -= s[4];
395 MyBase::mm[5] -= s[5];
396 MyBase::mm[6] -= s[6];
397 MyBase::mm[7] -= s[7];
398 MyBase::mm[8] -= s[8];
403 template <
typename S>
410 MyBase::mm[0] =
static_cast<T
>(s0[0] * s1[0] +
413 MyBase::mm[1] =
static_cast<T
>(s0[0] * s1[1] +
416 MyBase::mm[2] =
static_cast<T
>(s0[0] * s1[2] +
420 MyBase::mm[3] =
static_cast<T
>(s0[3] * s1[0] +
423 MyBase::mm[4] =
static_cast<T
>(s0[3] * s1[1] +
426 MyBase::mm[5] =
static_cast<T
>(s0[3] * s1[2] +
430 MyBase::mm[6] =
static_cast<T
>(s0[6] * s1[0] +
433 MyBase::mm[7] =
static_cast<T
>(s0[6] * s1[1] +
436 MyBase::mm[8] =
static_cast<T
>(s0[6] * s1[2] +
447 MyBase::mm[4] * MyBase::mm[8] - MyBase::mm[5] * MyBase::mm[7],
448 MyBase::mm[5] * MyBase::mm[6] - MyBase::mm[3] * MyBase::mm[8],
449 MyBase::mm[3] * MyBase::mm[7] - MyBase::mm[4] * MyBase::mm[6],
450 MyBase::mm[2] * MyBase::mm[7] - MyBase::mm[1] * MyBase::mm[8],
451 MyBase::mm[0] * MyBase::mm[8] - MyBase::mm[2] * MyBase::mm[6],
452 MyBase::mm[1] * MyBase::mm[6] - MyBase::mm[0] * MyBase::mm[7],
453 MyBase::mm[1] * MyBase::mm[5] - MyBase::mm[2] * MyBase::mm[4],
454 MyBase::mm[2] * MyBase::mm[3] - MyBase::mm[0] * MyBase::mm[5],
455 MyBase::mm[0] * MyBase::mm[4] - MyBase::mm[1] * MyBase::mm[3]);
462 MyBase::mm[4] * MyBase::mm[8] - MyBase::mm[5] * MyBase::mm[7],
463 MyBase::mm[2] * MyBase::mm[7] - MyBase::mm[1] * MyBase::mm[8],
464 MyBase::mm[1] * MyBase::mm[5] - MyBase::mm[2] * MyBase::mm[4],
465 MyBase::mm[5] * MyBase::mm[6] - MyBase::mm[3] * MyBase::mm[8],
466 MyBase::mm[0] * MyBase::mm[8] - MyBase::mm[2] * MyBase::mm[6],
467 MyBase::mm[2] * MyBase::mm[3] - MyBase::mm[0] * MyBase::mm[5],
468 MyBase::mm[3] * MyBase::mm[7] - MyBase::mm[4] * MyBase::mm[6],
469 MyBase::mm[1] * MyBase::mm[6] - MyBase::mm[0] * MyBase::mm[7],
470 MyBase::mm[0] * MyBase::mm[4] - MyBase::mm[1] * MyBase::mm[3]);
478 MyBase::mm[0], MyBase::mm[3], MyBase::mm[6],
479 MyBase::mm[1], MyBase::mm[4], MyBase::mm[7],
480 MyBase::mm[2], MyBase::mm[5], MyBase::mm[8]);
490 const T det = inv.
mm[0]*MyBase::mm[0] + inv.
mm[1]*MyBase::mm[3] + inv.
mm[2]*MyBase::mm[6];
496 return inv * (T(1)/det);
502 const T co00 = MyBase::mm[4]*MyBase::mm[8] - MyBase::mm[5]*MyBase::mm[7];
503 const T co10 = MyBase::mm[5]*MyBase::mm[6] - MyBase::mm[3]*MyBase::mm[8];
504 const T co20 = MyBase::mm[3]*MyBase::mm[7] - MyBase::mm[4]*MyBase::mm[6];
505 return MyBase::mm[0]*co00 + MyBase::mm[1]*co10 + MyBase::mm[2]*co20;
511 return MyBase::mm[0]+MyBase::mm[4]+MyBase::mm[8];
525 template<
typename T0>
528 return static_cast< Vec3<T0> >(v * *
this);
533 template<
typename T0>
536 return static_cast< Vec3<T0> >(*
this * v);
546 ret.
mm[0] *= diag(0);
547 ret.
mm[1] *= diag(1);
548 ret.
mm[2] *= diag(2);
549 ret.
mm[3] *= diag(0);
550 ret.
mm[4] *= diag(1);
551 ret.
mm[5] *= diag(2);
552 ret.
mm[6] *= diag(0);
553 ret.
mm[7] *= diag(1);
554 ret.
mm[8] *= diag(2);
562 template <
typename T0,
typename T1>
568 for (
int i=0; i<9; ++i) {
576 template <
typename T0,
typename T1>
581 template <
typename S,
typename T>
587 template <
typename S,
typename T>
597 template <
typename T0,
typename T1>
607 template <
typename T0,
typename T1>
617 template <
typename T0,
typename T1>
627 template<
typename T,
typename MT>
633 _v[0]*m[0] + _v[1]*m[1] + _v[2]*m[2],
634 _v[0]*m[3] + _v[1]*m[4] + _v[2]*m[5],
635 _v[0]*m[6] + _v[1]*m[7] + _v[2]*m[8]);
640 template<
typename T,
typename MT>
646 _v[0]*m[0] + _v[1]*m[3] + _v[2]*m[6],
647 _v[0]*m[1] + _v[1]*m[4] + _v[2]*m[7],
648 _v[0]*m[2] + _v[1]*m[5] + _v[2]*m[8]);
653 template<
typename T,
typename MT>
663 template <
typename T>
666 return Mat3<T>(v1[0]*v2[0], v1[0]*v2[1], v1[0]*v2[2],
667 v1[1]*v2[0], v1[1]*v2[1], v1[1]*v2[2],
668 v1[2]*v2[0], v1[2]*v2[1], v1[2]*v2[2]);
675 template<
typename T,
typename T0>
685 namespace mat3_internal {
694 double cotan_of_2_theta;
696 double cosin_of_theta;
702 double Sjj_minus_Sii = D[j] - D[i];
705 tan_of_theta = Sij / Sjj_minus_Sii;
708 cotan_of_2_theta = 0.5*Sjj_minus_Sii / Sij ;
710 if (cotan_of_2_theta < 0.) {
712 -1./(sqrt(1. + cotan_of_2_theta*cotan_of_2_theta) - cotan_of_2_theta);
715 1./(sqrt(1. + cotan_of_2_theta*cotan_of_2_theta) + cotan_of_2_theta);
719 cosin_of_theta = 1./sqrt( 1. + tan_of_theta * tan_of_theta);
720 sin_of_theta = cosin_of_theta * tan_of_theta;
721 z = tan_of_theta * Sij;
725 for (
int k = 0; k < i; ++k) {
727 S(k,i) = cosin_of_theta * temp - sin_of_theta * S(k,j);
728 S(k,j)= sin_of_theta * temp + cosin_of_theta * S(k,j);
730 for (
int k = i+1; k < j; ++k) {
732 S(i,k) = cosin_of_theta * temp - sin_of_theta * S(k,j);
733 S(k,j) = sin_of_theta * temp + cosin_of_theta * S(k,j);
735 for (
int k = j+1; k < n; ++k) {
737 S(i,k) = cosin_of_theta * temp - sin_of_theta * S(j,k);
738 S(j,k) = sin_of_theta * temp + cosin_of_theta * S(j,k);
740 for (
int k = 0; k < n; ++k)
743 Q(k,i) = cosin_of_theta * temp - sin_of_theta*Q(k,j);
744 Q(k,j) = sin_of_theta * temp + cosin_of_theta*Q(k,j);
759 unsigned int MAX_ITERATIONS=250)
769 for (
int i = 0; i < n; ++i) {
773 unsigned int iterations(0);
780 for (
int i = 0; i < n; ++i) {
781 for (
int j = i+1; j < n; ++j) {
794 for (
int i = 0; i < n; ++i) {
795 for (
int j = i+1; j < n; ++j){
801 if (fabs(S(i,j)) > max_element) {
802 max_element = fabs(S(i,j));
809 }
while (iterations < MAX_ITERATIONS);
822 template<>
inline math::Mat3s zeroVal<math::Mat3s>() {
return math::Mat3s::zero(); }
823 template<>
inline math::Mat3d zeroVal<math::Mat3d>() {
return math::Mat3d::zero(); }
828 #endif // OPENVDB_MATH_MAT3_H_HAS_BEEN_INCLUDED
4x4 -matrix class.
Definition: Mat4.h:24
const Mat3< T > & operator*=(S scalar)
Multiplication operator, e.g. M = scalar * M;.
Definition: Mat3.h:352
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:371
T angle(const Vec2< T > &v1, const Vec2< T > &v2)
Definition: Vec2.h:445
const T * asPointer() const
Definition: Mat3.h:193
T mm[SIZE *SIZE]
Definition: Mat.h:165
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:582
static const Mat3< T > & identity()
Predefined constant for identity matrix.
Definition: Mat3.h:128
T det() const
Determinant of matrix.
Definition: Mat3.h:500
double ValueType
Definition: Mat.h:30
Vec3< T > col(int j) const
Get jth column, e.g. Vec3d v = m.col(0);.
Definition: Mat3.h:175
void setSkew(const Vec3< T > &v)
Set the matrix as cross product of the given vector.
Definition: Mat3.h:268
bool operator==(const Mat3< T0 > &m0, const Mat3< T1 > &m1)
Equality operator, does exact floating point comparisons.
Definition: Mat3.h:563
Mat3(Source *a)
Definition: Mat3.h:83
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:258
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:542
T * asPointer()
Definition: Mat3.h:192
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:758
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:51
3x3 matrix class.
Definition: Mat3.h:29
double value_type
Definition: Mat.h:29
Mat3(const Mat3< Source > &m)
Conversion constructor.
Definition: Mat3.h:108
Mat3(const Quat< T > &q)
Definition: Mat3.h:40
T operator()(int i, int j) const
Definition: Mat3.h:208
Vec3< T > row(int i) const
Get ith row, e.g. Vec3d v = m.row(1);.
Definition: Mat3.h:159
Mat3 snapBasis(Axis axis, const Vec3< T > &direction)
Definition: Mat3.h:518
Tolerance for floating-point comparison.
Definition: Math.h:110
Mat3 cofactor() const
Return the cofactor matrix of this matrix.
Definition: Mat3.h:444
Mat3 transpose() const
returns transpose of this
Definition: Mat3.h:475
Vec3< T0 > transform(const Vec3< T0 > &v) const
Definition: Mat3.h:526
Mat3(const Mat< 3, T > &m)
Copy constructor.
Definition: Mat3.h:97
Mat3 adjoint() const
Return the adjoint of this matrix, i.e., the transpose of its cofactor.
Definition: Mat3.h:459
void pivot(int i, int j, Mat3< T > &S, Vec3< T > &D, Mat3< T > &Q)
Definition: Mat3.h:689
void setToRotation(const Vec3< T > &axis, T angle)
Set this matrix to the rotation specified by axis and angle.
Definition: Mat3.h:279
const T * operator[](int i) const
Definition: Mat3.h:189
void setSymmetric(const Vec3< T > &vdiag, const Vec3< T > &vtri)
Set diagonal and symmetric triangular components.
Definition: Mat3.h:244
static const Mat3< T > & zero()
Predefined constant for zero matrix.
Definition: Mat3.h:138
void setZero()
Set this matrix to zero.
Definition: Mat3.h:283
void powSolve(const MatType &aA, MatType &aB, double aPower, double aTol=0.01)
Definition: Mat.h:827
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:642
Vec3< T0 > pretransform(const Vec3< T0 > &v) const
Definition: Mat3.h:534
Mat3()
Trivial constructor, the matrix is NOT initialized.
Definition: Mat3.h:36
bool isExactlyEqual(const T0 &a, const T1 &b)
Return true if a is exactly equal to b.
Definition: Math.h:408
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:230
Mat3(const Vec3< Source > &v1, const Vec3< Source > &v2, const Vec3< Source > &v3, bool rows=true)
Definition: Mat3.h:69
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:608
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:368
void setToRotation(const Quat< T > &q)
Set this matrix to the rotation matrix specified by the quaternion.
Definition: Mat3.h:274
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:322
T trace() const
Trace of matrix.
Definition: Mat3.h:509
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:629
Mat3(const Mat4< T > &m)
Conversion from Mat4 (copies top left)
Definition: Mat3.h:118
void setIdentity()
Set this matrix to identity.
Definition: Mat3.h:297
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:386
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:146
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:216
void setCol(int j, const Vec3< T > &v)
Set jth column to vector v.
Definition: Mat3.h:166
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:598
const Mat3 & operator=(const Mat3< Source > &m)
Assignment operator.
Definition: Mat3.h:312
Mat3 inverse(T tolerance=0) const
Definition: Mat3.h:486
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:588
Mat3< T > operator-() const
Negation operator, for e.g. m1 = -m2;.
Definition: Mat3.h:336
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:756
Mat3< T > powLerp(const Mat3< T0 > &m1, const Mat3< T0 > &m2, T t)
Definition: Mat3.h:676
T & operator()(int i, int j)
Definition: Mat3.h:198
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:94
Definition: Exceptions.h:56
Mat3< double > Mat3d
Definition: Mat3.h:816
bool operator!=(const Mat3< T0 > &m0, const Mat3< T1 > &m1)
Inequality operator, does exact floating point comparisons.
Definition: Mat3.h:577
MatType skew(const Vec3< typename MatType::value_type > &skew)
Return a matrix as the cross product of the given vector.
Definition: Mat.h:713
Definition: Exceptions.h:13
void setRow(int i, const Vec3< T > &v)
Set ith row to vector v.
Definition: Mat3.h:148
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:618
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:82
const Mat3< T > & operator*=(const Mat3< S > &m1)
Multiply this matrix by the given matrix.
Definition: Mat3.h:404
Axis
Definition: Math.h:869
Mat3< T > outerProduct(const Vec3< T > &v1, const Vec3< T > &v2)
Definition: Mat3.h:664