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));
188 return MyBase::mm[3*i+j];
198 return MyBase::mm[3*i+j];
204 MyBase::mm[0] = v1[0];
205 MyBase::mm[1] = v1[1];
206 MyBase::mm[2] = v1[2];
207 MyBase::mm[3] = v2[0];
208 MyBase::mm[4] = v2[1];
209 MyBase::mm[5] = v2[2];
210 MyBase::mm[6] = v3[0];
211 MyBase::mm[7] = v3[1];
212 MyBase::mm[8] = v3[2];
218 MyBase::mm[0] = v1[0];
219 MyBase::mm[1] = v2[0];
220 MyBase::mm[2] = v3[0];
221 MyBase::mm[3] = v1[1];
222 MyBase::mm[4] = v2[1];
223 MyBase::mm[5] = v3[1];
224 MyBase::mm[6] = v1[2];
225 MyBase::mm[7] = v2[2];
226 MyBase::mm[8] = v3[2];
232 MyBase::mm[0] = vdiag[0];
233 MyBase::mm[1] = vtri[0];
234 MyBase::mm[2] = vtri[1];
235 MyBase::mm[3] = vtri[0];
236 MyBase::mm[4] = vdiag[1];
237 MyBase::mm[5] = vtri[2];
238 MyBase::mm[6] = vtri[1];
239 MyBase::mm[7] = vtri[2];
240 MyBase::mm[8] = vdiag[2];
247 vdiag[0], vtri[0], vtri[1],
248 vtri[0], vdiag[1], vtri[2],
249 vtri[1], vtri[2], vdiag[2]
261 {*
this = rotation<Mat3<T> >(q);}
266 {*
this = rotation<Mat3<T> >(axis,
angle);}
297 template<
typename Source>
303 std::copy(src, (src + this->numElements()), MyBase::mm);
308 bool eq(
const Mat3 &m, T eps=1.0e-8)
const 325 -MyBase::mm[0], -MyBase::mm[1], -MyBase::mm[2],
326 -MyBase::mm[3], -MyBase::mm[4], -MyBase::mm[5],
327 -MyBase::mm[6], -MyBase::mm[7], -MyBase::mm[8]
337 template <
typename S>
340 MyBase::mm[0] *= scalar;
341 MyBase::mm[1] *= scalar;
342 MyBase::mm[2] *= scalar;
343 MyBase::mm[3] *= scalar;
344 MyBase::mm[4] *= scalar;
345 MyBase::mm[5] *= scalar;
346 MyBase::mm[6] *= scalar;
347 MyBase::mm[7] *= scalar;
348 MyBase::mm[8] *= scalar;
353 template <
typename S>
358 MyBase::mm[0] += s[0];
359 MyBase::mm[1] += s[1];
360 MyBase::mm[2] += s[2];
361 MyBase::mm[3] += s[3];
362 MyBase::mm[4] += s[4];
363 MyBase::mm[5] += s[5];
364 MyBase::mm[6] += s[6];
365 MyBase::mm[7] += s[7];
366 MyBase::mm[8] += s[8];
371 template <
typename S>
376 MyBase::mm[0] -= s[0];
377 MyBase::mm[1] -= s[1];
378 MyBase::mm[2] -= s[2];
379 MyBase::mm[3] -= s[3];
380 MyBase::mm[4] -= s[4];
381 MyBase::mm[5] -= s[5];
382 MyBase::mm[6] -= s[6];
383 MyBase::mm[7] -= s[7];
384 MyBase::mm[8] -= s[8];
389 template <
typename S>
396 MyBase::mm[0] =
static_cast<T
>(s0[0] * s1[0] +
399 MyBase::mm[1] =
static_cast<T
>(s0[0] * s1[1] +
402 MyBase::mm[2] =
static_cast<T
>(s0[0] * s1[2] +
406 MyBase::mm[3] =
static_cast<T
>(s0[3] * s1[0] +
409 MyBase::mm[4] =
static_cast<T
>(s0[3] * s1[1] +
412 MyBase::mm[5] =
static_cast<T
>(s0[3] * s1[2] +
416 MyBase::mm[6] =
static_cast<T
>(s0[6] * s1[0] +
419 MyBase::mm[7] =
static_cast<T
>(s0[6] * s1[1] +
422 MyBase::mm[8] =
static_cast<T
>(s0[6] * s1[2] +
433 MyBase::mm[4] * MyBase::mm[8] - MyBase::mm[5] * MyBase::mm[7],
434 MyBase::mm[5] * MyBase::mm[6] - MyBase::mm[3] * MyBase::mm[8],
435 MyBase::mm[3] * MyBase::mm[7] - MyBase::mm[4] * MyBase::mm[6],
436 MyBase::mm[2] * MyBase::mm[7] - MyBase::mm[1] * MyBase::mm[8],
437 MyBase::mm[0] * MyBase::mm[8] - MyBase::mm[2] * MyBase::mm[6],
438 MyBase::mm[1] * MyBase::mm[6] - MyBase::mm[0] * MyBase::mm[7],
439 MyBase::mm[1] * MyBase::mm[5] - MyBase::mm[2] * MyBase::mm[4],
440 MyBase::mm[2] * MyBase::mm[3] - MyBase::mm[0] * MyBase::mm[5],
441 MyBase::mm[0] * MyBase::mm[4] - MyBase::mm[1] * MyBase::mm[3]);
448 MyBase::mm[4] * MyBase::mm[8] - MyBase::mm[5] * MyBase::mm[7],
449 MyBase::mm[2] * MyBase::mm[7] - MyBase::mm[1] * MyBase::mm[8],
450 MyBase::mm[1] * MyBase::mm[5] - MyBase::mm[2] * MyBase::mm[4],
451 MyBase::mm[5] * MyBase::mm[6] - MyBase::mm[3] * MyBase::mm[8],
452 MyBase::mm[0] * MyBase::mm[8] - MyBase::mm[2] * MyBase::mm[6],
453 MyBase::mm[2] * MyBase::mm[3] - MyBase::mm[0] * MyBase::mm[5],
454 MyBase::mm[3] * MyBase::mm[7] - MyBase::mm[4] * MyBase::mm[6],
455 MyBase::mm[1] * MyBase::mm[6] - MyBase::mm[0] * MyBase::mm[7],
456 MyBase::mm[0] * MyBase::mm[4] - MyBase::mm[1] * MyBase::mm[3]);
464 MyBase::mm[0], MyBase::mm[3], MyBase::mm[6],
465 MyBase::mm[1], MyBase::mm[4], MyBase::mm[7],
466 MyBase::mm[2], MyBase::mm[5], MyBase::mm[8]);
476 const T det = inv.
mm[0]*MyBase::mm[0] + inv.
mm[1]*MyBase::mm[3] + inv.
mm[2]*MyBase::mm[6];
482 return inv * (T(1)/det);
488 const T co00 = MyBase::mm[4]*MyBase::mm[8] - MyBase::mm[5]*MyBase::mm[7];
489 const T co10 = MyBase::mm[5]*MyBase::mm[6] - MyBase::mm[3]*MyBase::mm[8];
490 const T co20 = MyBase::mm[3]*MyBase::mm[7] - MyBase::mm[4]*MyBase::mm[6];
491 return MyBase::mm[0]*co00 + MyBase::mm[1]*co10 + MyBase::mm[2]*co20;
497 return MyBase::mm[0]+MyBase::mm[4]+MyBase::mm[8];
511 template<
typename T0>
514 return static_cast< Vec3<T0> >(v * *
this);
519 template<
typename T0>
522 return static_cast< Vec3<T0> >(*
this * v);
532 ret.
mm[0] *= diag(0);
533 ret.
mm[1] *= diag(1);
534 ret.
mm[2] *= diag(2);
535 ret.
mm[3] *= diag(0);
536 ret.
mm[4] *= diag(1);
537 ret.
mm[5] *= diag(2);
538 ret.
mm[6] *= diag(0);
539 ret.
mm[7] *= diag(1);
540 ret.
mm[8] *= diag(2);
548 template <
typename T0,
typename T1>
554 for (
int i=0; i<9; ++i) {
562 template <
typename T0,
typename T1>
567 template <
typename S,
typename T>
573 template <
typename S,
typename T>
583 template <
typename T0,
typename T1>
593 template <
typename T0,
typename T1>
603 template <
typename T0,
typename T1>
613 template<
typename T,
typename MT>
619 _v[0]*m[0] + _v[1]*m[1] + _v[2]*m[2],
620 _v[0]*m[3] + _v[1]*m[4] + _v[2]*m[5],
621 _v[0]*m[6] + _v[1]*m[7] + _v[2]*m[8]);
626 template<
typename T,
typename MT>
632 _v[0]*m[0] + _v[1]*m[3] + _v[2]*m[6],
633 _v[0]*m[1] + _v[1]*m[4] + _v[2]*m[7],
634 _v[0]*m[2] + _v[1]*m[5] + _v[2]*m[8]);
639 template<
typename T,
typename MT>
649 template <
typename T>
652 return Mat3<T>(v1[0]*v2[0], v1[0]*v2[1], v1[0]*v2[2],
653 v1[1]*v2[0], v1[1]*v2[1], v1[1]*v2[2],
654 v1[2]*v2[0], v1[2]*v2[1], v1[2]*v2[2]);
661 template<
typename T,
typename T0>
671 namespace mat3_internal {
680 double cotan_of_2_theta;
682 double cosin_of_theta;
688 double Sjj_minus_Sii = D[j] - D[i];
691 tan_of_theta = Sij / Sjj_minus_Sii;
694 cotan_of_2_theta = 0.5*Sjj_minus_Sii / Sij ;
696 if (cotan_of_2_theta < 0.) {
698 -1./(sqrt(1. + cotan_of_2_theta*cotan_of_2_theta) - cotan_of_2_theta);
701 1./(sqrt(1. + cotan_of_2_theta*cotan_of_2_theta) + cotan_of_2_theta);
705 cosin_of_theta = 1./sqrt( 1. + tan_of_theta * tan_of_theta);
706 sin_of_theta = cosin_of_theta * tan_of_theta;
707 z = tan_of_theta * Sij;
711 for (
int k = 0; k < i; ++k) {
713 S(k,i) = cosin_of_theta * temp - sin_of_theta * S(k,j);
714 S(k,j)= sin_of_theta * temp + cosin_of_theta * S(k,j);
716 for (
int k = i+1; k < j; ++k) {
718 S(i,k) = cosin_of_theta * temp - sin_of_theta * S(k,j);
719 S(k,j) = sin_of_theta * temp + cosin_of_theta * S(k,j);
721 for (
int k = j+1; k < n; ++k) {
723 S(i,k) = cosin_of_theta * temp - sin_of_theta * S(j,k);
724 S(j,k) = sin_of_theta * temp + cosin_of_theta * S(j,k);
726 for (
int k = 0; k < n; ++k)
729 Q(k,i) = cosin_of_theta * temp - sin_of_theta*Q(k,j);
730 Q(k,j) = sin_of_theta * temp + cosin_of_theta*Q(k,j);
745 unsigned int MAX_ITERATIONS=250)
755 for (
int i = 0; i < n; ++i) {
759 unsigned int iterations(0);
766 for (
int i = 0; i < n; ++i) {
767 for (
int j = i+1; j < n; ++j) {
780 for (
int i = 0; i < n; ++i) {
781 for (
int j = i+1; j < n; ++j){
787 if (fabs(S(i,j)) > max_element) {
788 max_element = fabs(S(i,j));
795 }
while (iterations < MAX_ITERATIONS);
807 for (
unsigned i = 0; i < 9; ++i, ++op, ++ip) *op =
math::Abs(*ip);
811 template<
typename Type1,
typename Type2>
818 for (
unsigned i = 0; i < 9; ++i, ++op, ++ip) {
830 return cwiseLessThan<3, T>(m0, m1);
837 return cwiseGreaterThan<3, T>(m0, m1);
847 template<>
inline math::Mat3s zeroVal<math::Mat3s>() {
return math::Mat3s::zero(); }
848 template<>
inline math::Mat3d zeroVal<math::Mat3d>() {
return math::Mat3d::zero(); }
853 #endif // OPENVDB_MATH_MAT3_H_HAS_BEEN_INCLUDED
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:628
#define OPENVDB_THROW(exception, message)
Definition: openvdb/Exceptions.h:82
Mat3(const Vec3< Source > &v1, const Vec3< Source > &v2, const Vec3< Source > &v3, bool rows=true)
Definition: Mat3.h:69
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:216
Mat3 adjoint() const
Return the adjoint of this matrix, i.e., the transpose of its cofactor.
Definition: Mat3.h:445
Mat3< T > outerProduct(const Vec3< T > &v1, const Vec3< T > &v2)
Definition: Mat3.h:650
Mat3(const Mat4< T > &m)
Conversion from Mat4 (copies top left)
Definition: Mat3.h:118
Mat3< T > Abs(const Mat3< T > &m)
Definition: Mat3.h:802
T mm[SIZE *SIZE]
Definition: Mat.h:175
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:574
Mat3 transpose() const
returns transpose of this
Definition: Mat3.h:461
Vec3< T0 > transform(const Vec3< T0 > &v) const
Definition: Mat3.h:512
bool cwiseLessThan(const Mat3< T > &m0, const Mat3< T > &m1)
Definition: Mat3.h:828
T trace() const
Trace of matrix.
Definition: Mat3.h:495
Mat3(const Mat< 3, T > &m)
Copy constructor.
Definition: Mat3.h:97
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:308
Mat3< T > operator-() const
Negation operator, for e.g. m1 = -m2;.
Definition: Mat3.h:322
void setIdentity()
Set this matrix to identity.
Definition: Mat3.h:283
4x4 -matrix class.
Definition: Mat3.h:22
Mat3(Source *a)
Definition: Mat3.h:83
void powSolve(const MatType &aA, MatType &aB, double aPower, double aTol=0.01)
Definition: Mat.h:837
void setToRotation(const Vec3< T > &axis, T angle)
Set this matrix to the rotation specified by axis and angle.
Definition: Mat3.h:265
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:202
Vec3< T0 > pretransform(const Vec3< T0 > &v) const
Definition: Mat3.h:520
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:766
Vec3< T > col(int j) const
Get jth column, e.g. Vec3d v = m.col(0);.
Definition: Mat3.h:175
Mat3()
Trivial constructor, the matrix is NOT initialized.
Definition: Mat3.h:36
void pivot(int i, int j, Mat3< T > &S, Vec3< T > &D, Mat3< T > &Q)
Definition: Mat3.h:675
void setSkew(const Vec3< T > &v)
Set the matrix as cross product of the given vector.
Definition: Mat3.h:254
void setSymmetric(const Vec3< T > &vdiag, const Vec3< T > &vtri)
Set diagonal and symmetric triangular components.
Definition: Mat3.h:230
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:94
Axis
Definition: Math.h:894
const Mat3< T > & operator*=(S scalar)
Multiplication operator, e.g. M = scalar * M;.
Definition: Mat3.h:338
Mat3(const Quat< T > &q)
Definition: Mat3.h:40
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:594
const Mat3< T > & operator*=(const Mat3< S > &m1)
Multiply this matrix by the given matrix.
Definition: Mat3.h:390
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:584
bool cwiseGreaterThan(const Mat3< T > &m0, const Mat3< T > &m1)
Definition: Mat3.h:835
T value_type
Definition: Mat.h:29
T operator()(int i, int j) const
Definition: Mat3.h:194
Mat3< T > powLerp(const Mat3< T0 > &m1, const Mat3< T0 > &m2, T t)
Definition: Mat3.h:662
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
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:568
3x3 matrix class.
Definition: Mat3.h:28
Definition: openvdb/Exceptions.h:13
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:744
bool isExactlyEqual(const T0 &a, const T1 &b)
Return true if a is exactly equal to b.
Definition: Math.h:434
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:244
bool operator==(const Mat3< T0 > &m0, const Mat3< T1 > &m1)
Equality operator, does exact floating point comparisons.
Definition: Mat3.h:549
void setToRotation(const Quat< T > &q)
Set this matrix to the rotation matrix specified by the quaternion.
Definition: Mat3.h:260
void setCol(int j, const Vec3< T > &v)
Set jth column to vector v.
Definition: Mat3.h:166
T det() const
Determinant of matrix.
Definition: Mat3.h:486
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:528
Mat3 inverse(T tolerance=0) const
Definition: Mat3.h:472
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:372
Tolerance for floating-point comparison.
Definition: Math.h:137
Mat3< Type1 > cwiseAdd(const Mat3< Type1 > &m, const Type2 s)
Definition: Mat3.h:813
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:615
static const Mat3< T > & zero()
Predefined constant for zero matrix.
Definition: Mat3.h:138
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:354
Mat3(const Mat3< Source > &m)
Conversion constructor.
Definition: Mat3.h:108
void setZero()
Set this matrix to zero.
Definition: Mat3.h:269
static const Mat3< T > & identity()
Predefined constant for identity matrix.
Definition: Mat3.h:128
MatType skew(const Vec3< typename MatType::value_type > &skew)
Return a matrix as the cross product of the given vector.
Definition: Mat.h:723
Mat3 cofactor() const
Return the cofactor matrix of this matrix.
Definition: Mat3.h:430
T ValueType
Definition: Mat.h:30
const Mat3 & operator=(const Mat3< Source > &m)
Assignment operator.
Definition: Mat3.h:298
Mat3 snapBasis(Axis axis, const Vec3< T > &direction)
Definition: Mat3.h:504
Definition: openvdb/Exceptions.h:56
T * asPointer()
Direct access to the internal data.
Definition: Mat.h:116
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:146
T & operator()(int i, int j)
Definition: Mat3.h:184
Mat3< double > Mat3d
Definition: Mat3.h:841
void setRow(int i, const Vec3< T > &v)
Set ith row to vector v.
Definition: Mat3.h:148
T angle(const Vec2< T > &v1, const Vec2< T > &v2)
Definition: Vec2.h:445
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:604
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:397
Vec3< T > row(int i) const
Get ith row, e.g. Vec3d v = m.row(1);.
Definition: Mat3.h:159
bool operator!=(const Mat3< T0 > &m0, const Mat3< T1 > &m1)
Inequality operator, does exact floating point comparisons.
Definition: Mat3.h:563