MathFu
An open source project by FPL.
 All Classes Namespaces Files Functions Variables Typedefs Friends Groups Pages
quaternion.h
Go to the documentation of this file.
1 /*
2 * Copyright 2014 Google Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 #ifndef MATHFU_QUATERNION_H_
17 #define MATHFU_QUATERNION_H_
18 
19 #ifdef _WIN32
20 #if !defined(_USE_MATH_DEFINES)
21 #define _USE_MATH_DEFINES // For M_PI.
22 #endif // !defined(_USE_MATH_DEFINES)
23 #endif // _WIN32
24 
25 #include "mathfu/matrix.h"
26 #include "mathfu/vector.h"
27 
28 #include <math.h>
29 
30 /// @file mathfu/quaternion.h
31 /// @brief Quaternion class and functions.
32 /// @addtogroup mathfu_quaternion
33 ///
34 /// MathFu provides a Quaternion class that utilizes SIMD optimized
35 /// Matrix and Vector classes.
36 
37 namespace mathfu {
38 
39 /// @addtogroup mathfu_quaternion
40 /// @{
41 /// @class Quaternion
42 ///
43 /// @brief Stores a Quaternion of type T and provides a set of utility
44 /// operations on each Quaternion.
45 /// @tparam T Type of each element in the Quaternion.
46 template <class T>
47 class Quaternion {
48  public:
49  /// @brief Construct an uninitialized Quaternion.
50  inline Quaternion() {}
51 
52  /// @brief Construct a Quaternion from a copy.
53  /// @param q Quaternion to copy.
54  inline Quaternion(const Quaternion<T>& q) {
55  s_ = q.s_;
56  v_ = q.v_;
57  }
58 
59  /// @brief Construct a Quaternion using scalar values to initialize each
60  /// element.
61  ///
62  /// @param s1 Scalar component.
63  /// @param s2 First element of the Vector component.
64  /// @param s3 Second element of the Vector component.
65  /// @param s4 Third element of the Vector component.
66  inline Quaternion(const T& s1, const T& s2, const T& s3, const T& s4) {
67  s_ = s1;
68  v_ = Vector<T, 3>(s2, s3, s4);
69  }
70 
71  /// @brief Construct a quaternion from a scalar and 3-dimensional Vector.
72  ///
73  /// @param s1 Scalar component.
74  /// @param v1 Vector component.
75  inline Quaternion(const T& s1, const Vector<T, 3>& v1) {
76  s_ = s1;
77  v_ = v1;
78  }
79 
80  /// @brief Return the scalar component of the quaternion.
81  ///
82  /// @return The scalar component
83  inline T& scalar() { return s_; }
84 
85  /// @brief Return the scalar component of the quaternion.
86  ///
87  /// @return The scalar component
88  inline const T& scalar() const { return s_; }
89 
90  /// @brief Set the scalar component of the quaternion.
91  ///
92  /// @param s Scalar component.
93  inline void set_scalar(const T& s) { s_ = s; }
94 
95  /// @brief Return the vector component of the quaternion.
96  ///
97  /// @return The scalar component
98  inline Vector<T, 3>& vector() { return v_; }
99 
100  /// @brief Return the vector component of the quaternion.
101  ///
102  /// @return The scalar component
103  inline const Vector<T, 3>& vector() const { return v_; }
104 
105  /// @brief Set the vector component of the quaternion.
106  ///
107  /// @param v Vector component.
108  inline void set_vector(const Vector<T, 3>& v) { v_ = v; }
109 
110  /// @brief Calculate the inverse Quaternion.
111  ///
112  /// This calculates the inverse such that <code>(q * q).Inverse()</code>
113  /// is the identity.
114  ///
115  /// @return Quaternion containing the result.
116  inline Quaternion<T> Inverse() const { return Quaternion<T>(s_, -v_); }
117 
118  /// @brief Multiply this Quaternion with another Quaternion.
119  ///
120  /// @note This is equivalent to
121  /// <code>FromMatrix(ToMatrix() * q.ToMatrix()).</code>
122  /// @param q Quaternion to multiply with.
123  /// @return Quaternion containing the result.
124  inline Quaternion<T> operator*(const Quaternion<T>& q) const {
125  return Quaternion<T>(
126  s_ * q.s_ - Vector<T, 3>::DotProduct(v_, q.v_),
127  s_ * q.v_ + q.s_ * v_ + Vector<T, 3>::CrossProduct(v_, q.v_));
128  }
129 
130  /// @brief Multiply this Quaternion by a scalar.
131  ///
132  /// This multiplies the angle of the rotation by a scalar factor.
133  /// @param s1 Scalar to multiply with.
134  /// @return Quaternion containing the result.
135  inline Quaternion<T> operator*(const T& s1) const {
136  T angle;
137  Vector<T, 3> axis;
138  ToAngleAxis(&angle, &axis);
139  angle *= s1;
140  return Quaternion<T>(cos(0.5f * angle),
141  axis.Normalized() * static_cast<T>(sin(0.5f * angle)));
142  }
143 
144  /// @brief Multiply a Vector by this Quaternion.
145  ///
146  /// This will rotate the specified vector by the rotation specified by this
147  /// Quaternion.
148  ///
149  /// @param v1 Vector to multiply by this Quaternion.
150  /// @return Rotated Vector.
151  inline Vector<T, 3> operator*(const Vector<T, 3>& v1) const {
152  T ss = s_ + s_;
153  return ss * Vector<T, 3>::CrossProduct(v_, v1) + (ss * s_ - 1) * v1 +
154  2 * Vector<T, 3>::DotProduct(v_, v1) * v_;
155  }
156 
157  /// @brief Normalize this quaterion (in-place).
158  ///
159  /// @return Length of the quaternion.
160  inline T Normalize() {
161  T length = sqrt(s_ * s_ + Vector<T, 3>::DotProduct(v_, v_));
162  T scale = (1 / length);
163  s_ *= scale;
164  v_ *= scale;
165  return length;
166  }
167 
168  /// @brief Calculate the normalized version of this quaternion.
169  ///
170  /// @return The normalized quaternion.
171  inline Quaternion<T> Normalized() const {
172  Quaternion<T> q(*this);
173  q.Normalize();
174  return q;
175  }
176 
177  /// @brief Convert this Quaternion to an Angle and axis.
178  ///
179  /// The returned angle is the size of the rotation in radians about the
180  /// axis represented by this Quaternion.
181  ///
182  /// @param angle Receives the angle.
183  /// @param axis Receives the normalized axis.
184  inline void ToAngleAxis(T* angle, Vector<T, 3>* axis) const {
185  *axis = s_ > 0 ? v_ : -v_;
186  *angle = 2 * atan2(axis->Normalize(), s_ > 0 ? s_ : -s_);
187  }
188 
189  /// @brief Convert this Quaternion to 3 Euler Angles.
190  ///
191  /// @return 3-dimensional Vector where each element is a angle of rotation
192  /// (in radians) around the x, y, and z axes.
193  inline Vector<T, 3> ToEulerAngles() const {
194  Matrix<T, 3> m(ToMatrix());
195  T cos2 = m[0] * m[0] + m[1] * m[1];
196  if (cos2 < 1e-6f) {
197  return Vector<T, 3>(
198  0,
199  m[2] < 0 ? static_cast<T>(0.5 * M_PI) : static_cast<T>(-0.5 * M_PI),
200  -std::atan2(m[3], m[4]));
201  } else {
202  return Vector<T, 3>(std::atan2(m[5], m[8]),
203  std::atan2(-m[2], std::sqrt(cos2)),
204  std::atan2(m[1], m[0]));
205  }
206  }
207 
208  /// @brief Convert to a 3x3 Matrix.
209  ///
210  /// @return 3x3 rotation Matrix.
211  inline Matrix<T, 3> ToMatrix() const {
212  const T x2 = v_[0] * v_[0], y2 = v_[1] * v_[1], z2 = v_[2] * v_[2];
213  const T sx = s_ * v_[0], sy = s_ * v_[1], sz = s_ * v_[2];
214  const T xz = v_[0] * v_[2], yz = v_[1] * v_[2], xy = v_[0] * v_[1];
215  return Matrix<T, 3>(1 - 2 * (y2 + z2), 2 * (xy + sz), 2 * (xz - sy),
216  2 * (xy - sz), 1 - 2 * (x2 + z2), 2 * (sx + yz),
217  2 * (sy + xz), 2 * (yz - sx), 1 - 2 * (x2 + y2));
218  }
219 
220  /// @brief Convert to a 4x4 Matrix.
221  ///
222  /// @return 4x4 transform Matrix.
223  inline Matrix<T, 4> ToMatrix4() const {
224  const T x2 = v_[0] * v_[0], y2 = v_[1] * v_[1], z2 = v_[2] * v_[2];
225  const T sx = s_ * v_[0], sy = s_ * v_[1], sz = s_ * v_[2];
226  const T xz = v_[0] * v_[2], yz = v_[1] * v_[2], xy = v_[0] * v_[1];
227  return Matrix<T, 4>(1 - 2 * (y2 + z2), 2 * (xy + sz), 2 * (xz - sy), 0.0f,
228  2 * (xy - sz), 1 - 2 * (x2 + z2), 2 * (sx + yz), 0.0f,
229  2 * (sy + xz), 2 * (yz - sx), 1 - 2 * (x2 + y2), 0.0f,
230  0.0f, 0.0f, 0.0f, 1.0f);
231  }
232 
233  /// @brief Create a Quaternion from an angle and axis.
234  ///
235  /// @param angle Angle in radians to rotate by.
236  /// @param axis Axis in 3D space to rotate around.
237  /// @return Quaternion containing the result.
238  static Quaternion<T> FromAngleAxis(const T& angle, const Vector<T, 3>& axis) {
239  const T halfAngle = static_cast<T>(0.5) * angle;
240  Vector<T, 3> localAxis(axis);
241  return Quaternion<T>(
242  cos(halfAngle),
243  localAxis.Normalized() * static_cast<T>(sin(halfAngle)));
244  }
245 
246  /// @brief Create a quaternion from 3 euler angles.
247  ///
248  /// @param angles 3-dimensional Vector where each element contains an
249  /// angle in radius to rotate by about the x, y and z axes.
250  /// @return Quaternion containing the result.
252  const Vector<T, 3> halfAngles(static_cast<T>(0.5) * angles[0],
253  static_cast<T>(0.5) * angles[1],
254  static_cast<T>(0.5) * angles[2]);
255  const T sinx = std::sin(halfAngles[0]);
256  const T cosx = std::cos(halfAngles[0]);
257  const T siny = std::sin(halfAngles[1]);
258  const T cosy = std::cos(halfAngles[1]);
259  const T sinz = std::sin(halfAngles[2]);
260  const T cosz = std::cos(halfAngles[2]);
261  return Quaternion<T>(cosx * cosy * cosz + sinx * siny * sinz,
262  sinx * cosy * cosz - cosx * siny * sinz,
263  cosx * siny * cosz + sinx * cosy * sinz,
264  cosx * cosy * sinz - sinx * siny * cosz);
265  }
266 
267  /// @brief Create a quaternion from a rotation Matrix.
268  ///
269  /// @param m 3x3 rotation Matrix.
270  /// @return Quaternion containing the result.
272  const T trace = m(0, 0) + m(1, 1) + m(2, 2);
273  if (trace > 0) {
274  const T s = sqrt(trace + 1) * 2;
275  const T oneOverS = 1 / s;
276  return Quaternion<T>(static_cast<T>(0.25) * s, (m[5] - m[7]) * oneOverS,
277  (m[6] - m[2]) * oneOverS, (m[1] - m[3]) * oneOverS);
278  } else if (m[0] > m[4] && m[0] > m[8]) {
279  const T s = sqrt(m[0] - m[4] - m[8] + 1) * 2;
280  const T oneOverS = 1 / s;
281  return Quaternion<T>((m[5] - m[7]) * oneOverS, static_cast<T>(0.25) * s,
282  (m[3] + m[1]) * oneOverS, (m[6] + m[2]) * oneOverS);
283  } else if (m[4] > m[8]) {
284  const T s = sqrt(m[4] - m[0] - m[8] + 1) * 2;
285  const T oneOverS = 1 / s;
286  return Quaternion<T>((m[6] - m[2]) * oneOverS, (m[3] + m[1]) * oneOverS,
287  static_cast<T>(0.25) * s, (m[5] + m[7]) * oneOverS);
288  } else {
289  const T s = sqrt(m[8] - m[0] - m[4] + 1) * 2;
290  const T oneOverS = 1 / s;
291  return Quaternion<T>((m[1] - m[3]) * oneOverS, (m[6] + m[2]) * oneOverS,
292  (m[5] + m[7]) * oneOverS, static_cast<T>(0.25) * s);
293  }
294  }
295 
296  /// @brief Create a quaternion from the upper-left 3x3 roation Matrix of a 4x4
297  /// Matrix.
298  ///
299  /// @param m 4x4 Matrix.
300  /// @return Quaternion containing the result.
302  const T trace = m(0, 0) + m(1, 1) + m(2, 2);
303  if (trace > 0) {
304  const T s = sqrt(trace + 1) * 2;
305  const T oneOverS = 1 / s;
306  return Quaternion<T>(static_cast<T>(0.25) * s, (m[6] - m[9]) * oneOverS,
307  (m[8] - m[2]) * oneOverS, (m[1] - m[4]) * oneOverS);
308  } else if (m[0] > m[5] && m[0] > m[10]) {
309  const T s = sqrt(m[0] - m[5] - m[10] + 1) * 2;
310  const T oneOverS = 1 / s;
311  return Quaternion<T>((m[6] - m[9]) * oneOverS, static_cast<T>(0.25) * s,
312  (m[4] + m[1]) * oneOverS, (m[8] + m[2]) * oneOverS);
313  } else if (m[5] > m[10]) {
314  const T s = sqrt(m[5] - m[0] - m[10] + 1) * 2;
315  const T oneOverS = 1 / s;
316  return Quaternion<T>((m[8] - m[2]) * oneOverS, (m[4] + m[1]) * oneOverS,
317  static_cast<T>(0.25) * s, (m[6] + m[9]) * oneOverS);
318  } else {
319  const T s = sqrt(m[10] - m[0] - m[5] + 1) * 2;
320  const T oneOverS = 1 / s;
321  return Quaternion<T>((m[1] - m[4]) * oneOverS, (m[8] + m[2]) * oneOverS,
322  (m[6] + m[9]) * oneOverS, static_cast<T>(0.25) * s);
323  }
324  }
325 
326  /// @brief Calculate the dot product of two Quaternions.
327  ///
328  /// @param q1 First quaternion.
329  /// @param q2 Second quaternion
330  /// @return The scalar dot product of both Quaternions.
331  static inline T DotProduct(const Quaternion<T>& q1, const Quaternion<T>& q2) {
332  return q1.s_ * q2.s_ + Vector<T, 3>::DotProduct(q1.v_, q2.v_);
333  }
334 
335  /// @brief Calculate the spherical linear interpolation between two
336  /// Quaternions.
337  ///
338  /// @param q1 Start Quaternion.
339  /// @param q2 End Quaternion.
340  /// @param s1 The scalar value determining how far from q1 and q2 the
341  /// resulting quaternion should be. A value of 0 corresponds to q1 and a
342  /// value of 1 corresponds to q2.
343  /// @result Quaternion containing the result.
344  static inline Quaternion<T> Slerp(const Quaternion<T>& q1,
345  const Quaternion<T>& q2, const T& s1) {
346  if (q1.s_ * q2.s_ + Vector<T, 3>::DotProduct(q1.v_, q2.v_) > 0.999999f)
347  return Quaternion<T>(q1.s_ * (1 - s1) + q2.s_ * s1,
348  q1.v_ * (1 - s1) + q2.v_ * s1);
349  return q1 * ((q1.Inverse() * q2) * s1);
350  }
351 
352  /// @brief Access an element of the quaternion.
353  ///
354  /// @param i Index of the element to access.
355  /// @return A reference to the accessed data that can be modified by the
356  /// caller.
357  inline T& operator[](const int i) {
358  if (i == 0) return s_;
359  return v_[i - 1];
360  }
361 
362  /// @brief Access an element of the quaternion.
363  ///
364  /// @param i Index of the element to access.
365  /// @return A const reference to the accessed.
366  inline const T& operator[](const int i) const {
367  return const_cast<Quaternion<T>*>(this)->operator[](i);
368  }
369 
370  /// @brief Returns a vector that is perpendicular to the supplied vector.
371  ///
372  /// @param v1 An arbitrary vector
373  /// @return A vector perpendicular to v1. Normally this will just be
374  /// the cross product of v1, v2. If they are parallel or opposite though,
375  /// the routine will attempt to pick a vector.
377  // We start out by taking the cross product of the vector and the x-axis to
378  // find something parallel to the input vectors. If that cross product
379  // turns out to be length 0 (i. e. the vectors already lie along the x axis)
380  // then we use the y-axis instead.
382  Vector<T, 3>(static_cast<T>(1), static_cast<T>(0), static_cast<T>(0)),
383  v);
384  // We use a fairly high epsilon here because we know that if this number
385  // is too small, the axis we'll get from a cross product with the y axis
386  // will be much better and more numerically stable.
387  if (axis.LengthSquared() < static_cast<T>(0.05)) {
389  Vector<T, 3>(static_cast<T>(0), static_cast<T>(1), static_cast<T>(0)),
390  v);
391  }
392  return axis;
393  }
394 
395  /// @brief Returns the a Quaternion that rotates from start to end.
396  ///
397  /// @param v1 The starting vector
398  /// @param v2 The vector to rotate to
399  /// @param preferred_axis the axis to use, if v1 and v2 are parallel.
400  /// @return A Quaternion describing the rotation from v1 to v2
401  /// See the comment on RotateFromToWithAxis for an explanation of the math.
403  const Vector<T, 3>& v1, const Vector<T, 3>& v2,
404  const Vector<T, 3>& preferred_axis) {
405  Vector<T, 3> start = v1.Normalized();
406  Vector<T, 3> end = v2.Normalized();
407 
408  T dot_product = Vector<T, 3>::DotProduct(start, end);
409  // Any rotation < 0.1 degrees is treated as no rotation
410  // in order to avoid division by zero errors.
411  // So we early-out in cases where it's less then 0.1 degrees.
412  // cos( 0.1 degrees) = 0.99999847691
413  if (dot_product >= static_cast<T>(0.99999847691)) {
415  }
416  // If the vectors point in opposite directions, return a 180 degree
417  // rotation, on the axis that they asked for.
418  if (dot_product <= static_cast<T>(-0.99999847691)) {
419  return Quaternion<T>(static_cast<T>(0), preferred_axis);
420  }
421  // Degenerate cases have been handled, so if we're here, we have to
422  // actually compute the angle we want:
423  Vector<T, 3> cross_product = Vector<T, 3>::CrossProduct(start, end);
424 
425  return Quaternion<T>(static_cast<T>(1.0) + dot_product, cross_product)
426  .Normalized();
427  }
428 
429  /// @brief Returns the a Quaternion that rotates from start to end.
430  ///
431  /// @param v1 The starting vector
432  /// @param v2 The vector to rotate to
433  /// @return A Quaternion describing the rotation from v1 to v2. In the case
434  /// where the vectors are parallel, it returns the identity. In the case
435  /// where
436  /// they point in opposite directions, it picks an arbitrary axis. (Since
437  /// there
438  /// are technically infinite possible quaternions to represent a 180 degree
439  /// rotation.)
440  ///
441  /// The final equation used here is fairly elegant, but its derivation is
442  /// not obvious: We want to find the quaternion that represents the angle
443  /// between Start and End.
444  ///
445  /// The angle can be expressed as a quaternion with the values:
446  /// angle: ArcCos(dotproduct(start, end) / (|start|*|end|)
447  /// axis: crossproduct(start, end).normalized * sin(angle/2)
448  ///
449  /// or written as:
450  /// quaternion(cos(angle/2), axis * sin(angle/2))
451  ///
452  /// Using the trig identity:
453  /// sin(angle * 2) = 2 * sin(angle) * cos*angle)
454  /// Via substitution, we can turn this into:
455  /// sin(angle/2) = 0.5 * sin(angle)/cos(angle/2)
456  ///
457  /// Using this substitution, we get:
458  /// quaternion( cos(angle/2),
459  /// 0.5 * crossproduct(start, end).normalized
460  /// * sin(angle) / cos(angle/2))
461  ///
462  /// If we scale the whole thing up by 2 * cos(angle/2) then we get:
463  /// quaternion(2 * cos(angle/2) * cos(angle/2),
464  /// crossproduct(start, end).normalized * sin(angle))
465  ///
466  /// (Note that the quaternion is no longer normalized after this scaling)
467  ///
468  /// Another trig identity:
469  /// cos(angle/2) = sqrt((1 + cos(angle) / 2)
470  ///
471  /// Substituting this in, we can simplify the quaternion scalar:
472  ///
473  /// quaternion(1 + cos(angle),
474  /// crossproduct(start, end).normalized * sin(angle))
475  ///
476  /// Because cross(start, end) has a magnitude of |start|*|end|*sin(angle),
477  /// crossproduct(start,end).normalized
478  /// is equivalent to
479  /// crossproduct(start,end) / |start| * |end| * sin(angle)
480  /// So after that substitution:
481  ///
482  /// quaternion(1 + cos(angle),
483  /// crossproduct(start, end) / (|start| * |end|))
484  ///
485  /// dotproduct(start, end) has the value of |start| * |end| * cos(angle),
486  /// so by algebra,
487  /// cos(angle) = dotproduct(start, end) / (|start| * |end|)
488  /// we can replace our quaternion scalar here also:
489  ///
490  /// quaternion(1 + dotproduct(start, end) / (|start| * |end|),
491  /// crossproduct(start, end) / (|start| * |end|))
492  ///
493  /// If start and end are normalized, then |start| * |end| = 1, giving us a
494  /// final quaternion of:
495  ///
496  /// quaternion(1 + dotproduct(start, end), crossproduct(start, end))
497  static inline Quaternion<T> RotateFromTo(const Vector<T, 3>& v1,
498  const Vector<T, 3>& v2) {
499  Vector<T, 3> start = v1.Normalized();
500  Vector<T, 3> end = v2.Normalized();
501 
502  T dot_product = Vector<T, 3>::DotProduct(start, end);
503  // Any rotation < 0.1 degrees is treated as no rotation
504  // in order to avoid division by zero errors.
505  // So we early-out in cases where it's less then 0.1 degrees.
506  // cos( 0.1 degrees) = 0.99999847691
507  if (dot_product >= static_cast<T>(0.99999847691)) {
509  }
510  // If the vectors point in opposite directions, return a 180 degree
511  // rotation, on an arbitrary axis.
512  if (dot_product <= static_cast<T>(-0.99999847691)) {
513  return Quaternion<T>(0, PerpendicularVector(start));
514  }
515  // Degenerate cases have been handled, so if we're here, we have to
516  // actually compute the angle we want:
517  Vector<T, 3> cross_product = Vector<T, 3>::CrossProduct(start, end);
518 
519  return Quaternion<T>(static_cast<T>(1.0) + dot_product, cross_product)
520  .Normalized();
521  }
522 
523  /// @brief Contains a quaternion doing the identity transform.
525 
527 
528  private:
529  T s_;
530  Vector<T, 3> v_;
531 };
532 
533 template <typename T>
535 /// @}
536 
537 /// @addtogroup mathfu_quaternion
538 /// @{
539 
540 /// @brief Multiply a Quaternion by a scalar.
541 ///
542 /// This multiplies the angle of the rotation of the specified Quaternion
543 /// by a scalar factor.
544 /// @param s Scalar to multiply with.
545 /// @param q Quaternion to scale.
546 /// @return Quaternion containing the result.
547 ///
548 /// @related Quaternion
549 template <class T>
550 inline Quaternion<T> operator*(const T& s, const Quaternion<T>& q) {
551  return q * s;
552 }
553 /// @}
554 
555 } // namespace mathfu
556 #endif // MATHFU_QUATERNION_H_
Definition: vector_3.h:24
Vector of d elements with type T.
Definition: vector.h:63
static Quaternion< T > RotateFromTo(const Vector< T, 3 > &v1, const Vector< T, 3 > &v2)
Returns the a Quaternion that rotates from start to end.
Definition: quaternion.h:497
#define MATHFU_DEFINE_CLASS_SIMD_AWARE_NEW_DELETE
Macro which defines the new and delete for MathFu classes.
Definition: utilities.h:600
Quaternion(const T &s1, const Vector< T, 3 > &v1)
Construct a quaternion from a scalar and 3-dimensional Vector.
Definition: quaternion.h:75
T & operator[](const int i)
Access an element of the quaternion.
Definition: quaternion.h:357
static Quaternion< T > FromEulerAngles(const Vector< T, 3 > &angles)
Create a quaternion from 3 euler angles.
Definition: quaternion.h:251
Matrix stores a set of "rows" by "columns" elements of type T and provides functions that operate on ...
Definition: matrix.h:147
static Quaternion< T > RotateFromToWithAxis(const Vector< T, 3 > &v1, const Vector< T, 3 > &v2, const Vector< T, 3 > &preferred_axis)
Returns the a Quaternion that rotates from start to end.
Definition: quaternion.h:402
Quaternion< T > operator*(const T &s1) const
Multiply this Quaternion by a scalar.
Definition: quaternion.h:135
Quaternion(const T &s1, const T &s2, const T &s3, const T &s4)
Construct a Quaternion using scalar values to initialize each element.
Definition: quaternion.h:66
void ToAngleAxis(T *angle, Vector< T, 3 > *axis) const
Convert this Quaternion to an Angle and axis.
Definition: quaternion.h:184
Quaternion(const Quaternion< T > &q)
Construct a Quaternion from a copy.
Definition: quaternion.h:54
Quaternion< T > operator*(const T &s, const Quaternion< T > &q)
Multiply a Quaternion by a scalar.
Definition: quaternion.h:550
static Quaternion< T > Slerp(const Quaternion< T > &q1, const Quaternion< T > &q2, const T &s1)
Calculate the spherical linear interpolation between two Quaternions.
Definition: quaternion.h:344
const T & operator[](const int i) const
Access an element of the quaternion.
Definition: quaternion.h:366
Quaternion< T > operator*(const Quaternion< T > &q) const
Multiply this Quaternion with another Quaternion.
Definition: quaternion.h:124
Vector< T, 3 > & vector()
Return the vector component of the quaternion.
Definition: quaternion.h:98
static Quaternion< T > FromAngleAxis(const T &angle, const Vector< T, 3 > &axis)
Create a Quaternion from an angle and axis.
Definition: quaternion.h:238
static T DotProduct(const Vector< T, d > &v1, const Vector< T, d > &v2)
Calculate the dot product of two vectors.
Definition: vector.h:426
const Vector< T, 3 > & vector() const
Return the vector component of the quaternion.
Definition: quaternion.h:103
void set_scalar(const T &s)
Set the scalar component of the quaternion.
Definition: quaternion.h:93
static Quaternion< T > identity
Contains a quaternion doing the identity transform.
Definition: quaternion.h:524
Quaternion< T > Inverse() const
Calculate the inverse Quaternion.
Definition: quaternion.h:116
Matrix class and functions.
T Normalize()
Normalize this quaterion (in-place).
Definition: quaternion.h:160
Quaternion< T > Normalized() const
Calculate the normalized version of this quaternion.
Definition: quaternion.h:171
const T & scalar() const
Return the scalar component of the quaternion.
Definition: quaternion.h:88
Vector class and functions.
Matrix< T, 4 > ToMatrix4() const
Convert to a 4x4 Matrix.
Definition: quaternion.h:223
Stores a Quaternion of type T and provides a set of utility operations on each Quaternion.
Definition: quaternion.h:47
static Quaternion< T > FromMatrix(const Matrix< T, 3 > &m)
Create a quaternion from a rotation Matrix.
Definition: quaternion.h:271
Quaternion()
Construct an uninitialized Quaternion.
Definition: quaternion.h:50
Vector< T, 3 > operator*(const Vector< T, 3 > &v1) const
Multiply a Vector by this Quaternion.
Definition: quaternion.h:151
void set_vector(const Vector< T, 3 > &v)
Set the vector component of the quaternion.
Definition: quaternion.h:108
Matrix< T, 3 > ToMatrix() const
Convert to a 3x3 Matrix.
Definition: quaternion.h:211
Vector< T, 3 > ToEulerAngles() const
Convert this Quaternion to 3 Euler Angles.
Definition: quaternion.h:193
T & scalar()
Return the scalar component of the quaternion.
Definition: quaternion.h:83
static Vector< T, 3 > CrossProduct(const Vector< T, 3 > &v1, const Vector< T, 3 > &v2)
Calculate the cross product of two vectors.
Definition: vector.h:446
static Quaternion< T > FromMatrix(const Matrix< T, 4 > &m)
Create a quaternion from the upper-left 3x3 roation Matrix of a 4x4 Matrix.
Definition: quaternion.h:301
static Vector< T, 3 > PerpendicularVector(const Vector< T, 3 > &v)
Returns a vector that is perpendicular to the supplied vector.
Definition: quaternion.h:376
static T DotProduct(const Quaternion< T > &q1, const Quaternion< T > &q2)
Calculate the dot product of two Quaternions.
Definition: quaternion.h:331