quaternion.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 #ifndef PXR_BASE_GF_QUATERNION_H
25 #define PXR_BASE_GF_QUATERNION_H
26 
29 
30 #include "pxr/pxr.h"
31 #include "pxr/base/gf/api.h"
32 #include "pxr/base/gf/vec3d.h"
33 #include "pxr/base/tf/hash.h"
34 
35 #include <iosfwd>
36 
37 PXR_NAMESPACE_OPEN_SCOPE
38 
50 {
51  public:
52 
55  }
56 
63  explicit GfQuaternion(int realVal)
64  : _real(realVal), _imaginary(0)
65  {
66  }
67 
69  GfQuaternion(double real, const GfVec3d &imaginary)
70  : _real(real), _imaginary(imaginary) {
71  }
72 
74  void SetReal(double real) {
75  _real = real;
76  }
77 
79  void SetImaginary(const GfVec3d &imaginary) {
80  _imaginary = imaginary;
81  }
82 
84  double GetReal() const {
85  return _real;
86  }
87 
89  const GfVec3d & GetImaginary() const {
90  return _imaginary;
91  }
92 
95  static GfQuaternion GetZero() {
96  return GfQuaternion(0.0, GfVec3d(0.0, 0.0, 0.0));
97  }
98 
102  return GfQuaternion(1.0, GfVec3d(0.0, 0.0, 0.0));
103  }
104 
106  GF_API
107  double GetLength() const;
108 
112  GF_API
113  GfQuaternion GetNormalized(double eps = GF_MIN_VECTOR_LENGTH) const;
114 
118  GF_API
119  double Normalize(double eps = GF_MIN_VECTOR_LENGTH);
120 
122  GF_API
123  GfQuaternion GetInverse() const;
124 
126  friend inline size_t hash_value(const GfQuaternion &q) {
127  return TfHash::Combine(q.GetReal(), q.GetImaginary());
128  }
129 
132  bool operator ==(const GfQuaternion &q) const {
133  return (GetReal() == q.GetReal() &&
134  GetImaginary() == q.GetImaginary());
135  }
136 
139  bool operator !=(const GfQuaternion &q) const {
140  return ! (*this == q);
141  }
142 
144  GF_API
146 
148  GF_API
149  GfQuaternion & operator *=(double s);
150 
152  GfQuaternion & operator /=(double s) {
153  return (*this) *= 1.0 / s;
154  }
155 
158  _real += q._real;
159  _imaginary += q._imaginary;
160  return *this;
161  }
162 
165  _real -= q._real;
166  _imaginary -= q._imaginary;
167  return *this;
168  }
169 
172  const GfQuaternion &q2) {
173  GfQuaternion qt = q1;
174  return qt += q2;
175  }
176 
179  const GfQuaternion &q2) {
180  GfQuaternion qt = q1;
181  return qt -= q2;
182  }
183 
186  const GfQuaternion &q2) {
187  GfQuaternion qt = q1;
188  return qt *= q2;
189  }
190 
192  friend GfQuaternion operator *(const GfQuaternion &q, double s) {
193  GfQuaternion qt = q;
194  return qt *= s;
195  }
196 
198  friend GfQuaternion operator *(double s, const GfQuaternion &q) {
199  GfQuaternion qt = q;
200  return qt *= s;
201  }
202 
204  friend GfQuaternion operator /(const GfQuaternion &q, double s) {
205  GfQuaternion qt = q;
206  return qt /= s;
207  }
208 
214  GF_API
215  friend GfQuaternion GfSlerp(double alpha,
216  const GfQuaternion& q0,
217  const GfQuaternion& q1);
218 
219  // TODO Remove this legacy alias/overload.
220  friend GF_API GfQuaternion GfSlerp(const GfQuaternion& q0,
221  const GfQuaternion& q1,
222  double alpha);
223 
224  private:
226  double _real;
228  GfVec3d _imaginary;
229 
231  double _GetLengthSquared() const {
232  return (_real * _real + GfDot(_imaginary, _imaginary));
233  }
234 };
235 
236 // Friend functions must be declared.
237 GF_API GfQuaternion GfSlerp(double alpha, const GfQuaternion& q0, const GfQuaternion& q1);
238 GF_API GfQuaternion GfSlerp(const GfQuaternion& q0, const GfQuaternion& q1, double alpha);
239 
242 GF_API std::ostream& operator<<(std::ostream& out, const GfQuaternion& q);
243 
244 
246 inline double
247 GfDot(const GfQuaternion &q1, const GfQuaternion &q2) {
248  return (q1.GetReal() * q2.GetReal()) + GfDot(q1.GetImaginary(), q2.GetImaginary());
249 }
250 
251 PXR_NAMESPACE_CLOSE_SCOPE
252 
253 #endif // PXR_BASE_GF_QUATERNION_H
friend GfQuaternion operator *(const GfQuaternion &q1, const GfQuaternion &q2)
Returns the product of quaternions q1 and q2.
Definition: quaternion.h:185
friend GfQuaternion operator -(const GfQuaternion &q1, const GfQuaternion &q2)
Component-wise binary difference operator.
Definition: quaternion.h:178
GfQuaternion & operator/=(double s)
Scales this quaternion by 1 / s.
Definition: quaternion.h:152
friend size_t hash_value(const GfQuaternion &q)
Hash.
Definition: quaternion.h:126
GF_API GfQuaternion GfSlerp(double alpha, const GfQuaternion &q0, const GfQuaternion &q1)
bool operator !=(const GfQuaternion &q) const
Component-wise quaternion inequality test.
Definition: quaternion.h:139
GfQuaternion(int realVal)
This constructor initializes the real part to the argument and the imaginary parts to zero.
Definition: quaternion.h:63
static GfQuaternion GetZero()
Returns the zero quaternion, which has a real part of 0 and an imaginary part of (0,...
Definition: quaternion.h:95
friend GfQuaternion operator+(const GfQuaternion &q1, const GfQuaternion &q2)
Component-wise binary sum operator.
Definition: quaternion.h:171
GF_API friend GfQuaternion GfSlerp(double alpha, const GfQuaternion &q0, const GfQuaternion &q1)
Spherically interpolate between q0 and q1.
GfQuaternion(double real, const GfVec3d &imaginary)
This constructor initializes the real and imaginary parts.
Definition: quaternion.h:69
bool operator==(const GfQuaternion &q) const
Component-wise quaternion equality test.
Definition: quaternion.h:132
Basic type: complex number with scalar real part and vector imaginary part.
Definition: quaternion.h:49
GfQuaternion & operator+=(const GfQuaternion &q)
Component-wise unary sum operator.
Definition: quaternion.h:157
friend GfQuaternion operator *(const GfQuaternion &q, double s)
Returns the product of quaternion q and scalar s.
Definition: quaternion.h:192
double GfDot(const GfQuaternion &q1, const GfQuaternion &q2)
Returns the dot (inner) product of two quaternions.
Definition: quaternion.h:247
static size_t Combine(Args &&... args)
Produce a hash code by combining the hash codes of several objects.
Definition: hash.h:519
GF_API double Normalize(double eps=GF_MIN_VECTOR_LENGTH)
Normalizes this quaternion in place to unit length, returning the length before normalization.
static GfQuaternion GetIdentity()
Returns the identity quaternion, which has a real part of 1 and an imaginary part of (0,...
Definition: quaternion.h:101
double GetReal() const
Returns the real part of the quaternion.
Definition: quaternion.h:84
friend GfQuaternion operator/(const GfQuaternion &q, double s)
Returns the product of quaternion q and scalar 1 / s.
Definition: quaternion.h:204
GfQuaternion & operator -=(const GfQuaternion &q)
Component-wise unary difference operator.
Definition: quaternion.h:164
GF_API std::ostream & operator<<(std::ostream &out, const GfQuaternion &q)
Output a GfQuaternion using the format (r + (x, y, z)).
GF_API double GetLength() const
Returns geometric length of this quaternion.
GF_API GfQuaternion & operator *=(const GfQuaternion &q)
Post-multiplies quaternion q into this quaternion.
Basic type for a vector of 3 double components.
Definition: vec3d.h:62
void SetReal(double real)
Sets the real part of the quaternion.
Definition: quaternion.h:74
void SetImaginary(const GfVec3d &imaginary)
Sets the imaginary part of the quaternion.
Definition: quaternion.h:79
const GfVec3d & GetImaginary() const
Returns the imaginary part of the quaternion.
Definition: quaternion.h:89
GfQuaternion()
The default constructor leaves the quaternion undefined.
Definition: quaternion.h:54
friend GfQuaternion operator *(double s, const GfQuaternion &q)
Returns the product of quaternion q and scalar s.
Definition: quaternion.h:198
GF_API GfQuaternion GetNormalized(double eps=GF_MIN_VECTOR_LENGTH) const
Returns a normalized (unit-length) version of this quaternion.
GF_API GfQuaternion GetInverse() const
Returns the inverse of this quaternion.
#define GF_MIN_VECTOR_LENGTH
This constant is used to determine whether the length of a vector is too small to handle accurately.
Definition: limits.h:34