POLYSOLV.C 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711
  1. /****************************************************************************
  2. * polysolv.c
  3. *
  4. * This file was written by Alexander Enzmann. He wrote the code for
  5. * 4th-6th order shapes and generously provided us these enhancements.
  6. *
  7. * from Persistence of Vision(tm) Ray Tracer
  8. * Copyright 1996,1999 Persistence of Vision Team
  9. *---------------------------------------------------------------------------
  10. * NOTICE: This source code file is provided so that users may experiment
  11. * with enhancements to POV-Ray and to port the software to platforms other
  12. * than those supported by the POV-Ray Team. There are strict rules under
  13. * which you are permitted to use this file. The rules are in the file
  14. * named POVLEGAL.DOC which should be distributed with this file.
  15. * If POVLEGAL.DOC is not available or for more info please contact the POV-Ray
  16. * Team Coordinator by email to team-coord@povray.org or visit us on the web at
  17. * http://www.povray.org. The latest version of POV-Ray may be found at this site.
  18. *
  19. * This program is based on the popular DKB raytracer version 2.12.
  20. * DKBTrace was originally written by David K. Buck.
  21. * DKBTrace Ver 2.0-2.12 were written by David K. Buck & Aaron A. Collins.
  22. *
  23. *****************************************************************************/
  24. #include "frame.h"
  25. #include "povray.h"
  26. #include "povproto.h"
  27. #include "vector.h"
  28. #include "polysolv.h"
  29. /*****************************************************************************
  30. * Local preprocessor defines
  31. ******************************************************************************/
  32. /* WARNING WARNING WARNING
  33. The following three constants have been defined so that quartic equations
  34. will properly render on fpu/compiler combinations that only have 64 bit
  35. IEEE precision. Do not arbitrarily change any of these values.
  36. If you have a machine with a proper fpu/compiler combo - like a Mac with
  37. a 68881, then use the native floating format (96 bits) and tune the
  38. values for a little less tolerance: something like: factor1 = 1.0e15,
  39. factor2 = -1.0e-7, factor3 = 1.0e-10.
  40. The meaning of the three constants are:
  41. factor1 - the magnitude of difference between coefficients in a
  42. quartic equation at which the Sturmian root solver will
  43. kick in. The Sturm solver is quite a bit slower than
  44. the algebraic solver, so the bigger this is, the faster
  45. the root solving will go. The algebraic solver is less
  46. accurate so the bigger this is, the more likely you will
  47. get bad roots.
  48. factor2 - Tolerance value that defines how close the quartic equation
  49. is to being a square of a quadratic. The closer this can
  50. get to zero before roots disappear, the less the chance
  51. you will get spurious roots.
  52. factor3 - Similar to factor2 at a later stage of the algebraic solver.
  53. */
  54. #ifndef FUDGE_FACTOR1
  55. #define FUDGE_FACTOR1 1.0e12
  56. #endif
  57. #ifndef FUDGE_FACTOR2
  58. #define FUDGE_FACTOR2 -1.0e-5
  59. #endif
  60. #ifndef FUDGE_FACTOR3
  61. #define FUDGE_FACTOR3 1.0e-7
  62. #endif
  63. /* Constants. */
  64. #define TWO_M_PI_3 2.0943951023931954923084
  65. #define FOUR_M_PI_3 4.1887902047863909846168
  66. /* Max number of iterations. */
  67. #define MAX_ITERATIONS 50
  68. /* A coefficient smaller than SMALL_ENOUGH is considered to be zero (0.0). */
  69. #define SMALL_ENOUGH 1.0e-10
  70. /* Smallest relative error we want. */
  71. #define RELERROR 1.0e-12
  72. /*****************************************************************************
  73. * Local typedefs
  74. ******************************************************************************/
  75. typedef struct p
  76. {
  77. int ord;
  78. DBL coef[MAX_ORDER+1];
  79. }
  80. polynomial;
  81. /*****************************************************************************
  82. * Local variables
  83. ******************************************************************************/
  84. /*****************************************************************************
  85. * Static functions
  86. ******************************************************************************/
  87. static int solve_quadratic (DBL *x, DBL *y);
  88. static int solve_cubic (DBL *x, DBL *y);
  89. static int solve_quartic (DBL *x, DBL *y);
  90. static int polysolve (int order, DBL *Coeffs, DBL *roots);
  91. static int modp (polynomial *u, polynomial *v, polynomial *r);
  92. static int regula_falsa (int order, DBL *coef, DBL a, DBL b, DBL *val);
  93. static int sbisect (int np, polynomial *sseq, DBL min, DBL max, int atmin, int atmax, DBL *roots);
  94. static int numchanges (int np, polynomial *sseq, DBL a);
  95. static DBL polyeval (DBL x, int n, DBL *Coeffs);
  96. static int buildsturm (int ord, polynomial *sseq);
  97. static int visible_roots (int np, polynomial *sseq, int *atneg, int *atpos);
  98. static int difficult_coeffs (int n, DBL *x);
  99. /*****************************************************************************
  100. *
  101. * FUNCTION
  102. *
  103. * modp
  104. *
  105. * INPUT
  106. *
  107. * OUTPUT
  108. *
  109. * RETURNS
  110. *
  111. * AUTHOR
  112. *
  113. * Alexander Enzmann
  114. *
  115. * DESCRIPTION
  116. *
  117. * Calculates the modulus of u(x) / v(x) leaving it in r.
  118. * It returns 0 if r(x) is a constant.
  119. *
  120. * NOTE: This function assumes the leading coefficient of v is 1 or -1.
  121. *
  122. * CHANGES
  123. *
  124. * Okt 1996 : Added bug fix by Heiko Eissfeldt. [DB]
  125. *
  126. ******************************************************************************/
  127. static int modp(polynomial *u, polynomial *v, polynomial *r)
  128. {
  129. int k, j;
  130. *r = *u;
  131. if (v->coef[v->ord] < 0.0)
  132. {
  133. for (k = u->ord - v->ord - 1; k >= 0; k -= 2)
  134. {
  135. r->coef[k] = -r->coef[k];
  136. }
  137. for (k = u->ord - v->ord; k >= 0; k--)
  138. {
  139. for (j = v->ord + k - 1; j >= k; j--)
  140. {
  141. r->coef[j] = -r->coef[j] - r->coef[v->ord + k] * v->coef[j - k];
  142. }
  143. }
  144. }
  145. else
  146. {
  147. for (k = u->ord - v->ord; k >= 0; k--)
  148. {
  149. for (j = v->ord + k - 1; j >= k; j--)
  150. {
  151. r->coef[j] -= r->coef[v->ord + k] * v->coef[j - k];
  152. }
  153. }
  154. }
  155. k = v->ord - 1;
  156. while (k >= 0 && fabs(r->coef[k]) < SMALL_ENOUGH)
  157. {
  158. r->coef[k] = 0.0;
  159. k--;
  160. }
  161. r->ord = (k < 0) ? 0 : k;
  162. return(r->ord);
  163. }
  164. /*****************************************************************************
  165. *
  166. * FUNCTION
  167. *
  168. * buildsturm
  169. *
  170. * INPUT
  171. *
  172. * OUTPUT
  173. *
  174. * RETURNS
  175. *
  176. * AUTHOR
  177. *
  178. * Alexander Enzmann
  179. *
  180. * DESCRIPTION
  181. *
  182. * Build the sturmian sequence for a polynomial.
  183. *
  184. * CHANGES
  185. *
  186. * -
  187. *
  188. ******************************************************************************/
  189. static int buildsturm(int ord, polynomial *sseq)
  190. {
  191. int i;
  192. DBL f, *fp, *fc;
  193. polynomial *sp;
  194. sseq[0].ord = ord;
  195. sseq[1].ord = ord - 1;
  196. /* calculate the derivative and normalize the leading coefficient. */
  197. f = fabs(sseq[0].coef[ord] * ord);
  198. fp = sseq[1].coef;
  199. fc = sseq[0].coef + 1;
  200. for (i = 1; i <= ord; i++)
  201. {
  202. *fp++ = *fc++ * i / f;
  203. }
  204. /* construct the rest of the Sturm sequence */
  205. for (sp = sseq + 2; modp(sp - 2, sp - 1, sp); sp++)
  206. {
  207. /* reverse the sign and normalize */
  208. f = -fabs(sp->coef[sp->ord]);
  209. for (fp = &sp->coef[sp->ord]; fp >= sp->coef; fp--)
  210. {
  211. *fp /= f;
  212. }
  213. }
  214. /* reverse the sign */
  215. sp->coef[0] = -sp->coef[0];
  216. return(sp - sseq);
  217. }
  218. /*****************************************************************************
  219. *
  220. * FUNCTION
  221. *
  222. * visible_roots
  223. *
  224. * INPUT
  225. *
  226. * OUTPUT
  227. *
  228. * RETURNS
  229. *
  230. * AUTHOR
  231. *
  232. * Alexander Enzmann
  233. *
  234. * DESCRIPTION
  235. *
  236. * Find out how many visible intersections there are.
  237. *
  238. * CHANGES
  239. *
  240. * -
  241. *
  242. ******************************************************************************/
  243. static int visible_roots(int np, polynomial *sseq, int *atzer, int *atpos)
  244. {
  245. int atposinf, atzero;
  246. polynomial *s;
  247. DBL f, lf;
  248. atposinf = atzero = 0;
  249. /* changes at positve infinity */
  250. lf = sseq[0].coef[sseq[0].ord];
  251. for (s = sseq + 1; s <= sseq + np; s++)
  252. {
  253. f = s->coef[s->ord];
  254. if (lf == 0.0 || lf * f < 0)
  255. {
  256. atposinf++;
  257. }
  258. lf = f;
  259. }
  260. /* Changes at zero */
  261. lf = sseq[0].coef[0];
  262. for (s = sseq+1; s <= sseq + np; s++)
  263. {
  264. f = s->coef[0];
  265. if (lf == 0.0 || lf * f < 0)
  266. {
  267. atzero++;
  268. }
  269. lf = f;
  270. }
  271. *atzer = atzero;
  272. *atpos = atposinf;
  273. return(atzero - atposinf);
  274. }
  275. /*****************************************************************************
  276. *
  277. * FUNCTION
  278. *
  279. * numchanges
  280. *
  281. * INPUT
  282. *
  283. * OUTPUT
  284. *
  285. * RETURNS
  286. *
  287. * AUTHOR
  288. *
  289. * Alexander Enzmann
  290. *
  291. * DESCRIPTION
  292. *
  293. * Return the number of sign changes in the Sturm sequence in
  294. * sseq at the value a.
  295. *
  296. * CHANGES
  297. *
  298. * -
  299. *
  300. ******************************************************************************/
  301. static int numchanges(int np, polynomial *sseq, DBL a)
  302. {
  303. int changes;
  304. DBL f, lf;
  305. polynomial *s;
  306. changes = 0;
  307. lf = polyeval(a, sseq[0].ord, sseq[0].coef);
  308. for (s = sseq + 1; s <= sseq + np; s++)
  309. {
  310. f = polyeval(a, s->ord, s->coef);
  311. if (lf == 0.0 || lf * f < 0)
  312. {
  313. changes++;
  314. }
  315. lf = f;
  316. }
  317. return(changes);
  318. }
  319. /*****************************************************************************
  320. *
  321. * FUNCTION
  322. *
  323. * sbisect
  324. *
  325. * INPUT
  326. *
  327. * OUTPUT
  328. *
  329. * RETURNS
  330. *
  331. * AUTHOR
  332. *
  333. * Alexander Enzmann
  334. *
  335. * DESCRIPTION
  336. *
  337. * Uses a bisection based on the sturm sequence for the polynomial
  338. * described in sseq to isolate intervals in which roots occur,
  339. * the roots are returned in the roots array in order of magnitude.
  340. *
  341. * NOTE: This routine has one severe bug: When the interval containing the
  342. * root [min, max] has a root at one of its endpoints, as well as one
  343. * within the interval, the root at the endpoint will be returned
  344. * rather than the one inside.
  345. *
  346. * CHANGES
  347. *
  348. * -
  349. *
  350. ******************************************************************************/
  351. static int sbisect(int np, polynomial *sseq, DBL min_value, DBL max_value, int atmin, int atmax, DBL *roots)
  352. {
  353. DBL mid;
  354. int n1, n2, its, atmid;
  355. if ((atmin - atmax) == 1)
  356. {
  357. /* first try using regula-falsa to find the root. */
  358. if (regula_falsa(sseq->ord, sseq->coef, min_value, max_value, roots))
  359. {
  360. return(1);
  361. }
  362. else
  363. {
  364. /* That failed, so now find it by bisection */
  365. for (its = 0; its < MAX_ITERATIONS; its++)
  366. {
  367. mid = (min_value + max_value) / 2;
  368. atmid = numchanges(np, sseq, mid);
  369. /* The follow only happens if there is a bug. And
  370. unfortunately, there is. CEY 04/97
  371. */
  372. if ((atmid<atmax) || (atmid>atmin))
  373. {
  374. return(0);
  375. }
  376. if (fabs(mid) > RELERROR)
  377. {
  378. if (fabs((max_value - min_value) / mid) < RELERROR)
  379. {
  380. roots[0] = mid;
  381. return(1);
  382. }
  383. }
  384. else
  385. {
  386. if (fabs(max_value - min_value) < RELERROR)
  387. {
  388. roots[0] = mid;
  389. return(1);
  390. }
  391. }
  392. if ((atmin - atmid) == 0)
  393. {
  394. min_value = mid;
  395. }
  396. else
  397. {
  398. max_value = mid;
  399. }
  400. }
  401. /* Bisection took too long - just return what we got */
  402. roots[0] = mid;
  403. return(1);
  404. }
  405. }
  406. /* There is more than one root in the interval.
  407. Bisect to find new intervals. */
  408. for (its = 0; its < MAX_ITERATIONS; its++)
  409. {
  410. mid = (min_value + max_value) / 2;
  411. atmid = numchanges(np, sseq, mid);
  412. /* The follow only happens if there is a bug. And
  413. unfortunately, there is. CEY 04/97
  414. */
  415. if ((atmid<atmax) || (atmid>atmin))
  416. {
  417. return(0);
  418. }
  419. if (fabs(mid) > RELERROR)
  420. {
  421. if (fabs((max_value - min_value) / mid) < RELERROR)
  422. {
  423. roots[0] = mid;
  424. return(1);
  425. }
  426. }
  427. else
  428. {
  429. if (fabs(max_value - min_value) < RELERROR)
  430. {
  431. roots[0] = mid;
  432. return(1);
  433. }
  434. }
  435. n1 = atmin - atmid;
  436. n2 = atmid - atmax;
  437. if ((n1 != 0) && (n2 != 0))
  438. {
  439. n1 = sbisect(np, sseq, min_value, mid, atmin, atmid, roots);
  440. n2 = sbisect(np, sseq, mid, max_value, atmid, atmax, &roots[n1]);
  441. return(n1 + n2);
  442. }
  443. if (n1 == 0)
  444. {
  445. min_value = mid;
  446. }
  447. else
  448. {
  449. max_value = mid;
  450. }
  451. }
  452. /* Took too long to bisect. Just return what we got. */
  453. roots[0] = mid;
  454. return(1);
  455. }
  456. /*****************************************************************************
  457. *
  458. * FUNCTION
  459. *
  460. * polyeval
  461. *
  462. * INPUT
  463. *
  464. * OUTPUT
  465. *
  466. * RETURNS
  467. *
  468. * AUTHOR
  469. *
  470. * Alexander Enzmann
  471. *
  472. * DESCRIPTION
  473. *
  474. * Evaluate the value of a polynomial at the given value x.
  475. *
  476. * The coefficients are stored in c in the following order:
  477. *
  478. * c[0] + c[1] * x + c[2] * x ^ 2 + c[3] * x ^ 3 + ...
  479. *
  480. * CHANGES
  481. *
  482. * -
  483. *
  484. ******************************************************************************/
  485. static DBL polyeval(DBL x, int n, DBL *Coeffs)
  486. {
  487. register int i;
  488. DBL val;
  489. val = Coeffs[n];
  490. for (i = n-1; i >= 0; i--)
  491. {
  492. val = val * x + Coeffs[i];
  493. }
  494. return(val);
  495. }
  496. /*****************************************************************************
  497. *
  498. * FUNCTION
  499. *
  500. * regular_falsa
  501. *
  502. * INPUT
  503. *
  504. * OUTPUT
  505. *
  506. * RETURNS
  507. *
  508. * AUTHOR
  509. *
  510. * Alexander Enzmann
  511. *
  512. * DESCRIPTION
  513. *
  514. * Close in on a root by using regula-falsa.
  515. *
  516. * CHANGES
  517. *
  518. * -
  519. *
  520. ******************************************************************************/
  521. static int regula_falsa(int order, DBL *coef, DBL a, DBL b, DBL *val)
  522. {
  523. int its;
  524. DBL fa, fb, x, fx, lfx;
  525. fa = polyeval(a, order, coef);
  526. fb = polyeval(b, order, coef);
  527. if (fa * fb > 0.0)
  528. {
  529. return 0;
  530. }
  531. if (fabs(fa) < SMALL_ENOUGH)
  532. {
  533. *val = a;
  534. return(1);
  535. }
  536. if (fabs(fb) < SMALL_ENOUGH)
  537. {
  538. *val = b;
  539. return(1);
  540. }
  541. lfx = fa;
  542. for (its = 0; its < MAX_ITERATIONS; its++)
  543. {
  544. x = (fb * a - fa * b) / (fb - fa);
  545. fx = polyeval(x, order, coef);
  546. if (fabs(x) > RELERROR)
  547. {
  548. if (fabs(fx / x) < RELERROR)
  549. {
  550. *val = x;
  551. return(1);
  552. }
  553. }
  554. else
  555. {
  556. if (fabs(fx) < RELERROR)
  557. {
  558. *val = x;
  559. return(1);
  560. }
  561. }
  562. if (fa < 0)
  563. {
  564. if (fx < 0)
  565. {
  566. a = x;
  567. fa = fx;
  568. if ((lfx * fx) > 0)
  569. {
  570. fb /= 2;
  571. }
  572. }
  573. else
  574. {
  575. b = x;
  576. fb = fx;
  577. if ((lfx * fx) > 0)
  578. {
  579. fa /= 2;
  580. }
  581. }
  582. }
  583. else
  584. {
  585. if (fx < 0)
  586. {
  587. b = x;
  588. fb = fx;
  589. if ((lfx * fx) > 0)
  590. {
  591. fa /= 2;
  592. }
  593. }
  594. else
  595. {
  596. a = x;
  597. fa = fx;
  598. if ((lfx * fx) > 0)
  599. {
  600. fb /= 2;
  601. }
  602. }
  603. }
  604. /* Check for underflow in the domain */
  605. if (fabs(b-a) < RELERROR)
  606. {
  607. *val = x;
  608. return(1);
  609. }
  610. lfx = fx;
  611. }
  612. return(0);
  613. }
  614. /*****************************************************************************
  615. *
  616. * FUNCTION
  617. *
  618. * solve_quadratic
  619. *
  620. * INPUT
  621. *
  622. * OUTPUT
  623. *
  624. * RETURNS
  625. *
  626. * AUTHOR
  627. *
  628. * Alexander Enzmann
  629. *
  630. * DESCRIPTION
  631. *
  632. * Solve the quadratic equation:
  633. *
  634. * x[0] * x^2 + x[1] * x + x[2] = 0.
  635. *
  636. * The value returned by this function is the number of real roots.
  637. * The roots themselves are returned in y[0], y[1].
  638. *
  639. * CHANGES
  640. *
  641. * -
  642. *
  643. ******************************************************************************/
  644. static int solve_quadratic(DBL *x, DBL *y)
  645. {
  646. DBL d, t, a, b, c;
  647. a = x[0];
  648. b = -x[1];
  649. c = x[2];
  650. if (a == 0.0)
  651. {
  652. if (b == 0.0)
  653. {
  654. return(0);
  655. }
  656. y[0] = c / b;
  657. return(1);
  658. }
  659. d = b * b - 4.0 * a * c;
  660. /* Treat values of d around 0 as 0. */
  661. if ((d > -SMALL_ENOUGH) && (d < SMALL_ENOUGH))
  662. {
  663. y[0] = 0.5 * b / a;
  664. return(1);
  665. }
  666. else
  667. {
  668. if (d < 0.0)
  669. {
  670. return(0);
  671. }
  672. }
  673. d = sqrt(d);
  674. t = 2.0 * a;
  675. y[0] = (b + d) / t;
  676. y[1] = (b - d) / t;
  677. return(2);
  678. }
  679. /*****************************************************************************
  680. *
  681. * FUNCTION
  682. *
  683. * solve_cubic
  684. *
  685. * INPUT
  686. *
  687. * OUTPUT
  688. *
  689. * RETURNS
  690. *
  691. * AUTHOR
  692. *
  693. * Alexander Enzmann
  694. *
  695. * DESCRIPTION
  696. *
  697. *
  698. * Solve the cubic equation:
  699. *
  700. * x[0] * x^3 + x[1] * x^2 + x[2] * x + x[3] = 0.
  701. *
  702. * The result of this function is an integer that tells how many real
  703. * roots exist. Determination of how many are distinct is up to the
  704. * process that calls this routine. The roots that exist are stored
  705. * in (y[0], y[1], y[2]).
  706. *
  707. * NOTE: This function relies very heavily on trigonometric functions and
  708. * the square root function. If an alternative solution is found
  709. * that does not rely on transcendentals this code will be replaced.
  710. *
  711. * CHANGES
  712. *
  713. * -
  714. *
  715. ******************************************************************************/
  716. static int solve_cubic(DBL *x, DBL *y)
  717. {
  718. DBL Q, R, Q3, R2, sQ, d, an, theta;
  719. DBL A2, a0, a1, a2, a3;
  720. a0 = x[0];
  721. if (a0 == 0.0)
  722. {
  723. return(solve_quadratic(&x[1], y));
  724. }
  725. else
  726. {
  727. if (a0 != 1.0)
  728. {
  729. a1 = x[1] / a0;
  730. a2 = x[2] / a0;
  731. a3 = x[3] / a0;
  732. }
  733. else
  734. {
  735. a1 = x[1];
  736. a2 = x[2];
  737. a3 = x[3];
  738. }
  739. }
  740. A2 = a1 * a1;
  741. Q = (A2 - 3.0 * a2) / 9.0;
  742. /* Modified to save some multiplications and to avoid a floating point
  743. exception that occured with DJGPP and full optimization. [DB 8/94] */
  744. R = (a1 * (A2 - 4.5 * a2) + 13.5 * a3) / 27.0;
  745. Q3 = Q * Q * Q;
  746. R2 = R * R;
  747. d = Q3 - R2;
  748. an = a1 / 3.0;
  749. if (d >= 0.0)
  750. {
  751. /* Three real roots. */
  752. d = R / sqrt(Q3);
  753. theta = acos(d) / 3.0;
  754. sQ = -2.0 * sqrt(Q);
  755. y[0] = sQ * cos(theta) - an;
  756. y[1] = sQ * cos(theta + TWO_M_PI_3) - an;
  757. y[2] = sQ * cos(theta + FOUR_M_PI_3) - an;
  758. return(3);
  759. }
  760. else
  761. {
  762. sQ = pow(sqrt(R2 - Q3) + fabs(R), 1.0 / 3.0);
  763. if (R < 0)
  764. {
  765. y[0] = (sQ + Q / sQ) - an;
  766. }
  767. else
  768. {
  769. y[0] = -(sQ + Q / sQ) - an;
  770. }
  771. return(1);
  772. }
  773. }
  774. #ifdef USE_NEW_DIFFICULT_COEFFS
  775. /*****************************************************************************
  776. *
  777. * FUNCTION
  778. *
  779. * difficult_coeffs
  780. *
  781. * INPUT
  782. *
  783. * OUTPUT
  784. *
  785. * RETURNS
  786. *
  787. * AUTHOR
  788. *
  789. * Alexander Enzmann
  790. *
  791. * DESCRIPTION
  792. *
  793. * Test to see if any coeffs are more than 6 orders of magnitude
  794. * larger than the smallest.
  795. *
  796. * CHANGES
  797. *
  798. * -
  799. *
  800. ******************************************************************************/
  801. static int difficult_coeffs(int n, DBL *x)
  802. {
  803. int i, flag = 0;
  804. DBL t, biggest;
  805. biggest = fabs(x[0]);
  806. for (i = 1; i <= n; i++)
  807. {
  808. t = fabs(x[i]);
  809. if (t > biggest)
  810. {
  811. biggest = t;
  812. }
  813. }
  814. /* Everything is zero no sense in doing any more */
  815. if (biggest == 0.0)
  816. {
  817. return(0);
  818. }
  819. for (i = 0; i <= n; i++)
  820. {
  821. if (x[i] != 0.0)
  822. {
  823. if (fabs(biggest / x[i]) > FUDGE_FACTOR1)
  824. {
  825. x[i] = 0.0;
  826. flag = 1;
  827. }
  828. }
  829. }
  830. return(flag);
  831. }
  832. #else
  833. /*****************************************************************************
  834. *
  835. * FUNCTION
  836. *
  837. * difficult_coeffs
  838. *
  839. * INPUT
  840. *
  841. * OUTPUT
  842. *
  843. * RETURNS
  844. *
  845. * AUTHOR
  846. *
  847. * Alexander Enzmann
  848. *
  849. * DESCRIPTION
  850. *
  851. * Test to see if any coeffs are more than 6 orders of magnitude
  852. * larger than the smallest (function from POV 1.0) [DB 8/94].
  853. *
  854. * CHANGES
  855. *
  856. * -
  857. *
  858. ******************************************************************************/
  859. static int difficult_coeffs(int n, DBL *x)
  860. {
  861. int i;
  862. DBL biggest;
  863. biggest = 0.0;
  864. for (i = 0; i <= n; i++)
  865. {
  866. if (fabs(x[i]) > biggest)
  867. {
  868. biggest = x[i];
  869. }
  870. }
  871. /* Everything is zero no sense in doing any more */
  872. if (biggest == 0.0)
  873. {
  874. return(0);
  875. }
  876. for (i = 0; i <= n; i++)
  877. {
  878. if (x[i] != 0.0)
  879. {
  880. if (fabs(biggest / x[i]) > FUDGE_FACTOR1)
  881. {
  882. return(1);
  883. }
  884. }
  885. }
  886. return(0);
  887. }
  888. #endif
  889. #ifdef TEST_SOLVER
  890. /*****************************************************************************
  891. *
  892. * FUNCTION
  893. *
  894. * solve_quartic
  895. *
  896. * INPUT
  897. *
  898. * OUTPUT
  899. *
  900. * RETURNS
  901. *
  902. * AUTHOR
  903. *
  904. * Alexander Enzmann
  905. *
  906. * DESCRIPTION
  907. *
  908. * The old way of solving quartics algebraically.
  909. * This is an adaptation of the method of Lodovico Ferrari (Circa 1731).
  910. *
  911. * CHANGES
  912. *
  913. * -
  914. *
  915. ******************************************************************************/
  916. static int solve_quartic(DBL *x, DBL *results)
  917. {
  918. DBL cubic[4], roots[3];
  919. DBL a0, a1, y, d1, x1, t1, t2;
  920. DBL c0, c1, c2, c3, c4, d2, q1, q2;
  921. int i;
  922. c0 = x[0];
  923. if (c0 != 1.0)
  924. {
  925. c1 = x[1] / c0;
  926. c2 = x[2] / c0;
  927. c3 = x[3] / c0;
  928. c4 = x[4] / c0;
  929. }
  930. else
  931. {
  932. c1 = x[1];
  933. c2 = x[2];
  934. c3 = x[3];
  935. c4 = x[4];
  936. }
  937. /* The first step is to take the original equation:
  938. x^4 + b*x^3 + c*x^2 + d*x + e = 0
  939. and rewrite it as:
  940. x^4 + b*x^3 = -c*x^2 - d*x - e,
  941. adding (b*x/2)^2 + (x^2 + b*x/2)y + y^2/4 to each side gives a
  942. perfect square on the lhs:
  943. (x^2 + b*x/2 + y/2)^2 = (b^2/4 - c + y)x^2 + (b*y/2 - d)x + y^2/4 - e
  944. By choosing the appropriate value for y, the rhs can be made a perfect
  945. square also. This value is found when the rhs is treated as a quadratic
  946. in x with the discriminant equal to 0. This will be true when:
  947. (b*y/2 - d)^2 - 4.0 * (b^2/4 - c*y)*(y^2/4 - e) = 0, or
  948. y^3 - c*y^2 + (b*d - 4*e)*y - b^2*e + 4*c*e - d^2 = 0.
  949. This is called the resolvent of the quartic equation. */
  950. a0 = 4.0 * c4;
  951. cubic[0] = 1.0;
  952. cubic[1] = -1.0 * c2;
  953. cubic[2] = c1 * c3 - a0;
  954. cubic[3] = a0 * c2 - c1 * c1 * c4 - c3 * c3;
  955. i = solve_cubic(&cubic[0], &roots[0]);
  956. if (i > 0)
  957. {
  958. y = roots[0];
  959. }
  960. else
  961. {
  962. return(0);
  963. }
  964. /* What we are left with is a quadratic squared on the lhs and a
  965. linear term on the right. The linear term has one of two signs,
  966. take each and add it to the lhs. The form of the quartic is now:
  967. a' = b^2/4 - c + y, b' = b*y/2 - d, (from rhs quadritic above)
  968. (x^2 + b*x/2 + y/2) = +sqrt(a'*(x + 1/2 * b'/a')^2), and
  969. (x^2 + b*x/2 + y/2) = -sqrt(a'*(x + 1/2 * b'/a')^2).
  970. By taking the linear term from each of the right hand sides and
  971. adding to the appropriate part of the left hand side, two quadratic
  972. formulas are created. By solving each of these the four roots of
  973. the quartic are determined.
  974. */
  975. i = 0;
  976. a0 = c1 / 2.0;
  977. a1 = y / 2.0;
  978. t1 = a0 * a0 - c2 + y;
  979. if (t1 < 0.0)
  980. {
  981. if (t1 > FUDGE_FACTOR2)
  982. {
  983. t1 = 0.0;
  984. }
  985. else
  986. {
  987. /* First Special case, a' < 0 means all roots are complex. */
  988. return(0);
  989. }
  990. }
  991. }
  992. if (t1 < FUDGE_FACTOR3)
  993. {
  994. /* Second special case, the "x" term on the right hand side above
  995. has vanished. In this case:
  996. (x^2 + b*x/2 + y/2) = +sqrt(y^2/4 - e), and
  997. (x^2 + b*x/2 + y/2) = -sqrt(y^2/4 - e). */
  998. t2 = a1 * a1 - c4;
  999. if (t2 < 0.0)
  1000. {
  1001. return(0);
  1002. }
  1003. x1 = 0.0;
  1004. d1 = sqrt(t2);
  1005. }
  1006. else
  1007. {
  1008. x1 = sqrt(t1);
  1009. d1 = 0.5 * (a0 * y - c3) / x1;
  1010. }
  1011. /* Solve the first quadratic */
  1012. q1 = -a0 - x1;
  1013. q2 = a1 + d1;
  1014. d2 = q1 * q1 - 4.0 * q2;
  1015. if (d2 >= 0.0)
  1016. {
  1017. d2 = sqrt(d2);
  1018. results[0] = 0.5 * (q1 + d2);
  1019. results[1] = 0.5 * (q1 - d2);
  1020. i = 2;
  1021. }
  1022. /* Solve the second quadratic */
  1023. q1 = q1 + x1 + x1;
  1024. q2 = a1 - d1;
  1025. d2 = q1 * q1 - 4.0 * q2;
  1026. if (d2 >= 0.0)
  1027. {
  1028. d2 = sqrt(d2);
  1029. results[i++] = 0.5 * (q1 + d2);
  1030. results[i++] = 0.5 * (q1 - d2);
  1031. }
  1032. return(i);
  1033. }
  1034. #else
  1035. /*****************************************************************************
  1036. *
  1037. * FUNCTION
  1038. *
  1039. * solve_quartic
  1040. *
  1041. * INPUT
  1042. *
  1043. * OUTPUT
  1044. *
  1045. * RETURNS
  1046. *
  1047. * AUTHOR
  1048. *
  1049. * Alexander Enzmann
  1050. *
  1051. * DESCRIPTION
  1052. *
  1053. * Solve a quartic using the method of Francois Vieta (Circa 1735).
  1054. *
  1055. * CHANGES
  1056. *
  1057. * -
  1058. *
  1059. ******************************************************************************/
  1060. static int solve_quartic(DBL *x, DBL *results)
  1061. {
  1062. DBL cubic[4], roots[3];
  1063. DBL c12, z, p, q, q1, q2, r, d1, d2;
  1064. DBL c0, c1, c2, c3, c4;
  1065. int i;
  1066. /* Make sure the quartic has a leading coefficient of 1.0 */
  1067. c0 = x[0];
  1068. if (c0 != 1.0)
  1069. {
  1070. c1 = x[1] / c0;
  1071. c2 = x[2] / c0;
  1072. c3 = x[3] / c0;
  1073. c4 = x[4] / c0;
  1074. }
  1075. else
  1076. {
  1077. c1 = x[1];
  1078. c2 = x[2];
  1079. c3 = x[3];
  1080. c4 = x[4];
  1081. }
  1082. /* Compute the cubic resolvant */
  1083. c12 = c1 * c1;
  1084. p = -0.375 * c12 + c2;
  1085. q = 0.125 * c12 * c1 - 0.5 * c1 * c2 + c3;
  1086. r = -0.01171875 * c12 * c12 + 0.0625 * c12 * c2 - 0.25 * c1 * c3 + c4;
  1087. cubic[0] = 1.0;
  1088. cubic[1] = -0.5 * p;
  1089. cubic[2] = -r;
  1090. cubic[3] = 0.5 * r * p - 0.125 * q * q;
  1091. i = solve_cubic(cubic, roots);
  1092. if (i > 0)
  1093. {
  1094. z = roots[0];
  1095. }
  1096. else
  1097. {
  1098. return(0);
  1099. }
  1100. d1 = 2.0 * z - p;
  1101. if (d1 < 0.0)
  1102. {
  1103. if (d1 > -SMALL_ENOUGH)
  1104. {
  1105. d1 = 0.0;
  1106. }
  1107. else
  1108. {
  1109. return(0);
  1110. }
  1111. }
  1112. if (d1 < SMALL_ENOUGH)
  1113. {
  1114. d2 = z * z - r;
  1115. if (d2 < 0.0)
  1116. {
  1117. return(0);
  1118. }
  1119. d2 = sqrt(d2);
  1120. }
  1121. else
  1122. {
  1123. d1 = sqrt(d1);
  1124. d2 = 0.5 * q / d1;
  1125. }
  1126. /* Set up useful values for the quadratic factors */
  1127. q1 = d1 * d1;
  1128. q2 = -0.25 * c1;
  1129. i = 0;
  1130. /* Solve the first quadratic */
  1131. p = q1 - 4.0 * (z - d2);
  1132. if (p == 0)
  1133. {
  1134. results[i++] = -0.5 * d1 - q2;
  1135. }
  1136. else
  1137. {
  1138. if (p > 0)
  1139. {
  1140. p = sqrt(p);
  1141. results[i++] = -0.5 * (d1 + p) + q2;
  1142. results[i++] = -0.5 * (d1 - p) + q2;
  1143. }
  1144. }
  1145. /* Solve the second quadratic */
  1146. p = q1 - 4.0 * (z + d2);
  1147. if (p == 0)
  1148. {
  1149. results[i++] = 0.5 * d1 - q2;
  1150. }
  1151. else
  1152. {
  1153. if (p > 0)
  1154. {
  1155. p = sqrt(p);
  1156. results[i++] = 0.5 * (d1 + p) + q2;
  1157. results[i++] = 0.5 * (d1 - p) + q2;
  1158. }
  1159. }
  1160. return(i);
  1161. }
  1162. #endif
  1163. /*****************************************************************************
  1164. *
  1165. * FUNCTION
  1166. *
  1167. * polysolve
  1168. *
  1169. * INPUT
  1170. *
  1171. * OUTPUT
  1172. *
  1173. * RETURNS
  1174. *
  1175. * AUTHOR
  1176. *
  1177. * Alexander Enzmann
  1178. *
  1179. * DESCRIPTION
  1180. *
  1181. * Root solver based on the Sturm sequences for a polynomial.
  1182. *
  1183. * CHANGES
  1184. *
  1185. * Okt 1996 : Added bug fix by Heiko Eissfeldt. [DB]
  1186. *
  1187. ******************************************************************************/
  1188. static int polysolve(int order, DBL *Coeffs, DBL *roots)
  1189. {
  1190. polynomial sseq[MAX_ORDER+1];
  1191. DBL min_value, max_value;
  1192. int i, nroots, np, atmin, atmax;
  1193. /* Put the coefficients into the top of the stack. */
  1194. for (i = 0; i <= order; i++)
  1195. {
  1196. sseq[0].coef[order-i] = Coeffs[i] / Coeffs[0];
  1197. }
  1198. /* Build the Sturm sequence */
  1199. np = buildsturm(order, &sseq[0]);
  1200. /* Get the total number of visible roots */
  1201. if ((nroots = visible_roots(np, sseq, &atmin, &atmax)) == 0)
  1202. {
  1203. return(0);
  1204. }
  1205. /* Bracket the roots */
  1206. min_value = 0.0;
  1207. max_value = Max_Distance;
  1208. atmin = numchanges(np, sseq, min_value);
  1209. atmax = numchanges(np, sseq, max_value);
  1210. nroots = atmin - atmax;
  1211. if (nroots == 0)
  1212. {
  1213. return(0);
  1214. }
  1215. /* perform the bisection. */
  1216. return(sbisect(np, sseq, min_value, max_value, atmin, atmax, roots));
  1217. }
  1218. /*****************************************************************************
  1219. *
  1220. * FUNCTION
  1221. *
  1222. * Solve_Polynomial
  1223. *
  1224. * INPUT
  1225. *
  1226. * n - order of polynomial
  1227. * c - coefficients
  1228. * r - roots
  1229. * sturm - TRUE, if sturm should be used for n=3,4
  1230. * epsilon - Tolerance to discard small root
  1231. *
  1232. * OUTPUT
  1233. *
  1234. * r
  1235. *
  1236. * RETURNS
  1237. *
  1238. * int - number of roots found
  1239. *
  1240. * AUTHOR
  1241. *
  1242. * Dieter Bayer
  1243. *
  1244. * DESCRIPTION
  1245. *
  1246. * Solve the polynomial equation
  1247. *
  1248. * c[0] * x ^ n + c[1] * x ^ (n-1) + ... + c[n-1] * x + c[n] = 0
  1249. *
  1250. * If the equation has a root r, |r| < epsilon, this root is eliminated
  1251. * and the equation of order n-1 will be solved. This will avoid the problem
  1252. * of "surface acne" in (most) cases while increasing the speed of the
  1253. * root solving (polynomial's order is reduced by one).
  1254. *
  1255. * WARNING: This function can only be used for polynomials if small roots
  1256. * (i.e. |x| < epsilon) are not needed. This is the case for ray/object
  1257. * intersection tests because only intersecions with t > 0 are valid.
  1258. *
  1259. * NOTE: Only one root at x = 0 will be eliminated.
  1260. *
  1261. * NOTE: If epsilon = 0 no roots will be eliminated.
  1262. *
  1263. *
  1264. * The method and idea for root elimination was taken from:
  1265. *
  1266. * Han-Wen Nienhuys, "Polynomials", Ray Tracing News, July 6, 1994,
  1267. * Volume 7, Number 3
  1268. *
  1269. *
  1270. * CHANGES
  1271. *
  1272. * Jul 1994 : Creation.
  1273. *
  1274. ******************************************************************************/
  1275. int Solve_Polynomial(int n, DBL *c0, DBL *r, int sturm, DBL epsilon)
  1276. {
  1277. int roots, i;
  1278. DBL *c;
  1279. Increase_Counter(stats[Polynomials_Tested]);
  1280. roots = 0;
  1281. /*
  1282. * Determine the "real" order of the polynomial, i.e.
  1283. * eliminate small leading coefficients.
  1284. */
  1285. i = 0;
  1286. while ((fabs(c0[i]) < SMALL_ENOUGH) && (i < n))
  1287. {
  1288. i++;
  1289. }
  1290. n -= i;
  1291. c = &c0[i];
  1292. switch (n)
  1293. {
  1294. case 0:
  1295. break;
  1296. case 1:
  1297. /* Solve linear polynomial. */
  1298. if (c[0] != 0.0)
  1299. {
  1300. r[roots++] = -c[1] / c[0];
  1301. }
  1302. break;
  1303. case 2:
  1304. /* Solve quadratic polynomial. */
  1305. roots = solve_quadratic(c, r);
  1306. break;
  1307. case 3:
  1308. /* Root elimination? */
  1309. if (epsilon > 0.0)
  1310. {
  1311. if ((c[2] != 0.0) && (fabs(c[3]/c[2]) < epsilon))
  1312. {
  1313. Increase_Counter(stats[Roots_Eliminated]);
  1314. roots = solve_quadratic(c, r);
  1315. break;
  1316. }
  1317. }
  1318. /* Solve cubic polynomial. */
  1319. if (sturm)
  1320. {
  1321. roots = polysolve(3, c, r);
  1322. }
  1323. else
  1324. {
  1325. roots = solve_cubic(c, r);
  1326. }
  1327. break;
  1328. case 4:
  1329. /* Root elimination? */
  1330. if (epsilon > 0.0)
  1331. {
  1332. if ((c[3] != 0.0) && (fabs(c[4]/c[3]) < epsilon))
  1333. {
  1334. Increase_Counter(stats[Roots_Eliminated]);
  1335. if (sturm)
  1336. {
  1337. roots = polysolve(3, c, r);
  1338. }
  1339. else
  1340. {
  1341. roots = solve_cubic(c, r);
  1342. }
  1343. break;
  1344. }
  1345. }
  1346. /* Test for difficult coeffs. */
  1347. if (difficult_coeffs(4, c))
  1348. {
  1349. sturm = TRUE;
  1350. }
  1351. /* Solve quartic polynomial. */
  1352. if (sturm)
  1353. {
  1354. roots = polysolve(4, c, r);
  1355. }
  1356. else
  1357. {
  1358. roots = solve_quartic(c, r);
  1359. }
  1360. break;
  1361. default:
  1362. if (epsilon > 0.0)
  1363. {
  1364. if ((c[n-1] != 0.0) && (fabs(c[n]/c[n-1]) < epsilon))
  1365. {
  1366. Increase_Counter(stats[Roots_Eliminated]);
  1367. roots = polysolve(n-1, c, r);
  1368. }
  1369. }
  1370. /* Solve n-th order polynomial. */
  1371. roots = polysolve(n, c, r);
  1372. break;
  1373. }
  1374. return(roots);
  1375. }