rotation.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Copyright 2019 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 CARDBOARD_SDK_UTIL_ROTATION_H_
  17. #define CARDBOARD_SDK_UTIL_ROTATION_H_
  18. #include "matrix_3x3.h"
  19. #include "vector.h"
  20. #include "vectorutils.h"
  21. namespace cardboard {
  22. // The Rotation class represents a rotation around a 3-dimensional axis. It
  23. // uses normalized quaternions internally to make the math robust.
  24. class Rotation {
  25. public:
  26. // Convenience typedefs for vector of the correct type.
  27. typedef Vector<3> VectorType;
  28. typedef Vector<4> QuaternionType;
  29. // The default constructor creates an identity Rotation, which has no effect.
  30. Rotation() { quat_.Set(0, 0, 0, 1); }
  31. // Returns an identity Rotation, which has no effect.
  32. static Rotation Identity() { return Rotation(); }
  33. // Sets the Rotation from a quaternion (4D vector), which is first normalized.
  34. void SetQuaternion(const QuaternionType& quaternion) { quat_ = Normalized(quaternion); }
  35. // Returns the Rotation as a normalized quaternion (4D vector).
  36. const QuaternionType& GetQuaternion() const { return quat_; }
  37. // Sets the Rotation to rotate by the given angle around the given axis,
  38. // following the right-hand rule. The axis does not need to be unit
  39. // length. If it is zero length, this results in an identity Rotation.
  40. void SetAxisAndAngle(const VectorType& axis, double angle);
  41. // Returns the right-hand rule axis and angle corresponding to the
  42. // Rotation. If the Rotation is the identity rotation, this returns the +X
  43. // axis and an angle of 0.
  44. void GetAxisAndAngle(VectorType* axis, double* angle) const;
  45. // Convenience function that constructs and returns a Rotation given an axis
  46. // and angle.
  47. static Rotation FromAxisAndAngle(const VectorType& axis, double angle)
  48. {
  49. Rotation r;
  50. r.SetAxisAndAngle(axis, angle);
  51. return r;
  52. }
  53. // Convenience function that constructs and returns a Rotation given a
  54. // quaternion.
  55. static Rotation FromQuaternion(const QuaternionType& quat)
  56. {
  57. Rotation r;
  58. r.SetQuaternion(quat);
  59. return r;
  60. }
  61. // Convenience function that constructs and returns a Rotation given a
  62. // rotation matrix R with $R^\top R = I && det(R) = 1$.
  63. static Rotation FromRotationMatrix(const Matrix3x3& mat);
  64. // Convenience function that constructs and returns a Rotation given Euler
  65. // angles that are applied in the order of rotate-Z by roll, rotate-X by
  66. // pitch, rotate-Y by yaw (same as GetRollPitchYaw).
  67. static Rotation FromRollPitchYaw(double roll, double pitch, double yaw)
  68. {
  69. VectorType x(1, 0, 0), y(0, 1, 0), z(0, 0, 1);
  70. return FromAxisAndAngle(z, roll) * (FromAxisAndAngle(x, pitch) * FromAxisAndAngle(y, yaw));
  71. }
  72. // Convenience function that constructs and returns a Rotation given Euler
  73. // angles that are applied in the order of rotate-Y by yaw, rotate-X by
  74. // pitch, rotate-Z by roll (same as GetYawPitchRoll).
  75. static Rotation FromYawPitchRoll(double yaw, double pitch, double roll)
  76. {
  77. VectorType x(1, 0, 0), y(0, 1, 0), z(0, 0, 1);
  78. return FromAxisAndAngle(y, yaw) * (FromAxisAndAngle(x, pitch) * FromAxisAndAngle(z, roll));
  79. }
  80. // Constructs and returns a Rotation that rotates one vector to another along
  81. // the shortest arc. This returns an identity rotation if either vector has
  82. // zero length.
  83. static Rotation RotateInto(const VectorType& from, const VectorType& to);
  84. // The negation operator returns the inverse rotation.
  85. friend Rotation operator-(const Rotation& r)
  86. {
  87. // Because we store normalized quaternions, the inverse is found by
  88. // negating the vector part.
  89. return Rotation(-r.quat_[0], -r.quat_[1], -r.quat_[2], r.quat_[3]);
  90. }
  91. // Appends a rotation to this one.
  92. Rotation& operator*=(const Rotation& r)
  93. {
  94. const QuaternionType& qr = r.quat_;
  95. QuaternionType& qt = quat_;
  96. SetQuaternion(QuaternionType(qr[3] * qt[0] + qr[0] * qt[3] + qr[2] * qt[1] - qr[1] * qt[2],
  97. qr[3] * qt[1] + qr[1] * qt[3] + qr[0] * qt[2] - qr[2] * qt[0],
  98. qr[3] * qt[2] + qr[2] * qt[3] + qr[1] * qt[0] - qr[0] * qt[1],
  99. qr[3] * qt[3] - qr[0] * qt[0] - qr[1] * qt[1] - qr[2] * qt[2]));
  100. return *this;
  101. }
  102. // Binary multiplication operator - returns a composite Rotation.
  103. friend const Rotation operator*(const Rotation& r0, const Rotation& r1)
  104. {
  105. Rotation r = r0;
  106. r *= r1;
  107. return r;
  108. }
  109. // Multiply a Rotation and a Vector to get a Vector.
  110. VectorType operator*(const VectorType& v) const;
  111. private:
  112. // Private constructor that builds a Rotation from quaternion components.
  113. Rotation(double q0, double q1, double q2, double q3)
  114. : quat_(q0, q1, q2, q3)
  115. {
  116. }
  117. // Applies a Rotation to a Vector to rotate the Vector. Method borrowed from:
  118. // http://blog.molecular-matters.com/2013/05/24/a-faster-quaternion-vector-multiplication/
  119. VectorType ApplyToVector(const VectorType& v) const
  120. {
  121. VectorType im(quat_[0], quat_[1], quat_[2]);
  122. VectorType temp = 2.0 * Cross(im, v);
  123. return v + quat_[3] * temp + Cross(im, temp);
  124. }
  125. // The rotation represented as a normalized quaternion. (Unit quaternions are
  126. // required for constructing rotation matrices, so it makes sense to always
  127. // store them that way.) The vector part is in the first 3 elements, and the
  128. // scalar part is in the last element.
  129. QuaternionType quat_;
  130. };
  131. } // namespace cardboard
  132. #endif // CARDBOARD_SDK_UTIL_ROTATION_H_