matrix3.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * matrix3.cpp
  3. * Copyright (C) Andrew Tridgell 2012
  4. *
  5. * This file is free software: you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the
  7. * Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This file is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. * See the GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #pragma GCC optimize("O2")
  19. #include "AP_Math.h"
  20. // create a rotation matrix given some euler angles
  21. // this is based on http://gentlenav.googlecode.com/files/EulerAngles.pdf
  22. template <typename T>
  23. void Matrix3<T>::from_euler(float roll, float pitch, float yaw)
  24. {
  25. const float cp = cosf(pitch);
  26. const float sp = sinf(pitch);
  27. const float sr = sinf(roll);
  28. const float cr = cosf(roll);
  29. const float sy = sinf(yaw);
  30. const float cy = cosf(yaw);
  31. a.x = cp * cy;
  32. a.y = (sr * sp * cy) - (cr * sy);
  33. a.z = (cr * sp * cy) + (sr * sy);
  34. b.x = cp * sy;
  35. b.y = (sr * sp * sy) + (cr * cy);
  36. b.z = (cr * sp * sy) - (sr * cy);
  37. c.x = -sp;
  38. c.y = sr * cp;
  39. c.z = cr * cp;
  40. }
  41. // calculate euler angles from a rotation matrix
  42. // this is based on http://gentlenav.googlecode.com/files/EulerAngles.pdf
  43. template <typename T>
  44. void Matrix3<T>::to_euler(float *roll, float *pitch, float *yaw) const
  45. {
  46. if (pitch != nullptr) {
  47. *pitch = -safe_asin(c.x);
  48. }
  49. if (roll != nullptr) {
  50. *roll = atan2f(c.y, c.z);
  51. }
  52. if (yaw != nullptr) {
  53. *yaw = atan2f(b.x, a.x);
  54. }
  55. }
  56. template <typename T>
  57. void Matrix3<T>::from_rotation(enum Rotation rotation)
  58. {
  59. (*this).a(1,0,0);
  60. (*this).b(0,1,0);
  61. (*this).c(0,0,1);
  62. (*this).a.rotate(rotation);
  63. (*this).b.rotate(rotation);
  64. (*this).c.rotate(rotation);
  65. (*this).transpose();
  66. }
  67. /*
  68. calculate Euler angles (312 convention) for the matrix.
  69. See http://www.atacolorado.com/eulersequences.doc
  70. vector is returned in r, p, y order
  71. */
  72. template <typename T>
  73. Vector3<T> Matrix3<T>::to_euler312() const
  74. {
  75. return Vector3<T>(asinf(c.y),
  76. atan2f(-c.x, c.z),
  77. atan2f(-a.y, b.y));
  78. }
  79. /*
  80. fill the matrix from Euler angles in radians in 312 convention
  81. */
  82. template <typename T>
  83. void Matrix3<T>::from_euler312(float roll, float pitch, float yaw)
  84. {
  85. const float c3 = cosf(pitch);
  86. const float s3 = sinf(pitch);
  87. const float s2 = sinf(roll);
  88. const float c2 = cosf(roll);
  89. const float s1 = sinf(yaw);
  90. const float c1 = cosf(yaw);
  91. a.x = c1 * c3 - s1 * s2 * s3;
  92. b.y = c1 * c2;
  93. c.z = c3 * c2;
  94. a.y = -c2*s1;
  95. a.z = s3*c1 + c3*s2*s1;
  96. b.x = c3*s1 + s3*s2*c1;
  97. b.z = s1*s3 - s2*c1*c3;
  98. c.x = -s3*c2;
  99. c.y = s2;
  100. }
  101. // apply an additional rotation from a body frame gyro vector
  102. // to a rotation matrix.
  103. template <typename T>
  104. void Matrix3<T>::rotate(const Vector3<T> &g)
  105. {
  106. (*this) += Matrix3<T>{
  107. a.y * g.z - a.z * g.y, a.z * g.x - a.x * g.z, a.x * g.y - a.y * g.x,
  108. b.y * g.z - b.z * g.y, b.z * g.x - b.x * g.z, b.x * g.y - b.y * g.x,
  109. c.y * g.z - c.z * g.y, c.z * g.x - c.x * g.z, c.x * g.y - c.y * g.x
  110. };
  111. }
  112. /*
  113. re-normalise a rotation matrix
  114. */
  115. template <typename T>
  116. void Matrix3<T>::normalize(void)
  117. {
  118. const float error = a * b;
  119. const Vector3<T> t0 = a - (b * (0.5f * error));
  120. const Vector3<T> t1 = b - (a * (0.5f * error));
  121. const Vector3<T> t2 = t0 % t1;
  122. a = t0 * (1.0f / t0.length());
  123. b = t1 * (1.0f / t1.length());
  124. c = t2 * (1.0f / t2.length());
  125. }
  126. // multiplication by a vector
  127. template <typename T>
  128. Vector3<T> Matrix3<T>::operator *(const Vector3<T> &v) const
  129. {
  130. return Vector3<T>(a.x * v.x + a.y * v.y + a.z * v.z,
  131. b.x * v.x + b.y * v.y + b.z * v.z,
  132. c.x * v.x + c.y * v.y + c.z * v.z);
  133. }
  134. // multiplication by a vector, extracting only the xy components
  135. template <typename T>
  136. Vector2<T> Matrix3<T>::mulXY(const Vector3<T> &v) const
  137. {
  138. return Vector2<T>(a.x * v.x + a.y * v.y + a.z * v.z,
  139. b.x * v.x + b.y * v.y + b.z * v.z);
  140. }
  141. // multiplication of transpose by a vector
  142. template <typename T>
  143. Vector3<T> Matrix3<T>::mul_transpose(const Vector3<T> &v) const
  144. {
  145. return Vector3<T>(a.x * v.x + b.x * v.y + c.x * v.z,
  146. a.y * v.x + b.y * v.y + c.y * v.z,
  147. a.z * v.x + b.z * v.y + c.z * v.z);
  148. }
  149. // multiplication by another Matrix3<T>
  150. template <typename T>
  151. Matrix3<T> Matrix3<T>::operator *(const Matrix3<T> &m) const
  152. {
  153. Matrix3<T> temp (Vector3<T>(a.x * m.a.x + a.y * m.b.x + a.z * m.c.x,
  154. a.x * m.a.y + a.y * m.b.y + a.z * m.c.y,
  155. a.x * m.a.z + a.y * m.b.z + a.z * m.c.z),
  156. Vector3<T>(b.x * m.a.x + b.y * m.b.x + b.z * m.c.x,
  157. b.x * m.a.y + b.y * m.b.y + b.z * m.c.y,
  158. b.x * m.a.z + b.y * m.b.z + b.z * m.c.z),
  159. Vector3<T>(c.x * m.a.x + c.y * m.b.x + c.z * m.c.x,
  160. c.x * m.a.y + c.y * m.b.y + c.z * m.c.y,
  161. c.x * m.a.z + c.y * m.b.z + c.z * m.c.z));
  162. return temp;
  163. }
  164. template <typename T>
  165. Matrix3<T> Matrix3<T>::transposed(void) const
  166. {
  167. return Matrix3<T>(Vector3<T>(a.x, b.x, c.x),
  168. Vector3<T>(a.y, b.y, c.y),
  169. Vector3<T>(a.z, b.z, c.z));
  170. }
  171. template <typename T>
  172. T Matrix3<T>::det() const
  173. {
  174. return a.x * (b.y * c.z - b.z * c.y) +
  175. a.y * (b.z * c.x - b.x * c.z) +
  176. a.z * (b.x * c.y - b.y * c.x);
  177. }
  178. template <typename T>
  179. bool Matrix3<T>::inverse(Matrix3<T>& inv) const
  180. {
  181. const T d = det();
  182. if (is_zero(d)) {
  183. return false;
  184. }
  185. inv.a.x = (b.y * c.z - c.y * b.z) / d;
  186. inv.a.y = (a.z * c.y - a.y * c.z) / d;
  187. inv.a.z = (a.y * b.z - a.z * b.y) / d;
  188. inv.b.x = (b.z * c.x - b.x * c.z) / d;
  189. inv.b.y = (a.x * c.z - a.z * c.x) / d;
  190. inv.b.z = (b.x * a.z - a.x * b.z) / d;
  191. inv.c.x = (b.x * c.y - c.x * b.y) / d;
  192. inv.c.y = (c.x * a.y - a.x * c.y) / d;
  193. inv.c.z = (a.x * b.y - b.x * a.y) / d;
  194. return true;
  195. }
  196. template <typename T>
  197. bool Matrix3<T>::invert()
  198. {
  199. Matrix3<T> inv;
  200. bool success = inverse(inv);
  201. if (success) {
  202. *this = inv;
  203. }
  204. return success;
  205. }
  206. template <typename T>
  207. void Matrix3<T>::zero(void)
  208. {
  209. a.x = a.y = a.z = 0;
  210. b.x = b.y = b.z = 0;
  211. c.x = c.y = c.z = 0;
  212. }
  213. // create rotation matrix for rotation about the vector v by angle theta
  214. // See: http://www.euclideanspace.com/maths/geometry/rotations/conversions/angleToMatrix/
  215. template <typename T>
  216. void Matrix3<T>::from_axis_angle(const Vector3<T> &v, float theta)
  217. {
  218. const float C = cosf(theta);
  219. const float S = sinf(theta);
  220. const float t = 1.0f - C;
  221. const Vector3f normv = v.normalized();
  222. const float x = normv.x;
  223. const float y = normv.y;
  224. const float z = normv.z;
  225. a.x = t*x*x + C;
  226. a.y = t*x*y - z*S;
  227. a.z = t*x*z + y*S;
  228. b.x = t*x*y + z*S;
  229. b.y = t*y*y + C;
  230. b.z = t*y*z - x*S;
  231. c.x = t*x*z - y*S;
  232. c.y = t*y*z + x*S;
  233. c.z = t*z*z + C;
  234. }
  235. // only define for float
  236. template void Matrix3<float>::zero(void);
  237. template void Matrix3<float>::rotate(const Vector3<float> &g);
  238. template void Matrix3<float>::normalize(void);
  239. template void Matrix3<float>::from_euler(float roll, float pitch, float yaw);
  240. template void Matrix3<float>::to_euler(float *roll, float *pitch, float *yaw) const;
  241. template void Matrix3<float>::from_rotation(enum Rotation rotation);
  242. template void Matrix3<float>::from_euler312(float roll, float pitch, float yaw);
  243. template void Matrix3<float>::from_axis_angle(const Vector3<float> &v, float theta);
  244. template Vector3<float> Matrix3<float>::to_euler312(void) const;
  245. template Vector3<float> Matrix3<float>::operator *(const Vector3<float> &v) const;
  246. template Vector3<float> Matrix3<float>::mul_transpose(const Vector3<float> &v) const;
  247. template Matrix3<float> Matrix3<float>::operator *(const Matrix3<float> &m) const;
  248. template Matrix3<float> Matrix3<float>::transposed(void) const;
  249. template float Matrix3<float>::det() const;
  250. template bool Matrix3<float>::inverse(Matrix3<float>& inv) const;
  251. template bool Matrix3<float>::invert();
  252. template Vector2<float> Matrix3<float>::mulXY(const Vector3<float> &v) const;
  253. template void Matrix3<double>::zero(void);
  254. template void Matrix3<double>::rotate(const Vector3<double> &g);
  255. template void Matrix3<double>::from_euler(float roll, float pitch, float yaw);
  256. template void Matrix3<double>::to_euler(float *roll, float *pitch, float *yaw) const;
  257. template Vector3<double> Matrix3<double>::operator *(const Vector3<double> &v) const;
  258. template Vector3<double> Matrix3<double>::mul_transpose(const Vector3<double> &v) const;
  259. template Matrix3<double> Matrix3<double>::operator *(const Matrix3<double> &m) const;
  260. template Matrix3<double> Matrix3<double>::transposed(void) const;
  261. template double Matrix3<double>::det() const;
  262. template bool Matrix3<double>::inverse(Matrix3<double>& inv) const;
  263. template bool Matrix3<double>::invert();
  264. template Vector2<double> Matrix3<double>::mulXY(const Vector3<double> &v) const;