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