HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
matrix4d.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 // matrix4.template.h file to make changes.
27 
28 #ifndef GF_MATRIX4D_H
29 #define GF_MATRIX4D_H
30 
31 /// \file gf/matrix4d.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/vec4d.h"
39 #include "pxr/base/gf/traits.h"
41 #include "pxr/base/gf/limits.h"
42 #include "pxr/base/gf/math.h"
43 #include "pxr/base/gf/vec3d.h"
44 
45 #include <hboost/functional/hash.hpp>
46 
47 #include <iosfwd>
48 #include <vector>
49 
51 
52 template <>
53 struct GfIsGfMatrix<class GfMatrix4d> { static const bool value = true; };
54 
55 class GfMatrix4d;
56 class GfMatrix4f;
57 class GfQuatd;
58 class GfRotation;
59 class GfMatrix3d;
60 
61 /// \class GfMatrix4d
62 /// \ingroup group_gf_LinearAlgebra
63 ///
64 /// Stores a 4x4 matrix of \c double elements. A basic type.
65 ///
66 /// Matrices are defined to be in row-major order, so <c>matrix[i][j]</c>
67 /// indexes the element in the \e i th row and the \e j th column.
68 ///
69 /// <h3>3D Transformations</h3>
70 ///
71 /// The following methods interpret a GfMatrix4d as a 3D
72 /// transformation: SetRotate(), SetScale(), SetTranslate(), SetLookAt(),
73 /// Factor(), ExtractTranslation(), ExtractRotation(), Transform(), TransformDir().
74 /// By convention, vectors are treated primarily as row vectors,
75 /// implying the following:
76 /// \li Transformation matrices are organized to deal with row
77 /// vectors, not column vectors. For example, the last row of a matrix
78 /// contains the translation amounts.
79 /// \li Each of the Set() methods below completely rewrites the
80 /// matrix; for example, SetTranslate() yields a matrix
81 /// which does nothing but translate.
82 /// \li When multiplying two transformation matrices, the matrix
83 /// on the left applies a more local transformation to a row
84 /// vector. For example, if R represents a rotation
85 /// matrix and T represents a translation matrix, the
86 /// product R*T will rotate a row vector, then translate
87 /// it.
89 {
90 public:
91  typedef double ScalarType;
92 
93  static const size_t numRows = 4;
94  static const size_t numColumns = 4;
95 
96  /// Default constructor. Leaves the matrix component values undefined.
97  GfMatrix4d() = default;
98 
99  /// Constructor. Initializes the matrix from 16 independent
100  /// \c double values, specified in row-major order. For example,
101  /// parameter \e m10 specifies the value in row 1 and column 0.
102  GfMatrix4d(double m00, double m01, double m02, double m03,
103  double m10, double m11, double m12, double m13,
104  double m20, double m21, double m22, double m23,
105  double m30, double m31, double m32, double m33) {
106  Set(m00, m01, m02, m03,
107  m10, m11, m12, m13,
108  m20, m21, m22, m23,
109  m30, m31, m32, m33);
110  }
111 
112  /// Constructor. Initializes the matrix from a 4x4 array
113  /// of \c double values, specified in row-major order.
114  GfMatrix4d(const double m[4][4]) {
115  Set(m);
116  }
117 
118  /// Constructor. Explicitly initializes the matrix to \e s times the
119  /// identity matrix.
120  explicit GfMatrix4d(double s) {
121  SetDiagonal(s);
122  }
123 
124  /// Constructor. Explicitly initializes the matrix to diagonal form,
125  /// with the \e i th element on the diagonal set to <c>v[i]</c>.
126  explicit GfMatrix4d(const GfVec4d& v) {
127  SetDiagonal(v);
128  }
129 
130  /// Constructor. Initialize the matrix from a vector of vectors of
131  /// double. The vector is expected to be 4x4. If it is
132  /// too big, only the first 4 rows and/or columns will be used.
133  /// If it is too small, uninitialized elements will be filled in with
134  /// the corresponding elements from an identity matrix.
135  ///
136  GF_API
137  explicit GfMatrix4d(const std::vector< std::vector<double> >& v);
138 
139  /// Constructor. Initialize the matrix from a vector of vectors of
140  /// float. The vector is expected to be 4x4. If it is
141  /// too big, only the first 4 rows and/or columns will be used.
142  /// If it is too small, uninitialized elements will be filled in with
143  /// the corresponding elements from an identity matrix.
144  ///
145  GF_API
146  explicit GfMatrix4d(const std::vector< std::vector<float> >& v);
147 
148  /// Constructor. Initialize the matrix from 4 row vectors of
149  /// double. Each vector is expected to length 4. If it is too
150  /// big, only the first 4 items will be used. If it is too small,
151  /// uninitialized elements will be filled in with the
152  /// corresponding elements from an identity matrix.
153  ///
154  GF_API
155  explicit GfMatrix4d(const std::vector<double>& r0,
156  const std::vector<double>& r1,
157  const std::vector<double>& r2,
158  const std::vector<double>& r3);
159 
160  /// Constructor. Initialize the matrix from 4 row vectors of
161  /// float. Each vector is expected to length 4. If it is too
162  /// big, only the first 4 items will be used. If it is too small,
163  /// uninitialized elements will be filled in with the
164  /// corresponding elements from an identity matrix.
165  ///
166  GF_API
167  explicit GfMatrix4d(const std::vector<float>& r0,
168  const std::vector<float>& r1,
169  const std::vector<float>& r2,
170  const std::vector<float>& r3);
171 
172  /// Constructor. Initializes a transformation matrix to perform the
173  /// indicated rotation and translation.
174  GF_API
176  const GfVec3d& translate);
177 
178  /// Constructor. Initializes a transformation matrix to perform the
179  /// indicated rotation and translation.
180  GF_API
181  GfMatrix4d(const GfMatrix3d& rotmx,
182  const GfVec3d& translate);
183  /// This explicit constructor converts a "float" matrix to a "double" matrix.
184  GF_API
185  explicit GfMatrix4d(const class GfMatrix4f& m);
186 
187  /// Sets a row of the matrix from a Vec4.
188  void SetRow(int i, const GfVec4d & v) {
189  _mtx[i][0] = v[0];
190  _mtx[i][1] = v[1];
191  _mtx[i][2] = v[2];
192  _mtx[i][3] = v[3];
193  }
194 
195  /// Sets a column of the matrix from a Vec4.
196  void SetColumn(int i, const GfVec4d & v) {
197  _mtx[0][i] = v[0];
198  _mtx[1][i] = v[1];
199  _mtx[2][i] = v[2];
200  _mtx[3][i] = v[3];
201  }
202 
203  /// Gets a row of the matrix as a Vec4.
204  GfVec4d GetRow(int i) const {
205  return GfVec4d(_mtx[i][0], _mtx[i][1], _mtx[i][2], _mtx[i][3]);
206  }
207 
208  /// Gets a column of the matrix as a Vec4.
209  GfVec4d GetColumn(int i) const {
210  return GfVec4d(_mtx[0][i], _mtx[1][i], _mtx[2][i], _mtx[3][i]);
211  }
212 
213  /// Sets the matrix from 16 independent \c double values,
214  /// specified in row-major order. For example, parameter \e m10 specifies
215  /// the value in row 1 and column 0.
216  GfMatrix4d& Set(double m00, double m01, double m02, double m03,
217  double m10, double m11, double m12, double m13,
218  double m20, double m21, double m22, double m23,
219  double m30, double m31, double m32, double m33) {
220  _mtx[0][0] = m00; _mtx[0][1] = m01; _mtx[0][2] = m02; _mtx[0][3] = m03;
221  _mtx[1][0] = m10; _mtx[1][1] = m11; _mtx[1][2] = m12; _mtx[1][3] = m13;
222  _mtx[2][0] = m20; _mtx[2][1] = m21; _mtx[2][2] = m22; _mtx[2][3] = m23;
223  _mtx[3][0] = m30; _mtx[3][1] = m31; _mtx[3][2] = m32; _mtx[3][3] = m33;
224  return *this;
225  }
226 
227  /// Sets the matrix from a 4x4 array of \c double
228  /// values, specified in row-major order.
229  GfMatrix4d& Set(const double m[4][4]) {
230  _mtx[0][0] = m[0][0];
231  _mtx[0][1] = m[0][1];
232  _mtx[0][2] = m[0][2];
233  _mtx[0][3] = m[0][3];
234  _mtx[1][0] = m[1][0];
235  _mtx[1][1] = m[1][1];
236  _mtx[1][2] = m[1][2];
237  _mtx[1][3] = m[1][3];
238  _mtx[2][0] = m[2][0];
239  _mtx[2][1] = m[2][1];
240  _mtx[2][2] = m[2][2];
241  _mtx[2][3] = m[2][3];
242  _mtx[3][0] = m[3][0];
243  _mtx[3][1] = m[3][1];
244  _mtx[3][2] = m[3][2];
245  _mtx[3][3] = m[3][3];
246  return *this;
247  }
248 
249  /// Sets the matrix to the identity matrix.
251  return SetDiagonal(1);
252  }
253 
254  /// Sets the matrix to zero.
256  return SetDiagonal(0);
257  }
258 
259  /// Sets the matrix to \e s times the identity matrix.
260  GF_API
261  GfMatrix4d& SetDiagonal(double s);
262 
263  /// Sets the matrix to have diagonal (<c>v[0], v[1], v[2], v[3]</c>).
264  GF_API
265  GfMatrix4d& SetDiagonal(const GfVec4d&);
266 
267  /// Fills a 4x4 array of \c double values with the values in
268  /// the matrix, specified in row-major order.
269  GF_API
270  double* Get(double m[4][4]) const;
271 
272  /// Returns raw access to components of matrix as an array of
273  /// \c double values. Components are in row-major order.
274  double* data() {
275  return _mtx.GetData();
276  }
277 
278  /// Returns const raw access to components of matrix as an array of
279  /// \c double values. Components are in row-major order.
280  const double* data() const {
281  return _mtx.GetData();
282  }
283 
284  /// Returns vector components as an array of \c double values.
285  double* GetArray() {
286  return _mtx.GetData();
287  }
288 
289  /// Returns vector components as a const array of \c double values.
290  const double* GetArray() const {
291  return _mtx.GetData();
292  }
293 
294  /// Accesses an indexed row \e i of the matrix as an array of 4 \c
295  /// double values so that standard indexing (such as <c>m[0][1]</c>)
296  /// works correctly.
297  double* operator [](int i) { return _mtx[i]; }
298 
299  /// Accesses an indexed row \e i of the matrix as an array of 4 \c
300  /// double values so that standard indexing (such as <c>m[0][1]</c>)
301  /// works correctly.
302  const double* operator [](int i) const { return _mtx[i]; }
303 
304  /// Hash.
305  friend inline size_t hash_value(GfMatrix4d const &m) {
306  int nElems = 4 * 4;
307  size_t h = 0;
308  const double *p = m.GetArray();
309  while (nElems--)
310  hboost::hash_combine(h, *p++);
311  return h;
312  }
313 
314  /// Tests for element-wise matrix equality. All elements must match
315  /// exactly for matrices to be considered equal.
316  GF_API
317  bool operator ==(const GfMatrix4d& m) const;
318 
319  /// Tests for element-wise matrix equality. All elements must match
320  /// exactly for matrices to be considered equal.
321  GF_API
322  bool operator ==(const GfMatrix4f& m) const;
323 
324  /// Tests for element-wise matrix inequality. All elements must match
325  /// exactly for matrices to be considered equal.
326  bool operator !=(const GfMatrix4d& m) const {
327  return !(*this == m);
328  }
329 
330  /// Tests for element-wise matrix inequality. All elements must match
331  /// exactly for matrices to be considered equal.
332  bool operator !=(const GfMatrix4f& m) const {
333  return !(*this == m);
334  }
335 
336  /// Returns the transpose of the matrix.
337  GF_API
338  GfMatrix4d GetTranspose() const;
339 
340  /// Returns the inverse of the matrix, or FLT_MAX * SetIdentity() if the
341  /// matrix is singular. (FLT_MAX is the largest value a \c float can have,
342  /// as defined by the system.) The matrix is considered singular if the
343  /// determinant is less than or equal to the optional parameter \e eps. If
344  /// \e det is non-null, <c>*det</c> is set to the determinant.
345  GF_API
346  GfMatrix4d GetInverse(double* det = NULL, double eps = 0) const;
347 
348  /// Returns the determinant of the matrix.
349  GF_API
350  double GetDeterminant() const;
351 
352  /// Sets a row of the matrix from a Vec3.
353  /// The fourth element of the row is ignored.
354  void SetRow3(int i, const GfVec3d & v) {
355  _mtx[i][0] = v[0];
356  _mtx[i][1] = v[1];
357  _mtx[i][2] = v[2];
358  }
359 
360  /// Gets a row of the matrix as a Vec3.
361  GfVec3d GetRow3(int i) const {
362  return GfVec3d(_mtx[i][0], _mtx[i][1], _mtx[i][2]);
363  }
364 
365  /// Returns the determinant of the upper 3x3 matrix. This method is useful
366  /// when the matrix describes a linear transformation such as a rotation or
367  /// scale because the other values in the 4x4 matrix are not important.
368  double GetDeterminant3() const {
369  return _GetDeterminant3(0, 1, 2, 0, 1, 2);
370  }
371 
372  /// Returns true, if the row vectors of the upper 3x3 matrix form an
373  /// orthogonal basis. Note they do not have to be unit length for this
374  /// test to return true.
375  bool HasOrthogonalRows3() const {
376  // XXX Should add GfAreOrthogonal(v0, v1, v2) (which also
377  // GfRotation::Decompose() could use).
378  GfVec3d axis0(GetRow3(0)), axis1(GetRow3(1)), axis2(GetRow3(2));
379  return (GfAbs(GfDot(axis0, axis1)) < GF_MIN_ORTHO_TOLERANCE &&
380  GfAbs(GfDot(axis0, axis2)) < GF_MIN_ORTHO_TOLERANCE &&
381  GfAbs(GfDot(axis1, axis2)) < GF_MIN_ORTHO_TOLERANCE);
382  }
383 
384  /// Makes the matrix orthonormal in place. This is an iterative method
385  /// that is much more stable than the previous cross/cross method. If the
386  /// iterative method does not converge, a warning is issued.
387  ///
388  /// Returns true if the iteration converged, false otherwise. Leaves any
389  /// translation part of the matrix unchanged. If \a issueWarning is true,
390  /// this method will issue a warning if the iteration does not converge,
391  /// otherwise it will be silent.
392  GF_API
393  bool Orthonormalize(bool issueWarning=true);
394 
395  /// Returns an orthonormalized copy of the matrix.
396  GF_API
397  GfMatrix4d GetOrthonormalized(bool issueWarning=true) const;
398 
399  /// Returns the sign of the determinant of the upper 3x3 matrix, i.e. 1
400  /// for a right-handed matrix, -1 for a left-handed matrix, and 0 for a
401  /// singular matrix.
402  GF_API
403  double GetHandedness() const;
404 
405  /// Returns true if the vectors in the upper 3x3 matrix form a
406  /// right-handed coordinate system.
407  bool IsRightHanded() const {
408  return GetHandedness() == 1.0;
409  }
410 
411  /// Returns true if the vectors in the upper 3x3 matrix form a left-handed
412  /// coordinate system.
413  bool IsLeftHanded() const {
414  return GetHandedness() == -1.0;
415  }
416 
417  /// Post-multiplies matrix \e m into this matrix.
418  GF_API
420 
421  /// Multiplies the matrix by a double.
422  GF_API
423  GfMatrix4d& operator *=(double);
424 
425  /// Returns the product of a matrix and a double.
426  friend GfMatrix4d operator *(const GfMatrix4d& m1, double d)
427  {
428  GfMatrix4d m = m1;
429  return m *= d;
430  }
431 
432  ///
433  // Returns the product of a matrix and a double.
434  friend GfMatrix4d operator *(double d, const GfMatrix4d& m)
435  {
436  return m * d;
437  }
438 
439  /// Adds matrix \e m to this matrix.
440  GF_API
442 
443  /// Subtracts matrix \e m from this matrix.
444  GF_API
446 
447  /// Returns the unary negation of matrix \e m.
448  GF_API
449  friend GfMatrix4d operator -(const GfMatrix4d& m);
450 
451  /// Adds matrix \e m2 to \e m1
452  friend GfMatrix4d operator +(const GfMatrix4d& m1, const GfMatrix4d& m2)
453  {
454  GfMatrix4d tmp(m1);
455  tmp += m2;
456  return tmp;
457  }
458 
459  /// Subtracts matrix \e m2 from \e m1.
460  friend GfMatrix4d operator -(const GfMatrix4d& m1, const GfMatrix4d& m2)
461  {
462  GfMatrix4d tmp(m1);
463  tmp -= m2;
464  return tmp;
465  }
466 
467  /// Multiplies matrix \e m1 by \e m2.
468  friend GfMatrix4d operator *(const GfMatrix4d& m1, const GfMatrix4d& m2)
469  {
470  GfMatrix4d tmp(m1);
471  tmp *= m2;
472  return tmp;
473  }
474 
475  /// Divides matrix \e m1 by \e m2 (that is, <c>m1 * inv(m2)</c>).
476  friend GfMatrix4d operator /(const GfMatrix4d& m1, const GfMatrix4d& m2)
477  {
478  return(m1 * m2.GetInverse());
479  }
480 
481  /// Returns the product of a matrix \e m and a column vector \e vec.
482  friend inline GfVec4d operator *(const GfMatrix4d& m, const GfVec4d& vec) {
483  return GfVec4d(vec[0] * m._mtx[0][0] + vec[1] * m._mtx[0][1] + vec[2] * m._mtx[0][2] + vec[3] * m._mtx[0][3],
484  vec[0] * m._mtx[1][0] + vec[1] * m._mtx[1][1] + vec[2] * m._mtx[1][2] + vec[3] * m._mtx[1][3],
485  vec[0] * m._mtx[2][0] + vec[1] * m._mtx[2][1] + vec[2] * m._mtx[2][2] + vec[3] * m._mtx[2][3],
486  vec[0] * m._mtx[3][0] + vec[1] * m._mtx[3][1] + vec[2] * m._mtx[3][2] + vec[3] * m._mtx[3][3]);
487  }
488 
489  /// Returns the product of row vector \e vec and a matrix \e m.
490  friend inline GfVec4d operator *(const GfVec4d &vec, const GfMatrix4d& m) {
491  return GfVec4d(vec[0] * m._mtx[0][0] + vec[1] * m._mtx[1][0] + vec[2] * m._mtx[2][0] + vec[3] * m._mtx[3][0],
492  vec[0] * m._mtx[0][1] + vec[1] * m._mtx[1][1] + vec[2] * m._mtx[2][1] + vec[3] * m._mtx[3][1],
493  vec[0] * m._mtx[0][2] + vec[1] * m._mtx[1][2] + vec[2] * m._mtx[2][2] + vec[3] * m._mtx[3][2],
494  vec[0] * m._mtx[0][3] + vec[1] * m._mtx[1][3] + vec[2] * m._mtx[2][3] + vec[3] * m._mtx[3][3]);
495  }
496 
497  /// Returns the product of a matrix \e m and a column vector \e vec.
498  /// Note that the return type is a \c GfVec4f.
499  GF_API
500  friend GfVec4f operator *(const GfMatrix4d& m, const GfVec4f& vec);
501 
502  /// Returns the product of row vector \e vec and a matrix \e m.
503  /// Note that the return type is a \c GfVec4f.
504  GF_API
505  friend GfVec4f operator *(const GfVec4f &vec, const GfMatrix4d& m);
506 
507  /// Sets matrix to specify a uniform scaling by \e scaleFactor.
508  GF_API
509  GfMatrix4d& SetScale(double scaleFactor);
510 
511  /// Returns the matrix with any scaling or shearing removed,
512  /// leaving only the rotation and translation.
513  /// If the matrix cannot be decomposed, returns the original matrix.
514  GF_API
516 
517  /// \name 3D Transformation Utilities
518  /// @{
519 
520  /// Sets the matrix to specify a rotation equivalent to \e rot,
521  /// and clears the translation.
522  GF_API
523  GfMatrix4d& SetRotate(const GfQuatd &rot);
524 
525  /// Sets the matrix to specify a rotation equivalent to \e rot,
526  /// without clearing the translation.
527  GF_API
529 
530  /// Sets the matrix to specify a rotation equivalent to \e rot,
531  /// and clears the translation.
532  GF_API
534 
535  /// Sets the matrix to specify a rotation equivalent to \e rot,
536  /// without clearing the translation.
537  GF_API
539 
540  /// Sets the matrix to specify a rotation equivalent to \e mx,
541  /// and clears the translation.
542  GF_API
543  GfMatrix4d& SetRotate(const GfMatrix3d &mx);
544 
545  /// Sets the matrix to specify a rotation equivalent to \e mx,
546  /// without clearing the translation.
547  GF_API
548  GfMatrix4d& SetRotateOnly(const GfMatrix3d &mx);
549 
550  /// Sets the matrix to specify a nonuniform scaling in x, y, and z by
551  /// the factors in vector \e scaleFactors.
552  GF_API
553  GfMatrix4d& SetScale(const GfVec3d &scaleFactors);
554 
555  /// Sets matrix to specify a translation by the vector \e trans,
556  /// and clears the rotation.
557  GF_API
559 
560  /// Sets matrix to specify a translation by the vector \e trans,
561  /// without clearing the rotation.
562  GF_API
564 
565  /// Sets matrix to specify a rotation by \e rotate and a
566  /// translation by \e translate.
567  GF_API
569  const GfVec3d& translate);
570 
571  /// Sets matrix to specify a rotation by \e rotmx and a
572  /// translation by \e translate.
573  GF_API
574  GfMatrix4d& SetTransform(const GfMatrix3d& rotmx,
575  const GfVec3d& translate);
576 
577  /// Sets the matrix to specify a viewing matrix from parameters
578  /// similar to those used by <c>gluLookAt(3G)</c>. \e eyePoint
579  /// represents the eye point in world space. \e centerPoint
580  /// represents the world-space center of attention. \e upDirection
581  /// is a vector indicating which way is up.
582  GF_API
583  GfMatrix4d& SetLookAt(const GfVec3d &eyePoint,
584  const GfVec3d &centerPoint,
585  const GfVec3d &upDirection);
586 
587  /// Sets the matrix to specify a viewing matrix from a world-space
588  /// \e eyePoint and a world-space rotation that rigidly rotates the
589  /// orientation from its canonical frame, which is defined to be
590  /// looking along the <c>-z</c> axis with the <c>+y</c> axis as the up
591  /// direction.
592  GF_API
593  GfMatrix4d& SetLookAt(const GfVec3d &eyePoint,
594  const GfRotation &orientation);
595 
596  /// Factors the matrix into 5 components:
597  /// \li <c>\e M = r * s * -r * u * t</c>
598  /// where
599  /// \li \e t is a translation.
600  /// \li \e u and \e r are rotations, and \e -r is the transpose
601  /// (inverse) of \e r. The \e u matrix may contain shear
602  /// information.
603  /// \li \e s is a scale.
604  /// Any projection information could be returned in matrix \e p,
605  /// but currently p is never modified.
606  ///
607  /// Returns \c false if the matrix is singular (as determined by \e eps).
608  /// In that case, any zero scales in \e s are clamped to \e eps
609  /// to allow computation of \e u.
610  GF_API
611  bool Factor(GfMatrix4d* r, GfVec3d* s, GfMatrix4d* u,
612  GfVec3d* t, GfMatrix4d* p,
613  double eps = 1e-10) const;
614 
615  /// Returns the translation part of the matrix, defined as the first three
616  /// elements of the last row.
618  return GfVec3d(_mtx[3][0], _mtx[3][1], _mtx[3][2]);
619  }
620 
621  /// Returns the rotation corresponding to this matrix. This works well
622  /// only if the matrix represents a rotation.
623  ///
624  /// For good results, consider calling Orthonormalize() before calling
625  /// this method.
626  GF_API
627  GfRotation ExtractRotation() const;
628 
629  /// Decompose the rotation corresponding to this matrix about 3 orthogonal
630  /// axes. If the axes are not orthogonal, warnings will be spewed.
631  ///
632  /// This is a convenience method that is equivalent to calling
633  /// ExtractRotation().Decompose().
634  GF_API
635  GfVec3d DecomposeRotation(const GfVec3d &axis0,
636  const GfVec3d &axis1,
637  const GfVec3d &axis2) const;
638 
639  /// Returns the rotation corresponding to this matrix. This works well
640  /// only if the matrix represents a rotation.
641  ///
642  /// For good results, consider calling Orthonormalize() before calling
643  /// this method.
644  GF_API
646 
647  /// Transforms the row vector \e vec by the matrix, returning the result.
648  /// This treats the vector as a 4-component vector whose fourth component
649  /// is 1.
650  GfVec3d Transform(const GfVec3d &vec) const {
651  return GfProject(GfVec4d(
652  vec[0] * _mtx[0][0] + vec[1] * _mtx[1][0] + vec[2] * _mtx[2][0] + _mtx[3][0],
653  vec[0] * _mtx[0][1] + vec[1] * _mtx[1][1] + vec[2] * _mtx[2][1] + _mtx[3][1],
654  vec[0] * _mtx[0][2] + vec[1] * _mtx[1][2] + vec[2] * _mtx[2][2] + _mtx[3][2],
655  vec[0] * _mtx[0][3] + vec[1] * _mtx[1][3] + vec[2] * _mtx[2][3] + _mtx[3][3]));
656  }
657 
658  /// Transforms the row vector \e vec by the matrix, returning the result.
659  /// This treats the vector as a 4-component vector whose fourth component
660  /// is 1. This is an overloaded method; it differs from the other version
661  /// in that it returns a different value type.
662  GfVec3f Transform(const GfVec3f &vec) const {
663  return GfVec3f(GfProject(GfVec4d(
664  vec[0] * _mtx[0][0] + vec[1] * _mtx[1][0] + vec[2] * _mtx[2][0] + _mtx[3][0],
665  vec[0] * _mtx[0][1] + vec[1] * _mtx[1][1] + vec[2] * _mtx[2][1] + _mtx[3][1],
666  vec[0] * _mtx[0][2] + vec[1] * _mtx[1][2] + vec[2] * _mtx[2][2] + _mtx[3][2],
667  vec[0] * _mtx[0][3] + vec[1] * _mtx[1][3] + vec[2] * _mtx[2][3] + _mtx[3][3])));
668  }
669 
670  /// Transforms row vector \e vec by the matrix, returning the result. This
671  /// treats the vector as a direction vector, so the translation
672  /// information in the matrix is ignored. That is, it treats the vector as
673  /// a 4-component vector whose fourth component is 0.
674  GfVec3d TransformDir(const GfVec3d &vec) const {
675  return GfVec3d(
676  vec[0] * _mtx[0][0] + vec[1] * _mtx[1][0] + vec[2] * _mtx[2][0],
677  vec[0] * _mtx[0][1] + vec[1] * _mtx[1][1] + vec[2] * _mtx[2][1],
678  vec[0] * _mtx[0][2] + vec[1] * _mtx[1][2] + vec[2] * _mtx[2][2]);
679  }
680 
681  /// Transforms row vector \e vec by the matrix, returning the result. This
682  /// treats the vector as a direction vector, so the translation
683  /// information in the matrix is ignored. That is, it treats the vector as
684  /// a 4-component vector whose fourth component is 0. This is an
685  /// overloaded method; it differs from the other version in that it
686  /// returns a different value type.
687  GfVec3f TransformDir(const GfVec3f &vec) const {
688  return GfVec3f(
689  vec[0] * _mtx[0][0] + vec[1] * _mtx[1][0] + vec[2] * _mtx[2][0],
690  vec[0] * _mtx[0][1] + vec[1] * _mtx[1][1] + vec[2] * _mtx[2][1],
691  vec[0] * _mtx[0][2] + vec[1] * _mtx[1][2] + vec[2] * _mtx[2][2]);
692  }
693 
694  /// Transforms the row vector \e vec by the matrix, returning the result.
695  /// This treats the vector as a 4-component vector whose fourth component
696  /// is 1 and ignores the fourth column of the matrix (i.e. assumes it is
697  /// (0, 0, 0, 1)).
698  GfVec3d TransformAffine(const GfVec3d &vec) const {
699  return GfVec3d(
700  vec[0] * _mtx[0][0] + vec[1] * _mtx[1][0] + vec[2] * _mtx[2][0] + _mtx[3][0],
701  vec[0] * _mtx[0][1] + vec[1] * _mtx[1][1] + vec[2] * _mtx[2][1] + _mtx[3][1],
702  vec[0] * _mtx[0][2] + vec[1] * _mtx[1][2] + vec[2] * _mtx[2][2] + _mtx[3][2]);
703  }
704 
705  /// Transforms the row vector \e vec by the matrix, returning the result.
706  /// This treats the vector as a 4-component vector whose fourth component
707  /// is 1 and ignores the fourth column of the matrix (i.e. assumes it is
708  /// (0, 0, 0, 1)).
709  GfVec3f TransformAffine(const GfVec3f &vec) const {
710  return GfVec3f(
711  vec[0] * _mtx[0][0] + vec[1] * _mtx[1][0] + vec[2] * _mtx[2][0] + _mtx[3][0],
712  vec[0] * _mtx[0][1] + vec[1] * _mtx[1][1] + vec[2] * _mtx[2][1] + _mtx[3][1],
713  vec[0] * _mtx[0][2] + vec[1] * _mtx[1][2] + vec[2] * _mtx[2][2] + _mtx[3][2]);
714  }
715  /// @}
716 
717 private:
718  /// Returns the determinant of the 3x3 submatrix specified by the three
719  /// given row and column indices (0-3 for each).
720  GF_API
721  double _GetDeterminant3(size_t row1, size_t row2, size_t row3,
722  size_t col1, size_t col2, size_t col3) const;
723 
724  /// Diagonalizes the upper 3x3 matrix of a matrix known to be symmetric.
725  void _Jacobi3(GfVec3d *eigenvalues, GfVec3d eigenvectors[3]) const;
726 
727  /// Set the 3x3 submatrix to the rotation given by a quaternion,
728  /// defined by the real component \p r and imaginary components \p i.
729  void _SetRotateFromQuat(double r, const GfVec3d& i);
730 
731 
732 private:
733  /// Matrix storage, in row-major order.
735 
736  // Friend declarations
737  friend class GfMatrix4f;
738 };
739 
740 
741 /// Tests for equality within a given tolerance, returning \c true if the
742 /// difference between each component of the matrix is less than or equal
743 /// to \p tolerance, or false otherwise.
744 GF_API
745 bool GfIsClose(GfMatrix4d const &m1, GfMatrix4d const &m2, double tolerance);
746 
747 /// Output a GfMatrix4d
748 /// \ingroup group_gf_DebuggingOutput
749 GF_API std::ostream& operator<<(std::ostream &, GfMatrix4d const &);
750 
752 
753 #endif // GF_MATRIX4D_H
const double * GetArray() const
Returns vector components as a const array of double values.
Definition: matrix4d.h:290
GfMatrix4d & SetIdentity()
Sets the matrix to the identity matrix.
Definition: matrix4d.h:250
GLdouble s
Definition: glew.h:1390
GF_API GfMatrix4d & SetRotate(const GfQuatd &rot)
GfVec3f GfProject(const GfVec4f &v)
Definition: homogeneous.h:65
friend GfMatrix4d operator*(const GfMatrix4d &m1, double d)
Returns the product of a matrix and a double.
Definition: matrix4d.h:426
GF_API double * Get(double m[4][4]) const
GF_API GfMatrix4d & operator*=(const GfMatrix4d &m)
Post-multiplies matrix m into this matrix.
GF_API bool Orthonormalize(bool issueWarning=true)
GF_API bool operator==(const GfMatrix4d &m) const
bool IsRightHanded() const
Definition: matrix4d.h:407
GfMatrix4d(double m00, double m01, double m02, double m03, double m10, double m11, double m12, double m13, double m20, double m21, double m22, double m23, double m30, double m31, double m32, double m33)
Definition: matrix4d.h:102
bool IsLeftHanded() const
Definition: matrix4d.h:413
GF_API GfMatrix4d & SetScale(double scaleFactor)
Sets matrix to specify a uniform scaling by scaleFactor.
static const size_t numColumns
Definition: matrix4d.h:94
*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:643
GF_API GfMatrix4d GetInverse(double *det=NULL, double eps=0) const
GfVec3f TransformAffine(const GfVec3f &vec) const
Definition: matrix4d.h:709
friend GfMatrix4d operator+(const GfMatrix4d &m1, const GfMatrix4d &m2)
Adds matrix m2 to m1.
Definition: matrix4d.h:452
GF_API GfMatrix4d & SetTranslate(const GfVec3d &trans)
GA_API const UT_StringHolder rot
void SetColumn(int i, const GfVec4d &v)
Sets a column of the matrix from a Vec4.
Definition: matrix4d.h:196
Definition: vec3f.h:63
GF_API GfMatrix4d & SetTransform(const GfRotation &rotate, const GfVec3d &translate)
Definition: vec4d.h:63
GF_API GfMatrix4d & operator-=(const GfMatrix4d &m)
Subtracts matrix m from this matrix.
GfHalf GfDot(GfHalf a, GfHalf b)
Definition: half.h:55
GF_API GfMatrix4d GetOrthonormalized(bool issueWarning=true) const
Returns an orthonormalized copy of the matrix.
const GLdouble * m
Definition: glew.h:9124
double * data()
Definition: matrix4d.h:274
GF_API GfMatrix4d & SetTranslateOnly(const GfVec3d &t)
const GLdouble * v
Definition: glew.h:1391
GF_API GfMatrix4d & operator+=(const GfMatrix4d &m)
Adds matrix m to this matrix.
GF_API GfVec3d DecomposeRotation(const GfVec3d &axis0, const GfVec3d &axis1, const GfVec3d &axis2) const
GfVec3d Transform(const GfVec3d &vec) const
Definition: matrix4d.h:650
bool HasOrthogonalRows3() const
Definition: matrix4d.h:375
GfVec3d TransformDir(const GfVec3d &vec) const
Definition: matrix4d.h:674
double GfAbs(double f)
Definition: math.h:112
friend GfMatrix4d operator/(const GfMatrix4d &m1, const GfMatrix4d &m2)
Divides matrix m1 by m2 (that is, m1 * inv(m2)).
Definition: matrix4d.h:476
GfMatrix4d()=default
Default constructor. Leaves the matrix component values undefined.
double * operator[](int i)
Definition: matrix4d.h:297
GF_API GfMatrix4d GetTranspose() const
Returns the transpose of the matrix.
GF_API GfMatrix4d & SetDiagonal(double s)
Sets the matrix to s times the identity matrix.
GfMatrix4d & Set(const double m[4][4])
Definition: matrix4d.h:229
double ScalarType
Definition: matrix4d.h:91
T * GetData()
Return a pointer to the start of all the data.
Definition: matrixData.h:50
GfMatrix4d(const double m[4][4])
Definition: matrix4d.h:114
GA_API const UT_StringHolder trans
static const size_t numRows
Definition: matrix4d.h:93
GfMatrix4d(const GfVec4d &v)
Definition: matrix4d.h:126
double GetDeterminant3() const
Definition: matrix4d.h:368
GF_API double GetDeterminant() const
Returns the determinant of the matrix.
ImageBuf OIIO_API rotate(const ImageBuf &src, float angle, string_view filtername=string_view(), float filterwidth=0.0f, bool recompute_roi=false, ROI roi={}, int nthreads=0)
GfVec4d GetRow(int i) const
Gets a row of the matrix as a Vec4.
Definition: matrix4d.h:204
GF_API friend GfMatrix4d operator-(const GfMatrix4d &m)
Returns the unary negation of matrix m.
GF_API double GetHandedness() const
double * GetArray()
Returns vector components as an array of double values.
Definition: matrix4d.h:285
GfVec3d TransformAffine(const GfVec3d &vec) const
Definition: matrix4d.h:698
GF_API GfRotation ExtractRotation() const
GF_API bool Factor(GfMatrix4d *r, GfVec3d *s, GfMatrix4d *u, GfVec3d *t, GfMatrix4d *p, double eps=1e-10) const
GLfloat GLfloat GLfloat GLfloat h
Definition: glew.h:8011
GF_API GfMatrix4d & SetLookAt(const GfVec3d &eyePoint, const GfVec3d &centerPoint, const GfVec3d &upDirection)
friend size_t hash_value(GfMatrix4d const &m)
Hash.
Definition: matrix4d.h:305
GfMatrix4d(double s)
Definition: matrix4d.h:120
GF_API GfMatrix3d ExtractRotationMatrix() const
GF_API bool GfIsClose(GfMatrix4d const &m1, GfMatrix4d const &m2, double tolerance)
GF_API std::ostream & operator<<(std::ostream &, GfMatrix4d const &)
GfMatrix4d & Set(double m00, double m01, double m02, double m03, double m10, double m11, double m12, double m13, double m20, double m21, double m22, double m23, double m30, double m31, double m32, double m33)
Definition: matrix4d.h:216
Definition: vec4f.h:63
GLfloat GLfloat p
Definition: glew.h:16321
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1245
GfVec3f Transform(const GfVec3f &vec) const
Definition: matrix4d.h:662
GfVec3f TransformDir(const GfVec3f &vec) const
Definition: matrix4d.h:687
GF_API GfMatrix4d & SetRotateOnly(const GfQuatd &rot)
const double * data() const
Definition: matrix4d.h:280
void SetRow(int i, const GfVec4d &v)
Sets a row of the matrix from a Vec4.
Definition: matrix4d.h:188
GLdouble GLdouble GLdouble r
Definition: glew.h:1406
Definition: vec3d.h:63
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:83
GF_API GfMatrix4d RemoveScaleShear() const
void SetRow3(int i, const GfVec3d &v)
Definition: matrix4d.h:354
Definition: quatd.h:60
PUGI__FN char_t * translate(char_t *buffer, const char_t *from, const char_t *to, size_t to_length)
Definition: pugixml.cpp:8352
GfVec3d ExtractTranslation() const
Definition: matrix4d.h:617
GfVec4d GetColumn(int i) const
Gets a column of the matrix as a Vec4.
Definition: matrix4d.h:209
GLsizei const GLfloat * value
Definition: glew.h:1849
GfMatrix4d & SetZero()
Sets the matrix to zero.
Definition: matrix4d.h:255
GLdouble GLdouble t
Definition: glew.h:1398
bool operator!=(const GfMatrix4d &m) const
Definition: matrix4d.h:326
GfVec3d GetRow3(int i) const
Gets a row of the matrix as a Vec3.
Definition: matrix4d.h:361
#define GF_MIN_ORTHO_TOLERANCE
Definition: limits.h:39
#define GF_API
Definition: api.h:40