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