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;
53 template<
typename Source>
54 Mat3(Source a, Source b, Source c,
55 Source d, Source e, Source f,
56 Source g, Source h, Source i)
58 MyBase::mm[0] =
static_cast<T
>(a);
59 MyBase::mm[1] =
static_cast<T
>(b);
60 MyBase::mm[2] =
static_cast<T
>(c);
61 MyBase::mm[3] =
static_cast<T
>(d);
62 MyBase::mm[4] =
static_cast<T
>(e);
63 MyBase::mm[5] =
static_cast<T
>(f);
64 MyBase::mm[6] =
static_cast<T
>(g);
65 MyBase::mm[7] =
static_cast<T
>(h);
66 MyBase::mm[8] =
static_cast<T
>(i);
71 template<
typename Source>
75 this->setRows(v1, v2, v3);
77 this->setColumns(v1, v2, v3);
85 template<
typename Source>
88 MyBase::mm[0] =
static_cast<T
>(a[0]);
89 MyBase::mm[1] =
static_cast<T
>(a[1]);
90 MyBase::mm[2] =
static_cast<T
>(a[2]);
91 MyBase::mm[3] =
static_cast<T
>(a[3]);
92 MyBase::mm[4] =
static_cast<T
>(a[4]);
93 MyBase::mm[5] =
static_cast<T
>(a[5]);
94 MyBase::mm[6] =
static_cast<T
>(a[6]);
95 MyBase::mm[7] =
static_cast<T
>(a[7]);
96 MyBase::mm[8] =
static_cast<T
>(a[8]);
100 template<
typename Source>
103 for (
int i=0; i<3; ++i) {
104 for (
int j=0; j<3; ++j) {
105 MyBase::mm[i*3 + j] =
static_cast<T
>(m[i][j]);
113 for (
int i=0; i<3; ++i) {
114 for (
int j=0; j<3; ++j) {
115 MyBase::mm[i*3 + j] = m[i][j];
146 MyBase::mm[i3+0] = v[0];
147 MyBase::mm[i3+1] = v[1];
148 MyBase::mm[i3+2] = v[2];
155 return Vec3<T>((*
this)(i,0), (*
this)(i,1), (*
this)(i,2));
162 MyBase::mm[0+j] = v[0];
163 MyBase::mm[3+j] = v[1];
164 MyBase::mm[6+j] = v[2];
171 return Vec3<T>((*
this)(0,j), (*
this)(1,j), (*
this)(2,j));
181 return MyBase::mm[3*i+j];
191 return MyBase::mm[3*i+j];
197 MyBase::mm[0] = v1[0];
198 MyBase::mm[1] = v1[1];
199 MyBase::mm[2] = v1[2];
200 MyBase::mm[3] = v2[0];
201 MyBase::mm[4] = v2[1];
202 MyBase::mm[5] = v2[2];
203 MyBase::mm[6] = v3[0];
204 MyBase::mm[7] = v3[1];
205 MyBase::mm[8] = v3[2];
211 MyBase::mm[0] = v1[0];
212 MyBase::mm[1] = v2[0];
213 MyBase::mm[2] = v3[0];
214 MyBase::mm[3] = v1[1];
215 MyBase::mm[4] = v2[1];
216 MyBase::mm[5] = v3[1];
217 MyBase::mm[6] = v1[2];
218 MyBase::mm[7] = v2[2];
219 MyBase::mm[8] = v3[2];
225 MyBase::mm[0] = vdiag[0];
226 MyBase::mm[1] = vtri[0];
227 MyBase::mm[2] = vtri[1];
228 MyBase::mm[3] = vtri[0];
229 MyBase::mm[4] = vdiag[1];
230 MyBase::mm[5] = vtri[2];
231 MyBase::mm[6] = vtri[1];
232 MyBase::mm[7] = vtri[2];
233 MyBase::mm[8] = vdiag[2];
240 vdiag[0], vtri[0], vtri[1],
241 vtri[0], vdiag[1], vtri[2],
242 vtri[1], vtri[2], vdiag[2]
254 {*
this = rotation<Mat3<T> >(q);}
259 {*
this = rotation<Mat3<T> >(axis,
angle);}
290 template<
typename Source>
296 std::copy(src, (src + this->numElements()), MyBase::mm);
301 bool eq(
const Mat3 &m, T eps=1.0e-8)
const
318 -MyBase::mm[0], -MyBase::mm[1], -MyBase::mm[2],
319 -MyBase::mm[3], -MyBase::mm[4], -MyBase::mm[5],
320 -MyBase::mm[6], -MyBase::mm[7], -MyBase::mm[8]
330 template <
typename S>
333 MyBase::mm[0] *= scalar;
334 MyBase::mm[1] *= scalar;
335 MyBase::mm[2] *= scalar;
336 MyBase::mm[3] *= scalar;
337 MyBase::mm[4] *= scalar;
338 MyBase::mm[5] *= scalar;
339 MyBase::mm[6] *= scalar;
340 MyBase::mm[7] *= scalar;
341 MyBase::mm[8] *= scalar;
346 template <
typename S>
351 MyBase::mm[0] += s[0];
352 MyBase::mm[1] += s[1];
353 MyBase::mm[2] += s[2];
354 MyBase::mm[3] += s[3];
355 MyBase::mm[4] += s[4];
356 MyBase::mm[5] += s[5];
357 MyBase::mm[6] += s[6];
358 MyBase::mm[7] += s[7];
359 MyBase::mm[8] += s[8];
364 template <
typename S>
369 MyBase::mm[0] -= s[0];
370 MyBase::mm[1] -= s[1];
371 MyBase::mm[2] -= s[2];
372 MyBase::mm[3] -= s[3];
373 MyBase::mm[4] -= s[4];
374 MyBase::mm[5] -= s[5];
375 MyBase::mm[6] -= s[6];
376 MyBase::mm[7] -= s[7];
377 MyBase::mm[8] -= s[8];
382 template <
typename S>
389 MyBase::mm[0] =
static_cast<T
>(s0[0] * s1[0] +
392 MyBase::mm[1] =
static_cast<T
>(s0[0] * s1[1] +
395 MyBase::mm[2] =
static_cast<T
>(s0[0] * s1[2] +
399 MyBase::mm[3] =
static_cast<T
>(s0[3] * s1[0] +
402 MyBase::mm[4] =
static_cast<T
>(s0[3] * s1[1] +
405 MyBase::mm[5] =
static_cast<T
>(s0[3] * s1[2] +
409 MyBase::mm[6] =
static_cast<T
>(s0[6] * s1[0] +
412 MyBase::mm[7] =
static_cast<T
>(s0[6] * s1[1] +
415 MyBase::mm[8] =
static_cast<T
>(s0[6] * s1[2] +
426 MyBase::mm[4] * MyBase::mm[8] - MyBase::mm[5] * MyBase::mm[7],
427 MyBase::mm[5] * MyBase::mm[6] - MyBase::mm[3] * MyBase::mm[8],
428 MyBase::mm[3] * MyBase::mm[7] - MyBase::mm[4] * MyBase::mm[6],
429 MyBase::mm[2] * MyBase::mm[7] - MyBase::mm[1] * MyBase::mm[8],
430 MyBase::mm[0] * MyBase::mm[8] - MyBase::mm[2] * MyBase::mm[6],
431 MyBase::mm[1] * MyBase::mm[6] - MyBase::mm[0] * MyBase::mm[7],
432 MyBase::mm[1] * MyBase::mm[5] - MyBase::mm[2] * MyBase::mm[4],
433 MyBase::mm[2] * MyBase::mm[3] - MyBase::mm[0] * MyBase::mm[5],
434 MyBase::mm[0] * MyBase::mm[4] - MyBase::mm[1] * MyBase::mm[3]);
441 MyBase::mm[4] * MyBase::mm[8] - MyBase::mm[5] * MyBase::mm[7],
442 MyBase::mm[2] * MyBase::mm[7] - MyBase::mm[1] * MyBase::mm[8],
443 MyBase::mm[1] * MyBase::mm[5] - MyBase::mm[2] * MyBase::mm[4],
444 MyBase::mm[5] * MyBase::mm[6] - MyBase::mm[3] * MyBase::mm[8],
445 MyBase::mm[0] * MyBase::mm[8] - MyBase::mm[2] * MyBase::mm[6],
446 MyBase::mm[2] * MyBase::mm[3] - MyBase::mm[0] * MyBase::mm[5],
447 MyBase::mm[3] * MyBase::mm[7] - MyBase::mm[4] * MyBase::mm[6],
448 MyBase::mm[1] * MyBase::mm[6] - MyBase::mm[0] * MyBase::mm[7],
449 MyBase::mm[0] * MyBase::mm[4] - MyBase::mm[1] * MyBase::mm[3]);
457 MyBase::mm[0], MyBase::mm[3], MyBase::mm[6],
458 MyBase::mm[1], MyBase::mm[4], MyBase::mm[7],
459 MyBase::mm[2], MyBase::mm[5], MyBase::mm[8]);
469 const T det = inv.
mm[0]*MyBase::mm[0] + inv.
mm[1]*MyBase::mm[3] + inv.
mm[2]*MyBase::mm[6];
475 return inv * (T(1)/det);
481 const T co00 = MyBase::mm[4]*MyBase::mm[8] - MyBase::mm[5]*MyBase::mm[7];
482 const T co10 = MyBase::mm[5]*MyBase::mm[6] - MyBase::mm[3]*MyBase::mm[8];
483 const T co20 = MyBase::mm[3]*MyBase::mm[7] - MyBase::mm[4]*MyBase::mm[6];
484 return MyBase::mm[0]*co00 + MyBase::mm[1]*co10 + MyBase::mm[2]*co20;
490 return MyBase::mm[0]+MyBase::mm[4]+MyBase::mm[8];
504 template<
typename T0>
507 return static_cast< Vec3<T0> >(v * *
this);
512 template<
typename T0>
515 return static_cast< Vec3<T0> >(*
this * v);
525 ret.
mm[0] *= diag(0);
526 ret.
mm[1] *= diag(1);
527 ret.
mm[2] *= diag(2);
528 ret.
mm[3] *= diag(0);
529 ret.
mm[4] *= diag(1);
530 ret.
mm[5] *= diag(2);
531 ret.
mm[6] *= diag(0);
532 ret.
mm[7] *= diag(1);
533 ret.
mm[8] *= diag(2);
541template <
typename T0,
typename T1>
547 for (
int i=0; i<9; ++i) {
555template <
typename T0,
typename T1>
560template <
typename S,
typename T>
566template <
typename S,
typename T>
576template <
typename T0,
typename T1>
586template <
typename T0,
typename T1>
596template <
typename T0,
typename T1>
606template<
typename T,
typename MT>
612 _v[0]*m[0] + _v[1]*m[1] + _v[2]*m[2],
613 _v[0]*m[3] + _v[1]*m[4] + _v[2]*m[5],
614 _v[0]*m[6] + _v[1]*m[7] + _v[2]*m[8]);
619template<
typename T,
typename MT>
625 _v[0]*m[0] + _v[1]*m[3] + _v[2]*m[6],
626 _v[0]*m[1] + _v[1]*m[4] + _v[2]*m[7],
627 _v[0]*m[2] + _v[1]*m[5] + _v[2]*m[8]);
632template<
typename T,
typename MT>
645 return Mat3<T>(v1[0]*v2[0], v1[0]*v2[1], v1[0]*v2[2],
646 v1[1]*v2[0], v1[1]*v2[1], v1[1]*v2[2],
647 v1[2]*v2[0], v1[2]*v2[1], v1[2]*v2[2]);
654template<
typename T,
typename T0>
664namespace mat3_internal {
673 double cotan_of_2_theta;
675 double cosin_of_theta;
681 double Sjj_minus_Sii = D[j] - D[i];
684 tan_of_theta = Sij / Sjj_minus_Sii;
687 cotan_of_2_theta = 0.5*Sjj_minus_Sii / Sij ;
689 if (cotan_of_2_theta < 0.) {
691 -1./(sqrt(1. + cotan_of_2_theta*cotan_of_2_theta) - cotan_of_2_theta);
694 1./(sqrt(1. + cotan_of_2_theta*cotan_of_2_theta) + cotan_of_2_theta);
698 cosin_of_theta = 1./sqrt( 1. + tan_of_theta * tan_of_theta);
699 sin_of_theta = cosin_of_theta * tan_of_theta;
700 z = tan_of_theta * Sij;
704 for (
int k = 0; k < i; ++k) {
706 S(k,i) = cosin_of_theta * temp - sin_of_theta * S(k,j);
707 S(k,j)= sin_of_theta * temp + cosin_of_theta * S(k,j);
709 for (
int k = i+1; k < j; ++k) {
711 S(i,k) = cosin_of_theta * temp - sin_of_theta * S(k,j);
712 S(k,j) = sin_of_theta * temp + cosin_of_theta * S(k,j);
714 for (
int k = j+1; k < n; ++k) {
716 S(i,k) = cosin_of_theta * temp - sin_of_theta * S(j,k);
717 S(j,k) = sin_of_theta * temp + cosin_of_theta * S(j,k);
719 for (
int k = 0; k < n; ++k)
722 Q(k,i) = cosin_of_theta * temp - sin_of_theta*Q(k,j);
723 Q(k,j) = sin_of_theta * temp + cosin_of_theta*Q(k,j);
738 unsigned int MAX_ITERATIONS=250)
748 for (
int i = 0; i < n; ++i) {
752 unsigned int iterations(0);
759 for (
int i = 0; i < n; ++i) {
760 for (
int j = i+1; j < n; ++j) {
773 for (
int i = 0; i < n; ++i) {
774 for (
int j = i+1; j < n; ++j){
780 if (fabs(S(i,j)) > max_element) {
781 max_element = fabs(S(i,j));
787 mat3_internal::pivot(ip, jp, S, D, Q);
788 }
while (iterations < MAX_ITERATIONS);
800 for (
unsigned i = 0; i < 9; ++i, ++op, ++ip) *op = math::Abs(*ip);
804template<
typename Type1,
typename Type2>
811 for (
unsigned i = 0; i < 9; ++i, ++op, ++ip) {
823 return cwiseLessThan<3, T>(m0, m1);
830 return cwiseGreaterThan<3, T>(m0, m1);
843template<>
inline math::Mat3s zeroVal<math::Mat3s>() {
return math::Mat3s::zero(); }
844template<>
inline math::Mat3d zeroVal<math::Mat3d>() {
return math::Mat3d::zero(); }
#define OPENVDB_IS_POD(Type)
Definition: Math.h:56
Definition: Exceptions.h:56
3x3 matrix class.
Definition: Mat3.h:29
Mat3(Source *a)
Definition: Mat3.h:86
Mat3(const Mat4< T > &m)
Conversion from Mat4 (copies top left)
Definition: Mat3.h:111
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:365
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:195
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:561
bool operator!=(const Mat3< T0 > &m0, const Mat3< T1 > &m1)
Inequality operator, does exact floating point comparisons.
Definition: Mat3.h:556
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:577
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:567
void setIdentity()
Set this matrix to identity.
Definition: Mat3.h:276
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:54
Mat3(const Mat3< Source > &m)
Conversion constructor.
Definition: Mat3.h:101
void setToRotation(const Vec3< T > &axis, T angle)
Set this matrix to the rotation specified by axis and angle.
Definition: Mat3.h:258
void setZero()
Set this matrix to zero.
Definition: Mat3.h:262
const Mat3 & operator=(const Mat3< Source > &m)
Assignment operator.
Definition: Mat3.h:291
Mat3(const Quat< T > &q)
Definition: Mat3.h:43
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:347
Vec3< T0 > pretransform(const Vec3< T0 > &v) const
Definition: Mat3.h:513
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:301
void setToRotation(const Quat< T > &q)
Set this matrix to the rotation matrix specified by the quaternion.
Definition: Mat3.h:253
Mat3 inverse(T tolerance=0) const
Definition: Mat3.h:465
T trace() const
Trace of matrix.
Definition: Mat3.h:488
Mat3< T > operator-() const
Negation operator, for e.g. m1 = -m2;.
Definition: Mat3.h:315
void setCol(int j, const Vec3< T > &v)
Set jth column to vector v.
Definition: Mat3.h:159
Mat3 adjoint() const
Return the adjoint of this matrix, i.e., the transpose of its cofactor.
Definition: Mat3.h:438
void setSkew(const Vec3< T > &v)
Set the matrix as cross product of the given vector.
Definition: Mat3.h:247
const Mat3< T > & operator*=(S scalar)
Multiplication operator, e.g. M = scalar * M;.
Definition: Mat3.h:331
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:608
Mat3 snapBasis(Axis axis, const Vec3< T > &direction)
Definition: Mat3.h:497
Mat3 transpose() const
returns transpose of this
Definition: Mat3.h:454
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:521
T det() const
Determinant of matrix.
Definition: Mat3.h:479
static const Mat3< T > & zero()
Predefined constant for zero matrix.
Definition: Mat3.h:131
Vec3< T > col(int j) const
Get jth column, e.g. Vec3d v = m.col(0);.
Definition: Mat3.h:168
T & operator()(int i, int j)
Definition: Mat3.h:177
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:237
Vec3< T0 > transform(const Vec3< T0 > &v) const
Definition: Mat3.h:505
static const Mat3< T > & identity()
Predefined constant for identity matrix.
Definition: Mat3.h:121
T operator()(int i, int j) const
Definition: Mat3.h:187
bool operator==(const Mat3< T0 > &m0, const Mat3< T1 > &m1)
Equality operator, does exact floating point comparisons.
Definition: Mat3.h:542
const Mat3< T > & operator*=(const Mat3< S > &m1)
Multiply this matrix by the given matrix.
Definition: Mat3.h:383
Vec3< T > row(int i) const
Get ith row, e.g. Vec3d v = m.row(1);.
Definition: Mat3.h:152
void setRow(int i, const Vec3< T > &v)
Set ith row to vector v.
Definition: Mat3.h:141
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:621
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:209
Mat3 cofactor() const
Return the cofactor matrix of this matrix.
Definition: Mat3.h:423
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:587
Mat3(const Vec3< Source > &v1, const Vec3< Source > &v2, const Vec3< Source > &v3, bool rows=true)
Definition: Mat3.h:72
void setSymmetric(const Vec3< T > &vdiag, const Vec3< T > &vtri)
Set diagonal and symmetric triangular components.
Definition: Mat3.h:223
T value_type
Data type held by the matrix.
Definition: Mat3.h:32
4x4 -matrix class.
Definition: Mat4.h:31
T mm[SIZE *SIZE]
Definition: Mat.h:160
T * asPointer()
Direct access to the internal data.
Definition: Mat.h:101
void pivot(int i, int j, Mat3< T > &S, Vec3< T > &D, Mat3< T > &Q)
Definition: Mat3.h:668
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:751
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:737
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:597
bool cwiseLessThan(const Mat< SIZE, T > &m0, const Mat< SIZE, T > &m1)
Definition: Mat.h:1015
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:406
Mat3< Type1 > cwiseAdd(const Mat3< Type1 > &m, const Type2 s)
Definition: Mat3.h:806
Mat3< T > powLerp(const Mat3< T0 > &m1, const Mat3< T0 > &m2, T t)
Definition: Mat3.h:655
Coord Abs(const Coord &xyz)
Definition: Coord.h:515
Mat3< T > outerProduct(const Vec3< T > &v1, const Vec3< T > &v2)
Definition: Mat3.h:643
T angle(const Vec2< T > &v1, const Vec2< T > &v2)
Definition: Vec2.h:446
void powSolve(const MatType &aA, MatType &aB, double aPower, double aTol=0.01)
Definition: Mat.h:822
bool isExactlyEqual(const T0 &a, const T1 &b)
Return true if a is exactly equal to b.
Definition: Math.h:443
MatType skew(const Vec3< typename MatType::value_type > &skew)
Return a matrix as the cross product of the given vector.
Definition: Mat.h:708
Axis
Definition: Math.h:901
bool cwiseGreaterThan(const Mat< SIZE, T > &m0, const Mat< SIZE, T > &m1)
Definition: Mat.h:1029
Definition: Exceptions.h:13
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:74
Tolerance for floating-point comparison.
Definition: Math.h:148
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h.in:121
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h.in:212