mSolver.cc 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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. static inline F32 mCbrt(F32 val)
  26. {
  27. if(val < 0.f)
  28. return(-mPow(-val, F32(1.f/3.f)));
  29. else
  30. return(mPow(val, F32(1.f/3.f)));
  31. }
  32. static inline U32 mSolveLinear(F32 a, F32 b, F32 * x)
  33. {
  34. if(mIsZero(a))
  35. return(0);
  36. x[0] = -b/a;
  37. return(1);
  38. }
  39. static U32 mSolveQuadratic_c(F32 a, F32 b, F32 c, F32 * x)
  40. {
  41. // really linear?
  42. if(mIsZero(a))
  43. return(mSolveLinear(b, c, x));
  44. // get the descriminant: (b^2 - 4ac)
  45. F32 desc = (b * b) - (4.f * a * c);
  46. // solutions:
  47. // desc < 0: two imaginary solutions
  48. // desc > 0: two real solutions (b +- sqrt(desc)) / 2a
  49. // desc = 0: one real solution (b / 2a)
  50. if(mIsZero(desc))
  51. {
  52. x[0] = b / (2.f * a);
  53. return(1);
  54. }
  55. else if(desc > 0.f)
  56. {
  57. F32 sqrdesc = mSqrt(desc);
  58. F32 den = (2.f * a);
  59. x[0] = (-b + sqrdesc) / den;
  60. x[1] = (-b - sqrdesc) / den;
  61. if(x[1] < x[0])
  62. mSwap(x[0], x[1]);
  63. return(2);
  64. }
  65. else
  66. return(0);
  67. }
  68. //--------------------------------------------------------------------------
  69. // from Graphics Gems I: pp 738-742
  70. U32 mSolveCubic_c(F32 a, F32 b, F32 c, F32 d, F32 * x)
  71. {
  72. if(mIsZero(a))
  73. return(mSolveQuadratic(b, c, d, x));
  74. // normal form: x^3 + Ax^2 + BX + C = 0
  75. F32 A = b / a;
  76. F32 B = c / a;
  77. F32 C = d / a;
  78. // substitute x = y - A/3 to eliminate quadric term and depress
  79. // the cubic equation to (x^3 + px + q = 0)
  80. F32 A2 = A * A;
  81. F32 A3 = A2 * A;
  82. F32 p = (1.f/3.f) * (((-1.f/3.f) * A2) + B);
  83. F32 q = (1.f/2.f) * (((2.f/27.f) * A3) - ((1.f/3.f) * A * B) + C);
  84. // use Cardano's fomula to solve the depressed cubic
  85. F32 p3 = p * p * p;
  86. F32 q2 = q * q;
  87. F32 D = q2 + p3;
  88. U32 num = 0;
  89. if(mIsZero(D)) // 1 or 2 solutions
  90. {
  91. if(mIsZero(q)) // 1 triple solution
  92. {
  93. x[0] = 0.f;
  94. num = 1;
  95. }
  96. else // 1 single and 1 double
  97. {
  98. F32 u = mCbrt(-q);
  99. x[0] = 2.f * u;
  100. x[1] = -u;
  101. num = 2;
  102. }
  103. }
  104. else if(D < 0.f) // 3 solutions: casus irreducibilis
  105. {
  106. F32 phi = (1.f/3.f) * mAcos(-q / mSqrt(-p3));
  107. F32 t = 2.f * mSqrt(-p);
  108. x[0] = t * mCos(phi);
  109. x[1] = -t * mCos(phi + ((F32)M_PI / 3.0f));
  110. x[2] = -t * mCos(phi - ((F32)M_PI / 3.0f));
  111. num = 3;
  112. }
  113. else // 1 solution
  114. {
  115. F32 sqrtD = mSqrt(D);
  116. F32 u = mCbrt(sqrtD - q);
  117. F32 v = -mCbrt(sqrtD + q);
  118. x[0] = u + v;
  119. num = 1;
  120. }
  121. // resubstitute
  122. F32 sub = (1.f/3.f) * A;
  123. for(U32 i = 0; i < num; i++)
  124. x[i] -= sub;
  125. // sort the roots
  126. for(S32 j = 0; j < (S32)(num - 1); j++)
  127. for(S32 k = j + 1; k < (S32)num; k++)
  128. if(x[k] < x[j])
  129. mSwap(x[k], x[j]);
  130. return(num);
  131. }
  132. //--------------------------------------------------------------------------
  133. // from Graphics Gems I: pp 738-742
  134. U32 mSolveQuartic_c(F32 a, F32 b, F32 c, F32 d, F32 e, F32 * x)
  135. {
  136. if(mIsZero(a))
  137. return(mSolveCubic(b, c, d, e, x));
  138. // normal form: x^4 + ax^3 + bx^2 + cx + d = 0
  139. F32 A = b / a;
  140. F32 B = c / a;
  141. F32 C = d / a;
  142. F32 D = e / a;
  143. // substitue x = y - A/4 to eliminate cubic term:
  144. // x^4 + px^2 + qx + r = 0
  145. F32 A2 = A * A;
  146. F32 A3 = A2 * A;
  147. F32 A4 = A2 * A2;
  148. F32 p = ((-3.f/8.f) * A2) + B;
  149. F32 q = ((1.f/8.f) * A3) - ((1.f/2.f) * A * B) + C;
  150. F32 r = ((-3.f/256.f) * A4) + ((1.f/16.f) * A2 * B) - ((1.f/4.f) * A * C) + D;
  151. U32 num = 0;
  152. if(mIsZero(r)) // no absolute term: y(y^3 + py + q) = 0
  153. {
  154. num = mSolveCubic(1.f, 0.f, p, q, x);
  155. x[num++] = 0.f;
  156. }
  157. else
  158. {
  159. // solve the resolvent cubic
  160. F32 q2 = q * q;
  161. a = 1.f;
  162. b = (-1.f/2.f) * p;
  163. c = -r;
  164. d = ((1.f/2.f) * r * p) - ((1.f/8.f) * q2);
  165. mSolveCubic(a, b, c, d, x);
  166. F32 z = x[0];
  167. // build 2 quadratic equations from the one solution
  168. F32 u = (z * z) - r;
  169. F32 v = (2.f * z) - p;
  170. if(mIsZero(u))
  171. u = 0.f;
  172. else if(u > 0.f)
  173. u = mSqrt(u);
  174. else
  175. return(0);
  176. if(mIsZero(v))
  177. v = 0.f;
  178. else if(v > 0.f)
  179. v = mSqrt(v);
  180. else
  181. return(0);
  182. // solve the two quadratics
  183. a = 1.f;
  184. b = v;
  185. c = z - u;
  186. num = mSolveQuadratic(a, b, c, x);
  187. a = 1.f;
  188. b = -v;
  189. c = z + u;
  190. num += mSolveQuadratic(a, b, c, x + num);
  191. }
  192. // resubstitute
  193. F32 sub = (1.f/4.f) * A;
  194. for(U32 i = 0; i < num; i++)
  195. x[i] -= sub;
  196. // sort the roots
  197. for(S32 j = 0; j < (S32)(num - 1); j++)
  198. for(S32 k = j + 1; k < (S32)num; k++)
  199. if(x[k] < x[j])
  200. mSwap(x[k], x[j]);
  201. return(num);
  202. }
  203. U32 (*mSolveQuadratic)( F32 a, F32 b, F32 c, F32* x ) = mSolveQuadratic_c;
  204. U32 (*mSolveCubic)( F32 a, F32 b, F32 c, F32 d, F32* x ) = mSolveCubic_c;
  205. U32 (*mSolveQuartic)( F32 a, F32 b, F32 c, F32 d, F32 e, F32* x ) = mSolveQuartic_c;