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