7 #ifndef OPENVDB_MATH_MAT_HAS_BEEN_INCLUDED 8 #define OPENVDB_MATH_MAT_HAS_BEEN_INCLUDED 25 template<
unsigned SIZE,
typename T>
34 static unsigned numRows() {
return SIZE; }
44 for (
unsigned i(0); i < numElements(); ++i) {
51 for (
unsigned i = 0; i < numElements(); ++i) {
68 str(
unsigned indentation = 0)
const {
74 indent.append(indentation+1,
' ');
79 for (
unsigned i(0); i < SIZE; i++) {
84 for (
unsigned j(0); j < SIZE; j++) {
87 if (j) ret.append(
", ");
88 ret.append(std::to_string(mm[(i*SIZE)+j]));
115 void write(std::ostream& os)
const {
116 os.write(reinterpret_cast<const char*>(&mm),
sizeof(T)*SIZE*SIZE);
120 is.read(reinterpret_cast<char*>(&mm),
sizeof(T)*SIZE*SIZE);
125 T x =
static_cast<T
>(std::fabs(mm[0]));
126 for (
unsigned i = 1; i < numElements(); ++i) {
127 x =
std::max(x, static_cast<T>(std::fabs(mm[i])));
134 for (
unsigned i = 0; i < numElements(); ++i) {
142 for (
unsigned i = 0; i < numElements(); ++i) {
150 for (
unsigned i = 0; i < numElements(); ++i) {
158 for (
unsigned i = 0; i < numElements(); ++i) {
169 template<
typename T>
class Quat;
170 template<
typename T>
class Vec3;
175 template<
class MatType>
178 typename MatType::value_type eps = static_cast<typename MatType::value_type>(1.0e-8))
180 using T =
typename MatType::value_type;
203 r[0][0]=T(1) - (yy+zz); r[0][1]=xy + wz; r[0][2]=xz - wy;
204 r[1][0]=xy - wz; r[1][1]=T(1) - (xx+zz); r[1][2]=yz + wx;
205 r[2][0]=xz + wy; r[2][1]=yz - wx; r[2][2]=T(1) - (xx+yy);
207 if(MatType::numColumns() == 4)
padMat4(r);
216 template<
class MatType>
220 using T =
typename MatType::value_type;
221 T c =
static_cast<T
>(cos(
angle));
222 T s =
static_cast<T
>(sin(
angle));
225 result.setIdentity();
247 throw ValueError(
"Unrecognized rotation axis");
254 template<
class MatType>
258 using T =
typename MatType::value_type;
259 T txy, txz, tyz, sx, sy, sz;
264 T c(cos(
double(
angle)));
265 T s(sin(
double(
angle)));
270 result[0][0] = axis[0]*axis[0] * t + c;
271 result[1][1] = axis[1]*axis[1] * t + c;
272 result[2][2] = axis[2]*axis[2] * t + c;
274 txy = axis[0]*axis[1] * t;
277 txz = axis[0]*axis[2] * t;
280 tyz = axis[1]*axis[2] * t;
285 result[0][1] = txy + sz;
286 result[1][0] = txy - sz;
288 result[0][2] = txz - sy;
289 result[2][0] = txz + sy;
291 result[1][2] = tyz + sx;
292 result[2][1] = tyz - sx;
294 if(MatType::numColumns() == 4)
padMat4(result);
295 return MatType(result);
336 template<
class MatType>
341 typename MatType::value_type eps = static_cast<typename MatType::value_type>(1.0e-8))
343 using ValueType =
typename MatType::value_type;
345 ValueType phi, theta, psi;
347 switch(rotationOrder)
351 theta = ValueType(M_PI_2);
352 phi = ValueType(0.5 * atan2(mat[1][2], mat[1][1]));
355 theta = ValueType(-M_PI_2);
356 phi = ValueType(0.5 * atan2(mat[1][2], mat[1][1]));
359 psi = ValueType(atan2(-mat[1][0],mat[0][0]));
360 phi = ValueType(atan2(-mat[2][1],mat[2][2]));
361 theta = ValueType(atan2(mat[2][0],
362 sqrt( mat[2][1]*mat[2][1] +
363 mat[2][2]*mat[2][2])));
365 return V(phi, theta, psi);
368 theta = ValueType(M_PI_2);
369 phi = ValueType(0.5 * atan2(mat[0][1], mat[0][0]));
372 theta = ValueType(-M_PI/2);
373 phi = ValueType(0.5 * atan2(mat[0][1],mat[2][1]));
376 psi = ValueType(atan2(-mat[0][2], mat[2][2]));
377 phi = ValueType(atan2(-mat[1][0], mat[1][1]));
378 theta = ValueType(atan2(mat[1][2],
379 sqrt(mat[0][2] * mat[0][2] +
380 mat[2][2] * mat[2][2])));
382 return V(theta, psi, phi);
386 theta = ValueType(M_PI_2);
387 phi = ValueType(0.5 * atan2(mat[2][0], mat[2][2]));
390 theta = ValueType(-M_PI/2);
391 phi = ValueType(0.5 * atan2(mat[2][0], mat[1][0]));
394 psi = ValueType(atan2(-mat[2][1], mat[1][1]));
395 phi = ValueType(atan2(-mat[0][2], mat[0][0]));
396 theta = ValueType(atan2(mat[0][1],
397 sqrt(mat[0][0] * mat[0][0] +
398 mat[0][2] * mat[0][2])));
400 return V(psi, phi, theta);
405 theta = ValueType(0.0);
406 phi = ValueType(0.5 * atan2(mat[1][2], mat[1][1]));
409 theta = ValueType(M_PI);
410 psi = ValueType(0.5 * atan2(mat[2][1], -mat[1][1]));
413 psi = ValueType(atan2(mat[2][0], -mat[1][0]));
414 phi = ValueType(atan2(mat[0][2], mat[0][1]));
415 theta = ValueType(atan2(sqrt(mat[0][1] * mat[0][1] +
416 mat[0][2] * mat[0][2]),
419 return V(phi, psi, theta);
424 theta = ValueType(0.0);
425 phi = ValueType(0.5 * atan2(mat[0][1], mat[0][0]));
428 theta = ValueType(M_PI);
429 phi = ValueType(0.5 * atan2(mat[0][1], mat[0][0]));
432 psi = ValueType(atan2(mat[0][2], mat[1][2]));
433 phi = ValueType(atan2(mat[2][0], -mat[2][1]));
434 theta = ValueType(atan2(sqrt(mat[0][2] * mat[0][2] +
435 mat[1][2] * mat[1][2]),
438 return V(theta, psi, phi);
443 theta = ValueType(-M_PI_2);
444 phi = ValueType(0.5 * atan2(-mat[1][0], mat[0][0]));
447 theta = ValueType(M_PI_2);
448 phi = ValueType(0.5 * atan2(mat[1][0], mat[0][0]));
451 psi = ValueType(atan2(mat[0][1], mat[1][1]));
452 phi = ValueType(atan2(mat[2][0], mat[2][2]));
453 theta = ValueType(atan2(-mat[2][1],
454 sqrt(mat[0][1] * mat[0][1] +
455 mat[1][1] * mat[1][1])));
457 return V(theta, phi, psi);
462 theta = ValueType(-M_PI_2);
463 phi = ValueType(0.5 * atan2(-mat[1][0], mat[1][1]));
466 theta = ValueType(M_PI_2);
467 phi = ValueType(0.5 * atan2(mat[2][1], mat[2][0]));
470 psi = ValueType(atan2(mat[1][2], mat[2][2]));
471 phi = ValueType(atan2(mat[0][1], mat[0][0]));
472 theta = ValueType(atan2(-mat[0][2],
473 sqrt(mat[0][1] * mat[0][1] +
474 mat[0][0] * mat[0][0])));
476 return V(psi, theta, phi);
481 theta = ValueType(M_PI_2);
482 psi = ValueType(0.5 * atan2(mat[2][1], mat[2][2]));
485 theta = ValueType(-M_PI_2);
486 psi = ValueType(0.5 * atan2(- mat[2][1], mat[2][2]));
489 psi = ValueType(atan2(mat[2][0], mat[0][0]));
490 phi = ValueType(atan2(mat[1][2], mat[1][1]));
491 theta = ValueType(atan2(- mat[1][0],
492 sqrt(mat[1][1] * mat[1][1] +
493 mat[1][2] * mat[1][2])));
495 return V(phi, psi, theta);
505 template<
typename MatType,
typename ValueType1,
typename ValueType2>
510 typename MatType::value_type eps = static_cast<typename MatType::value_type>(1.0e-8))
512 using T =
typename MatType::value_type;
542 Vec3<T> u, v, p(0.0, 0.0, 0.0);
544 double x =
Abs(v1[0]);
545 double y =
Abs(v1[1]);
546 double z =
Abs(v1[2]);
564 double udot = u.
dot(u);
565 double vdot = v.
dot(v);
567 double a = -2 / udot;
568 double b = -2 / vdot;
569 double c = 4 * u.
dot(v) / (udot * vdot);
572 result.setIdentity();
574 for (
int j = 0; j < 3; j++) {
575 for (
int i = 0; i < 3; i++)
576 result[i][j] = static_cast<T>(
577 a * u[i] * u[j] + b * v[i] * v[j] + c * v[j] * u[i]);
583 if(MatType::numColumns() == 4)
padMat4(result);
587 double c = v1.
dot(v2);
588 double a = (1.0 - c) / cross.
dot(cross);
590 double a0 = a * cross[0];
591 double a1 = a * cross[1];
592 double a2 = a * cross[2];
594 double a01 = a0 * cross[1];
595 double a02 = a0 * cross[2];
596 double a12 = a1 * cross[2];
600 r[0][0] =
static_cast<T
>(c + a0 * cross[0]);
601 r[0][1] =
static_cast<T
>(a01 + cross[2]);
602 r[0][2] =
static_cast<T
>(a02 - cross[1]);
603 r[1][0] =
static_cast<T
>(a01 - cross[2]);
604 r[1][1] =
static_cast<T
>(c + a1 * cross[1]);
605 r[1][2] =
static_cast<T
>(a12 + cross[0]);
606 r[2][0] =
static_cast<T
>(a02 + cross[1]);
607 r[2][1] =
static_cast<T
>(a12 - cross[0]);
608 r[2][2] =
static_cast<T
>(c + a2 * cross[2]);
610 if(MatType::numColumns() == 4)
padMat4(r);
618 template<
class MatType>
626 result.setIdentity();
636 template<
class MatType>
642 V(mat[0][0], mat[0][1], mat[0][2]).length(),
643 V(mat[1][0], mat[1][1], mat[1][2]).length(),
644 V(mat[2][0], mat[2][1], mat[2][2]).length());
651 template<
class MatType>
653 unit(
const MatType &mat,
typename MatType::value_type eps = 1.0e-8)
656 return unit(mat, eps, dud);
664 template<
class MatType>
668 typename MatType::value_type eps,
671 using T =
typename MatType::value_type;
674 for (
int i(0); i < 3; i++) {
677 Vec3<T>(in[i][0], in[i][1], in[i][2]).
unit(eps, scaling[i]));
678 for (
int j=0; j<3; j++) result[i][j] = u[j];
680 for (
int j=0; j<3; j++) result[i][j] = 0;
691 template <
class MatType>
695 int index0 =
static_cast<int>(axis0);
696 int index1 =
static_cast<int>(axis1);
699 result.setIdentity();
700 if (axis0 == axis1) {
701 result[index1][index0] =
shear + 1;
703 result[index1][index0] =
shear;
711 template<
class MatType>
715 using T =
typename MatType::value_type;
718 r[0][0] = T(0); r[0][1] =
skew.z(); r[0][2] = -
skew.y();
719 r[1][0] = -
skew.z(); r[1][1] = T(0); r[2][1] =
skew.x();
720 r[2][0] =
skew.y(); r[2][1] = -
skew.x(); r[2][2] = T(0);
722 if(MatType::numColumns() == 4)
padMat4(r);
729 template<
class MatType>
734 using T =
typename MatType::value_type;
736 Vec3<T> horizontal(vertical.
unit().cross(forward).unit());
737 Vec3<T> up(forward.cross(horizontal).unit());
741 r[0][0]=horizontal.
x(); r[0][1]=horizontal.
y(); r[0][2]=horizontal.
z();
742 r[1][0]=up.
x(); r[1][1]=up.
y(); r[1][2]=up.
z();
743 r[2][0]=forward.
x(); r[2][1]=forward.
y(); r[2][2]=forward.
z();
745 if(MatType::numColumns() == 4)
padMat4(r);
754 template<
class MatType>
758 using T =
typename MatType::value_type;
761 Vec3<T> ourUnitAxis(source.row(axis).unit());
764 T parallel = unitDir.
dot(ourUnitAxis);
774 T angleBetween(
angle(unitDir, ourUnitAxis));
779 rotation.setToRotation(rotationAxis, angleBetween);
786 template<
class MatType>
790 dest[0][3] = dest[1][3] = dest[2][3] = 0;
791 dest[3][2] = dest[3][1] = dest[3][0] = 0;
800 template<
typename MatType>
802 sqrtSolve(
const MatType& aA, MatType& aB,
double aTol=0.01)
804 unsigned int iterations =
static_cast<unsigned int>(log(aTol)/log(0.5));
808 Z[0] = MatType::identity();
810 unsigned int current = 0;
811 for (
unsigned int iteration=0; iteration < iterations; iteration++) {
812 unsigned int last = current;
815 MatType invY = Y[last].inverse();
816 MatType invZ = Z[last].inverse();
818 Y[current] = 0.5 * (Y[last] + invZ);
819 Z[current] = 0.5 * (Z[last] + invY);
825 template<
typename MatType>
827 powSolve(
const MatType& aA, MatType& aB,
double aPower,
double aTol=0.01)
829 unsigned int iterations =
static_cast<unsigned int>(log(aTol)/log(0.5));
831 const bool inverted = (aPower < 0.0);
832 if (inverted) { aPower = -aPower; }
834 unsigned int whole =
static_cast<unsigned int>(aPower);
835 double fraction = aPower - whole;
837 MatType R = MatType::identity();
838 MatType partial = aA;
840 double contribution = 1.0;
841 for (
unsigned int iteration = 0; iteration < iterations; iteration++) {
844 if (fraction >= contribution) {
846 fraction -= contribution;
852 if (whole & 1) { R *= partial; }
854 if (whole) { partial *= partial; }
857 if (inverted) { aB = R.inverse(); }
863 template<
typename MatType>
867 return m.eq(MatType::identity());
872 template<
typename MatType>
876 using ValueType =
typename MatType::ValueType;
883 template<
typename MatType>
887 return m.eq(m.transpose());
892 template<
typename MatType>
896 using ValueType =
typename MatType::ValueType;
897 if (!
isApproxEqual(std::abs(m.det()), ValueType(1.0)))
return false;
899 MatType temp = m * m.transpose();
900 return temp.eq(MatType::identity());
905 template<
typename MatType>
909 int n = MatType::size;
910 typename MatType::ValueType temp(0);
911 for (
int i = 0; i < n; ++i) {
912 for (
int j = 0; j < n; ++j) {
914 temp += std::abs(mat(i,j));
918 return isApproxEqual(temp,
typename MatType::ValueType(0.0));
923 template<
typename MatType>
924 typename MatType::ValueType
927 int n = MatType::size;
928 typename MatType::ValueType norm = 0;
930 for(
int j = 0; j<n; ++j) {
931 typename MatType::ValueType column_sum = 0;
933 for (
int i = 0; i<n; ++i) {
934 column_sum += fabs(matrix(i,j));
944 template<
typename MatType>
945 typename MatType::ValueType
948 int n = MatType::size;
949 typename MatType::ValueType norm = 0;
951 for(
int i = 0; i<n; ++i) {
952 typename MatType::ValueType row_sum = 0;
954 for (
int j = 0; j<n; ++j) {
955 row_sum += fabs(matrix(i,j));
971 template<
typename MatType>
974 MatType& positive_hermitian,
unsigned int MAX_ITERATIONS=100)
977 MatType new_unitary(input);
982 unsigned int iteration(0);
984 typename MatType::ValueType linf_of_u;
985 typename MatType::ValueType l1nm_of_u;
986 typename MatType::ValueType linf_of_u_inv;
987 typename MatType::ValueType l1nm_of_u_inv;
988 typename MatType::ValueType l1_error = 100;
992 unitary_inv = unitary.inverse();
997 l1nm_of_u_inv =
lOneNorm(unitary_inv);
999 gamma = sqrt( sqrt( (l1nm_of_u_inv * linf_of_u_inv ) / (l1nm_of_u * linf_of_u) ));
1001 new_unitary = 0.5*(gamma * unitary + (1./gamma) * unitary_inv.transpose() );
1004 unitary = new_unitary;
1007 if (iteration > MAX_ITERATIONS)
return false;
1011 positive_hermitian = unitary.transpose() * input;
1019 #endif // OPENVDB_MATH_MAT_HAS_BEEN_INCLUDED Vec3< T > cross(const Vec3< T > &v) const
Return the cross product of "this" vector and v;.
Definition: Vec3.h:218
Definition: Exceptions.h:61
bool isIdentity(const MatType &m)
Determine if a matrix is an identity matrix.
Definition: Mat.h:865
SIZE_
Definition: Mat.h:31
MatType & padMat4(MatType &dest)
Write 0s along Mat4's last row and column, and a 1 on its diagonal.
Definition: Mat.h:788
bool isZero(const Type &x)
Return true if x is exactly equal to zero.
Definition: Math.h:281
bool polarDecomposition(const MatType &input, MatType &unitary, MatType &positive_hermitian, unsigned int MAX_ITERATIONS=100)
Decompose an invertible 3×3 matrix into a unitary matrix followed by a symmetric matrix (positive sem...
Definition: Mat.h:973
T & z()
Definition: Vec3.h:85
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:351
void write(std::ostream &os) const
Definition: Mat.h:115
Vec3< typename MatType::value_type > getScale(const MatType &mat)
Return a Vec3 representing the lengths of the passed matrix's upper 3×3's rows.
Definition: Mat.h:638
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
T ValueType
Definition: Mat.h:30
MatType skew(const Vec3< typename MatType::value_type > &skew)
Return a matrix as the cross product of the given vector.
Definition: Mat.h:713
bool isInvertible(const MatType &m)
Determine if a matrix is invertible.
Definition: Mat.h:874
friend std::ostream & operator<<(std::ostream &ostr, const Mat< SIZE, T > &m)
Write a Mat to an output stream.
Definition: Mat.h:107
T & x()
Reference to the component, e.g. q.x() = 4.5f;.
Definition: Quat.h:201
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:82
Coord Abs(const Coord &xyz)
Definition: Coord.h:515
Vec3< typename MatType::value_type > eulerAngles(const MatType &mat, RotationOrder rotationOrder, typename MatType::value_type eps=static_cast< typename MatType::value_type >(1.0e-8))
Return the Euler angles composing the given rotation matrix.
Definition: Mat.h:338
MatType::ValueType lOneNorm(const MatType &matrix)
Return the L1 norm of an N×N matrix.
Definition: Mat.h:946
static unsigned numColumns()
Definition: Mat.h:35
Mat(Mat const &src)
Copy constructor. Used when the class signature matches exactly.
Definition: Mat.h:43
T mm[SIZE *SIZE]
Definition: Mat.h:165
RotationOrder
Definition: Math.h:856
T & y()
Definition: Vec3.h:84
bool isZero() const
True if all elements are exactly zero.
Definition: Mat.h:157
Axis
Definition: Math.h:849
static unsigned numElements()
Definition: Mat.h:36
T absMax() const
Return the maximum of the absolute of all elements in this matrix.
Definition: Mat.h:124
bool isSymmetric(const MatType &m)
Determine if a matrix is symmetric.
Definition: Mat.h:885
Mat()
Definition: Mat.h:40
bool isNan(const float x)
Return true if x is a NaN (Not-A-Number) value.
Definition: Math.h:339
T dot(const Quat &q) const
Dot product.
Definition: Quat.h:470
Definition: Exceptions.h:65
bool isInfinite(const float x)
Return true if x is an infinity value (either positive infinity or negative infinity).
Definition: Math.h:329
MatType unit(const MatType &in, typename MatType::value_type eps, Vec3< typename MatType::value_type > &scaling)
Return a copy of the given matrix with its upper 3×3 rows normalized, and return the length of each o...
Definition: Mat.h:666
bool isUnitary(const MatType &m)
Determine if a matrix is unitary (i.e., rotation or reflection).
Definition: Mat.h:894
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:102
T & w()
Definition: Quat.h:204
Definition: Exceptions.h:13
Definition: Exceptions.h:56
T dot(const Vec3< T > &v) const
Dot product.
Definition: Vec3.h:189
std::string str(unsigned indentation=0) const
Definition: Mat.h:68
T & y()
Definition: Quat.h:202
static unsigned numRows()
Definition: Mat.h:34
T & x()
Reference to the component, e.g. v.x() = 4.5f;.
Definition: Vec3.h:83
MatType aim(const Vec3< typename MatType::value_type > &direction, const Vec3< typename MatType::value_type > &vertical)
Return an orientation matrix such that z points along direction, and y is along the direction / verti...
Definition: Mat.h:731
bool isFinite() const
True if no Nan or Inf values are present.
Definition: Mat.h:149
MatType::ValueType lInfinityNorm(const MatType &matrix)
Return the L∞ norm of an N×N matrix.
Definition: Mat.h:925
Tolerance for floating-point comparison.
Definition: Math.h:90
void powSolve(const MatType &aA, MatType &aB, double aPower, double aTol=0.01)
Definition: Mat.h:827
MatType rotation(const Vec3< ValueType1 > &_v1, const Vec3< ValueType2 > &_v2, typename MatType::value_type eps=static_cast< typename MatType::value_type >(1.0e-8))
Return a rotation matrix that maps v1 onto v2 about the cross product of v1 and v2.
Definition: Mat.h:507
T value_type
Definition: Mat.h:29
T & z()
Definition: Quat.h:203
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
MatType shear(Axis axis0, Axis axis1, typename MatType::value_type shear)
Set the matrix to a shear along axis0 by a fraction of axis1.
Definition: Mat.h:693
T angle(const Vec2< T > &v1, const Vec2< T > &v2)
Definition: Vec2.h:445
bool isInfinite() const
True if an Inf is present in this matrix.
Definition: Mat.h:141
Mat & operator=(Mat const &src)
Definition: Mat.h:49
bool isFinite(const float x)
Return true if x is finite.
Definition: Math.h:319
void sqrtSolve(const MatType &aA, MatType &aB, double aTol=0.01)
Solve for A=B*B, given A.
Definition: Mat.h:802
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:154
bool normalize(T eps=T(1.0e-7))
this = normalized this
Definition: Vec3.h:360
bool isDiagonal(const MatType &mat)
Determine if a matrix is diagonal.
Definition: Mat.h:907
void read(std::istream &is)
Definition: Mat.h:119
MatType scale(const Vec3< typename MatType::value_type > &s)
Return a matrix that scales by s.
Definition: Mat.h:620
Vec3< T > unit(T eps=0) const
return normalized this, throws if null vector
Definition: Vec3.h:372
bool isNan() const
True if a Nan is present in this matrix.
Definition: Mat.h:133