All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 GF_QUATERNION_H
25 #define 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 
97  return GfQuaternion(1.0, GfVec3d(0.0, 0.0, 0.0));
98  }
99 
101  GF_API
102  double GetLength() const;
103 
107  GF_API
108  GfQuaternion GetNormalized(double eps = GF_MIN_VECTOR_LENGTH) const;
109 
113  GF_API
114  double Normalize(double eps = GF_MIN_VECTOR_LENGTH);
115 
117  GF_API
118  GfQuaternion GetInverse() const;
119 
121  friend inline size_t hash_value(const GfQuaternion &q) {
122  size_t h = 0;
123  boost::hash_combine(h, q.GetReal());
124  boost::hash_combine(h, q.GetImaginary());
125  return h;
126  }
127 
130  bool operator ==(const GfQuaternion &q) const {
131  return (GetReal() == q.GetReal() &&
132  GetImaginary() == q.GetImaginary());
133  }
134 
137  bool operator !=(const GfQuaternion &q) const {
138  return ! (*this == q);
139  }
140 
142  GF_API
144 
146  GF_API
147  GfQuaternion & operator *=(double s);
148 
150  GfQuaternion & operator /=(double s) {
151  return (*this) *= 1.0 / s;
152  }
153 
156  _real += q._real;
157  _imaginary += q._imaginary;
158  return *this;
159  }
160 
163  _real -= q._real;
164  _imaginary -= q._imaginary;
165  return *this;
166  }
167 
170  const GfQuaternion &q2) {
171  GfQuaternion qt = q1;
172  return qt += q2;
173  }
174 
177  const GfQuaternion &q2) {
178  GfQuaternion qt = q1;
179  return qt -= q2;
180  }
181 
184  const GfQuaternion &q2) {
185  GfQuaternion qt = q1;
186  return qt *= q2;
187  }
188 
190  friend GfQuaternion operator *(const GfQuaternion &q, double s) {
191  GfQuaternion qt = q;
192  return qt *= s;
193  }
194 
196  friend GfQuaternion operator *(double s, const GfQuaternion &q) {
197  GfQuaternion qt = q;
198  return qt *= s;
199  }
200 
202  friend GfQuaternion operator /(const GfQuaternion &q, double s) {
203  GfQuaternion qt = q;
204  return qt /= s;
205  }
206 
212  GF_API
213  friend GfQuaternion GfSlerp(double alpha,
214  const GfQuaternion& q0,
215  const GfQuaternion& q1);
216 
217  // TODO Remove this legacy alias/overload.
218  friend GF_API GfQuaternion GfSlerp(const GfQuaternion& q0,
219  const GfQuaternion& q1,
220  double alpha);
221 
222  private:
224  double _real;
226  GfVec3d _imaginary;
227 
229  double _GetLengthSquared() const {
230  return (_real * _real + GfDot(_imaginary, _imaginary));
231  }
232 };
233 
234 // Friend functions must be declared.
235 GF_API GfQuaternion GfSlerp(double alpha, const GfQuaternion& q0, const GfQuaternion& q1);
236 GF_API GfQuaternion GfSlerp(const GfQuaternion& q0, const GfQuaternion& q1, double alpha);
237 
240 GF_API std::ostream& operator<<(std::ostream& out, const GfQuaternion& q);
241 
242 PXR_NAMESPACE_CLOSE_SCOPE
243 
244 #endif // GF_QUATERNION_H
GfQuaternion & operator-=(const GfQuaternion &q)
Component-wise unary difference operator.
Definition: quaternion.h:162
GfQuaternion & operator/=(double s)
Scales this quaternion by 1 / s.
Definition: quaternion.h:150
friend size_t hash_value(const GfQuaternion &q)
Hash.
Definition: quaternion.h:121
GfQuaternion(int realVal)
This constructor initializes the real part to the argument and the imaginary parts to zero...
Definition: quaternion.h:64
friend GfQuaternion operator+(const GfQuaternion &q1, const GfQuaternion &q2)
Component-wise binary sum operator.
Definition: quaternion.h:169
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
Basic type: complex number with scalar real part and vector imaginary part.
Definition: quaternion.h:50
bool operator!=(const GfQuaternion &q) const
Component-wise quaternion inequality test.
Definition: quaternion.h:137
bool operator==(const GfQuaternion &q) const
Component-wise quaternion equality test.
Definition: quaternion.h:130
GfQuaternion & operator+=(const GfQuaternion &q)
Component-wise unary sum operator.
Definition: quaternion.h:155
double GetReal() const
Returns the real part of the quaternion.
Definition: quaternion.h:85
GF_API GfQuatd GfSlerp(double alpha, const GfQuatd &q0, const GfQuatd &q1)
Spherically linearly interpolate between q0 and q1.
GF_API double Normalize(double eps=GF_MIN_VECTOR_LENGTH)
Normalizes this quaternion in place to unit length, returning the length before normalization.
friend GfQuaternion operator*(const GfQuaternion &q1, const GfQuaternion &q2)
Returns the product of quaternions q1 and q2.
Definition: quaternion.h:183
static GfQuaternion GetIdentity()
Returns the identity quaternion, which has a real part of 1 and an imaginary part of (0...
Definition: quaternion.h:96
GF_API GfQuaternion GetNormalized(double eps=GF_MIN_VECTOR_LENGTH) const
Returns a normalized (unit-length) version of this quaternion.
GF_API double GetLength() const
Returns geometric length of this quaternion.
friend GfQuaternion operator/(const GfQuaternion &q, double s)
Returns the product of quaternion q and scalar 1 / s.
Definition: quaternion.h:202
GF_API std::ostream & operator<<(std::ostream &, const GfBBox3d &)
Output a GfBBox3d using the format [(range) matrix zeroArea].
friend GfQuaternion operator-(const GfQuaternion &q1, const GfQuaternion &q2)
Component-wise binary difference operator.
Definition: quaternion.h:176
GF_API GfQuaternion GetInverse() const
Returns the inverse of this quaternion.
GF_API GfQuaternion & operator*=(const GfQuaternion &q)
Post-multiplies quaternion q into this quaternion.
const GfVec3d & GetImaginary() const
Returns the imaginary part of the quaternion.
Definition: quaternion.h:90
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
GfQuaternion()
The default constructor leaves the quaternion undefined.
Definition: quaternion.h:55
#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
GfHalf GfDot(GfHalf a, GfHalf b)
Returns the dot (inner) product of two vectors.
Definition: half.h:55