maths.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /* -----------------------------------------------------------------------------
  2. Copyright (c) 2006 Simon Brown si@sjbrown.co.uk
  3. Permission is hereby granted, free of charge, to any person obtaining
  4. a copy of this software and associated documentation files (the
  5. "Software"), to deal in the Software without restriction, including
  6. without limitation the rights to use, copy, modify, merge, publish,
  7. distribute, sublicense, and/or sell copies of the Software, and to
  8. permit persons to whom the Software is furnished to do so, subject to
  9. the following conditions:
  10. The above copyright notice and this permission notice shall be included
  11. in all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  13. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  14. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  15. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  16. CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  17. TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  18. SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. -------------------------------------------------------------------------- */
  20. /*! @file
  21. The symmetric eigensystem solver algorithm is from
  22. http://www.geometrictools.com/Documentation/EigenSymmetric3x3.pdf
  23. */
  24. #include "maths.h"
  25. #include "simd.h"
  26. #include <cfloat>
  27. namespace squish {
  28. Sym3x3 ComputeWeightedCovariance( int n, Vec3 const* points, float const* weights )
  29. {
  30. // compute the centroid
  31. float total = 0.0f;
  32. Vec3 centroid( 0.0f );
  33. for( int i = 0; i < n; ++i )
  34. {
  35. total += weights[i];
  36. centroid += weights[i]*points[i];
  37. }
  38. if( total > FLT_EPSILON )
  39. centroid /= total;
  40. // accumulate the covariance matrix
  41. Sym3x3 covariance( 0.0f );
  42. for( int i = 0; i < n; ++i )
  43. {
  44. Vec3 a = points[i] - centroid;
  45. Vec3 b = weights[i]*a;
  46. covariance[0] += a.X()*b.X();
  47. covariance[1] += a.X()*b.Y();
  48. covariance[2] += a.X()*b.Z();
  49. covariance[3] += a.Y()*b.Y();
  50. covariance[4] += a.Y()*b.Z();
  51. covariance[5] += a.Z()*b.Z();
  52. }
  53. // return it
  54. return covariance;
  55. }
  56. #if 0
  57. static Vec3 GetMultiplicity1Evector( Sym3x3 const& matrix, float evalue )
  58. {
  59. // compute M
  60. Sym3x3 m;
  61. m[0] = matrix[0] - evalue;
  62. m[1] = matrix[1];
  63. m[2] = matrix[2];
  64. m[3] = matrix[3] - evalue;
  65. m[4] = matrix[4];
  66. m[5] = matrix[5] - evalue;
  67. // compute U
  68. Sym3x3 u;
  69. u[0] = m[3]*m[5] - m[4]*m[4];
  70. u[1] = m[2]*m[4] - m[1]*m[5];
  71. u[2] = m[1]*m[4] - m[2]*m[3];
  72. u[3] = m[0]*m[5] - m[2]*m[2];
  73. u[4] = m[1]*m[2] - m[4]*m[0];
  74. u[5] = m[0]*m[3] - m[1]*m[1];
  75. // find the largest component
  76. float mc = std::fabs( u[0] );
  77. int mi = 0;
  78. for( int i = 1; i < 6; ++i )
  79. {
  80. float c = std::fabs( u[i] );
  81. if( c > mc )
  82. {
  83. mc = c;
  84. mi = i;
  85. }
  86. }
  87. // pick the column with this component
  88. switch( mi )
  89. {
  90. case 0:
  91. return Vec3( u[0], u[1], u[2] );
  92. case 1:
  93. case 3:
  94. return Vec3( u[1], u[3], u[4] );
  95. default:
  96. return Vec3( u[2], u[4], u[5] );
  97. }
  98. }
  99. static Vec3 GetMultiplicity2Evector( Sym3x3 const& matrix, float evalue )
  100. {
  101. // compute M
  102. Sym3x3 m;
  103. m[0] = matrix[0] - evalue;
  104. m[1] = matrix[1];
  105. m[2] = matrix[2];
  106. m[3] = matrix[3] - evalue;
  107. m[4] = matrix[4];
  108. m[5] = matrix[5] - evalue;
  109. // find the largest component
  110. float mc = std::fabs( m[0] );
  111. int mi = 0;
  112. for( int i = 1; i < 6; ++i )
  113. {
  114. float c = std::fabs( m[i] );
  115. if( c > mc )
  116. {
  117. mc = c;
  118. mi = i;
  119. }
  120. }
  121. // pick the first eigenvector based on this index
  122. switch( mi )
  123. {
  124. case 0:
  125. case 1:
  126. return Vec3( -m[1], m[0], 0.0f );
  127. case 2:
  128. return Vec3( m[2], 0.0f, -m[0] );
  129. case 3:
  130. case 4:
  131. return Vec3( 0.0f, -m[4], m[3] );
  132. default:
  133. return Vec3( 0.0f, -m[5], m[4] );
  134. }
  135. }
  136. Vec3 ComputePrincipleComponent( Sym3x3 const& matrix )
  137. {
  138. // compute the cubic coefficients
  139. float c0 = matrix[0]*matrix[3]*matrix[5]
  140. + 2.0f*matrix[1]*matrix[2]*matrix[4]
  141. - matrix[0]*matrix[4]*matrix[4]
  142. - matrix[3]*matrix[2]*matrix[2]
  143. - matrix[5]*matrix[1]*matrix[1];
  144. float c1 = matrix[0]*matrix[3] + matrix[0]*matrix[5] + matrix[3]*matrix[5]
  145. - matrix[1]*matrix[1] - matrix[2]*matrix[2] - matrix[4]*matrix[4];
  146. float c2 = matrix[0] + matrix[3] + matrix[5];
  147. // compute the quadratic coefficients
  148. float a = c1 - ( 1.0f/3.0f )*c2*c2;
  149. float b = ( -2.0f/27.0f )*c2*c2*c2 + ( 1.0f/3.0f )*c1*c2 - c0;
  150. // compute the root count check
  151. float Q = 0.25f*b*b + ( 1.0f/27.0f )*a*a*a;
  152. // test the multiplicity
  153. if( FLT_EPSILON < Q )
  154. {
  155. // only one root, which implies we have a multiple of the identity
  156. return Vec3( 1.0f );
  157. }
  158. else if( Q < -FLT_EPSILON )
  159. {
  160. // three distinct roots
  161. float theta = std::atan2( std::sqrt( -Q ), -0.5f*b );
  162. float rho = std::sqrt( 0.25f*b*b - Q );
  163. float rt = std::pow( rho, 1.0f/3.0f );
  164. float ct = std::cos( theta/3.0f );
  165. float st = std::sin( theta/3.0f );
  166. float l1 = ( 1.0f/3.0f )*c2 + 2.0f*rt*ct;
  167. float l2 = ( 1.0f/3.0f )*c2 - rt*( ct + ( float )sqrt( 3.0f )*st );
  168. float l3 = ( 1.0f/3.0f )*c2 - rt*( ct - ( float )sqrt( 3.0f )*st );
  169. // pick the larger
  170. if( std::fabs( l2 ) > std::fabs( l1 ) )
  171. l1 = l2;
  172. if( std::fabs( l3 ) > std::fabs( l1 ) )
  173. l1 = l3;
  174. // get the eigenvector
  175. return GetMultiplicity1Evector( matrix, l1 );
  176. }
  177. else // if( -FLT_EPSILON <= Q && Q <= FLT_EPSILON )
  178. {
  179. // two roots
  180. float rt;
  181. if( b < 0.0f )
  182. rt = -std::pow( -0.5f*b, 1.0f/3.0f );
  183. else
  184. rt = std::pow( 0.5f*b, 1.0f/3.0f );
  185. float l1 = ( 1.0f/3.0f )*c2 + rt; // repeated
  186. float l2 = ( 1.0f/3.0f )*c2 - 2.0f*rt;
  187. // get the eigenvector
  188. if( std::fabs( l1 ) > std::fabs( l2 ) )
  189. return GetMultiplicity2Evector( matrix, l1 );
  190. else
  191. return GetMultiplicity1Evector( matrix, l2 );
  192. }
  193. }
  194. #else
  195. #define POWER_ITERATION_COUNT 8
  196. Vec3 ComputePrincipleComponent( Sym3x3 const& matrix )
  197. {
  198. Vec4 const row0( matrix[0], matrix[1], matrix[2], 0.0f );
  199. Vec4 const row1( matrix[1], matrix[3], matrix[4], 0.0f );
  200. Vec4 const row2( matrix[2], matrix[4], matrix[5], 0.0f );
  201. Vec4 v = VEC4_CONST( 1.0f );
  202. for( int i = 0; i < POWER_ITERATION_COUNT; ++i )
  203. {
  204. // matrix multiply
  205. Vec4 w = row0*v.SplatX();
  206. w = MultiplyAdd(row1, v.SplatY(), w);
  207. w = MultiplyAdd(row2, v.SplatZ(), w);
  208. // get max component from xyz in all channels
  209. Vec4 a = Max(w.SplatX(), Max(w.SplatY(), w.SplatZ()));
  210. // divide through and advance
  211. v = w*Reciprocal(a);
  212. }
  213. return v.GetVec3();
  214. }
  215. #endif
  216. } // namespace squish