HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
quath.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 // quat.template.h file to make changes.
10 
11 #ifndef PXR_BASE_GF_QUATH_H
12 #define PXR_BASE_GF_QUATH_H
13 
14 /// \file gf/quath.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/vec3h.h"
21 #include "pxr/base/gf/traits.h"
22 #include "pxr/base/gf/half.h"
23 #include "pxr/base/tf/hash.h"
24 
25 #include <iosfwd>
26 
28 
29 template <>
30 struct GfIsGfQuat<class GfQuath> { static const bool value = true; };
31 
32 
33 /// Return the dot (inner) product of two quaternions.
34 GfHalf GfDot(const GfQuath& q1, const GfQuath& q2);
35 
36 
37 /// \class GfQuath
38 /// \ingroup group_gf_LinearAlgebra
39 ///
40 /// Basic type: a quaternion, a complex number with a real coefficient and
41 /// three imaginary coefficients, stored as a 3-vector.
42 ///
43 class GfQuath
44 {
45  public:
46  typedef GfHalf ScalarType;
48 
49  /// GfQuath value-initializes to zero and performs no default
50  /// initialization, like float or double.
51  GfQuath() = default;
52 
53  /// Initialize the real coefficient to \p realVal and the imaginary
54  /// coefficients to zero.
55  ///
56  /// Since quaternions typically must be normalized, reasonable values for
57  /// \p realVal are -1, 0, or 1. Other values are legal but are likely to
58  /// be meaningless.
59  ///
60  explicit GfQuath (GfHalf realVal) : _imaginary(0), _real(realVal) {}
61 
62  /// Initialize the real and imaginary coefficients.
64  : _imaginary(i, j, k), _real(real)
65  {
66  }
67 
68  /// Initialize the real and imaginary coefficients.
69  GfQuath(GfHalf real, const GfVec3h &imaginary)
70  : _imaginary(imaginary), _real(real)
71  {
72  }
73 
74  /// Construct from GfQuatd.
75  GF_API
76  explicit GfQuath(class GfQuatd const &other);
77  /// Construct from GfQuatf.
78  GF_API
79  explicit GfQuath(class GfQuatf const &other);
80 
81  /// Return the zero quaternion, with real coefficient 0 and an
82  /// imaginary coefficients all zero.
83  static GfQuath GetZero() { return GfQuath(0.0); }
84 
85  /// Return the identity quaternion, with real coefficient 1 and an
86  /// imaginary coefficients all zero.
87  static GfQuath GetIdentity() { return GfQuath(1.0); }
88 
89  /// Return the real coefficient.
90  GfHalf GetReal() const { return _real; }
91 
92  /// Set the real coefficient.
93  void SetReal(GfHalf real) { _real = real; }
94 
95  /// Return the imaginary coefficient.
96  const GfVec3h &GetImaginary() const { return _imaginary; }
97 
98  /// Set the imaginary coefficients.
99  void SetImaginary(const GfVec3h &imaginary) {
100  _imaginary = imaginary;
101  }
102 
103  /// Set the imaginary coefficients.
105  _imaginary.Set(i, j, k);
106  }
107 
108  /// Return geometric length of this quaternion.
109  GfHalf GetLength() const { return GfSqrt(_GetLengthSquared()); }
110 
111  /// length of this quaternion is smaller than \p eps, return the identity
112  /// quaternion.
113  GfQuath
115  GfQuath ret(*this);
116  ret.Normalize(eps);
117  return ret;
118  }
119 
120  /// Normalizes this quaternion in place to unit length, returning the
121  /// length before normalization. If the length of this quaternion is
122  /// smaller than \p eps, this sets the quaternion to identity.
123  GF_API
125 
126  /// Return this quaternion's conjugate, which is the quaternion with the
127  /// same real coefficient and negated imaginary coefficients.
129  return GfQuath(GetReal(), -GetImaginary());
130  }
131 
132  /// Return this quaternion's inverse, or reciprocal. This is the
133  /// quaternion's conjugate divided by it's squared length.
134  GfQuath GetInverse() const {
135  return GetConjugate() / _GetLengthSquared();
136  }
137 
138  /// Transform the GfVec3h point. If the quaternion is normalized,
139  /// the transformation is a rotation. Given a GfQuath q, q.Transform(point)
140  /// is equivalent to:
141  ///
142  /// (q * GfQuath(0, point) * q.GetInverse()).GetImaginary()
143  ///
144  /// but is more efficient.
145  GF_API
146  GfVec3h Transform(const GfVec3h& point) const;
147 
148  /// Hash.
149  friend inline size_t hash_value(const GfQuath &q) {
150  return TfHash::Combine(q.GetReal(), q.GetImaginary());
151  }
152 
153  /// Component-wise negation.
154  GfQuath operator-() const {
155  return GfQuath(-GetReal(), -GetImaginary());
156  }
157 
158  /// Component-wise quaternion equality test. The real and imaginary parts
159  /// must match exactly for quaternions to be considered equal.
160  bool operator==(const GfQuath &q) const {
161  return (GetReal() == q.GetReal() &&
162  GetImaginary() == q.GetImaginary());
163  }
164 
165  /// Component-wise quaternion inequality test. The real and imaginary
166  /// parts must match exactly for quaternions to be considered equal.
167  bool operator!=(const GfQuath &q) const {
168  return !(*this == q);
169  }
170 
171  /// Post-multiply quaternion \p q into this quaternion.
172  GF_API
173  GfQuath &operator *=(const GfQuath &q);
174 
175  /// Multiply this quaternion's coefficients by \p s.
177  _real *= s;
178  _imaginary *= s;
179  return *this;
180  }
181 
182  /// Divide this quaternion's coefficients by \p s.
184  _real /= s;
185  _imaginary /= s;
186  return *this;
187  }
188 
189  /// Add quaternion \p q to this quaternion.
191  _real += q._real;
192  _imaginary += q._imaginary;
193  return *this;
194  }
195 
196  /// Component-wise unary difference operator.
198  _real -= q._real;
199  _imaginary -= q._imaginary;
200  return *this;
201  }
202 
203  /// Component-wise binary sum operator.
204  friend GfQuath
205  operator +(const GfQuath &q1, const GfQuath &q2) {
206  return GfQuath(q1) += q2;
207  }
208 
209  /// Component-wise binary difference operator.
210  friend GfQuath
211  operator -(const GfQuath &q1, const GfQuath &q2) {
212  return GfQuath(q1) -= q2;
213  }
214 
215  /// Returns the product of quaternions \p q1 and \p q2.
216  friend GfQuath
217  operator *(const GfQuath &q1, const GfQuath &q2) {
218  return GfQuath(q1) *= q2;
219  }
220 
221  /// Returns the product of quaternion \p q and scalar \p s.
222  friend GfQuath
224  return GfQuath(q) *= s;
225  }
226 
227  /// Returns the product of quaternion \p q and scalar \p s.
228  friend GfQuath
230  return GfQuath(q) *= s;
231  }
232 
233  /// Returns the product of quaternion \p q and scalar 1 / \p s.
234  friend GfQuath
236  return GfQuath(q) /= s;
237  }
238 
239  private:
240  /// Imaginary part
241  GfVec3h _imaginary;
242 
243  /// Real part
244  GfHalf _real;
245 
246  /// Returns the square of the length
247  GfHalf
248  _GetLengthSquared() const {
249  return GfDot(*this, *this);
250  }
251 };
252 
253 /// Spherically linearly interpolate between \p q0 and \p q1.
254 ///
255 /// If the interpolant \p alpha is zero, then the result is \p q0, while
256 /// \p alpha of one yields \p q1.
258 GfSlerp(double alpha, const GfQuath& q0, const GfQuath& q1);
259 
261 GfSlerp(const GfQuath& q0, const GfQuath& q1, double alpha);
262 
263 inline GfHalf
264 GfDot(GfQuath const &q1, GfQuath const &q2) {
265  return GfDot(q1.GetImaginary(), q2.GetImaginary()) +
266  q1.GetReal()*q2.GetReal();
267 }
268 
269 /// Output a GfQuatd using the format (re, i, j, k)
270 /// \ingroup group_gf_DebuggingOutput
271 GF_API std::ostream& operator<<(std::ostream &, GfQuath const &);
272 
274 
275 #endif // PXR_BASE_GF_QUATH_H
Definition: quath.h:43
void SetImaginary(GfHalf i, GfHalf j, GfHalf k)
Set the imaginary coefficients.
Definition: quath.h:104
friend GfQuath operator+(const GfQuath &q1, const GfQuath &q2)
Component-wise binary sum operator.
Definition: quath.h:205
GfHalf GetLength() const
Return geometric length of this quaternion.
Definition: quath.h:109
double GfSqrt(double f)
Definition: math.h:187
*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
GfQuath()=default
bool operator!=(const GfQuath &q) const
Definition: quath.h:167
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
GLsizei const GLfloat * value
Definition: glcorearb.h:824
static GfQuath GetZero()
Definition: quath.h:83
GfQuath(GfHalf real, GfHalf i, GfHalf j, GfHalf k)
Initialize the real and imaginary coefficients.
Definition: quath.h:63
void SetImaginary(const GfVec3h &imaginary)
Set the imaginary coefficients.
Definition: quath.h:99
GfQuath(GfHalf realVal)
Definition: quath.h:60
GLdouble s
Definition: glad.h:3009
GF_API std::ostream & operator<<(std::ostream &, GfQuath const &)
friend GfQuath operator*(const GfQuath &q1, const GfQuath &q2)
Returns the product of quaternions q1 and q2.
Definition: quath.h:217
GfQuath & operator/=(GfHalf s)
Divide this quaternion's coefficients by s.
Definition: quath.h:183
GLdouble GLdouble GLdouble q
Definition: glad.h:2445
Definition: quatf.h:42
static GfQuath GetIdentity()
Definition: quath.h:87
bool operator==(const GfQuath &q) const
Definition: quath.h:160
GfQuath & operator-=(const GfQuath &q)
Component-wise unary difference operator.
Definition: quath.h:197
GfHalf GetReal() const
Return the real coefficient.
Definition: quath.h:90
GfQuath(GfHalf real, const GfVec3h &imaginary)
Initialize the real and imaginary coefficients.
Definition: quath.h:69
const GfVec3h & GetImaginary() const
Return the imaginary coefficient.
Definition: quath.h:96
friend size_t hash_value(const GfQuath &q)
Hash.
Definition: quath.h:149
friend GfQuath operator/(const GfQuath &q, GfHalf s)
Returns the product of quaternion q and scalar 1 / s.
Definition: quath.h:235
GfQuath GetConjugate() const
Definition: quath.h:128
GF_API GfVec3h Transform(const GfVec3h &point) const
GLfloat GLfloat GLfloat alpha
Definition: glcorearb.h:112
GfQuath GetInverse() const
Definition: quath.h:134
GF_API GfHalf Normalize(GfHalf eps=GF_MIN_VECTOR_LENGTH)
GLint j
Definition: glad.h:2733
GfVec3h ImaginaryType
Definition: quath.h:47
static size_t Combine(Args &&...args)
Produce a hash code by combining the hash codes of several objects.
Definition: hash.h:487
GfQuath operator-() const
Component-wise negation.
Definition: quath.h:154
GfHalf GfDot(const GfQuath &q1, const GfQuath &q2)
Return the dot (inner) product of two quaternions.
Definition: quath.h:264
GF_API GfQuath GfSlerp(double alpha, const GfQuath &q0, const GfQuath &q1)
void SetReal(GfHalf real)
Set the real coefficient.
Definition: quath.h:93
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
GfQuath GetNormalized(GfHalf eps=GF_MIN_VECTOR_LENGTH) const
Definition: quath.h:114
Definition: quatd.h:42
GF_API GfQuath & operator*=(const GfQuath &q)
Post-multiply quaternion q into this quaternion.
GfHalf ScalarType
Definition: quath.h:46
Definition: vec3h.h:46
GfVec3h & Set(GfHalf s0, GfHalf s1, GfHalf s2)
Set all elements with passed arguments.
Definition: vec3h.h:114
#define GF_MIN_VECTOR_LENGTH
Definition: limits.h:17
#define GF_API
Definition: api.h:23
GfQuath & operator+=(const GfQuath &q)
Add quaternion q to this quaternion.
Definition: quath.h:190