helper_3dmath.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // I2C device class (I2Cdev) demonstration Arduino sketch for MPU6050 class, 3D math helper
  2. // 6/5/2012 by Jeff Rowberg <jeff@rowberg.net>
  3. // Updates should (hopefully) always be available at https://github.com/jrowberg/i2cdevlib
  4. //
  5. // Changelog:
  6. // 2012-06-05 - add 3D math helper file to DMP6 example sketch
  7. /* ============================================
  8. I2Cdev device library code is placed under the MIT license
  9. Copyright (c) 2012 Jeff Rowberg
  10. Permission is hereby granted, free of charge, to any person obtaining a copy
  11. of this software and associated documentation files (the "Software"), to deal
  12. in the Software without restriction, including without limitation the rights
  13. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  14. copies of the Software, and to permit persons to whom the Software is
  15. furnished to do so, subject to the following conditions:
  16. The above copyright notice and this permission notice shall be included in
  17. all copies or substantial portions of the Software.
  18. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. THE SOFTWARE.
  25. ===============================================
  26. */
  27. #ifndef _HELPER_3DMATH_H_
  28. #define _HELPER_3DMATH_H_
  29. class Quaternion {
  30. public:
  31. float w;
  32. float x;
  33. float y;
  34. float z;
  35. Quaternion() {
  36. w = 1.0f;
  37. x = 0.0f;
  38. y = 0.0f;
  39. z = 0.0f;
  40. }
  41. Quaternion(float nw, float nx, float ny, float nz) {
  42. w = nw;
  43. x = nx;
  44. y = ny;
  45. z = nz;
  46. }
  47. Quaternion getProduct(Quaternion q) {
  48. // Quaternion multiplication is defined by:
  49. // (Q1 * Q2).w = (w1w2 - x1x2 - y1y2 - z1z2)
  50. // (Q1 * Q2).x = (w1x2 + x1w2 + y1z2 - z1y2)
  51. // (Q1 * Q2).y = (w1y2 - x1z2 + y1w2 + z1x2)
  52. // (Q1 * Q2).z = (w1z2 + x1y2 - y1x2 + z1w2
  53. return Quaternion(
  54. w*q.w - x*q.x - y*q.y - z*q.z, // new w
  55. w*q.x + x*q.w + y*q.z - z*q.y, // new x
  56. w*q.y - x*q.z + y*q.w + z*q.x, // new y
  57. w*q.z + x*q.y - y*q.x + z*q.w); // new z
  58. }
  59. Quaternion getConjugate() {
  60. return Quaternion(w, -x, -y, -z);
  61. }
  62. float getMagnitude() {
  63. return sqrt(w*w + x*x + y*y + z*z);
  64. }
  65. void normalize() {
  66. float m = getMagnitude();
  67. w /= m;
  68. x /= m;
  69. y /= m;
  70. z /= m;
  71. }
  72. Quaternion getNormalized() {
  73. Quaternion r(w, x, y, z);
  74. r.normalize();
  75. return r;
  76. }
  77. };
  78. class VectorInt16 {
  79. public:
  80. int16_t x;
  81. int16_t y;
  82. int16_t z;
  83. VectorInt16() {
  84. x = 0;
  85. y = 0;
  86. z = 0;
  87. }
  88. VectorInt16(int16_t nx, int16_t ny, int16_t nz) {
  89. x = nx;
  90. y = ny;
  91. z = nz;
  92. }
  93. float getMagnitude() {
  94. return sqrt(x*x + y*y + z*z);
  95. }
  96. void normalize() {
  97. float m = getMagnitude();
  98. x /= m;
  99. y /= m;
  100. z /= m;
  101. }
  102. VectorInt16 getNormalized() {
  103. VectorInt16 r(x, y, z);
  104. r.normalize();
  105. return r;
  106. }
  107. void rotate(Quaternion *q) {
  108. // http://www.cprogramming.com/tutorial/3d/quaternions.html
  109. // http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/transforms/index.htm
  110. // http://content.gpwiki.org/index.php/OpenGL:Tutorials:Using_Quaternions_to_represent_rotation
  111. // ^ or: http://webcache.googleusercontent.com/search?q=cache:xgJAp3bDNhQJ:content.gpwiki.org/index.php/OpenGL:Tutorials:Using_Quaternions_to_represent_rotation&hl=en&gl=us&strip=1
  112. // P_out = q * P_in * conj(q)
  113. // - P_out is the output vector
  114. // - q is the orientation quaternion
  115. // - P_in is the input vector (a*aReal)
  116. // - conj(q) is the conjugate of the orientation quaternion (q=[w,x,y,z], q*=[w,-x,-y,-z])
  117. Quaternion p(0, x, y, z);
  118. // quaternion multiplication: q * p, stored back in p
  119. p = q -> getProduct(p);
  120. // quaternion multiplication: p * conj(q), stored back in p
  121. p = p.getProduct(q -> getConjugate());
  122. // p quaternion is now [0, x', y', z']
  123. x = p.x;
  124. y = p.y;
  125. z = p.z;
  126. }
  127. VectorInt16 getRotated(Quaternion *q) {
  128. VectorInt16 r(x, y, z);
  129. r.rotate(q);
  130. return r;
  131. }
  132. };
  133. class VectorFloat {
  134. public:
  135. float x;
  136. float y;
  137. float z;
  138. VectorFloat() {
  139. x = 0;
  140. y = 0;
  141. z = 0;
  142. }
  143. VectorFloat(float nx, float ny, float nz) {
  144. x = nx;
  145. y = ny;
  146. z = nz;
  147. }
  148. float getMagnitude() {
  149. return sqrt(x*x + y*y + z*z);
  150. }
  151. void normalize() {
  152. float m = getMagnitude();
  153. x /= m;
  154. y /= m;
  155. z /= m;
  156. }
  157. VectorFloat getNormalized() {
  158. VectorFloat r(x, y, z);
  159. r.normalize();
  160. return r;
  161. }
  162. void rotate(Quaternion *q) {
  163. Quaternion p(0, x, y, z);
  164. // quaternion multiplication: q * p, stored back in p
  165. p = q -> getProduct(p);
  166. // quaternion multiplication: p * conj(q), stored back in p
  167. p = p.getProduct(q -> getConjugate());
  168. // p quaternion is now [0, x', y', z']
  169. x = p.x;
  170. y = p.y;
  171. z = p.z;
  172. }
  173. VectorFloat getRotated(Quaternion *q) {
  174. VectorFloat r(x, y, z);
  175. r.rotate(q);
  176. return r;
  177. }
  178. };
  179. #endif /* _HELPER_3DMATH_H_ */