HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
matrix3d.h
Go to the documentation of this file.
1 //
2 // Copyright 2016 Pixar
3 //
4 // Licensed under the terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 ////////////////////////////////////////////////////////////////////////
8 // This file is generated by a script. Do not edit directly. Edit the
9 // matrix3.template.h file to make changes.
10 
11 #ifndef PXR_BASE_GF_MATRIX3D_H
12 #define PXR_BASE_GF_MATRIX3D_H
13 
14 /// \file gf/matrix3d.h
15 /// \ingroup group_gf_LinearAlgebra
16 
17 #include "pxr/pxr.h"
18 #include "pxr/base/gf/api.h"
19 #include "pxr/base/gf/declare.h"
20 #include "pxr/base/gf/matrixData.h"
21 #include "pxr/base/gf/vec3d.h"
22 #include "pxr/base/gf/traits.h"
23 #include "pxr/base/tf/hash.h"
24 
25 #include <iosfwd>
26 #include <vector>
27 
29 
30 template <>
31 struct GfIsGfMatrix<class GfMatrix3d> { static const bool value = true; };
32 
33 class GfMatrix3d;
34 class GfMatrix3f;
35 class GfRotation;
36 class GfQuaternion;
37 class GfQuatd;
38 
39 /// \class GfMatrix3d
40 /// \ingroup group_gf_LinearAlgebra
41 ///
42 /// Stores a 3x3 matrix of \c double elements. A basic type.
43 ///
44 /// Matrices are defined to be in row-major order, so <c>matrix[i][j]</c>
45 /// indexes the element in the \e i th row and the \e j th column.
46 ///
47 /// <h3>3D Transformations</h3>
48 ///
49 /// Three methods, SetRotate(), SetScale(), and ExtractRotation(), interpret
50 /// a GfMatrix3d as a 3D transformation. By convention, vectors are treated
51 /// primarily as row vectors, implying the following:
52 ///
53 /// \li Transformation matrices are organized to deal with row
54 /// vectors, not column vectors.
55 /// \li Each of the Set() methods in this class completely rewrites the
56 /// matrix; for example, SetRotate() yields a matrix
57 /// which does nothing but rotate.
58 /// \li When multiplying two transformation matrices, the matrix
59 /// on the left applies a more local transformation to a row
60 /// vector. For example, if R represents a rotation
61 /// matrix and S represents a scale matrix, the
62 /// product R*S will rotate a row vector, then scale
63 /// it.
65 {
66 public:
67  typedef double ScalarType;
68 
69  static const size_t numRows = 3;
70  static const size_t numColumns = 3;
71 
72  /// GfMatrix3d value-initializes to zero and performs no default
73  /// initialization, like float or double.
74  GfMatrix3d() = default;
75 
76  /// Constructor. Initializes the matrix from 9 independent
77  /// \c double values, specified in row-major order. For example,
78  /// parameter \e m10 specifies the value in row 1 and column 0.
79  GfMatrix3d(double m00, double m01, double m02,
80  double m10, double m11, double m12,
81  double m20, double m21, double m22) {
82  Set(m00, m01, m02,
83  m10, m11, m12,
84  m20, m21, m22);
85  }
86 
87  /// Constructor. Initializes the matrix from a 3x3 array
88  /// of \c double values, specified in row-major order.
89  GfMatrix3d(const double m[3][3]) {
90  Set(m);
91  }
92 
93  /// Constructor. Explicitly initializes the matrix to \e s times the
94  /// identity matrix.
95  explicit GfMatrix3d(double s) {
96  SetDiagonal(s);
97  }
98 
99  /// This explicit constructor initializes the matrix to \p s times
100  /// the identity matrix.
101  explicit GfMatrix3d(int s) {
102  SetDiagonal(s);
103  }
104 
105  /// Constructor. Explicitly initializes the matrix to diagonal form,
106  /// with the \e i th element on the diagonal set to <c>v[i]</c>.
107  explicit GfMatrix3d(const GfVec3d& v) {
108  SetDiagonal(v);
109  }
110 
111  /// Constructor. Initialize the matrix from a vector of vectors of
112  /// double. The vector is expected to be 3x3. If it is
113  /// too big, only the first 3 rows and/or columns will be used.
114  /// If it is too small, uninitialized elements will be filled in with
115  /// the corresponding elements from an identity matrix.
116  ///
117  GF_API
118  explicit GfMatrix3d(const std::vector< std::vector<double> >& v);
119 
120  /// Constructor. Initialize the matrix from a vector of vectors of
121  /// float. The vector is expected to be 3x3. If it is
122  /// too big, only the first 3 rows and/or columns will be used.
123  /// If it is too small, uninitialized elements will be filled in with
124  /// the corresponding elements from an identity matrix.
125  ///
126  GF_API
127  explicit GfMatrix3d(const std::vector< std::vector<float> >& v);
128 
129  /// Constructor. Initialize matrix from rotation.
130  GF_API
131  GfMatrix3d(const GfRotation& rot);
132 
133  /// Constructor. Initialize matrix from a quaternion.
134  GF_API
135  explicit GfMatrix3d(const GfQuatd& rot);
136 
137  /// This explicit constructor converts a "float" matrix to a "double" matrix.
138  GF_API
139  explicit GfMatrix3d(const class GfMatrix3f& m);
140 
141  /// Sets a row of the matrix from a Vec3.
142  void SetRow(int i, const GfVec3d & v) {
143  _mtx[i][0] = v[0];
144  _mtx[i][1] = v[1];
145  _mtx[i][2] = v[2];
146  }
147 
148  /// Sets a column of the matrix from a Vec3.
149  void SetColumn(int i, const GfVec3d & v) {
150  _mtx[0][i] = v[0];
151  _mtx[1][i] = v[1];
152  _mtx[2][i] = v[2];
153  }
154 
155  /// Gets a row of the matrix as a Vec3.
156  GfVec3d GetRow(int i) const {
157  return GfVec3d(_mtx[i][0], _mtx[i][1], _mtx[i][2]);
158  }
159 
160  /// Gets a column of the matrix as a Vec3.
161  GfVec3d GetColumn(int i) const {
162  return GfVec3d(_mtx[0][i], _mtx[1][i], _mtx[2][i]);
163  }
164 
165  /// Sets the matrix from 9 independent \c double values,
166  /// specified in row-major order. For example, parameter \e m10 specifies
167  /// the value in row 1 and column 0.
168  GfMatrix3d& Set(double m00, double m01, double m02,
169  double m10, double m11, double m12,
170  double m20, double m21, double m22) {
171  _mtx[0][0] = m00; _mtx[0][1] = m01; _mtx[0][2] = m02;
172  _mtx[1][0] = m10; _mtx[1][1] = m11; _mtx[1][2] = m12;
173  _mtx[2][0] = m20; _mtx[2][1] = m21; _mtx[2][2] = m22;
174  return *this;
175  }
176 
177  /// Sets the matrix from a 3x3 array of \c double
178  /// values, specified in row-major order.
179  GfMatrix3d& Set(const double m[3][3]) {
180  _mtx[0][0] = m[0][0];
181  _mtx[0][1] = m[0][1];
182  _mtx[0][2] = m[0][2];
183  _mtx[1][0] = m[1][0];
184  _mtx[1][1] = m[1][1];
185  _mtx[1][2] = m[1][2];
186  _mtx[2][0] = m[2][0];
187  _mtx[2][1] = m[2][1];
188  _mtx[2][2] = m[2][2];
189  return *this;
190  }
191 
192  /// Sets the matrix to the identity matrix.
194  return SetDiagonal(1);
195  }
196 
197  /// Sets the matrix to zero.
199  return SetDiagonal(0);
200  }
201 
202  /// Sets the matrix to \e s times the identity matrix.
203  GF_API
204  GfMatrix3d& SetDiagonal(double s);
205 
206  /// Sets the matrix to have diagonal (<c>v[0], v[1], v[2]</c>).
207  GF_API
208  GfMatrix3d& SetDiagonal(const GfVec3d&);
209 
210  /// Fills a 3x3 array of \c double values with the values in
211  /// the matrix, specified in row-major order.
212  GF_API
213  double* Get(double m[3][3]) const;
214 
215  /// Returns raw access to components of matrix as an array of
216  /// \c double values. Components are in row-major order.
217  double* data() {
218  return _mtx.GetData();
219  }
220 
221  /// Returns const raw access to components of matrix as an array of
222  /// \c double values. Components are in row-major order.
223  const double* data() const {
224  return _mtx.GetData();
225  }
226 
227  /// Returns vector components as an array of \c double values.
228  double* GetArray() {
229  return _mtx.GetData();
230  }
231 
232  /// Returns vector components as a const array of \c double values.
233  const double* GetArray() const {
234  return _mtx.GetData();
235  }
236 
237  /// Accesses an indexed row \e i of the matrix as an array of 3 \c
238  /// double values so that standard indexing (such as <c>m[0][1]</c>)
239  /// works correctly.
240  double* operator [](int i) { return _mtx[i]; }
241 
242  /// Accesses an indexed row \e i of the matrix as an array of 3 \c
243  /// double values so that standard indexing (such as <c>m[0][1]</c>)
244  /// works correctly.
245  const double* operator [](int i) const { return _mtx[i]; }
246 
247  /// Hash.
248  friend inline size_t hash_value(GfMatrix3d const &m) {
249  return TfHash::Combine(
250  m._mtx[0][0],
251  m._mtx[0][1],
252  m._mtx[0][2],
253  m._mtx[1][0],
254  m._mtx[1][1],
255  m._mtx[1][2],
256  m._mtx[2][0],
257  m._mtx[2][1],
258  m._mtx[2][2]
259  );
260  }
261 
262  /// Tests for element-wise matrix equality. All elements must match
263  /// exactly for matrices to be considered equal.
264  GF_API
265  bool operator ==(const GfMatrix3d& m) const;
266 
267  /// Tests for element-wise matrix equality. All elements must match
268  /// exactly for matrices to be considered equal.
269  GF_API
270  bool operator ==(const GfMatrix3f& m) const;
271 
272  /// Tests for element-wise matrix inequality. All elements must match
273  /// exactly for matrices to be considered equal.
274  bool operator !=(const GfMatrix3d& m) const {
275  return !(*this == m);
276  }
277 
278  /// Tests for element-wise matrix inequality. All elements must match
279  /// exactly for matrices to be considered equal.
280  bool operator !=(const GfMatrix3f& m) const {
281  return !(*this == m);
282  }
283 
284  /// Returns the transpose of the matrix.
285  GF_API
286  GfMatrix3d GetTranspose() const;
287 
288  /// Returns the inverse of the matrix, or FLT_MAX * SetIdentity() if the
289  /// matrix is singular. (FLT_MAX is the largest value a \c float can have,
290  /// as defined by the system.) The matrix is considered singular if the
291  /// determinant is less than or equal to the optional parameter \e eps. If
292  /// \e det is non-null, <c>*det</c> is set to the determinant.
293  GF_API
294  GfMatrix3d GetInverse(double* det = NULL, double eps = 0) const;
295 
296  /// Returns the determinant of the matrix.
297  GF_API
298  double GetDeterminant() const;
299 
300  /// Makes the matrix orthonormal in place. This is an iterative method that
301  /// is much more stable than the previous cross/cross method. If the
302  /// iterative method does not converge, a warning is issued.
303  ///
304  /// Returns true if the iteration converged, false otherwise. Leaves any
305  /// translation part of the matrix unchanged. If \a issueWarning is true,
306  /// this method will issue a warning if the iteration does not converge,
307  /// otherwise it will be silent.
308  GF_API
309  bool Orthonormalize(bool issueWarning=true);
310 
311  /// Returns an orthonormalized copy of the matrix.
312  GF_API
313  GfMatrix3d GetOrthonormalized(bool issueWarning=true) const;
314 
315  /// Returns the sign of the determinant of the matrix, i.e. 1 for a
316  /// right-handed matrix, -1 for a left-handed matrix, and 0 for a
317  /// singular matrix.
318  GF_API
319  double GetHandedness() const;
320 
321  /// Returns true if the vectors in the matrix form a right-handed
322  /// coordinate system.
323  bool IsRightHanded() const {
324  return GetHandedness() == 1.0;
325  }
326 
327  /// Returns true if the vectors in matrix form a left-handed
328  /// coordinate system.
329  bool IsLeftHanded() const {
330  return GetHandedness() == -1.0;
331  }
332 
333  /// Post-multiplies matrix \e m into this matrix.
334  GF_API
335  GfMatrix3d& operator *=(const GfMatrix3d& m);
336 
337  /// Multiplies the matrix by a double.
338  GF_API
339  GfMatrix3d& operator *=(double);
340 
341  /// Returns the product of a matrix and a double.
342  friend GfMatrix3d operator *(const GfMatrix3d& m1, double d)
343  {
344  GfMatrix3d m = m1;
345  return m *= d;
346  }
347 
348  ///
349  // Returns the product of a matrix and a double.
350  friend GfMatrix3d operator *(double d, const GfMatrix3d& m)
351  {
352  return m * d;
353  }
354 
355  /// Adds matrix \e m to this matrix.
356  GF_API
357  GfMatrix3d& operator +=(const GfMatrix3d& m);
358 
359  /// Subtracts matrix \e m from this matrix.
360  GF_API
361  GfMatrix3d& operator -=(const GfMatrix3d& m);
362 
363  /// Returns the unary negation of matrix \e m.
364  GF_API
365  friend GfMatrix3d operator -(const GfMatrix3d& m);
366 
367  /// Adds matrix \e m2 to \e m1
368  friend GfMatrix3d operator +(const GfMatrix3d& m1, const GfMatrix3d& m2)
369  {
370  GfMatrix3d tmp(m1);
371  tmp += m2;
372  return tmp;
373  }
374 
375  /// Subtracts matrix \e m2 from \e m1.
376  friend GfMatrix3d operator -(const GfMatrix3d& m1, const GfMatrix3d& m2)
377  {
378  GfMatrix3d tmp(m1);
379  tmp -= m2;
380  return tmp;
381  }
382 
383  /// Multiplies matrix \e m1 by \e m2.
384  friend GfMatrix3d operator *(const GfMatrix3d& m1, const GfMatrix3d& m2)
385  {
386  GfMatrix3d tmp(m1);
387  tmp *= m2;
388  return tmp;
389  }
390 
391  /// Divides matrix \e m1 by \e m2 (that is, <c>m1 * inv(m2)</c>).
392  friend GfMatrix3d operator /(const GfMatrix3d& m1, const GfMatrix3d& m2)
393  {
394  return(m1 * m2.GetInverse());
395  }
396 
397  /// Returns the product of a matrix \e m and a column vector \e vec.
398  friend inline GfVec3d operator *(const GfMatrix3d& m, const GfVec3d& vec) {
399  return GfVec3d(vec[0] * m._mtx[0][0] + vec[1] * m._mtx[0][1] + vec[2] * m._mtx[0][2],
400  vec[0] * m._mtx[1][0] + vec[1] * m._mtx[1][1] + vec[2] * m._mtx[1][2],
401  vec[0] * m._mtx[2][0] + vec[1] * m._mtx[2][1] + vec[2] * m._mtx[2][2]);
402  }
403 
404  /// Returns the product of row vector \e vec and a matrix \e m.
405  friend inline GfVec3d operator *(const GfVec3d &vec, const GfMatrix3d& m) {
406  return GfVec3d(vec[0] * m._mtx[0][0] + vec[1] * m._mtx[1][0] + vec[2] * m._mtx[2][0],
407  vec[0] * m._mtx[0][1] + vec[1] * m._mtx[1][1] + vec[2] * m._mtx[2][1],
408  vec[0] * m._mtx[0][2] + vec[1] * m._mtx[1][2] + vec[2] * m._mtx[2][2]);
409  }
410 
411  /// Sets matrix to specify a uniform scaling by \e scaleFactor.
412  GF_API
413  GfMatrix3d& SetScale(double scaleFactor);
414 
415  /// \name 3D Transformation Utilities
416  /// @{
417 
418  /// Sets the matrix to specify a rotation equivalent to \e rot.
419  GF_API
420  GfMatrix3d& SetRotate(const GfQuatd &rot);
421 
422  /// Sets the matrix to specify a rotation equivalent to \e rot.
423  GF_API
425 
426  /// Sets the matrix to specify a nonuniform scaling in x, y, and z by
427  /// the factors in vector \e scaleFactors.
428  GF_API
429  GfMatrix3d& SetScale(const GfVec3d &scaleFactors);
430 
431  /// Returns the rotation corresponding to this matrix. This works
432  /// well only if the matrix represents a rotation.
433  ///
434  /// For good results, consider calling Orthonormalize() before calling
435  /// this method.
436  GF_API
437  GfRotation ExtractRotation() const;
438 
439  /// Decompose the rotation corresponding to this matrix about 3
440  /// orthogonal axes. If the axes are not orthogonal, warnings
441  /// will be spewed.
442  ///
443  /// This is a convenience method that is equivalent to calling
444  /// ExtractRotation().Decompose().
445  GF_API
446  GfVec3d DecomposeRotation(const GfVec3d &axis0,
447  const GfVec3d &axis1,
448  const GfVec3d &axis2 ) const;
449 
450  /// Returns the quaternion corresponding to this matrix. This works
451  /// well only if the matrix represents a rotation.
452  ///
453  /// For good results, consider calling Orthonormalize() before calling
454  /// this method.
455  GF_API
457 
458  /// @}
459 
460 private:
461  /// Set the matrix to the rotation given by a quaternion,
462  /// defined by the real component \p r and imaginary components \p i.
463  void _SetRotateFromQuat(double r, const GfVec3d& i);
464 
465 
466 private:
467  /// Matrix storage, in row-major order.
469 
470  // Friend declarations
471  friend class GfMatrix3f;
472 };
473 
474 
475 /// Tests for equality within a given tolerance, returning \c true if the
476 /// difference between each component of the matrix is less than or equal
477 /// to \p tolerance, or false otherwise.
478 GF_API
479 bool GfIsClose(GfMatrix3d const &m1, GfMatrix3d const &m2, double tolerance);
480 
481 /// Output a GfMatrix3d
482 /// \ingroup group_gf_DebuggingOutput
483 GF_API std::ostream& operator<<(std::ostream &, GfMatrix3d const &);
484 
486 
487 #endif // PXR_BASE_GF_MATRIX3D_H
GF_API bool Orthonormalize(bool issueWarning=true)
bool IsLeftHanded() const
Definition: matrix3d.h:329
GfMatrix3d(const GfVec3d &v)
Definition: matrix3d.h:107
GF_API GfMatrix3d & operator-=(const GfMatrix3d &m)
Subtracts matrix m from this matrix.
GF_API GfMatrix3d & operator+=(const GfMatrix3d &m)
Adds matrix m to this matrix.
GF_API GfMatrix3d GetOrthonormalized(bool issueWarning=true) const
Returns an orthonormalized copy of the matrix.
*get result *(waiting if necessary)*A common idiom is to fire a bunch of sub tasks at the and then *wait for them to all complete We provide a helper class
Definition: thread.h:632
bool operator!=(const GfMatrix3d &m) const
Definition: matrix3d.h:274
GF_API double GetHandedness() const
GfMatrix3d(double s)
Definition: matrix3d.h:95
const GLdouble * v
Definition: glcorearb.h:837
double * GetArray()
Returns vector components as an array of double values.
Definition: matrix3d.h:228
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
GLsizei const GLfloat * value
Definition: glcorearb.h:824
GfMatrix3d(int s)
Definition: matrix3d.h:101
GA_API const UT_StringHolder rot
GLdouble s
Definition: glad.h:3009
GfMatrix3d()=default
GF_API GfMatrix3d & SetDiagonal(double s)
Sets the matrix to s times the identity matrix.
GF_API GfMatrix3d & SetRotate(const GfQuatd &rot)
Sets the matrix to specify a rotation equivalent to rot.
GfVec3d GetRow(int i) const
Gets a row of the matrix as a Vec3.
Definition: matrix3d.h:156
static const size_t numColumns
Definition: matrix3d.h:70
bool IsRightHanded() const
Definition: matrix3d.h:323
GF_API GfRotation ExtractRotation() const
friend GfMatrix3d operator*(const GfMatrix3d &m1, double d)
Returns the product of a matrix and a double.
Definition: matrix3d.h:342
GfMatrix3d(const double m[3][3])
Definition: matrix3d.h:89
T * GetData()
Return a pointer to the start of all the data.
Definition: matrixData.h:33
GF_API GfVec3d DecomposeRotation(const GfVec3d &axis0, const GfVec3d &axis1, const GfVec3d &axis2) const
const double * data() const
Definition: matrix3d.h:223
GfMatrix3d & SetZero()
Sets the matrix to zero.
Definition: matrix3d.h:198
GF_API GfQuaternion ExtractRotationQuaternion() const
GF_API double GetDeterminant() const
Returns the determinant of the matrix.
GF_API GfMatrix3d & operator*=(const GfMatrix3d &m)
Post-multiplies matrix m into this matrix.
friend GfMatrix3d operator+(const GfMatrix3d &m1, const GfMatrix3d &m2)
Adds matrix m2 to m1.
Definition: matrix3d.h:368
GF_API friend GfMatrix3d operator-(const GfMatrix3d &m)
Returns the unary negation of matrix m.
GF_API GfMatrix3d GetInverse(double *det=NULL, double eps=0) const
double * operator[](int i)
Definition: matrix3d.h:240
static size_t Combine(Args &&...args)
Produce a hash code by combining the hash codes of several objects.
Definition: hash.h:487
void SetColumn(int i, const GfVec3d &v)
Sets a column of the matrix from a Vec3.
Definition: matrix3d.h:149
double ScalarType
Definition: matrix3d.h:67
GF_API double * Get(double m[3][3]) const
GfVec3d GetColumn(int i) const
Gets a column of the matrix as a Vec3.
Definition: matrix3d.h:161
Definition: vec3d.h:45
GfMatrix3d & Set(const double m[3][3])
Definition: matrix3d.h:179
GfMatrix3d(double m00, double m01, double m02, double m10, double m11, double m12, double m20, double m21, double m22)
Definition: matrix3d.h:79
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
GfMatrix3d & Set(double m00, double m01, double m02, double m10, double m11, double m12, double m20, double m21, double m22)
Definition: matrix3d.h:168
GF_API bool GfIsClose(GfMatrix3d const &m1, GfMatrix3d const &m2, double tolerance)
GF_API bool operator==(const GfMatrix3d &m) const
const double * GetArray() const
Returns vector components as a const array of double values.
Definition: matrix3d.h:233
GLboolean r
Definition: glcorearb.h:1222
GF_API GfMatrix3d GetTranspose() const
Returns the transpose of the matrix.
Definition: quatd.h:42
GF_API std::ostream & operator<<(std::ostream &, GfMatrix3d const &)
GF_API GfMatrix3d & SetScale(double scaleFactor)
Sets matrix to specify a uniform scaling by scaleFactor.
static const size_t numRows
Definition: matrix3d.h:69
double * data()
Definition: matrix3d.h:217
#define GF_API
Definition: api.h:23
friend GfMatrix3d operator/(const GfMatrix3d &m1, const GfMatrix3d &m2)
Divides matrix m1 by m2 (that is, m1 * inv(m2)).
Definition: matrix3d.h:392
GfMatrix3d & SetIdentity()
Sets the matrix to the identity matrix.
Definition: matrix3d.h:193
friend size_t hash_value(GfMatrix3d const &m)
Hash.
Definition: matrix3d.h:248
void SetRow(int i, const GfVec3d &v)
Sets a row of the matrix from a Vec3.
Definition: matrix3d.h:142