rotation.h 6.1 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() {
  31. quat_.Set(0, 0, 0, 1);
  32. }
  33. // Returns an identity Rotation, which has no effect.
  34. static Rotation Identity() {
  35. return Rotation();
  36. }
  37. // Sets the Rotation from a quaternion (4D vector), which is first normalized.
  38. void SetQuaternion(const QuaternionType& quaternion) {
  39. quat_ = Normalized(quaternion);
  40. }
  41. // Returns the Rotation as a normalized quaternion (4D vector).
  42. const QuaternionType& GetQuaternion() const {
  43. return quat_;
  44. }
  45. // Sets the Rotation to rotate by the given angle around the given axis,
  46. // following the right-hand rule. The axis does not need to be unit
  47. // length. If it is zero length, this results in an identity Rotation.
  48. void SetAxisAndAngle(const VectorType& axis, double angle);
  49. // Returns the right-hand rule axis and angle corresponding to the
  50. // Rotation. If the Rotation is the identity rotation, this returns the +X
  51. // axis and an angle of 0.
  52. void GetAxisAndAngle(VectorType* axis, double* angle) const;
  53. // Convenience function that constructs and returns a Rotation given an axis
  54. // and angle.
  55. static Rotation FromAxisAndAngle(const VectorType& axis, double angle) {
  56. Rotation r;
  57. r.SetAxisAndAngle(axis, angle);
  58. return r;
  59. }
  60. // Convenience function that constructs and returns a Rotation given a
  61. // quaternion.
  62. static Rotation FromQuaternion(const QuaternionType& quat) {
  63. Rotation r;
  64. r.SetQuaternion(quat);
  65. return r;
  66. }
  67. // Convenience function that constructs and returns a Rotation given a
  68. // rotation matrix R with $R^\top R = I && det(R) = 1$.
  69. static Rotation FromRotationMatrix(const Matrix3x3& mat);
  70. // Convenience function that constructs and returns a Rotation given Euler
  71. // angles that are applied in the order of rotate-Z by roll, rotate-X by
  72. // pitch, rotate-Y by yaw (same as GetRollPitchYaw).
  73. static Rotation FromRollPitchYaw(double roll, double pitch, double yaw) {
  74. VectorType x(1, 0, 0), y(0, 1, 0), z(0, 0, 1);
  75. return FromAxisAndAngle(z, roll) * (FromAxisAndAngle(x, pitch) * FromAxisAndAngle(y, yaw));
  76. }
  77. // Convenience function that constructs and returns a Rotation given Euler
  78. // angles that are applied in the order of rotate-Y by yaw, rotate-X by
  79. // pitch, rotate-Z by roll (same as GetYawPitchRoll).
  80. static Rotation FromYawPitchRoll(double yaw, double pitch, double roll) {
  81. VectorType x(1, 0, 0), y(0, 1, 0), z(0, 0, 1);
  82. return FromAxisAndAngle(y, yaw) * (FromAxisAndAngle(x, pitch) * FromAxisAndAngle(z, roll));
  83. }
  84. // Constructs and returns a Rotation that rotates one vector to another along
  85. // the shortest arc. This returns an identity rotation if either vector has
  86. // zero length.
  87. static Rotation RotateInto(const VectorType& from, const VectorType& to);
  88. // The negation operator returns the inverse rotation.
  89. friend Rotation operator-(const Rotation& r) {
  90. // Because we store normalized quaternions, the inverse is found by
  91. // negating the vector part.
  92. return Rotation(-r.quat_[0], -r.quat_[1], -r.quat_[2], r.quat_[3]);
  93. }
  94. // Appends a rotation to this one.
  95. Rotation& operator*=(const Rotation& r) {
  96. const QuaternionType& qr = r.quat_;
  97. QuaternionType& qt = quat_;
  98. SetQuaternion(QuaternionType(
  99. qr[3] * qt[0] + qr[0] * qt[3] + qr[2] * qt[1] - qr[1] * qt[2],
  100. qr[3] * qt[1] + qr[1] * qt[3] + qr[0] * qt[2] - qr[2] * qt[0],
  101. qr[3] * qt[2] + qr[2] * qt[3] + qr[1] * qt[0] - qr[0] * qt[1],
  102. qr[3] * qt[3] - qr[0] * qt[0] - qr[1] * qt[1] - qr[2] * qt[2]));
  103. return *this;
  104. }
  105. // Binary multiplication operator - returns a composite Rotation.
  106. friend const Rotation operator*(const Rotation& r0, const Rotation& r1) {
  107. Rotation r = r0;
  108. r *= r1;
  109. return r;
  110. }
  111. // Multiply a Rotation and a Vector to get a Vector.
  112. VectorType operator*(const VectorType& v) const;
  113. private:
  114. // Private constructor that builds a Rotation from quaternion components.
  115. Rotation(double q0, double q1, double q2, double q3)
  116. : quat_(q0, q1, q2, q3) {
  117. }
  118. // Applies a Rotation to a Vector to rotate the Vector. Method borrowed from:
  119. // http://blog.molecular-matters.com/2013/05/24/a-faster-quaternion-vector-multiplication/
  120. VectorType ApplyToVector(const VectorType& v) const {
  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_