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