My Project
Loading...
Searching...
No Matches
CPolySolver.h
Go to the documentation of this file.
1//==============================================================================
2/*
3 Software License Agreement (BSD License)
4 Copyright (c) 2003-2024, CHAI3D
5 (www.chai3d.org)
6
7 All rights reserved.
8
9 Redistribution and use in source and binary forms, with or without
10 modification, are permitted provided that the following conditions
11 are met:
12
13 * Redistributions of source code must retain the above copyright
14 notice, this list of conditions and the following disclaimer.
15
16 * Redistributions in binary form must reproduce the above
17 copyright notice, this list of conditions and the following
18 disclaimer in the documentation and/or other materials provided
19 with the distribution.
20
21 * Neither the name of CHAI3D nor the names of its contributors may
22 be used to endorse or promote products derived from this software
23 without specific prior written permission.
24
25 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
28 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
29 COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
30 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
31 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
35 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 POSSIBILITY OF SUCH DAMAGE.
37
38 \author <http://www.chai3d.org>
39 \author Francois Conti
40 \version 3.3.0
41*/
42//==============================================================================
43
44//------------------------------------------------------------------------------
45#ifndef CPolySolverH
46#define CPolySolverH
47//------------------------------------------------------------------------------
48#include "math/CMaths.h"
49//------------------------------------------------------------------------------
50
51//------------------------------------------------------------------------------
52namespace chai3d {
53//------------------------------------------------------------------------------
54
55//==============================================================================
63//==============================================================================
64
65//------------------------------------------------------------------------------
69//------------------------------------------------------------------------------
70
72
73//==============================================================================
90//==============================================================================
91inline int cSolveLinear(double a_coefficient[2], double a_solution[1])
92{
93 if (cZero(a_coefficient[1]))
94 {
95 return (0);
96 }
97
98 a_solution[0] = - a_coefficient[0] / a_coefficient[1];
99
100 return (1);
101}
102
103
104//==============================================================================
120//==============================================================================
121inline int cSolveQuadric(double a_coefficient[3], double a_solution[2])
122{
123 double p, q, D;
124
125 // make sure we have a d2 equation
126 if (cZero(a_coefficient[2]))
127 {
128 return (cSolveLinear(a_coefficient, a_solution));
129 }
130
131 // normal for: x^2 + px + q
132 p = a_coefficient[1] / (2.0 * a_coefficient[2]);
133 q = a_coefficient[0] / a_coefficient[2];
134 D = p * p - q;
135
136 if (cZero(D))
137 {
138 // one double root
139 a_solution[0] = a_solution[1] = -p;
140 return (1);
141 }
142
143 if (D < 0.0)
144 {
145 // no real root
146 return (0);
147 }
148 else
149 {
150 // two real roots
151 double sqrt_D = sqrt(D);
152 a_solution[0] = sqrt_D - p;
153 a_solution[1] = -sqrt_D - p;
154 return (2);
155 }
156}
157
158
159//==============================================================================
175//==============================================================================
176inline int cSolveCubic(double a_coefficient[4], double a_solution[3])
177{
178 int i, num;
179 double sub,
180 A, B, C,
181 sq_A, p, q,
182 cb_p, D;
183
184 // make sure we have a d3 equation
185 if (cZero(a_coefficient[3]))
186 {
187 return cSolveQuadric(a_coefficient, a_solution);
188 }
189
190 // normalize the equation:x ^ 3 + Ax ^ 2 + Bx + C = 0
191 A = a_coefficient[2] / a_coefficient[3];
192 B = a_coefficient[1] / a_coefficient[3];
193 C = a_coefficient[0] / a_coefficient[3];
194
195 // substitute x = y - A / 3 to eliminate the quadric term: x^3 + px + q = 0
196 sq_A = A * A;
197 p = 1.0/3.0 * (-1.0/3.0 * sq_A + B);
198 q = 1.0/2.0 * (2.0/27.0 * A *sq_A - 1.0/3.0 * A * B + C);
199
200 // use Cardano's formula
201 cb_p = p * p * p;
202 D = q * q + cb_p;
203
204 if (cZero(D))
205 {
206 if (cZero(q))
207 {
208 // one triple solution
209 a_solution[0] = 0.0;
210 num = 1;
211 }
212 else
213 {
214 // one single and one double solution
215 double u = cCbrt(-q);
216 a_solution[0] = 2.0 * u;
217 a_solution[1] = - u;
218 num = 2;
219 }
220 }
221 else
222 {
223 if (D < 0.0)
224 {
225 // casus irreductibilis: three real solutions
226 double phi = 1.0/3.0 * acos(-q / sqrt(-cb_p));
227 double t = 2.0 * sqrt(-p);
228 a_solution[0] = t * cos(phi);
229 a_solution[1] = -t * cos(phi + M_PI / 3.0);
230 a_solution[2] = -t * cos(phi - M_PI / 3.0);
231 num = 3;
232 }
233 else
234 {
235 // one real solution
236 double sqrt_D = sqrt(D);
237 double u = cCbrt(sqrt_D + fabs(q));
238 if (q > 0.0)
239 {
240 a_solution[0] = - u + p / u ;
241 }
242 else
243 {
244 a_solution[0] = u - p / u;
245 }
246 num = 1;
247 }
248 }
249
250 // resubstitute
251 sub = 1.0 / 3.0 * A;
252 for (i = 0; i < num; i++)
253 {
254 a_solution[i] -= sub;
255 }
256
257 return (num);
258}
259
260
261//==============================================================================
277//==============================================================================
278inline int cSolveQuartic(double a_coefficient[5], double a_solution[4])
279{
280 double coeffs[4],
281 z, u, v, sub,
282 A, B, C, D,
283 sq_A, p, q, r;
284 int i, num;
285
286 // make sure we have a d4 equation
287 if (cZero(a_coefficient[4]))
288 {
289 return cSolveQuadric(a_coefficient, a_solution);
290 }
291
292 // normalize the equation:x ^ 4 + Ax ^ 3 + Bx ^ 2 + Cx + D = 0
293 A = a_coefficient[3] / a_coefficient[4];
294 B = a_coefficient[2] / a_coefficient[4];
295 C = a_coefficient[1] / a_coefficient[4];
296 D = a_coefficient[0] / a_coefficient[4];
297
298 // subsitute x = y - A / 4 to eliminate the cubic term: x^4 + px^2 + qx + r = 0
299 sq_A = A * A;
300 p = -3.0 / 8.0 * sq_A + B;
301 q = 1.0 / 8.0 * sq_A * A - 1.0 / 2.0 * A * B + C;
302 r = -3.0 / 256.0 * sq_A * sq_A + 1.0 / 16.0 * sq_A * B - 1.0 / 4.0 * A * C + D;
303
304 if (cZero(r))
305 {
306 // no absolute term:y(y ^ 3 + py + q) = 0
307 coeffs[0] = q;
308 coeffs[1] = p;
309 coeffs[2] = 0.0;
310 coeffs[3] = 1.0;
311
312 num = cSolveCubic(coeffs, a_solution);
313 a_solution[num++] = 0;
314 }
315 else
316 {
317 // solve the resolvent cubic...
318 coeffs[0] = 1.0 / 2.0 * r * p - 1.0 / 8.0 * q * q;
319 coeffs[1] = -r;
320 coeffs[2] = -1.0 / 2.0 * p;
321 coeffs[3] = 1.0;
322 (void) cSolveCubic(coeffs, a_solution);
323
324 // ...and take the one real solution...
325 z = a_solution[0];
326
327 // ...to build two quadratic equations
328 u = z * z - r;
329 v = 2.0 * z - p;
330
331 if (cZero(u))
332 {
333 u = 0.0;
334 }
335 else if (u > 0.0)
336 {
337 u = sqrt(u);
338 }
339 else
340 {
341 return (0);
342 }
343
344 if (cZero(v))
345 {
346 v = 0;
347 }
348 else if (v > 0.0)
349 {
350 v = sqrt(v);
351 }
352 else
353 {
354 return (0);
355 }
356
357 coeffs[0] = z - u;
358 coeffs[1] = q < 0 ? -v : v;
359 coeffs[2] = 1.0;
360
361 num = cSolveQuadric(coeffs, a_solution);
362
363 coeffs[0] = z + u;
364 coeffs[1] = q < 0 ? v : -v;
365 coeffs[2] = 1.0;
366
367 num += cSolveQuadric(coeffs, a_solution + num);
368 }
369
370 // resubstitute
371 sub = 1.0 / 4 * A;
372 for (i = 0; i < num; i++)
373 {
374 a_solution[i] -= sub;
375 }
376
377 return (num);
378}
379
381
382//------------------------------------------------------------------------------
383} // namespace chai3d
384//------------------------------------------------------------------------------
385
386//------------------------------------------------------------------------------
387#endif // CPolySolverH
388//------------------------------------------------------------------------------
389
390
Implements general math utility functions.
Definition CAudioBuffer.cpp:57
int cSolveCubic(double a_coefficient[4], double a_solution[3])
This function computes the solution of a cubic equation.
Definition CPolySolver.h:176
bool cZero(const double &a_value, const double &a_tolerance=C_TINY)
This function checks if a value is equal to or almost near zero.
Definition CMaths.h:155
double cCbrt(const double &a_value)
This function computes the cubic root of a scalar.
Definition CMaths.h:518
int cSolveLinear(double a_coefficient[2], double a_solution[1])
This function computes the solution of a linear equation.
Definition CPolySolver.h:91
int cSolveQuadric(double a_coefficient[3], double a_solution[2])
This function computes the solution of a quadric equation.
Definition CPolySolver.h:121
int cSolveQuartic(double a_coefficient[5], double a_solution[4])
This function computes the solution of a quartic equation.
Definition CPolySolver.h:278