BSPHERE.C 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  1. /*****************************************************************************
  2. * bsphere.c
  3. *
  4. * This module implements the bounding sphere calculations.
  5. *
  6. * from Persistence of Vision(tm) Ray Tracer
  7. * Copyright 1996,1999 Persistence of Vision Team
  8. *---------------------------------------------------------------------------
  9. * NOTICE: This source code file is provided so that users may experiment
  10. * with enhancements to POV-Ray and to port the software to platforms other
  11. * than those supported by the POV-Ray Team. There are strict rules under
  12. * which you are permitted to use this file. The rules are in the file
  13. * named POVLEGAL.DOC which should be distributed with this file.
  14. * If POVLEGAL.DOC is not available or for more info please contact the POV-Ray
  15. * Team Coordinator by email to team-coord@povray.org or visit us on the web at
  16. * http://www.povray.org. The latest version of POV-Ray may be found at this site.
  17. *
  18. * This program is based on the popular DKB raytracer version 2.12.
  19. * DKBTrace was originally written by David K. Buck.
  20. * DKBTrace Ver 2.0-2.12 were written by David K. Buck & Aaron A. Collins.
  21. *
  22. *****************************************************************************/
  23. #include "frame.h"
  24. #include "vector.h"
  25. #include "povproto.h"
  26. #include "bsphere.h"
  27. /*****************************************************************************
  28. * Local preprocessor defines
  29. ******************************************************************************/
  30. #define BRANCHING_FACTOR 4
  31. /*****************************************************************************
  32. * Local typedefs
  33. ******************************************************************************/
  34. /*****************************************************************************
  35. * Local variables
  36. ******************************************************************************/
  37. static int maxelements, Axis;
  38. /*****************************************************************************
  39. * Static functions
  40. ******************************************************************************/
  41. static void merge_spheres (VECTOR C, DBL *r, VECTOR C1, DBL r1, VECTOR C2, DBL r2);
  42. static void recompute_bound (BSPHERE_TREE *Node);
  43. static int find_axis (BSPHERE_TREE **Elements, int first, int last);
  44. static void build_area_table (BSPHERE_TREE **Elements, int a, int b, DBL *areas);
  45. static int sort_and_split (BSPHERE_TREE **Root, BSPHERE_TREE **Elements, int *nElem, int first, int last);
  46. static int CDECL comp_elements (CONST void *in_a, CONST void *in_b);
  47. /*****************************************************************************
  48. *
  49. * FUNCTION
  50. *
  51. * merge_spheres
  52. *
  53. * INPUT
  54. *
  55. * C, r - Center and radius^2 of new sphere
  56. * C1, r1, C2, r2 - Centers and radii^2 of spheres to merge
  57. *
  58. * OUTPUT
  59. *
  60. * C, r
  61. *
  62. * RETURNS
  63. *
  64. * AUTHOR
  65. *
  66. * Dieter Bayer
  67. *
  68. * DESCRIPTION
  69. *
  70. * Calculate a sphere that encloses two given spheres.
  71. *
  72. * CHANGES
  73. *
  74. * Jul 1994 : Creation.
  75. *
  76. * Oct 1994 : Added test for enclosed spheres. Calculate optimal sphere. [DB]
  77. *
  78. ******************************************************************************/
  79. static void merge_spheres(VECTOR C, DBL *r, VECTOR C1, DBL r1, VECTOR C2, DBL r2)
  80. {
  81. DBL l, r1r, r2r, k1, k2;
  82. VECTOR D;
  83. VSub(D, C1, C2);
  84. VLength(l, D);
  85. /* Check if one sphere encloses the other. */
  86. r1r = sqrt(r1);
  87. r2r = sqrt(r2);
  88. if (l + r1r <= r2r)
  89. {
  90. Assign_Vector(C, C2);
  91. *r = r2;
  92. return;
  93. }
  94. if (l + r2r <= r1r)
  95. {
  96. Assign_Vector(C, C1);
  97. *r = r1;
  98. return;
  99. }
  100. k1 = (1.0 + (r1r - r2r) / l) / 2.0;
  101. k2 = (1.0 + (r2r - r1r) / l) / 2.0;
  102. VLinComb2(C, k1, C1, k2, C2);
  103. *r = Sqr((l + r1r + r2r) / 2.0);
  104. }
  105. /*****************************************************************************
  106. *
  107. * FUNCTION
  108. *
  109. * recompute_bound
  110. *
  111. * INPUT
  112. *
  113. * Node - Pointer to node
  114. *
  115. * OUTPUT
  116. *
  117. * Node
  118. *
  119. * RETURNS
  120. *
  121. * AUTHOR
  122. *
  123. * Dieter Bayer
  124. *
  125. * DESCRIPTION
  126. *
  127. * Recompute the bounding sphere of a given node in the bounding hierarchy,
  128. * i. e. find the bounding sphere that encloses the bounding spheres
  129. * of all nodes.
  130. *
  131. * NOTE: The sphere found is probably not the tightest sphere possible!
  132. *
  133. * CHANGES
  134. *
  135. * Jul 1994 : Creation.
  136. *
  137. * Oct 1994 : Improved bounding sphere calculation. [DB]
  138. *
  139. ******************************************************************************/
  140. static void recompute_bound(BSPHERE_TREE *Node)
  141. {
  142. short i;
  143. DBL r2;
  144. VECTOR C;
  145. COOPERATE_1
  146. Assign_Vector(C, Node->Node[0]->C);
  147. r2 = Node->Node[0]->r2;
  148. for (i = 1; i < Node->Entries; i++)
  149. {
  150. merge_spheres(C, &r2, C, r2, Node->Node[i]->C, Node->Node[i]->r2);
  151. }
  152. Assign_Vector(Node->C, C);
  153. Node->r2 = r2;
  154. }
  155. /*****************************************************************************
  156. *
  157. * FUNCTION
  158. *
  159. * comp_elements
  160. *
  161. * INPUT
  162. *
  163. * in_a, in_b - elements to compare
  164. *
  165. * OUTPUT
  166. *
  167. * RETURNS
  168. *
  169. * int - result of comparison
  170. *
  171. * AUTHOR
  172. *
  173. * Dieter Bayer
  174. *
  175. * DESCRIPTION
  176. *
  177. * -
  178. *
  179. * CHANGES
  180. *
  181. * Oct 1994 : Creation. (Derived from the bounding slab creation code)
  182. *
  183. ******************************************************************************/
  184. static int CDECL comp_elements(CONST void *in_a, CONST void *in_b)
  185. {
  186. DBL am, bm;
  187. am = (*(BSPHERE_TREE **)in_a)->C[Axis];
  188. bm = (*(BSPHERE_TREE **)in_b)->C[Axis];
  189. if (am < bm - EPSILON)
  190. {
  191. return (-1);
  192. }
  193. else
  194. {
  195. if (am > bm + EPSILON)
  196. {
  197. return (1);
  198. }
  199. else
  200. {
  201. return (0);
  202. }
  203. }
  204. }
  205. /*****************************************************************************
  206. *
  207. * FUNCTION
  208. *
  209. * find_axis
  210. *
  211. * INPUT
  212. *
  213. * OUTPUT
  214. *
  215. * RETURNS
  216. *
  217. * AUTHOR
  218. *
  219. * Dieter Bayer
  220. *
  221. * DESCRIPTION
  222. *
  223. * -
  224. *
  225. * CHANGES
  226. *
  227. * Oct 1994 : Creation. (Derived from the bounding slab creation code)
  228. *
  229. ******************************************************************************/
  230. static int find_axis(BSPHERE_TREE **Elements, int first, int last)
  231. {
  232. int which = X;
  233. int i;
  234. DBL e, d = - BOUND_HUGE;
  235. VECTOR C, mins, maxs;
  236. Make_Vector(mins, BOUND_HUGE, BOUND_HUGE, BOUND_HUGE);
  237. Make_Vector(maxs, -BOUND_HUGE, -BOUND_HUGE, -BOUND_HUGE);
  238. for (i = first; i < last; i++)
  239. {
  240. Assign_Vector(C, Elements[i]->C);
  241. mins[X] = min(mins[X], C[X]);
  242. maxs[X] = max(maxs[X], C[X]);
  243. mins[Y] = min(mins[Y], C[Y]);
  244. maxs[Y] = max(maxs[Y], C[Y]);
  245. mins[Z] = min(mins[Z], C[Z]);
  246. maxs[Z] = max(maxs[Z], C[Z]);
  247. }
  248. e = maxs[X] - mins[X];
  249. if (e > d)
  250. {
  251. d = e; which = X;
  252. }
  253. e = maxs[Y] - mins[Y];
  254. if (e > d)
  255. {
  256. d = e; which = Y;
  257. }
  258. e = maxs[Z] - mins[Z];
  259. if (e > d)
  260. {
  261. which = Z;
  262. }
  263. return (which);
  264. }
  265. /*****************************************************************************
  266. *
  267. * FUNCTION
  268. *
  269. * build_area_table
  270. *
  271. * INPUT
  272. *
  273. * OUTPUT
  274. *
  275. * RETURNS
  276. *
  277. * AUTHOR
  278. *
  279. * Dieter Bayer
  280. *
  281. * DESCRIPTION
  282. *
  283. * Generates a table of bounding sphere surface areas.
  284. *
  285. * CHANGES
  286. *
  287. * Oct 1994 : Creation. (Derived from the bounding slab creation code)
  288. *
  289. ******************************************************************************/
  290. static void build_area_table(BSPHERE_TREE **Elements, int a, int b, DBL *areas)
  291. {
  292. int i, imin, dir;
  293. DBL r2;
  294. VECTOR C;
  295. if (a < b)
  296. {
  297. imin = a; dir = 1;
  298. }
  299. else
  300. {
  301. imin = b; dir = -1;
  302. }
  303. Assign_Vector(C, Elements[a]->C);
  304. r2 = Elements[a]->r2;
  305. for (i = a; i != (b + dir); i += dir)
  306. {
  307. merge_spheres(C, &r2, C, r2, Elements[i]->C, Elements[i]->r2);
  308. areas[i-imin] = r2;
  309. }
  310. }
  311. /*****************************************************************************
  312. *
  313. * FUNCTION
  314. *
  315. * sort_and_split
  316. *
  317. * INPUT
  318. *
  319. * OUTPUT
  320. *
  321. * RETURNS
  322. *
  323. * AUTHOR
  324. *
  325. * Dieter Bayer
  326. *
  327. * DESCRIPTION
  328. *
  329. * -
  330. *
  331. * CHANGES
  332. *
  333. * Oct 1994 : Creation. (Derived from the bounding slab creation code)
  334. *
  335. ******************************************************************************/
  336. static int sort_and_split(BSPHERE_TREE **Root, BSPHERE_TREE **Elements, int *nElem, int first, int last)
  337. {
  338. int size, i, best_loc;
  339. DBL *area_left, *area_right;
  340. DBL best_index, new_index;
  341. BSPHERE_TREE *cd;
  342. Axis = find_axis(Elements, first, last);
  343. size = last - first;
  344. if (size <= 0)
  345. {
  346. return (1);
  347. }
  348. /*
  349. * Actually, we could do this faster in several ways. We could use a
  350. * logn algorithm to find the median along the given axis, and then a
  351. * linear algorithm to partition along the axis. Oh well.
  352. */
  353. QSORT((void *)(&Elements[first]), (size_t)size, sizeof(BSPHERE_TREE *), comp_elements);
  354. /*
  355. * area_left[] and area_right[] hold the surface areas of the bounding
  356. * boxes to the left and right of any given point. E.g. area_left[i] holds
  357. * the surface area of the bounding box containing Elements 0 through i and
  358. * area_right[i] holds the surface area of the box containing Elements
  359. * i through size-1.
  360. */
  361. area_left = (DBL *)POV_MALLOC(size * sizeof(DBL), "blob bounding hierarchy");
  362. area_right = (DBL *)POV_MALLOC(size * sizeof(DBL), "blob bounding hierarchy");
  363. /* Precalculate the areas for speed. */
  364. build_area_table(Elements, first, last - 1, area_left);
  365. build_area_table(Elements, last - 1, first, area_right);
  366. best_index = area_right[0] * (size - 3.0);
  367. best_loc = - 1;
  368. /*
  369. * Find the most effective point to split. The best location will be
  370. * the one that minimizes the function N1*A1 + N2*A2 where N1 and N2
  371. * are the number of objects in the two groups and A1 and A2 are the
  372. * surface areas of the bounding boxes of the two groups.
  373. */
  374. for (i = 0; i < size - 1; i++)
  375. {
  376. new_index = (i + 1) * area_left[i] + (size - 1 - i) * area_right[i + 1];
  377. if (new_index < best_index)
  378. {
  379. best_index = new_index;
  380. best_loc = i + first;
  381. }
  382. }
  383. POV_FREE(area_left);
  384. POV_FREE(area_right);
  385. /*
  386. * Stop splitting if the BRANCHING_FACTOR is reached or
  387. * if splitting stops being effective.
  388. */
  389. if ((size <= BRANCHING_FACTOR) || (best_loc < 0))
  390. {
  391. cd = (BSPHERE_TREE *)POV_MALLOC(sizeof(BSPHERE_TREE), "blob bounding hierarchy");
  392. cd->Entries = (short)size;
  393. cd->Node = (BSPHERE_TREE **)POV_MALLOC(size*sizeof(BSPHERE_TREE *), "blob bounding hierarchy");
  394. for (i = 0; i < size; i++)
  395. {
  396. cd->Node[i] = Elements[first+i];
  397. }
  398. recompute_bound(cd);
  399. *Root = cd;
  400. if (*nElem > maxelements)
  401. {
  402. /* Prim array overrun, increase array by 50%. */
  403. maxelements = 1.5 * maxelements;
  404. /* For debugging only. */
  405. Debug_Info("Reallocing elements to %d\n", maxelements);
  406. Elements = (BSPHERE_TREE **)POV_REALLOC(Elements, maxelements * sizeof(BSPHERE_TREE *), "bounding slabs");
  407. }
  408. Elements[*nElem] = cd;
  409. (*nElem)++;
  410. return (1);
  411. }
  412. else
  413. {
  414. sort_and_split(Root, Elements, nElem, first, best_loc + 1);
  415. sort_and_split(Root, Elements, nElem, best_loc + 1, last);
  416. return (0);
  417. }
  418. }
  419. /*****************************************************************************
  420. *
  421. * FUNCTION
  422. *
  423. * Build_Bounding_Sphere_Hierarchy
  424. *
  425. * INPUT
  426. *
  427. * nElem - number of elements in Elements
  428. * Elements - array containing nElem elements
  429. *
  430. * OUTPUT
  431. *
  432. * Root - root node of the hierarchy
  433. *
  434. * RETURNS
  435. *
  436. * AUTHOR
  437. *
  438. * Dieter Bayer
  439. *
  440. * DESCRIPTION
  441. *
  442. * Create the bounding sphere hierarchy for a given set of elements.
  443. * One element consists of an element number (index into the array
  444. * of elements; e.g. blob components, triangles) and a bounding
  445. * sphere enclosing this element (center and squared radius).
  446. *
  447. * CHANGES
  448. *
  449. * Oct 1994 : Creation. (Derived from the bounding slab creation code)
  450. *
  451. ******************************************************************************/
  452. void Build_Bounding_Sphere_Hierarchy(BSPHERE_TREE **Root, int nElem, BSPHERE_TREE **Elements)
  453. {
  454. int low, high;
  455. /*
  456. * This is a resonable guess at the number of elements needed.
  457. * This array will be reallocated as needed if it isn't.
  458. */
  459. maxelements = 2 * nElem;
  460. /*
  461. * Do a sort on the elements, with the end result being
  462. * a tree of objects sorted along the x, y, and z axes.
  463. */
  464. if (nElem > 0)
  465. {
  466. low = 0;
  467. high = nElem;
  468. while (sort_and_split(Root, Elements, &nElem, low, high) == 0)
  469. {
  470. low = high;
  471. high = nElem;
  472. }
  473. }
  474. }
  475. /*****************************************************************************
  476. *
  477. * FUNCTION
  478. *
  479. * Destroy_Bounding_Sphere_Hierarchy
  480. *
  481. * INPUT
  482. *
  483. * Node - Pointer to current node
  484. *
  485. * OUTPUT
  486. *
  487. * Node
  488. *
  489. * RETURNS
  490. *
  491. * AUTHOR
  492. *
  493. * Dieter Bayer
  494. *
  495. * DESCRIPTION
  496. *
  497. * Destroy bounding sphere hierarchy.
  498. *
  499. * CHANGES
  500. *
  501. * Aug 1994 : Creation.
  502. *
  503. * Dec 1994 : Fixed memory leakage. [DB]
  504. *
  505. ******************************************************************************/
  506. void Destroy_Bounding_Sphere_Hierarchy(BSPHERE_TREE *Node)
  507. {
  508. short i;
  509. if (Node != NULL)
  510. {
  511. if (Node->Entries > 0)
  512. {
  513. /* This is a node. Free sub-nodes. */
  514. for (i = 0; i < Node->Entries; i++)
  515. {
  516. Destroy_Bounding_Sphere_Hierarchy(Node->Node[i]);
  517. }
  518. POV_FREE(Node->Node);
  519. }
  520. POV_FREE(Node);
  521. }
  522. }