mSolver.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "math/mMathFn.h"
  24. //--------------------------------------------------------------------------
  25. #define EQN_EPSILON (1e-8)
  26. static inline void swap(F32 & a, F32 & b)
  27. {
  28. F32 t = b;
  29. b = a;
  30. a = t;
  31. }
  32. static inline F32 mCbrt(F32 val)
  33. {
  34. if(val < 0.f)
  35. return(-mPow(-val, F32(1.f/3.f)));
  36. else
  37. return(mPow(val, F32(1.f/3.f)));
  38. }
  39. static inline U32 mSolveLinear(F32 a, F32 b, F32 * x)
  40. {
  41. if(mIsZero(a))
  42. return(0);
  43. x[0] = -b/a;
  44. return(1);
  45. }
  46. static U32 mSolveQuadratic_c(F32 a, F32 b, F32 c, F32 * x)
  47. {
  48. // really linear?
  49. if(mIsZero(a))
  50. return(mSolveLinear(b, c, x));
  51. // get the descriminant: (b^2 - 4ac)
  52. F32 desc = (b * b) - (4.f * a * c);
  53. // solutions:
  54. // desc < 0: two imaginary solutions
  55. // desc > 0: two real solutions (b +- sqrt(desc)) / 2a
  56. // desc = 0: one real solution (b / 2a)
  57. if(mIsZero(desc))
  58. {
  59. x[0] = b / (2.f * a);
  60. return(1);
  61. }
  62. else if(desc > 0.f)
  63. {
  64. F32 sqrdesc = mSqrt(desc);
  65. F32 den = (2.f * a);
  66. x[0] = (-b + sqrdesc) / den;
  67. x[1] = (-b - sqrdesc) / den;
  68. if(x[1] < x[0])
  69. swap(x[0], x[1]);
  70. return(2);
  71. }
  72. else
  73. return(0);
  74. }
  75. //--------------------------------------------------------------------------
  76. // from Graphics Gems I: pp 738-742
  77. U32 mSolveCubic_c(F32 a, F32 b, F32 c, F32 d, F32 * x)
  78. {
  79. if(mIsZero(a))
  80. return(mSolveQuadratic(b, c, d, x));
  81. // normal form: x^3 + Ax^2 + BX + C = 0
  82. F32 A = b / a;
  83. F32 B = c / a;
  84. F32 C = d / a;
  85. // substitute x = y - A/3 to eliminate quadric term and depress
  86. // the cubic equation to (x^3 + px + q = 0)
  87. F32 A2 = A * A;
  88. F32 A3 = A2 * A;
  89. F32 p = (1.f/3.f) * (((-1.f/3.f) * A2) + B);
  90. F32 q = (1.f/2.f) * (((2.f/27.f) * A3) - ((1.f/3.f) * A * B) + C);
  91. // use Cardano's fomula to solve the depressed cubic
  92. F32 p3 = p * p * p;
  93. F32 q2 = q * q;
  94. F32 D = q2 + p3;
  95. U32 num = 0;
  96. if(mIsZero(D)) // 1 or 2 solutions
  97. {
  98. if(mIsZero(q)) // 1 triple solution
  99. {
  100. x[0] = 0.f;
  101. num = 1;
  102. }
  103. else // 1 single and 1 double
  104. {
  105. F32 u = mCbrt(-q);
  106. x[0] = 2.f * u;
  107. x[1] = -u;
  108. num = 2;
  109. }
  110. }
  111. else if(D < 0.f) // 3 solutions: casus irreducibilis
  112. {
  113. F32 phi = (1.f/3.f) * mAcos(-q / mSqrt(-p3));
  114. F32 t = 2.f * mSqrt(-p);
  115. x[0] = t * mCos(phi);
  116. x[1] = -t * mCos(phi + (M_PI / 3.f));
  117. x[2] = -t * mCos(phi - (M_PI / 3.f));
  118. num = 3;
  119. }
  120. else // 1 solution
  121. {
  122. F32 sqrtD = mSqrt(D);
  123. F32 u = mCbrt(sqrtD - q);
  124. F32 v = -mCbrt(sqrtD + q);
  125. x[0] = u + v;
  126. num = 1;
  127. }
  128. // resubstitute
  129. F32 sub = (1.f/3.f) * A;
  130. for(U32 i = 0; i < num; i++)
  131. x[i] -= sub;
  132. // sort the roots
  133. for(S32 j = 0; j < (num - 1); j++)
  134. for(S32 k = j + 1; k < num; k++)
  135. if(x[k] < x[j])
  136. swap(x[k], x[j]);
  137. return(num);
  138. }
  139. //--------------------------------------------------------------------------
  140. // from Graphics Gems I: pp 738-742
  141. U32 mSolveQuartic_c(F32 a, F32 b, F32 c, F32 d, F32 e, F32 * x)
  142. {
  143. if(mIsZero(a))
  144. return(mSolveCubic(b, c, d, e, x));
  145. // normal form: x^4 + ax^3 + bx^2 + cx + d = 0
  146. F32 A = b / a;
  147. F32 B = c / a;
  148. F32 C = d / a;
  149. F32 D = e / a;
  150. // substitue x = y - A/4 to eliminate cubic term:
  151. // x^4 + px^2 + qx + r = 0
  152. F32 A2 = A * A;
  153. F32 A3 = A2 * A;
  154. F32 A4 = A2 * A2;
  155. F32 p = ((-3.f/8.f) * A2) + B;
  156. F32 q = ((1.f/8.f) * A3) - ((1.f/2.f) * A * B) + C;
  157. F32 r = ((-3.f/256.f) * A4) + ((1.f/16.f) * A2 * B) - ((1.f/4.f) * A * C) + D;
  158. U32 num = 0;
  159. if(mIsZero(r)) // no absolute term: y(y^3 + py + q) = 0
  160. {
  161. num = mSolveCubic(1.f, 0.f, p, q, x);
  162. x[num++] = 0.f;
  163. }
  164. else
  165. {
  166. // solve the resolvent cubic
  167. F32 q2 = q * q;
  168. a = 1.f;
  169. b = (-1.f/2.f) * p;
  170. c = -r;
  171. d = ((1.f/2.f) * r * p) - ((1.f/8.f) * q2);
  172. mSolveCubic(a, b, c, d, x);
  173. F32 z = x[0];
  174. // build 2 quadratic equations from the one solution
  175. F32 u = (z * z) - r;
  176. F32 v = (2.f * z) - p;
  177. if(mIsZero(u))
  178. u = 0.f;
  179. else if(u > 0.f)
  180. u = mSqrt(u);
  181. else
  182. return(0);
  183. if(mIsZero(v))
  184. v = 0.f;
  185. else if(v > 0.f)
  186. v = mSqrt(v);
  187. else
  188. return(0);
  189. // solve the two quadratics
  190. a = 1.f;
  191. b = v;
  192. c = z - u;
  193. num = mSolveQuadratic(a, b, c, x);
  194. a = 1.f;
  195. b = -v;
  196. c = z + u;
  197. num += mSolveQuadratic(a, b, c, x + num);
  198. }
  199. // resubstitute
  200. F32 sub = (1.f/4.f) * A;
  201. for(U32 i = 0; i < num; i++)
  202. x[i] -= sub;
  203. // sort the roots
  204. for(S32 j = 0; j < (num - 1); j++)
  205. for(S32 k = j + 1; k < num; k++)
  206. if(x[k] < x[j])
  207. swap(x[k], x[j]);
  208. return(num);
  209. }
  210. U32 (*mSolveQuadratic)( F32 a, F32 b, F32 c, F32* x ) = mSolveQuadratic_c;
  211. U32 (*mSolveCubic)( F32 a, F32 b, F32 c, F32 d, F32* x ) = mSolveCubic_c;
  212. U32 (*mSolveQuartic)( F32 a, F32 b, F32 c, F32 d, F32 e, F32* x ) = mSolveQuartic_c;