POLY.C 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517
  1. /****************************************************************************
  2. * poly.c
  3. *
  4. * This module implements the code for general 3 variable polynomial shapes
  5. *
  6. * This file was written by Alexander Enzmann. He wrote the code for
  7. * 4th - 6th order shapes and generously provided us these enhancements.
  8. *
  9. * from Persistence of Vision(tm) Ray Tracer
  10. * Copyright 1996,1999 Persistence of Vision Team
  11. *---------------------------------------------------------------------------
  12. * NOTICE: This source code file is provided so that users may experiment
  13. * with enhancements to POV-Ray and to port the software to platforms other
  14. * than those supported by the POV-Ray Team. There are strict rules under
  15. * which you are permitted to use this file. The rules are in the file
  16. * named POVLEGAL.DOC which should be distributed with this file.
  17. * If POVLEGAL.DOC is not available or for more info please contact the POV-Ray
  18. * Team Coordinator by email to team-coord@povray.org or visit us on the web at
  19. * http://www.povray.org. The latest version of POV-Ray may be found at this site.
  20. *
  21. * This program is based on the popular DKB raytracer version 2.12.
  22. * DKBTrace was originally written by David K. Buck.
  23. * DKBTrace Ver 2.0-2.12 were written by David K. Buck & Aaron A. Collins.
  24. *
  25. *****************************************************************************/
  26. #include "frame.h"
  27. #include "vector.h"
  28. #include "povproto.h"
  29. #include "bbox.h"
  30. #include "polysolv.h"
  31. #include "matrices.h"
  32. #include "objects.h"
  33. #include "poly.h"
  34. #include "povray.h"
  35. /*
  36. * Basic form of a quartic equation:
  37. *
  38. * a00*x^4 + a01*x^3*y + a02*x^3*z + a03*x^3 + a04*x^2*y^2 +
  39. * a05*x^2*y*z+ a06*x^2*y + a07*x^2*z^2 + a08*x^2*z + a09*x^2 +
  40. * a10*x*y^3 + a11*x*y^2*z + a12*x*y^2 + a13*x*y*z^2 + a14*x*y*z +
  41. * a15*x*y + a16*x*z^3 + a17*x*z^2 + a18*x*z + a19*x + a20*y^4 +
  42. * a21*y^3*z + a22*y^3 + a23*y^2*z^2 + a24*y^2*z + a25*y^2 + a26*y*z^3 +
  43. * a27*y*z^2 + a28*y*z + a29*y + a30*z^4 + a31*z^3 + a32*z^2 + a33*z + a34
  44. *
  45. */
  46. /*****************************************************************************
  47. * Local preprocessor defines
  48. ******************************************************************************/
  49. #define DEPTH_TOLERANCE 1.0e-4
  50. #define INSIDE_TOLERANCE 1.0e-4
  51. #define ROOT_TOLERANCE 1.0e-4
  52. #define COEFF_LIMIT 1.0e-20
  53. #define BINOMSIZE 40
  54. /*****************************************************************************
  55. * Local typedefs
  56. ******************************************************************************/
  57. /*****************************************************************************
  58. * Static functions
  59. ******************************************************************************/
  60. static int intersect (RAY *Ray, int Order, DBL *Coeffs, int Sturm_Flag, DBL *Depths);
  61. static void normal0 (VECTOR Result, int Order, DBL *Coeffs, VECTOR IPoint);
  62. static void normal1 (VECTOR Result, int Order, DBL *Coeffs, VECTOR IPoint);
  63. static DBL inside (VECTOR IPoint, int Order, DBL *Coeffs);
  64. static int intersect_linear (RAY *ray, DBL *Coeffs, DBL *Depths);
  65. static int intersect_quadratic (RAY *ray, DBL *Coeffs, DBL *Depths);
  66. static int factor_out (int n, int i, int *c, int *s);
  67. static long binomial (int n, int r);
  68. static void factor1 (int n, int *c, int *s);
  69. /* unused
  70. static DBL evaluate_linear (VECTOR P, DBL *a);
  71. static DBL evaluate_quadratic (VECTOR P, DBL *a);
  72. */
  73. static int All_Poly_Intersections (OBJECT *Object, RAY *Ray, ISTACK *Depth_Stack);
  74. static int Inside_Poly (VECTOR IPoint, OBJECT *Object);
  75. static void Poly_Normal (VECTOR Result, OBJECT *Object, INTERSECTION *Inter);
  76. static POLY *Copy_Poly (OBJECT *Object);
  77. static void Translate_Poly (OBJECT *Object, VECTOR Vector, TRANSFORM *Trans);
  78. static void Rotate_Poly (OBJECT *Object, VECTOR Vector, TRANSFORM *Trans);
  79. static void Scale_Poly (OBJECT *Object, VECTOR Vector, TRANSFORM *Trans);
  80. static void Transform_Poly (OBJECT *Object, TRANSFORM *Trans);
  81. static void Invert_Poly (OBJECT *Object);
  82. static void Destroy_Poly (OBJECT *Object);
  83. /*****************************************************************************
  84. * Local variables
  85. ******************************************************************************/
  86. METHODS Poly_Methods =
  87. {
  88. All_Poly_Intersections,
  89. Inside_Poly, Poly_Normal, (COPY_METHOD)Copy_Poly,
  90. Translate_Poly, Rotate_Poly,
  91. Scale_Poly, Transform_Poly, Invert_Poly, Destroy_Poly
  92. };
  93. /* The following table contains the binomial coefficients up to 15 */
  94. static int binomials[15][15] =
  95. {
  96. {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  97. {1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  98. {1, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  99. {1, 3, 3, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  100. {1, 4, 6, 4, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  101. {1, 5, 10, 10, 5, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  102. {1, 6, 15, 20, 15, 6, 1, 0, 0, 0, 0, 0, 0, 0, 0},
  103. {1, 7, 21, 35, 35, 21, 7, 1, 0, 0, 0, 0, 0, 0, 0},
  104. {1, 8, 28, 56, 70, 56, 28, 8, 1, 0, 0, 0, 0, 0, 0},
  105. {1, 9, 36, 84, 126, 126, 84, 36, 9, 1, 0, 0, 0, 0, 0},
  106. {1, 10, 45,120, 210, 252, 210, 120, 45, 10, 1, 0, 0, 0, 0},
  107. {1, 11, 55,165, 330, 462, 462, 330, 165, 55, 11, 1, 0, 0, 0},
  108. {1, 12, 66,220, 495, 792, 924, 792, 495, 220, 66, 12, 1, 0, 0},
  109. {1, 13, 78,286, 715,1287,1716,1716,1287, 715, 286, 78, 13, 1, 0},
  110. {1, 14, 91,364,1001,2002,3003,3432,3003,2002,1001,364, 91, 14, 1}
  111. };
  112. static DBL eqn_v[3][MAX_ORDER+1], eqn_vt[3][MAX_ORDER+1];
  113. /*****************************************************************************
  114. *
  115. * FUNCTION
  116. *
  117. * All_Poly_Intersections
  118. *
  119. * INPUT
  120. *
  121. * OUTPUT
  122. *
  123. * RETURNS
  124. *
  125. * AUTHOR
  126. *
  127. * Alexander Enzmann
  128. *
  129. * DESCRIPTION
  130. *
  131. * -
  132. *
  133. * CHANGES
  134. *
  135. * -
  136. *
  137. ******************************************************************************/
  138. static int All_Poly_Intersections(OBJECT *Object, RAY *Ray, ISTACK *Depth_Stack)
  139. {
  140. POLY *Poly = (POLY *) Object;
  141. DBL Depths[MAX_ORDER], len;
  142. VECTOR IPoint;
  143. int cnt, i, j, Intersection_Found, same_root;
  144. RAY New_Ray;
  145. /* Transform the ray into the polynomial's space */
  146. MInvTransPoint(New_Ray.Initial, Ray->Initial, Poly->Trans);
  147. MInvTransDirection(New_Ray.Direction, Ray->Direction, Poly->Trans);
  148. VLength(len, New_Ray.Direction);
  149. VInverseScaleEq(New_Ray.Direction, len);
  150. Intersection_Found = FALSE;
  151. Increase_Counter(stats[Ray_Poly_Tests]);
  152. switch (Poly->Order)
  153. {
  154. case 1:
  155. cnt = intersect_linear(&New_Ray, Poly->Coeffs, Depths);
  156. break;
  157. case 2:
  158. cnt = intersect_quadratic(&New_Ray, Poly->Coeffs, Depths);
  159. break;
  160. default:
  161. cnt = intersect(&New_Ray, Poly->Order, Poly->Coeffs, Test_Flag(Poly, STURM_FLAG), Depths);
  162. }
  163. if (cnt > 0)
  164. {
  165. Increase_Counter(stats[Ray_Poly_Tests_Succeeded]);
  166. }
  167. for (i = 0; i < cnt; i++)
  168. {
  169. if (Depths[i] > DEPTH_TOLERANCE)
  170. {
  171. same_root = FALSE;
  172. for (j = 0; j < i; j++)
  173. {
  174. if (Depths[i] == Depths[j])
  175. {
  176. same_root = TRUE;
  177. break;
  178. }
  179. }
  180. if (!same_root)
  181. {
  182. VEvaluateRay(IPoint, New_Ray.Initial, Depths[i], New_Ray.Direction);
  183. /* Transform the point into world space */
  184. MTransPoint(IPoint, IPoint, Poly->Trans);
  185. if (Point_In_Clip(IPoint, Object->Clip))
  186. {
  187. push_entry(Depths[i] / len,IPoint,Object,Depth_Stack);
  188. Intersection_Found = TRUE;
  189. }
  190. }
  191. }
  192. }
  193. return (Intersection_Found);
  194. }
  195. /*****************************************************************************
  196. *
  197. * FUNCTION
  198. *
  199. * evaluate_linear
  200. *
  201. * INPUT
  202. *
  203. * OUTPUT
  204. *
  205. * RETURNS
  206. *
  207. * AUTHOR
  208. *
  209. * Alexander Enzmann
  210. *
  211. * DESCRIPTION
  212. *
  213. * -
  214. *
  215. * CHANGES
  216. *
  217. * -
  218. *
  219. ******************************************************************************/
  220. /* For speedup of low order polynomials, expand out the terms
  221. involved in evaluating the poly. */
  222. /* unused
  223. static DBL evaluate_linear(VECTOR P, DBL *a)
  224. {
  225. return(a[0] * P[X]) + (a[1] * P[Y]) + (a[2] * P[Z]) + a[3];
  226. }
  227. */
  228. /*****************************************************************************
  229. *
  230. * FUNCTION
  231. *
  232. * evaluate_quadratic
  233. *
  234. * INPUT
  235. *
  236. * OUTPUT
  237. *
  238. * RETURNS
  239. *
  240. * AUTHOR
  241. *
  242. * Alexander Enzmann
  243. *
  244. * DESCRIPTION
  245. *
  246. * -
  247. *
  248. * CHANGES
  249. *
  250. * -
  251. *
  252. ******************************************************************************/
  253. /*
  254. static DBL evaluate_quadratic(VECTOR P, DBL *a)
  255. {
  256. DBL x, y, z;
  257. x = P[X];
  258. y = P[Y];
  259. z = P[Z];
  260. return(a[0] * x * x + a[1] * x * y + a[2] * x * z +
  261. a[3] * x + a[4] * y * y + a[5] * y * z +
  262. a[6] * y + a[7] * z * z + a[8] * z + a[9]);
  263. }
  264. */
  265. /*****************************************************************************
  266. *
  267. * FUNCTION
  268. *
  269. * factor_out
  270. *
  271. * INPUT
  272. *
  273. * OUTPUT
  274. *
  275. * RETURNS
  276. *
  277. * AUTHOR
  278. *
  279. * Alexander Enzmann
  280. *
  281. * DESCRIPTION
  282. *
  283. * Remove all factors of i from n.
  284. *
  285. * CHANGES
  286. *
  287. * -
  288. *
  289. ******************************************************************************/
  290. static int factor_out(int n, int i, int *c, int *s)
  291. {
  292. while (!(n % i))
  293. {
  294. n /= i;
  295. s[(*c)++] = i;
  296. }
  297. return(n);
  298. }
  299. /*****************************************************************************
  300. *
  301. * FUNCTION
  302. *
  303. * factor1
  304. *
  305. * INPUT
  306. *
  307. * OUTPUT
  308. *
  309. * RETURNS
  310. *
  311. * AUTHOR
  312. *
  313. * Alexander Enzmann
  314. *
  315. * DESCRIPTION
  316. *
  317. * Find all prime factors of n. (Note that n must be less than 2^15.
  318. *
  319. * CHANGES
  320. *
  321. * -
  322. *
  323. ******************************************************************************/
  324. static void factor1(int n, int *c, int *s)
  325. {
  326. int i,k;
  327. /* First factor out any 2s. */
  328. n = factor_out(n, 2, c, s);
  329. /* Now any odd factors. */
  330. k = (int)sqrt((DBL)n) + 1;
  331. for (i = 3; (n > 1) && (i <= k); i += 2)
  332. {
  333. if (!(n % i))
  334. {
  335. n = factor_out(n, i, c, s);
  336. k = (int)sqrt((DBL)n)+1;
  337. }
  338. }
  339. if (n > 1)
  340. {
  341. s[(*c)++] = n;
  342. }
  343. }
  344. /*****************************************************************************
  345. *
  346. * FUNCTION
  347. *
  348. * binomial
  349. *
  350. * INPUT
  351. *
  352. * OUTPUT
  353. *
  354. * RETURNS
  355. *
  356. * AUTHOR
  357. *
  358. * Alexander Enzmann
  359. *
  360. * DESCRIPTION
  361. *
  362. * Calculate the binomial coefficent of n, r.
  363. *
  364. * CHANGES
  365. *
  366. * -
  367. *
  368. ******************************************************************************/
  369. static long binomial(int n, int r)
  370. {
  371. int h,i,j,k,l;
  372. unsigned long result;
  373. static int stack1[BINOMSIZE], stack2[BINOMSIZE];
  374. if ((n < 0) || (r < 0) || (r > n))
  375. {
  376. result = 0L;
  377. }
  378. else
  379. {
  380. if (r == n)
  381. {
  382. result = 1L;
  383. }
  384. else
  385. {
  386. if ((r < 15) && (n < 15))
  387. {
  388. result = (long)binomials[n][r];
  389. }
  390. else
  391. {
  392. j = 0;
  393. for (i = r + 1; i <= n; i++)
  394. {
  395. stack1[j++] = i;
  396. }
  397. for (i = 2; i <= (n-r); i++)
  398. {
  399. h = 0;
  400. factor1(i, &h, stack2);
  401. for (k = 0; k < h; k++)
  402. {
  403. for (l = 0; l < j; l++)
  404. {
  405. if (!(stack1[l] % stack2[k]))
  406. {
  407. stack1[l] /= stack2[k];
  408. goto l1;
  409. }
  410. }
  411. }
  412. /* Error if we get here */
  413. /* Debug_Info("Failed to factor\n");*/
  414. l1:;
  415. }
  416. result = 1;
  417. for (i = 0; i < j; i++)
  418. {
  419. result *= stack1[i];
  420. }
  421. }
  422. }
  423. }
  424. return(result);
  425. }
  426. /*****************************************************************************
  427. *
  428. * FUNCTION
  429. *
  430. * inside
  431. *
  432. * INPUT
  433. *
  434. * OUTPUT
  435. *
  436. * RETURNS
  437. *
  438. * AUTHOR
  439. *
  440. * Alexander Enzmann
  441. *
  442. * DESCRIPTION
  443. *
  444. * -
  445. *
  446. * CHANGES
  447. *
  448. * -
  449. *
  450. ******************************************************************************/
  451. static DBL inside(VECTOR IPoint, int Order, DBL *Coeffs)
  452. {
  453. DBL x[MAX_ORDER+1], y[MAX_ORDER+1];
  454. DBL z[MAX_ORDER+1], c, Result;
  455. int i, j, k, term;
  456. x[0] = 1.0; y[0] = 1.0; z[0] = 1.0;
  457. x[1] = IPoint[X]; y[1] = IPoint[Y]; z[1] = IPoint[Z];
  458. for (i = 2; i <= Order; i++)
  459. {
  460. x[i] = x[1] * x[i-1];
  461. y[i] = y[1] * y[i-1];
  462. z[i] = z[1] * z[i-1];
  463. }
  464. Result = 0.0;
  465. term = 0;
  466. for (i = Order; i >= 0; i--)
  467. {
  468. for (j=Order-i;j>=0;j--)
  469. {
  470. for (k=Order-(i+j);k>=0;k--)
  471. {
  472. if ((c = Coeffs[term]) != 0.0)
  473. {
  474. Result += c * x[i] * y[j] * z[k];
  475. }
  476. term++;
  477. }
  478. }
  479. }
  480. return(Result);
  481. }
  482. /*****************************************************************************
  483. *
  484. * FUNCTION
  485. *
  486. * intersect
  487. *
  488. * INPUT
  489. *
  490. * OUTPUT
  491. *
  492. * RETURNS
  493. *
  494. * AUTHOR
  495. *
  496. * Alexander Enzmann
  497. *
  498. * DESCRIPTION
  499. *
  500. * Intersection of a ray and an arbitrary polynomial function.
  501. *
  502. * CHANGES
  503. *
  504. * -
  505. *
  506. ******************************************************************************/
  507. static int intersect(RAY *ray, int Order, DBL *Coeffs, int Sturm_Flag, DBL *Depths)
  508. {
  509. DBL eqn[MAX_ORDER+1];
  510. DBL t[3][MAX_ORDER+1];
  511. VECTOR P, D;
  512. DBL val;
  513. int h, i, j, k, i1, j1, k1, term;
  514. int offset;
  515. /* First we calculate the values of the individual powers
  516. of x, y, and z as they are represented by the ray */
  517. Assign_Vector(P,ray->Initial);
  518. Assign_Vector(D,ray->Direction);
  519. for (i = 0; i < 3; i++)
  520. {
  521. eqn_v[i][0] = 1.0;
  522. eqn_vt[i][0] = 1.0;
  523. }
  524. eqn_v[0][1] = P[X];
  525. eqn_v[1][1] = P[Y];
  526. eqn_v[2][1] = P[Z];
  527. eqn_vt[0][1] = D[X];
  528. eqn_vt[1][1] = D[Y];
  529. eqn_vt[2][1] = D[Z];
  530. for (i = 2; i <= Order; i++)
  531. {
  532. for (j=0;j<3;j++)
  533. {
  534. eqn_v[j][i] = eqn_v[j][1] * eqn_v[j][i-1];
  535. eqn_vt[j][i] = eqn_vt[j][1] * eqn_vt[j][i-1];
  536. }
  537. }
  538. for (i = 0; i <= Order; i++)
  539. {
  540. eqn[i] = 0.0;
  541. }
  542. /* Now walk through the terms of the polynomial. As we go
  543. we substitute the ray equation for each of the variables. */
  544. term = 0;
  545. for (i = Order; i >= 0; i--)
  546. {
  547. for (h = 0; h <= i; h++)
  548. {
  549. t[0][h] = binomial(i, h) * eqn_vt[0][i-h] * eqn_v[0][h];
  550. }
  551. for (j = Order-i; j >= 0; j--)
  552. {
  553. for (h = 0; h <= j; h++)
  554. {
  555. t[1][h] = binomial(j, h) * eqn_vt[1][j-h] * eqn_v[1][h];
  556. }
  557. for (k = Order-(i+j); k >= 0; k--)
  558. {
  559. if (Coeffs[term] != 0)
  560. {
  561. for (h = 0; h <= k; h++)
  562. {
  563. t[2][h] = binomial(k, h) * eqn_vt[2][k-h] * eqn_v[2][h];
  564. }
  565. /* Multiply together the three polynomials. */
  566. offset = Order - (i + j + k);
  567. for (i1 = 0; i1 <= i; i1++)
  568. {
  569. for (j1=0;j1<=j;j1++)
  570. {
  571. for (k1=0;k1<=k;k1++)
  572. {
  573. h = offset + i1 + j1 + k1;
  574. val = Coeffs[term];
  575. val *= t[0][i1];
  576. val *= t[1][j1];
  577. val *= t[2][k1];
  578. eqn[h] += val;
  579. }
  580. }
  581. }
  582. }
  583. term++;
  584. }
  585. }
  586. }
  587. for (i = 0, j = Order; i <= Order; i++)
  588. {
  589. if (eqn[i] != 0.0)
  590. {
  591. break;
  592. }
  593. else
  594. {
  595. j--;
  596. }
  597. }
  598. if (j > 1)
  599. {
  600. return(Solve_Polynomial(j, &eqn[i], Depths, Sturm_Flag, ROOT_TOLERANCE));
  601. }
  602. else
  603. {
  604. return(0);
  605. }
  606. }
  607. /*****************************************************************************
  608. *
  609. * FUNCTION
  610. *
  611. * intersect_linear
  612. *
  613. * INPUT
  614. *
  615. * OUTPUT
  616. *
  617. * RETURNS
  618. *
  619. * AUTHOR
  620. *
  621. * Alexander Enzmann
  622. *
  623. * DESCRIPTION
  624. *
  625. * Intersection of a ray and a quadratic.
  626. *
  627. * CHANGES
  628. *
  629. * -
  630. *
  631. ******************************************************************************/
  632. static int intersect_linear(RAY *ray, DBL *Coeffs, DBL *Depths)
  633. {
  634. DBL t0, t1, *a = Coeffs;
  635. t0 = a[0] * ray->Initial[X] + a[1] * ray->Initial[Y] + a[2] * ray->Initial[Z];
  636. t1 = a[0] * ray->Direction[X] + a[1] * ray->Direction[Y] +
  637. a[2] * ray->Direction[Z];
  638. if (fabs(t1) < EPSILON)
  639. {
  640. return(0);
  641. }
  642. Depths[0] = -(a[3] + t0) / t1;
  643. return(1);
  644. }
  645. /*****************************************************************************
  646. *
  647. * FUNCTION
  648. *
  649. * intersect_quadratic
  650. *
  651. * INPUT
  652. *
  653. * OUTPUT
  654. *
  655. * RETURNS
  656. *
  657. * AUTHOR
  658. *
  659. * Alexander Enzmann
  660. *
  661. * DESCRIPTION
  662. *
  663. * Intersection of a ray and a quadratic.
  664. *
  665. * CHANGES
  666. *
  667. * -
  668. *
  669. ******************************************************************************/
  670. static int intersect_quadratic(RAY *ray, DBL *Coeffs, DBL *Depths)
  671. {
  672. DBL x, y, z, x2, y2, z2;
  673. DBL xx, yy, zz, xx2, yy2, zz2;
  674. DBL *a, ac, bc, cc, d, t;
  675. x = ray->Initial[X];
  676. y = ray->Initial[Y];
  677. z = ray->Initial[Z];
  678. xx = ray->Direction[X];
  679. yy = ray->Direction[Y];
  680. zz = ray->Direction[Z];
  681. x2 = x * x; y2 = y * y; z2 = z * z;
  682. xx2 = xx * xx; yy2 = yy * yy; zz2 = zz * zz;
  683. a = Coeffs;
  684. /*
  685. * Determine the coefficients of t^n, where the line is represented
  686. * as (x,y,z) + (xx,yy,zz)*t.
  687. */
  688. ac = (a[0]*xx2 + a[1]*xx*yy + a[2]*xx*zz + a[4]*yy2 + a[5]*yy*zz + a[7]*zz2);
  689. bc = (2*a[0]*x*xx + a[1]*(x*yy + xx*y) + a[2]*(x*zz + xx*z) +
  690. a[3]*xx + 2*a[4]*y*yy + a[5]*(y*zz + yy*z) + a[6]*yy +
  691. 2*a[7]*z*zz + a[8]*zz);
  692. cc = a[0]*x2 + a[1]*x*y + a[2]*x*z + a[3]*x + a[4]*y2 +
  693. a[5]*y*z + a[6]*y + a[7]*z2 + a[8]*z + a[9];
  694. if (fabs(ac) < COEFF_LIMIT)
  695. {
  696. if (fabs(bc) < COEFF_LIMIT)
  697. {
  698. return(0);
  699. }
  700. Depths[0] = -cc / bc;
  701. return(1);
  702. }
  703. /*
  704. * Solve the quadratic formula & return results that are
  705. * within the correct interval.
  706. */
  707. d = bc * bc - 4.0 * ac * cc;
  708. if (d < 0.0)
  709. {
  710. return(0);
  711. }
  712. d = sqrt(d);
  713. bc = -bc;
  714. t = 2.0 * ac;
  715. Depths[0] = (bc + d) / t;
  716. Depths[1] = (bc - d) / t;
  717. return(2);
  718. }
  719. /*****************************************************************************
  720. *
  721. * FUNCTION
  722. *
  723. * normal0
  724. *
  725. * INPUT
  726. *
  727. * OUTPUT
  728. *
  729. * RETURNS
  730. *
  731. * AUTHOR
  732. *
  733. * Alexander Enzmann
  734. *
  735. * DESCRIPTION
  736. *
  737. * Normal to a polynomial (used for polynomials with order > 4).
  738. *
  739. * CHANGES
  740. *
  741. * -
  742. *
  743. ******************************************************************************/
  744. static void normal0(VECTOR Result, int Order, DBL *Coeffs, VECTOR IPoint)
  745. {
  746. int i, j, k, term;
  747. DBL val, *a, x[MAX_ORDER+1], y[MAX_ORDER+1], z[MAX_ORDER+1];
  748. x[0] = 1.0;
  749. y[0] = 1.0;
  750. z[0] = 1.0;
  751. x[1] = IPoint[X];
  752. y[1] = IPoint[Y];
  753. z[1] = IPoint[Z];
  754. for (i = 2; i <= Order; i++)
  755. {
  756. x[i] = IPoint[X] * x[i-1];
  757. y[i] = IPoint[Y] * y[i-1];
  758. z[i] = IPoint[Z] * z[i-1];
  759. }
  760. a = Coeffs;
  761. term = 0;
  762. Make_Vector(Result, 0.0, 0.0, 0.0);
  763. for (i = Order; i >= 0; i--)
  764. {
  765. for (j = Order-i; j >= 0; j--)
  766. {
  767. for (k = Order-(i+j); k >= 0; k--)
  768. {
  769. if (i >= 1)
  770. {
  771. val = x[i-1] * y[j] * z[k];
  772. Result[X] += i * a[term] * val;
  773. }
  774. if (j >= 1)
  775. {
  776. val = x[i] * y[j-1] * z[k];
  777. Result[Y] += j * a[term] * val;
  778. }
  779. if (k >= 1)
  780. {
  781. val = x[i] * y[j] * z[k-1];
  782. Result[Z] += k * a[term] * val;
  783. }
  784. term++;
  785. }
  786. }
  787. }
  788. }
  789. /*****************************************************************************
  790. *
  791. * FUNCTION
  792. *
  793. * nromal1
  794. *
  795. * INPUT
  796. *
  797. * OUTPUT
  798. *
  799. * RETURNS
  800. *
  801. * AUTHOR
  802. *
  803. * Alexander Enzmann
  804. *
  805. * DESCRIPTION
  806. *
  807. * Normal to a polynomial (for polynomials of order <= 4).
  808. *
  809. * CHANGES
  810. *
  811. * -
  812. *
  813. ******************************************************************************/
  814. static void normal1(VECTOR Result, int Order, DBL *Coeffs, VECTOR IPoint)
  815. {
  816. DBL *a, x, y, z, x2, y2, z2, x3, y3, z3;
  817. a = Coeffs;
  818. x = IPoint[X];
  819. y = IPoint[Y];
  820. z = IPoint[Z];
  821. switch (Order)
  822. {
  823. case 1:
  824. /* Linear partial derivatives */
  825. Make_Vector(Result, a[0], a[1], a[2])
  826. break;
  827. case 2:
  828. /* Quadratic partial derivatives */
  829. Result[X] = 2*a[0]*x+a[1]*y+a[2]*z+a[3];
  830. Result[Y] = a[1]*x+2*a[4]*y+a[5]*z+a[6];
  831. Result[Z] = a[2]*x+a[5]*y+2*a[7]*z+a[8];
  832. break;
  833. case 3:
  834. x2 = x * x; y2 = y * y; z2 = z * z;
  835. /* Cubic partial derivatives */
  836. Result[X] = 3*a[0]*x2 + 2*x*(a[1]*y + a[2]*z + a[3]) + a[4]*y2 +
  837. y*(a[5]*z + a[6]) + a[7]*z2 + a[8]*z + a[9];
  838. Result[Y] = a[1]*x2 + x*(2*a[4]*y + a[5]*z + a[6]) + 3*a[10]*y2 +
  839. 2*y*(a[11]*z + a[12]) + a[13]*z2 + a[14]*z + a[15];
  840. Result[Z] = a[2]*x2 + x*(a[5]*y + 2*a[7]*z + a[8]) + a[11]*y2 +
  841. y*(2*a[13]*z + a[14]) + 3*a[16]*z2 + 2*a[17]*z + a[18];
  842. break;
  843. case 4:
  844. /* Quartic partial derivatives */
  845. x2 = x * x; y2 = y * y; z2 = z * z;
  846. x3 = x * x2; y3 = y * y2; z3 = z * z2;
  847. Result[X] = 4*a[ 0]*x3+3*x2*(a[ 1]*y+a[ 2]*z+a[ 3])+
  848. 2*x*(a[ 4]*y2+y*(a[ 5]*z+a[ 6])+a[ 7]*z2+a[ 8]*z+a[ 9])+
  849. a[10]*y3+y2*(a[11]*z+a[12])+y*(a[13]*z2+a[14]*z+a[15])+
  850. a[16]*z3+a[17]*z2+a[18]*z+a[19];
  851. Result[Y] = a[ 1]*x3+x2*(2*a[ 4]*y+a[ 5]*z+a[ 6])+
  852. x*(3*a[10]*y2+2*y*(a[11]*z+a[12])+a[13]*z2+a[14]*z+a[15])+
  853. 4*a[20]*y3+3*y2*(a[21]*z+a[22])+2*y*(a[23]*z2+a[24]*z+a[25])+
  854. a[26]*z3+a[27]*z2+a[28]*z+a[29];
  855. Result[Z] = a[ 2]*x3+x2*(a[ 5]*y+2*a[ 7]*z+a[ 8])+
  856. x*(a[11]*y2+y*(2*a[13]*z+a[14])+3*a[16]*z2+2*a[17]*z+a[18])+
  857. a[21]*y3+y2*(2*a[23]*z+a[24])+y*(3*a[26]*z2+2*a[27]*z+a[28])+
  858. 4*a[30]*z3+3*a[31]*z2+2*a[32]*z+a[33];
  859. }
  860. }
  861. /*****************************************************************************
  862. *
  863. * FUNCTION
  864. *
  865. * Inside_Poly
  866. *
  867. * INPUT
  868. *
  869. * OUTPUT
  870. *
  871. * RETURNS
  872. *
  873. * AUTHOR
  874. *
  875. * Alexander Enzmann
  876. *
  877. * DESCRIPTION
  878. *
  879. * -
  880. *
  881. * CHANGES
  882. *
  883. * -
  884. *
  885. ******************************************************************************/
  886. static int Inside_Poly (VECTOR IPoint, OBJECT *Object)
  887. {
  888. VECTOR New_Point;
  889. DBL Result;
  890. /* Transform the point into polynomial's space */
  891. MInvTransPoint(New_Point, IPoint, ((POLY *)Object)->Trans);
  892. Result = inside(New_Point, ((POLY *)Object)->Order, ((POLY *)Object)->Coeffs);
  893. if (Result < INSIDE_TOLERANCE)
  894. {
  895. return ((int)(!Test_Flag(Object, INVERTED_FLAG)));
  896. }
  897. else
  898. {
  899. return ((int)Test_Flag(Object, INVERTED_FLAG));
  900. }
  901. }
  902. /*****************************************************************************
  903. *
  904. * FUNCTION
  905. *
  906. * Poly_Normal
  907. *
  908. * INPUT
  909. *
  910. * OUTPUT
  911. *
  912. * RETURNS
  913. *
  914. * AUTHOR
  915. *
  916. * Alexander Enzmann
  917. *
  918. * DESCRIPTION
  919. *
  920. * Normal to a polynomial.
  921. *
  922. * CHANGES
  923. *
  924. * -
  925. *
  926. ******************************************************************************/
  927. static void Poly_Normal(VECTOR Result, OBJECT *Object, INTERSECTION *Inter)
  928. {
  929. DBL val;
  930. VECTOR New_Point;
  931. POLY *Shape = (POLY *)Object;
  932. /* Transform the point into the polynomials space. */
  933. MInvTransPoint(New_Point, Inter->IPoint, Shape->Trans);
  934. if (((POLY *)Object)->Order > 4)
  935. {
  936. normal0(Result, Shape->Order, Shape->Coeffs, New_Point);
  937. }
  938. else
  939. {
  940. normal1(Result, Shape->Order, Shape->Coeffs, New_Point);
  941. }
  942. /* Transform back to world space. */
  943. MTransNormal(Result, Result, Shape->Trans);
  944. /* Normalize (accounting for the possibility of a 0 length normal). */
  945. VDot(val, Result, Result);
  946. if (val > 0.0)
  947. {
  948. val = 1.0 / sqrt(val);
  949. VScaleEq(Result, val);
  950. }
  951. else
  952. {
  953. Make_Vector(Result, 1.0, 0.0, 0.0)
  954. }
  955. }
  956. /*****************************************************************************
  957. *
  958. * FUNCTION
  959. *
  960. * Translate_Poly
  961. *
  962. * INPUT
  963. *
  964. * OUTPUT
  965. *
  966. * RETURNS
  967. *
  968. * AUTHOR
  969. *
  970. * Alexander Enzmann
  971. *
  972. * DESCRIPTION
  973. *
  974. * -
  975. *
  976. * CHANGES
  977. *
  978. * -
  979. *
  980. ******************************************************************************/
  981. static void Translate_Poly (OBJECT *Object, VECTOR Vector, TRANSFORM *Trans)
  982. {
  983. Transform_Poly(Object, Trans);
  984. }
  985. /*****************************************************************************
  986. *
  987. * FUNCTION
  988. *
  989. * Rotate_Poly
  990. *
  991. * INPUT
  992. *
  993. * OUTPUT
  994. *
  995. * RETURNS
  996. *
  997. * AUTHOR
  998. *
  999. * Alexander Enzmann
  1000. *
  1001. * DESCRIPTION
  1002. *
  1003. * -
  1004. *
  1005. * CHANGES
  1006. *
  1007. * -
  1008. *
  1009. ******************************************************************************/
  1010. static void Rotate_Poly (OBJECT *Object, VECTOR Vector, TRANSFORM *Trans)
  1011. {
  1012. Transform_Poly(Object, Trans);
  1013. }
  1014. /*****************************************************************************
  1015. *
  1016. * FUNCTION
  1017. *
  1018. * Scale_Poly
  1019. *
  1020. * INPUT
  1021. *
  1022. * OUTPUT
  1023. *
  1024. * RETURNS
  1025. *
  1026. * AUTHOR
  1027. *
  1028. * Alexander Enzmann
  1029. *
  1030. * DESCRIPTION
  1031. *
  1032. * -
  1033. *
  1034. * CHANGES
  1035. *
  1036. * -
  1037. *
  1038. ******************************************************************************/
  1039. static void Scale_Poly (OBJECT *Object, VECTOR Vector, TRANSFORM *Trans)
  1040. {
  1041. Transform_Poly(Object, Trans);
  1042. }
  1043. /*****************************************************************************
  1044. *
  1045. * FUNCTION
  1046. *
  1047. * Transform_Poly
  1048. *
  1049. * INPUT
  1050. *
  1051. * OUTPUT
  1052. *
  1053. * RETURNS
  1054. *
  1055. * AUTHOR
  1056. *
  1057. * Alexander Enzmann
  1058. *
  1059. * DESCRIPTION
  1060. *
  1061. * -
  1062. *
  1063. * CHANGES
  1064. *
  1065. * -
  1066. *
  1067. ******************************************************************************/
  1068. static void Transform_Poly(OBJECT *Object,TRANSFORM *Trans)
  1069. {
  1070. Compose_Transforms(((POLY *)Object)->Trans, Trans);
  1071. Compute_Poly_BBox((POLY *)Object);
  1072. }
  1073. /*****************************************************************************
  1074. *
  1075. * FUNCTION
  1076. *
  1077. * Invert_Poly
  1078. *
  1079. * INPUT
  1080. *
  1081. * OUTPUT
  1082. *
  1083. * RETURNS
  1084. *
  1085. * AUTHOR
  1086. *
  1087. * Alexander Enzmann
  1088. *
  1089. * DESCRIPTION
  1090. *
  1091. * -
  1092. *
  1093. * CHANGES
  1094. *
  1095. * -
  1096. *
  1097. ******************************************************************************/
  1098. static void Invert_Poly(OBJECT *Object)
  1099. {
  1100. Invert_Flag(Object, INVERTED_FLAG);
  1101. }
  1102. /*****************************************************************************
  1103. *
  1104. * FUNCTION
  1105. *
  1106. * Create_Poly
  1107. *
  1108. * INPUT
  1109. *
  1110. * OUTPUT
  1111. *
  1112. * RETURNS
  1113. *
  1114. * AUTHOR
  1115. *
  1116. * Alexander Enzmann
  1117. *
  1118. * DESCRIPTION
  1119. *
  1120. * -
  1121. *
  1122. * CHANGES
  1123. *
  1124. * -
  1125. *
  1126. ******************************************************************************/
  1127. POLY *Create_Poly(int Order)
  1128. {
  1129. POLY *New;
  1130. int i;
  1131. New = (POLY *)POV_MALLOC(sizeof (POLY), "poly");
  1132. INIT_OBJECT_FIELDS(New,POLY_OBJECT, &Poly_Methods);
  1133. New->Order = Order;
  1134. New->Trans = Create_Transform();
  1135. New->Coeffs = (DBL *)POV_MALLOC(term_counts(Order) * sizeof(DBL), "coefficients for POLY");
  1136. for (i = 0; i < term_counts(Order); i++)
  1137. {
  1138. New->Coeffs[i] = 0.0;
  1139. }
  1140. return(New);
  1141. }
  1142. /*****************************************************************************
  1143. *
  1144. * FUNCTION
  1145. *
  1146. * Copy_Poly
  1147. *
  1148. * INPUT
  1149. *
  1150. * OUTPUT
  1151. *
  1152. * RETURNS
  1153. *
  1154. * AUTHOR
  1155. *
  1156. * Alexander Enzmann
  1157. *
  1158. * DESCRIPTION
  1159. *
  1160. * Make a copy of a polynomial object.
  1161. *
  1162. * CHANGES
  1163. *
  1164. * -
  1165. *
  1166. ******************************************************************************/
  1167. static POLY *Copy_Poly(OBJECT *Object)
  1168. {
  1169. POLY *New;
  1170. int i;
  1171. New = Create_Poly (((POLY *)Object)->Order);
  1172. /* Get rid of transform created in Create_Poly. */
  1173. Destroy_Transform(New->Trans);
  1174. Copy_Flag(New, Object, STURM_FLAG);
  1175. Copy_Flag(New, Object, INVERTED_FLAG);
  1176. New->Trans = Copy_Transform(((POLY *)Object)->Trans);
  1177. for (i = 0; i < term_counts(New->Order); i++)
  1178. {
  1179. New->Coeffs[i] = ((POLY *)Object)->Coeffs[i];
  1180. }
  1181. return (New);
  1182. }
  1183. /*****************************************************************************
  1184. *
  1185. * FUNCTION
  1186. *
  1187. * Destroy_Poly
  1188. *
  1189. * INPUT
  1190. *
  1191. * OUTPUT
  1192. *
  1193. * RETURNS
  1194. *
  1195. * AUTHOR
  1196. *
  1197. * Alexander Enzmann
  1198. *
  1199. * DESCRIPTION
  1200. *
  1201. * -
  1202. *
  1203. * CHANGES
  1204. *
  1205. * -
  1206. *
  1207. ******************************************************************************/
  1208. static void Destroy_Poly(OBJECT *Object)
  1209. {
  1210. Destroy_Transform (((POLY *)Object)->Trans);
  1211. POV_FREE (((POLY *)Object)->Coeffs);
  1212. POV_FREE (Object);
  1213. }
  1214. /*****************************************************************************
  1215. *
  1216. * FUNCTION
  1217. *
  1218. * Compute_Poly_BBox
  1219. *
  1220. * INPUT
  1221. *
  1222. * Poly - Poly
  1223. *
  1224. * OUTPUT
  1225. *
  1226. * Poly
  1227. *
  1228. * RETURNS
  1229. *
  1230. * AUTHOR
  1231. *
  1232. * Dieter Bayer
  1233. *
  1234. * DESCRIPTION
  1235. *
  1236. * Calculate the bounding box of a poly.
  1237. *
  1238. * CHANGES
  1239. *
  1240. * Aug 1994 : Creation.
  1241. *
  1242. ******************************************************************************/
  1243. void Compute_Poly_BBox(POLY *Poly)
  1244. {
  1245. Make_BBox(Poly->BBox, -BOUND_HUGE/2, -BOUND_HUGE/2, -BOUND_HUGE/2, BOUND_HUGE, BOUND_HUGE, BOUND_HUGE);
  1246. if (Poly->Clip != NULL)
  1247. {
  1248. Poly->BBox = Poly->Clip->BBox;
  1249. }
  1250. }