float_math.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. #include "float_math.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <assert.h>
  6. #include <math.h>
  7. /*----------------------------------------------------------------------
  8. Copyright (c) 2004 Open Dynamics Framework Group
  9. www.physicstools.org
  10. All rights reserved.
  11. Redistribution and use in source and binary forms, with or without modification, are permitted provided
  12. that the following conditions are met:
  13. Redistributions of source code must retain the above copyright notice, this list of conditions
  14. and the following disclaimer.
  15. Redistributions in binary form must reproduce the above copyright notice,
  16. this list of conditions and the following disclaimer in the documentation
  17. and/or other materials provided with the distribution.
  18. Neither the name of the Open Dynamics Framework Group nor the names of its contributors may
  19. be used to endorse or promote products derived from this software without specific prior written permission.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES,
  21. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. DISCLAIMED. IN NO EVENT SHALL THE INTEL OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  23. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  25. IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26. THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. -----------------------------------------------------------------------*/
  28. // http://codesuppository.blogspot.com
  29. //
  30. // mailto: [email protected]
  31. //
  32. // http://www.amillionpixels.us
  33. //
  34. void fm_inverseRT(const float *matrix,const float *pos,float *t) // inverse rotate translate the point.
  35. {
  36. float _x = pos[0] - matrix[3*4+0];
  37. float _y = pos[1] - matrix[3*4+1];
  38. float _z = pos[2] - matrix[3*4+2];
  39. // Multiply inverse-translated source vector by inverted rotation transform
  40. t[0] = (matrix[0*4+0] * _x) + (matrix[0*4+1] * _y) + (matrix[0*4+2] * _z);
  41. t[1] = (matrix[1*4+0] * _x) + (matrix[1*4+1] * _y) + (matrix[1*4+2] * _z);
  42. t[2] = (matrix[2*4+0] * _x) + (matrix[2*4+1] * _y) + (matrix[2*4+2] * _z);
  43. }
  44. void fm_identity(float *matrix) // set 4x4 matrix to identity.
  45. {
  46. matrix[0*4+0] = 1;
  47. matrix[1*4+1] = 1;
  48. matrix[2*4+2] = 1;
  49. matrix[3*4+3] = 1;
  50. matrix[1*4+0] = 0;
  51. matrix[2*4+0] = 0;
  52. matrix[3*4+0] = 0;
  53. matrix[0*4+1] = 0;
  54. matrix[2*4+1] = 0;
  55. matrix[3*4+1] = 0;
  56. matrix[0*4+2] = 0;
  57. matrix[1*4+2] = 0;
  58. matrix[3*4+2] = 0;
  59. matrix[0*4+3] = 0;
  60. matrix[1*4+3] = 0;
  61. matrix[2*4+3] = 0;
  62. }
  63. void fm_eulerMatrix(float ax,float ay,float az,float *matrix) // convert euler (in radians) to a dest 4x4 matrix (translation set to zero)
  64. {
  65. float quat[4];
  66. fm_eulerToQuat(ax,ay,az,quat);
  67. fm_quatToMatrix(quat,matrix);
  68. }
  69. void fm_getAABB(unsigned int vcount,const float *points,unsigned int pstride,float *bmin,float *bmax)
  70. {
  71. const unsigned char *source = (const unsigned char *) points;
  72. bmin[0] = points[0];
  73. bmin[1] = points[1];
  74. bmin[2] = points[2];
  75. bmax[0] = points[0];
  76. bmax[1] = points[1];
  77. bmax[2] = points[2];
  78. for (unsigned int i=1; i<vcount; i++)
  79. {
  80. source+=pstride;
  81. const float *p = (const float *) source;
  82. if ( p[0] < bmin[0] ) bmin[0] = p[0];
  83. if ( p[1] < bmin[1] ) bmin[1] = p[1];
  84. if ( p[2] < bmin[2] ) bmin[2] = p[2];
  85. if ( p[0] > bmax[0] ) bmax[0] = p[0];
  86. if ( p[1] > bmax[1] ) bmax[1] = p[1];
  87. if ( p[2] > bmax[2] ) bmax[2] = p[2];
  88. }
  89. }
  90. void fm_eulerToQuat(float roll,float pitch,float yaw,float *quat) // convert euler angles to quaternion.
  91. {
  92. roll *= 0.5f;
  93. pitch *= 0.5f;
  94. yaw *= 0.5f;
  95. float cr = cosf(roll);
  96. float cp = cosf(pitch);
  97. float cy = cosf(yaw);
  98. float sr = sinf(roll);
  99. float sp = sinf(pitch);
  100. float sy = sinf(yaw);
  101. float cpcy = cp * cy;
  102. float spsy = sp * sy;
  103. float spcy = sp * cy;
  104. float cpsy = cp * sy;
  105. quat[0] = ( sr * cpcy - cr * spsy);
  106. quat[1] = ( cr * spcy + sr * cpsy);
  107. quat[2] = ( cr * cpsy - sr * spcy);
  108. quat[3] = cr * cpcy + sr * spsy;
  109. }
  110. void fm_quatToMatrix(const float *quat,float *matrix) // convert quaterinion rotation to matrix, zeros out the translation component.
  111. {
  112. float xx = quat[0]*quat[0];
  113. float yy = quat[1]*quat[1];
  114. float zz = quat[2]*quat[2];
  115. float xy = quat[0]*quat[1];
  116. float xz = quat[0]*quat[2];
  117. float yz = quat[1]*quat[2];
  118. float wx = quat[3]*quat[0];
  119. float wy = quat[3]*quat[1];
  120. float wz = quat[3]*quat[2];
  121. matrix[0*4+0] = 1 - 2 * ( yy + zz );
  122. matrix[1*4+0] = 2 * ( xy - wz );
  123. matrix[2*4+0] = 2 * ( xz + wy );
  124. matrix[0*4+1] = 2 * ( xy + wz );
  125. matrix[1*4+1] = 1 - 2 * ( xx + zz );
  126. matrix[2*4+1] = 2 * ( yz - wx );
  127. matrix[0*4+2] = 2 * ( xz - wy );
  128. matrix[1*4+2] = 2 * ( yz + wx );
  129. matrix[2*4+2] = 1 - 2 * ( xx + yy );
  130. matrix[3*4+0] = matrix[3*4+1] = matrix[3*4+2] = 0.0f;
  131. matrix[0*4+3] = matrix[1*4+3] = matrix[2*4+3] = 0.0f;
  132. matrix[3*4+3] = 1.0f;
  133. }
  134. void fm_quatRotate(const float *quat,const float *v,float *r) // rotate a vector directly by a quaternion.
  135. {
  136. float left[4];
  137. left[0] = quat[3]*v[0] + quat[1]*v[2] - v[1]*quat[2];
  138. left[1] = quat[3]*v[1] + quat[2]*v[0] - v[2]*quat[0];
  139. left[2] = quat[3]*v[2] + quat[0]*v[1] - v[0]*quat[1];
  140. left[3] = - quat[0]*v[0] - quat[1]*v[1] - quat[2]*v[2];
  141. r[0] = (left[3]*-quat[0]) + (quat[3]*left[0]) + (left[1]*-quat[2]) - (-quat[1]*left[2]);
  142. r[1] = (left[3]*-quat[1]) + (quat[3]*left[1]) + (left[2]*-quat[0]) - (-quat[2]*left[0]);
  143. r[2] = (left[3]*-quat[2]) + (quat[3]*left[2]) + (left[0]*-quat[1]) - (-quat[0]*left[1]);
  144. }
  145. void fm_getTranslation(const float *matrix,float *t)
  146. {
  147. t[0] = matrix[3*4+0];
  148. t[1] = matrix[3*4+1];
  149. t[2] = matrix[3*4+2];
  150. }
  151. void fm_matrixToQuat(const float *matrix,float *quat) // convert the 3x3 portion of a 4x4 matrix into a quaterion as x,y,z,w
  152. {
  153. float tr = matrix[0*4+0] + matrix[1*4+1] + matrix[2*4+2];
  154. // check the diagonal
  155. if (tr > 0.0f )
  156. {
  157. float s = (float) sqrt ( (double) (tr + 1.0f) );
  158. quat[3] = s * 0.5f;
  159. s = 0.5f / s;
  160. quat[0] = (matrix[1*4+2] - matrix[2*4+1]) * s;
  161. quat[1] = (matrix[2*4+0] - matrix[0*4+2]) * s;
  162. quat[2] = (matrix[0*4+1] - matrix[1*4+0]) * s;
  163. }
  164. else
  165. {
  166. // diagonal is negative
  167. int nxt[3] = {1, 2, 0};
  168. float qa[4];
  169. int i = 0;
  170. if (matrix[1*4+1] > matrix[0*4+0]) i = 1;
  171. if (matrix[2*4+2] > matrix[i*4+i]) i = 2;
  172. int j = nxt[i];
  173. int k = nxt[j];
  174. float s = sqrtf ( ((matrix[i*4+i] - (matrix[j*4+j] + matrix[k*4+k])) + 1.0f) );
  175. qa[i] = s * 0.5f;
  176. if (s != 0.0f ) s = 0.5f / s;
  177. qa[3] = (matrix[j*4+k] - matrix[k*4+j]) * s;
  178. qa[j] = (matrix[i*4+j] + matrix[j*4+i]) * s;
  179. qa[k] = (matrix[i*4+k] + matrix[k*4+i]) * s;
  180. quat[0] = qa[0];
  181. quat[1] = qa[1];
  182. quat[2] = qa[2];
  183. quat[3] = qa[3];
  184. }
  185. }
  186. float fm_sphereVolume(float radius) // return's the volume of a sphere of this radius (4/3 PI * R cubed )
  187. {
  188. return (4.0f / 3.0f ) * FM_PI * radius * radius * radius;
  189. }