2
Bullet Continuous Collision Detection and Physics Library
3
Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
5
This software is provided 'as-is', without any express or implied warranty.
6
In no event will the authors be held liable for any damages arising from the use of this software.
7
Permission is granted to anyone to use this software for any purpose,
8
including commercial applications, and to alter it and redistribute it freely,
9
subject to the following restrictions:
11
1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
12
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
13
3. This notice may not be removed or altered from any source distribution.
17
#include "BU_AlgebraicPolynomialSolver.h"
19
#include <SimdMinMax.h>
21
int BU_AlgebraicPolynomialSolver::Solve2Quadratic(SimdScalar p, SimdScalar q)
24
SimdScalar basic_h_local;
25
SimdScalar basic_h_local_delta;
27
basic_h_local = p * 0.5f;
28
basic_h_local_delta = basic_h_local * basic_h_local - q;
29
if (basic_h_local_delta > 0.0f) {
30
basic_h_local_delta = SimdSqrt(basic_h_local_delta);
31
m_roots[0] = - basic_h_local + basic_h_local_delta;
32
m_roots[1] = - basic_h_local - basic_h_local_delta;
35
else if (SimdGreaterEqual(basic_h_local_delta, SIMD_EPSILON)) {
36
m_roots[0] = - basic_h_local;
45
int BU_AlgebraicPolynomialSolver::Solve2QuadraticFull(SimdScalar a,SimdScalar b, SimdScalar c)
47
SimdScalar radical = b * b - 4.0f * a * c;
50
SimdScalar sqrtRadical = SimdSqrt(radical);
51
SimdScalar idenom = 1.0f/(2.0f * a);
52
m_roots[0]=(-b + sqrtRadical) * idenom;
53
m_roots[1]=(-b - sqrtRadical) * idenom;
61
((x) > 0.0f ? SimdPow((SimdScalar)(x), 0.333333333333333333333333f) : \
62
((x) < 0.0f ? -SimdPow((SimdScalar)-(x), 0.333333333333333333333333f) : 0.0f))
67
/* this function solves the following cubic equation: */
70
/* lead * x + a * x + b * x + c = 0. */
72
/* it returns the number of different roots found, and stores the roots in */
73
/* roots[0,2]. it returns -1 for a degenerate equation 0 = 0. */
75
int BU_AlgebraicPolynomialSolver::Solve3Cubic(SimdScalar lead, SimdScalar a, SimdScalar b, SimdScalar c)
78
SimdScalar delta, u, phi;
83
/* transform into normal form: x^3 + a x^2 + b x + c = 0 */
85
if (SimdEqual(lead, SIMD_EPSILON)) {
87
/* we have a x^2 + b x + c = 0 */
89
if (SimdEqual(a, SIMD_EPSILON)) {
91
/* we have b x + c = 0 */
93
if (SimdEqual(b, SIMD_EPSILON)) {
94
if (SimdEqual(c, SIMD_EPSILON)) {
109
return Solve2QuadraticFull(a,b,c);
120
/* we substitute x = y - a / 3 in order to eliminate the quadric term. */
121
/* we get x^3 + p x + q = 0 */
126
q = a * (2.0f * u - b) + c;
129
/* now use Cardano's formula */
131
if (SimdEqual(p, SIMD_EPSILON)) {
132
if (SimdEqual(q, SIMD_EPSILON)) {
134
/* one triple root */
141
/* one real and two complex roots */
143
m_roots[0] = cubic_rt(-q) - a;
149
delta = p * p * p + q * q;
152
/* one real and two complex roots. note that v = -p / u. */
154
u = -q + SimdSqrt(delta);
156
m_roots[0] = u - p / u - a;
159
else if (delta < 0.0) {
161
/* Casus irreducibilis: we have three real roots */
166
phi = SimdAcos(-q / p) / 3.0f;
167
dummy = SIMD_2_PI / 3.0f;
168
m_roots[0] = r * SimdCos(phi) - a;
169
m_roots[1] = r * SimdCos(phi + dummy) - a;
170
m_roots[2] = r * SimdCos(phi - dummy) - a;
175
/* one single and one SimdScalar root */
178
m_roots[0] = 2.0f * r - a;
186
/* this function solves the following quartic equation: */
189
/* lead * x + a * x + b * x + c * x + d = 0. */
191
/* it returns the number of different roots found, and stores the roots in */
192
/* roots[0,3]. it returns -1 for a degenerate equation 0 = 0. */
194
int BU_AlgebraicPolynomialSolver::Solve4Quartic(SimdScalar lead, SimdScalar a, SimdScalar b, SimdScalar c, SimdScalar d)
198
int i, num_roots, num_tmp;
203
/* transform into normal form: x^4 + a x^3 + b x^2 + c x + d = 0 */
205
if (SimdEqual(lead, SIMD_EPSILON)) {
207
/* we have a x^3 + b x^2 + c x + d = 0 */
209
if (SimdEqual(a, SIMD_EPSILON)) {
211
/* we have b x^2 + c x + d = 0 */
213
if (SimdEqual(b, SIMD_EPSILON)) {
215
/* we have c x + d = 0 */
217
if (SimdEqual(c, SIMD_EPSILON)) {
218
if (SimdEqual(d, SIMD_EPSILON)) {
233
return Solve2QuadraticFull(b,c,d);
238
return Solve3Cubic(1.0, b / a, c / a, d / a);
250
/* we substitute x = y - a / 4 in order to eliminate the cubic term. */
251
/* we get: y^4 + p y^2 + q y + r = 0. */
254
p = b - 6.0f * a * a;
255
q = a * (8.0f * a * a - 2.0f * b) + c;
256
r = a * (a * (b - 3.f * a * a) - c) + d;
257
if (SimdEqual(q, SIMD_EPSILON)) {
259
/* biquadratic equation: y^4 + p y^2 + r = 0. */
261
num_roots = Solve2Quadratic(p, r);
263
if (m_roots[0] > 0.0f) {
265
if ((m_roots[1] > 0.0f) && (m_roots[1] != m_roots[0])) {
266
u = SimdSqrt(m_roots[1]);
269
u = SimdSqrt(m_roots[0]);
275
u = SimdSqrt(m_roots[0]);
282
u = SimdSqrt(m_roots[0]);
291
else if (SimdEqual(r, SIMD_EPSILON)) {
293
/* no absolute term: y (y^3 + p y + q) = 0. */
295
num_roots = Solve3Cubic(1.0, 0.0, p, q);
296
for (i = 0; i < num_roots; ++i) m_roots[i] -= a;
297
if (num_roots != -1) {
298
m_roots[num_roots] = -a;
309
/* we solve the resolvent cubic equation */
311
num_roots = Solve3Cubic(1.0f, -0.5f * p, -r, 0.5f * r * p - 0.125f * q * q);
312
if (num_roots == -1) {
318
/* build two quadric equations */
324
if (SimdEqual(u, SIMD_EPSILON))
331
if (SimdEqual(v, SIMD_EPSILON))
338
if (q < 0.0f) v = -v;
340
num_roots=Solve2Quadratic(v, w);
341
for (i = 0; i < num_roots; ++i)
350
num_tmp = Solve2Quadratic(-v, w);
351
for (i = 0; i < num_tmp; ++i)
353
m_roots[i + num_roots] = tmp[i] - a;
357
return (num_tmp + num_roots);