quaternion.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. This program is free software: you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation, either version 3 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program. If not, see <http://www.gnu.org/licenses/>.
  12. */
  13. // Copyright 2012 Andrew Tridgell, all rights reserved.
  14. // Refactored by Jonathan Challinger
  15. #pragma once
  16. #include <cmath>
  17. #if MATH_CHECK_INDEXES
  18. #include <assert.h>
  19. #endif
  20. #include <math.h>
  21. class Quaternion {
  22. public:
  23. float q1, q2, q3, q4;
  24. // constructor creates a quaternion equivalent
  25. // to roll=0, pitch=0, yaw=0
  26. Quaternion()
  27. {
  28. q1 = 1;
  29. q2 = q3 = q4 = 0;
  30. }
  31. // setting constructor
  32. Quaternion(const float _q1, const float _q2, const float _q3, const float _q4) :
  33. q1(_q1), q2(_q2), q3(_q3), q4(_q4)
  34. {
  35. }
  36. // setting constructor
  37. Quaternion(const float _q[4]) :
  38. q1(_q[0]), q2(_q[1]), q3(_q[2]), q4(_q[3])
  39. {
  40. }
  41. // function call operator
  42. void operator()(const float _q1, const float _q2, const float _q3, const float _q4)
  43. {
  44. q1 = _q1;
  45. q2 = _q2;
  46. q3 = _q3;
  47. q4 = _q4;
  48. }
  49. // check if any elements are NAN
  50. bool is_nan(void) const WARN_IF_UNUSED
  51. {
  52. return isnan(q1) || isnan(q2) || isnan(q3) || isnan(q4);
  53. }
  54. // return the rotation matrix equivalent for this quaternion
  55. void rotation_matrix(Matrix3f &m) const;
  56. // return the rotation matrix equivalent for this quaternion after normalization
  57. void rotation_matrix_norm(Matrix3f &m) const;
  58. void from_rotation_matrix(const Matrix3f &m);
  59. // convert a vector from earth to body frame
  60. void earth_to_body(Vector3f &v) const;
  61. // create a quaternion from Euler angles
  62. void from_euler(float roll, float pitch, float yaw);
  63. void from_vector312(float roll ,float pitch, float yaw);
  64. void to_axis_angle(Vector3f &v);
  65. void from_axis_angle(Vector3f v);
  66. void from_axis_angle(const Vector3f &axis, float theta);
  67. void rotate(const Vector3f &v);
  68. void from_axis_angle_fast(Vector3f v);
  69. void from_axis_angle_fast(const Vector3f &axis, float theta);
  70. void rotate_fast(const Vector3f &v);
  71. // get euler roll angle
  72. float get_euler_roll() const;
  73. // get euler pitch angle
  74. float get_euler_pitch() const;
  75. // get euler yaw angle
  76. float get_euler_yaw() const;
  77. // create eulers from a quaternion
  78. void to_euler(float &roll, float &pitch, float &yaw) const;
  79. // create eulers from a quaternion
  80. Vector3f to_vector312(void) const;
  81. float length(void) const;
  82. void normalize();
  83. // initialise the quaternion to no rotation
  84. void initialise()
  85. {
  86. q1 = 1.0f;
  87. q2 = q3 = q4 = 0.0f;
  88. }
  89. Quaternion inverse(void) const;
  90. // allow a quaternion to be used as an array, 0 indexed
  91. float & operator[](uint8_t i)
  92. {
  93. float *_v = &q1;
  94. #if MATH_CHECK_INDEXES
  95. assert(i < 4);
  96. #endif
  97. return _v[i];
  98. }
  99. const float & operator[](uint8_t i) const
  100. {
  101. const float *_v = &q1;
  102. #if MATH_CHECK_INDEXES
  103. assert(i < 4);
  104. #endif
  105. return _v[i];
  106. }
  107. Quaternion operator*(const Quaternion &v) const;
  108. Quaternion &operator*=(const Quaternion &v);
  109. Quaternion operator/(const Quaternion &v) const;
  110. // angular difference between quaternions
  111. Quaternion angular_difference(const Quaternion &v) const;
  112. };