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 
34 #include <boost/functional/hash.hpp>
35 
36 #include <iosfwd>
37 
38 PXR_NAMESPACE_OPEN_SCOPE
39 
51 {
52  public:
53 
56  }
57 
64  explicit GfQuaternion(int realVal)
65  : _real(realVal), _imaginary(0)
66  {
67  }
68 
70  GfQuaternion(double real, const GfVec3d &imaginary)
71  : _real(real), _imaginary(imaginary) {
72  }
73 
75  void SetReal(double real) {
76  _real = real;
77  }
78 
80  void SetImaginary(const GfVec3d &imaginary) {
81  _imaginary = imaginary;
82  }
83 
85  double GetReal() const {
86  return _real;
87  }
88 
90  const GfVec3d & GetImaginary() const {
91  return _imaginary;
92  }
93 
96  static GfQuaternion GetZero() {
97  return GfQuaternion(0.0, GfVec3d(0.0, 0.0, 0.0));
98  }
99 
103  return GfQuaternion(1.0, GfVec3d(0.0, 0.0, 0.0));
104  }
105 
107  GF_API
108  double GetLength() const;
109 
113  GF_API
114  GfQuaternion GetNormalized(double eps = GF_MIN_VECTOR_LENGTH) const;
115 
119  GF_API
120  double Normalize(double eps = GF_MIN_VECTOR_LENGTH);
121 
123  GF_API
124  GfQuaternion GetInverse() const;
125 
127  friend inline size_t hash_value(const GfQuaternion &q) {
128  size_t h = 0;
129  boost::hash_combine(h, q.GetReal());
130  boost::hash_combine(h, q.GetImaginary());
131  return h;
132  }
133 
136  bool operator ==(const GfQuaternion &q) const {
137  return (GetReal() == q.GetReal() &&
138  GetImaginary() == q.GetImaginary());
139  }
140 
143  bool operator !=(const GfQuaternion &q) const {
144  return ! (*this == q);
145  }
146 
148  GF_API
150 
152  GF_API
153  GfQuaternion & operator *=(double s);
154 
156  GfQuaternion & operator /=(double s) {
157  return (*this) *= 1.0 / s;
158  }
159 
162  _real += q._real;
163  _imaginary += q._imaginary;
164  return *this;
165  }
166 
169  _real -= q._real;
170  _imaginary -= q._imaginary;
171  return *this;
172  }
173 
176  const GfQuaternion &q2) {
177  GfQuaternion qt = q1;
178  return qt += q2;
179  }
180 
183  const GfQuaternion &q2) {
184  GfQuaternion qt = q1;
185  return qt -= q2;
186  }
187 
190  const GfQuaternion &q2) {
191  GfQuaternion qt = q1;
192  return qt *= q2;
193  }
194 
196  friend GfQuaternion operator *(const GfQuaternion &q, double s) {
197  GfQuaternion qt = q;
198  return qt *= s;
199  }
200 
202  friend GfQuaternion operator *(double s, const GfQuaternion &q) {
203  GfQuaternion qt = q;
204  return qt *= s;
205  }
206 
208  friend GfQuaternion operator /(const GfQuaternion &q, double s) {
209  GfQuaternion qt = q;
210  return qt /= s;
211  }
212 
218  GF_API
219  friend GfQuaternion GfSlerp(double alpha,
220  const GfQuaternion& q0,
221  const GfQuaternion& q1);
222 
223  // TODO Remove this legacy alias/overload.
224  friend GF_API GfQuaternion GfSlerp(const GfQuaternion& q0,
225  const GfQuaternion& q1,
226  double alpha);
227 
228  private:
230  double _real;
232  GfVec3d _imaginary;
233 
235  double _GetLengthSquared() const {
236  return (_real * _real + GfDot(_imaginary, _imaginary));
237  }
238 };
239 
240 // Friend functions must be declared.
241 GF_API GfQuaternion GfSlerp(double alpha, const GfQuaternion& q0, const GfQuaternion& q1);
242 GF_API GfQuaternion GfSlerp(const GfQuaternion& q0, const GfQuaternion& q1, double alpha);
243 
246 GF_API std::ostream& operator<<(std::ostream& out, const GfQuaternion& q);
247 
248 
250 inline double
251 GfDot(const GfQuaternion &q1, const GfQuaternion &q2) {
252  return (q1.GetReal() * q2.GetReal()) + GfDot(q1.GetImaginary(), q2.GetImaginary());
253 }
254 
255 PXR_NAMESPACE_CLOSE_SCOPE
256 
257 #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:189
friend GfQuaternion operator -(const GfQuaternion &q1, const GfQuaternion &q2)
Component-wise binary difference operator.
Definition: quaternion.h:182
GfQuaternion & operator/=(double s)
Scales this quaternion by 1 / s.
Definition: quaternion.h:156
friend size_t hash_value(const GfQuaternion &q)
Hash.
Definition: quaternion.h:127
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:143
GfQuaternion(int realVal)
This constructor initializes the real part to the argument and the imaginary parts to zero.
Definition: quaternion.h:64
static GfQuaternion GetZero()
Returns the zero quaternion, which has a real part of 0 and an imaginary part of (0,...
Definition: quaternion.h:96
friend GfQuaternion operator+(const GfQuaternion &q1, const GfQuaternion &q2)
Component-wise binary sum operator.
Definition: quaternion.h:175
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:70
bool operator==(const GfQuaternion &q) const
Component-wise quaternion equality test.
Definition: quaternion.h:136
Basic type: complex number with scalar real part and vector imaginary part.
Definition: quaternion.h:50
GfQuaternion & operator+=(const GfQuaternion &q)
Component-wise unary sum operator.
Definition: quaternion.h:161
friend GfQuaternion operator *(const GfQuaternion &q, double s)
Returns the product of quaternion q and scalar s.
Definition: quaternion.h:196
double GfDot(const GfQuaternion &q1, const GfQuaternion &q2)
Returns the dot (inner) product of two quaternions.
Definition: quaternion.h:251
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:102
double GetReal() const
Returns the real part of the quaternion.
Definition: quaternion.h:85
friend GfQuaternion operator/(const GfQuaternion &q, double s)
Returns the product of quaternion q and scalar 1 / s.
Definition: quaternion.h:208
GfQuaternion & operator -=(const GfQuaternion &q)
Component-wise unary difference operator.
Definition: quaternion.h:168
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:63
void SetReal(double real)
Sets the real part of the quaternion.
Definition: quaternion.h:75
void SetImaginary(const GfVec3d &imaginary)
Sets the imaginary part of the quaternion.
Definition: quaternion.h:80
const GfVec3d & GetImaginary() const
Returns the imaginary part of the quaternion.
Definition: quaternion.h:90
GfQuaternion()
The default constructor leaves the quaternion undefined.
Definition: quaternion.h:55
friend GfQuaternion operator *(double s, const GfQuaternion &q)
Returns the product of quaternion q and scalar s.
Definition: quaternion.h:202
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