HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
rotation.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_ROTATION_H
25 #define PXR_BASE_GF_ROTATION_H
26 
27 /// \file gf/rotation.h
28 /// \ingroup group_gf_LinearAlgebra
29 
30 #include "pxr/pxr.h"
31 #include "pxr/base/gf/quaternion.h"
32 #include "pxr/base/gf/quatd.h"
33 #include "pxr/base/gf/matrix4d.h"
34 #include "pxr/base/gf/vec3d.h"
35 #include "pxr/base/gf/vec3f.h"
36 #include "pxr/base/gf/api.h"
37 
38 #include <hboost/functional/hash.hpp>
39 
40 #include <iosfwd>
41 
43 
44 /// \class GfRotation
45 /// \ingroup group_gf_LinearAlgebra
46 ///
47 /// Basic type: 3-space rotation specification.
48 ///
49 /// This class represents a rotation in 3-space. This stores an axis as a
50 /// normalized vector of 3 \c doubles and an angle in degrees (as a double).
51 /// Rotations follow the right-hand rule: a positive rotation about an axis
52 /// vector appears counter-clockwise when looking from the end of the vector
53 /// toward the origin.
54 ///
55 class GfRotation {
56 
57  public:
58 
59  /// The default constructor leaves the rotation undefined.
61  }
62 
63  /// This constructor initializes the rotation to be \p angle
64  /// degrees about \p axis.
65  GfRotation(const GfVec3d &axis, double angle) {
66  SetAxisAngle(axis, angle);
67  }
68 
69  /// This constructor initializes the rotation from a quaternion.
70  GfRotation(const GfQuaternion &quaternion) {
71  SetQuaternion(quaternion);
72  }
73 
74  /// This constructor initializes the rotation from a quaternion. Note that
75  /// this constructor accepts GfQuatf and GfQuath since they implicitly
76  /// convert to GfQuatd.
77  GfRotation(const GfQuatd &quat) { SetQuat(quat); }
78 
79  /// This constructor initializes the rotation to one that brings
80  /// the \p rotateFrom vector to align with \p rotateTo. The passed
81  /// vectors need not be unit length.
82  GF_API
83  GfRotation(const GfVec3d &rotateFrom, const GfVec3d &rotateTo) {
84  SetRotateInto(rotateFrom, rotateTo);
85  }
86 
87  /// Sets the rotation to be \p angle degrees about \p axis.
88  GfRotation & SetAxisAngle(const GfVec3d &axis, double angle) {
89  _axis = axis;
90  _angle = angle;
91  if (!GfIsClose(_axis * _axis, 1.0, 1e-10))
92  _axis.Normalize();
93  return *this;
94  }
95 
96  /// Sets the rotation from a quaternion. Note that this method accepts
97  /// GfQuatf and GfQuath since they implicitly convert to GfQuatd.
98  GF_API
99  GfRotation & SetQuat(const GfQuatd &quat);
100 
101  /// Sets the rotation from a quaternion.
103  return SetQuat(GfQuatd(quat.GetReal(), quat.GetImaginary()));
104  }
105 
106  /// Sets the rotation to one that brings the \p rotateFrom vector
107  /// to align with \p rotateTo. The passed vectors need not be unit
108  /// length.
109  GF_API
110  GfRotation & SetRotateInto(const GfVec3d &rotateFrom,
111  const GfVec3d &rotateTo);
112 
113  /// Sets the rotation to an identity rotation.
114  /// (This is chosen to be 0 degrees around the positive X axis.)
116  _axis.Set(1.0, 0.0, 0.0);
117  _angle = 0.0;
118  return *this;
119  }
120 
121  /// Returns the axis of rotation.
122  const GfVec3d & GetAxis() const {
123  return _axis;
124  }
125 
126  /// Returns the rotation angle in degrees.
127  double GetAngle() const {
128  return _angle;
129  }
130 
131  /// Returns the rotation expressed as a quaternion.
133  auto quat = GetQuat();
134  return GfQuaternion(quat.GetReal(), quat.GetImaginary());
135  }
136 
137  /// Returns the rotation expressed as a quaternion.
138  GF_API
139  GfQuatd GetQuat() const;
140 
141  /// Returns the inverse of this rotation.
143  return GfRotation(_axis, -_angle);
144  }
145 
146  /// Decompose rotation about 3 orthogonal axes.
147  /// If the axes are not orthogonal, warnings will be spewed.
148  GF_API
149  GfVec3d Decompose( const GfVec3d &axis0,
150  const GfVec3d &axis1,
151  const GfVec3d &axis2 ) const;
152 
153  // Full-featured method to Decompose a rotation matrix into Cardarian
154  // angles.
155  // Axes have must be normalized. If useHint is specified
156  // then the current values stored within thetaTw, thetaFB, thetaLR,
157  // and thetaSw will be treated as hint and used to help choose
158  // an equivalent rotation that is as close as possible to the hints.
159  //
160  // One can use this routine to generate any combination of the three
161  // angles by passing in nullptr for the angle that is to be omitted.
162  //
163  // Passing in valid pointers for all four angles will decompose into
164  // Tw, FB, and LR but allows Sw to be used for best matching of hint
165  // values. It also allows an swShift value to be passed in as a
166  // Sw that is applied after the rotation matrix to get a best fit rotation
167  // in four angles.
168  //
169  // Angles are in radians.
170  //
171  // Specify \p handedness as -1.0 or 1.0, same as for MultiRotate.
172  //
173  // NOTE:
174  // Geppetto math function Originally brought over to extMover
175  // from //depot/main/tools/src/menv/lib/gpt/util.h [10/16/06]
176  // And moved into GfRotation[12/1/08]. Updated for any
177  // combination of three angles [12/1/11].
178  //
179  GF_API
180  static void DecomposeRotation(const GfMatrix4d &rot,
181  const GfVec3d &TwAxis,
182  const GfVec3d &FBAxis,
183  const GfVec3d &LRAxis,
184  double handedness,
185  double *thetaTw,
186  double *thetaFB,
187  double *thetaLR,
188  double *thetaSw = nullptr,
189  bool useHint=false,
190  const double *swShift=nullptr);
191 
192  // This function projects the vectors \p v1 and \p v2 onto the plane
193  // normal to \p axis, and then returns the rotation about \p axis that
194  // brings \p v1 onto \p v2.
195  GF_API
197  const GfVec3d &v2,
198  const GfVec3d &axis);
199 
200  /// Replace the hint angles with the closest rotation of the given
201  /// rotation to the hint.
202  ///
203  /// Each angle in the rotation will be within Pi of the corresponding
204  /// hint angle and the sum of the differences with the hint will
205  /// be minimized. If a given rotation value is null then that angle will
206  /// be treated as 0.0 and ignored in the calculations.
207  ///
208  /// All angles are in radians. The rotation order is Tw/FB/LR/Sw.
209  GF_API
210  static void MatchClosestEulerRotation(
211  double targetTw, double targetFB, double targetLR, double targetSw,
212  double *thetaTw, double *thetaFB, double *thetaLR, double *thetaSw);
213 
214  /// Transforms row vector \p vec by the rotation, returning the result.
215  GF_API
216  GfVec3f TransformDir( const GfVec3f &vec ) const;
217 
218  /// \overload
219  GF_API
220  GfVec3d TransformDir( const GfVec3d &vec ) const;
221 
222  /// Hash.
223  friend inline size_t hash_value(const GfRotation &r) {
224  size_t h = 0;
225  hboost::hash_combine(h, r._axis);
226  hboost::hash_combine(h, r._angle);
227  return h;
228  }
229 
230  /// Component-wise rotation equality test. The axes and angles must match
231  /// exactly for rotations to be considered equal. (To compare equality of
232  /// the actual rotations, you can convert both to quaternions and test the
233  /// results for equality.)
234  bool operator ==(const GfRotation &r) const {
235  return (_axis == r._axis &&
236  _angle == r._angle);
237  }
238 
239  /// Component-wise rotation inequality test. The axes and angles must
240  /// match exactly for rotations to be considered equal. (To compare
241  /// equality of the actual rotations, you can convert both to quaternions
242  /// and test the results for equality.)
243  bool operator !=(const GfRotation &r) const {
244  return ! (*this == r);
245  }
246 
247  /// Post-multiplies rotation \p r into this rotation.
248  GF_API
250 
251  /// Scales rotation angle by multiplying by \p scale.
253  _angle *= scale;
254  return *this;
255  }
256 
257  /// Scales rotation angle by dividing by \p scale.
259  _angle /= scale;
260  return *this;
261  }
262 
263  /// Returns composite rotation of rotations \p r1 and \p r2.
264  friend GfRotation operator *(const GfRotation &r1,
265  const GfRotation &r2) {
266  GfRotation r = r1;
267  return r *= r2;
268  }
269 
270  /// Returns a rotation equivalent to \p r with its angle multiplied
271  /// by \p scale.
272  friend GfRotation operator *(const GfRotation &r, double scale) {
273  GfRotation rTmp = r;
274  return rTmp *= scale;
275  }
276 
277  /// Returns a rotation equivalent to \p r with its angle multiplied
278  /// by \p scale.
279  friend GfRotation operator *(double scale, const GfRotation &r) {
280  return (r * scale);
281  }
282 
283  /// Returns a rotation equivalent to \p r with its angle divided
284  /// by \p scale.
285  friend GfRotation operator /(const GfRotation &r, double scale) {
286  GfRotation rTmp = r;
287  return rTmp /= scale;
288  }
289 
290  private:
291  /// Axis storage.
292  /// This axis is normalized to unit length whenever it is set.
293  GfVec3d _axis;
294  /// Angle storage (represented in degrees).
295  double _angle;
296 };
297 
298 /// Output a GfRotation using the format [(x y z) a].
299 /// \ingroup group_gf_DebuggingOutput
300 GF_API std::ostream& operator<<(std::ostream&, const GfRotation&);
301 
303 
304 #endif // PXR_BASE_GF_ROTATION_H
GF_API GfVec3f TransformDir(const GfVec3f &vec) const
Transforms row vector vec by the rotation, returning the result.
GfRotation & SetAxisAngle(const GfVec3d &axis, double angle)
Sets the rotation to be angle degrees about axis.
Definition: rotation.h:88
GfRotation GetInverse() const
Returns the inverse of this rotation.
Definition: rotation.h:142
bool operator!=(const GfRotation &r) const
Definition: rotation.h:243
GLenum GLenum GLenum GLenum GLenum scale
Definition: glew.h:14163
GF_API GfQuatd GetQuat() const
Returns the rotation expressed as a quaternion.
GfRotation(const GfQuatd &quat)
Definition: rotation.h:77
GfRotation(const GfQuaternion &quaternion)
This constructor initializes the rotation from a quaternion.
Definition: rotation.h:70
friend size_t hash_value(const GfRotation &r)
Hash.
Definition: rotation.h:223
GA_API const UT_StringHolder rot
double GetReal() const
Returns the real part of the quaternion.
Definition: quaternion.h:85
Definition: vec3f.h:63
GF_API GfRotation(const GfVec3d &rotateFrom, const GfVec3d &rotateTo)
Definition: rotation.h:83
GF_API GfRotation & SetQuat(const GfQuatd &quat)
friend GfRotation operator*(const GfRotation &r1, const GfRotation &r2)
Returns composite rotation of rotations r1 and r2.
Definition: rotation.h:264
GfRotation()
The default constructor leaves the rotation undefined.
Definition: rotation.h:60
GF_API GfRotation & SetRotateInto(const GfVec3d &rotateFrom, const GfVec3d &rotateTo)
GLfloat GLfloat GLfloat v2
Definition: glcorearb.h:818
GfRotation & SetQuaternion(const GfQuaternion &quat)
Sets the rotation from a quaternion.
Definition: rotation.h:102
GF_API std::ostream & operator<<(std::ostream &, const GfRotation &)
GfRotation & SetIdentity()
Definition: rotation.h:115
GfRotation & operator/=(double scale)
Scales rotation angle by dividing by scale.
Definition: rotation.h:258
PXR_NAMESPACE_OPEN_SCOPE bool GfIsClose(double a, double b, double epsilon)
Definition: math.h:42
GfQuaternion GetQuaternion() const
Returns the rotation expressed as a quaternion.
Definition: rotation.h:132
static GF_API void DecomposeRotation(const GfMatrix4d &rot, const GfVec3d &TwAxis, const GfVec3d &FBAxis, const GfVec3d &LRAxis, double handedness, double *thetaTw, double *thetaFB, double *thetaLR, double *thetaSw=nullptr, bool useHint=false, const double *swShift=nullptr)
GF_API GfRotation & operator*=(const GfRotation &r)
Post-multiplies rotation r into this rotation.
GfRotation(const GfVec3d &axis, double angle)
Definition: rotation.h:65
friend GfRotation operator/(const GfRotation &r, double scale)
Definition: rotation.h:285
GLfloat GLfloat GLfloat GLfloat h
Definition: glcorearb.h:2002
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1394
GfVec3d & Set(double s0, double s1, double s2)
Set all elements with passed arguments.
Definition: vec3d.h:130
bool operator==(const GfRotation &r) const
Definition: rotation.h:234
Definition: vec3d.h:63
const GfVec3d & GetAxis() const
Returns the axis of rotation.
Definition: rotation.h:122
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
GLdouble angle
Definition: glew.h:9177
GF_API GfVec3d Decompose(const GfVec3d &axis0, const GfVec3d &axis1, const GfVec3d &axis2) const
static GF_API void MatchClosestEulerRotation(double targetTw, double targetFB, double targetLR, double targetSw, double *thetaTw, double *thetaFB, double *thetaLR, double *thetaSw)
Definition: quatd.h:60
GLfloat GLfloat v1
Definition: glcorearb.h:817
static GF_API GfRotation RotateOntoProjected(const GfVec3d &v1, const GfVec3d &v2, const GfVec3d &axis)
GLboolean r
Definition: glcorearb.h:1222
const GfVec3d & GetImaginary() const
Returns the imaginary part of the quaternion.
Definition: quaternion.h:90
#define GF_API
Definition: api.h:40
double GetAngle() const
Returns the rotation angle in degrees.
Definition: rotation.h:127