home *** CD-ROM | disk | FTP | other *** search
/ Crawly Crypt Collection 2 / crawlyvol2.bin / graphics / ftpovstc / bound.c < prev    next >
C/C++ Source or Header  |  1994-05-25  |  25KB  |  952 lines

  1. /****************************************************************************
  2. *
  3. *  ATTENTION!!!
  4. *
  5. *  THIS FILE HAS BEEN MODIFIED!!! IT IS NOT PART OF THE OFFICAL
  6. *  POV-RAY 2.2 DISTRIBUTION!!!
  7. *
  8. *  THIS FILE IS PART OF "FASTER THAN POV-RAY" (VERSION 1.1),
  9. *  A SPED-UP VERSION OF POV-RAY 2.2. USE AT YOUR OWN RISK!!!!!!
  10. *
  11. *  New files: addon0.c, addon1.c, addon2.c, addon3.c, addon.h
  12. *
  13. *  The additional modules were written by Dieter Bayer.
  14. *
  15. *  Send comments, suggestions, bugs, ideas ... to:
  16. *
  17. *  dieter@cip.e-technik.uni-erlangen.de
  18. *
  19. *  All changed/added lines are enclosed in #ifdef DB_CODE ... #endif
  20. *
  21. *  The vista projection was taken from:
  22. *
  23. *    A. Hashimoto, T. Akimoto, K. Mase, and Y. Suenaga, 
  24. *    "Vista Ray-Tracing: High Speed Ray Tracing Using Perspective
  25. *    Projection Image", New Advances in Computer Graphics, Proceedings
  26. *    of CG International '89, R. A. Earnshaw, B. Wyvill (Eds.), 
  27. *    Springer, ..., pp. 549-560
  28. *
  29. *  The idea for the light buffer was taken from:
  30. *
  31. *    E. Haines and D. Greenberg, "The Light Buffer: A Shadow-Testing 
  32. *    Accelerator", IEEE CG&A, Vol. 6, No. 9, Sept. 1986, pp. 6-16
  33. *
  34. *****************************************************************************/
  35.  
  36. /****************************************************************************
  37. *                bound.c
  38. *
  39. *  This module implements the bounding slab calculations.
  40. *  This file was written by Alexander Enzmann.    He wrote the code for
  41. *  POV-Ray's bounding slabs and generously provided us these enhancements.
  42. *  The slab intersection code was further hacked by Eric Haines to speed it up.
  43. *
  44. *  Just so everyone knows where this came from, the code is VERY heavily
  45. *  based on the slab code from Mark VandeWettering's MTV raytracer.
  46. *  POV-Ray is just joining the crowd of admirers of Mark's contribution to
  47. *  the public domain. [ARE]
  48. *
  49. *  from Persistence of Vision Raytracer
  50. *  Copyright 1993 Persistence of Vision Team
  51. *---------------------------------------------------------------------------
  52. *  NOTICE: This source code file is provided so that users may experiment
  53. *  with enhancements to POV-Ray and to port the software to platforms other
  54. *  than those supported by the POV-Ray Team.  There are strict rules under
  55. *  which you are permitted to use this file.  The rules are in the file
  56. *  named POVLEGAL.DOC which should be distributed with this file. If
  57. *  POVLEGAL.DOC is not available or for more info please contact the POV-Ray
  58. *  Team Coordinator by leaving a message in CompuServe's Graphics Developer's
  59. *  Forum.  The latest version of POV-Ray may be found there as well.
  60. *
  61. * This program is based on the popular DKB raytracer version 2.12.
  62. * DKBTrace was originally written by David K. Buck.
  63. * DKBTrace Ver 2.0-2.12 were written by David K. Buck & Aaron A. Collins.
  64. *
  65. *****************************************************************************/
  66.  
  67. #include "frame.h"
  68. #include "vector.h"
  69. #include "povproto.h"
  70.  
  71. #ifdef DB_CODE
  72.   /* Changes necessary for the vista/light buffer. */
  73.   /* I've moved the following structures to frame.h because they
  74.      are used in addon1.c. */
  75. #else
  76.   typedef struct {
  77.   int x,y,z ;
  78.   }
  79. VECTORI, *pVECTORI ;
  80.  
  81.   typedef struct {
  82.   VECTOR slab_num ;
  83.   VECTOR slab_den ;
  84.   VECTORI nonzero ;
  85.   VECTORI positive ;
  86.   }
  87. RAYINFO, *pRAYINFO ;
  88. #endif
  89.  
  90.  
  91. extern FRAME Frame;
  92. extern long Bounds_Threshold;
  93. extern int Use_Slabs;
  94. static int Axis = 0;
  95. static unsigned long maxprimcount = 0;
  96.  
  97. #ifdef DB_CODE
  98.   /* Changes necessary for the vista/light buffer. */
  99.   /* I've moved the following structure to frame.h because they
  100.      are used in addon1.c */
  101. #else
  102.   typedef struct t_qelem {
  103.   DBL     q_key;
  104.   OBJECT *q_obj;
  105.   }
  106. Qelem;
  107. #endif
  108.  
  109. static int FindAxis PARAMS((OBJECT **Prims, unsigned long first,
  110. unsigned long last));
  111. static COMPOSITE *Create_Composite PARAMS((void));
  112. static int SortAndSplit PARAMS((OBJECT **Root, OBJECT **Prims,
  113. unsigned long *nPrims, unsigned long first, unsigned long last));
  114. #ifdef DB_CODE
  115.   /* Changes necessary for the vista/light buffer. */
  116.   /* I need this functions in addon1.c and moved the headers into povproto.h */
  117. #else
  118. static void PriorityQueueInsert PARAMS((Qelem *Queue, unsigned *Qsize,
  119. DBL key, OBJECT *obj));
  120. static void CheckAndEnqueue PARAMS((Qelem *Queue, unsigned *Qsize,
  121. OBJECT *obj, RAYINFO *rayinfo));
  122. static void PriorityQueueDelete PARAMS((Qelem *Queue, unsigned *Qsize,
  123. DBL *key, OBJECT **obj));
  124. #endif
  125. /* QSORT_FUNCT_RET compslabs PARAMS((QSORT_FUNCT_PARAM in_a,
  126. QSORT_FUNCT_PARAM in_b)); */
  127.  
  128. /* Should move these out of here... */
  129. unsigned long totalQueues = 0;
  130. unsigned long totalQueueResets = 0;
  131. unsigned long nChecked = 0;
  132. unsigned long nEnqueued = 0;
  133.  
  134. unsigned MAXQUEUE = 512;
  135.  
  136. METHODS Composite_Methods =
  137.   {
  138.   NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, Destroy_Composite
  139. };
  140.  
  141. void Destroy_Composite (Object)
  142. OBJECT *Object;
  143.   {
  144.   free (Object);
  145.   }
  146.  
  147. static COMPOSITE *Create_Composite ()
  148.   {
  149.   COMPOSITE *New;
  150.  
  151.   if ((New = (COMPOSITE *) malloc (sizeof (COMPOSITE))) == NULL)
  152.     MAError ("composite");
  153.  
  154.   INIT_OBJECT_FIELDS(New, COMPOSITE_OBJECT, &Composite_Methods)
  155.     return New;
  156.   }
  157.  
  158. void
  159. recompute_bbox(bbox, trans)
  160. BBOX *bbox;
  161. TRANSFORM *trans;
  162.   {
  163.   VECTOR lower_left, lengths, corner;
  164.   VECTOR mins, maxs;
  165.   int i;
  166.  
  167.   lower_left = bbox->Lower_Left;
  168.   lengths    = bbox->Lengths;
  169.   Make_Vector(&mins,  BOUND_HUGE,  BOUND_HUGE,  BOUND_HUGE);
  170.   Make_Vector(&maxs, -BOUND_HUGE, -BOUND_HUGE, -BOUND_HUGE);
  171.   for (i=1;i<=8;i++)
  172.     {
  173.     corner = lower_left;
  174.     corner.x += ((i & 1) ? lengths.x : 0.0);
  175.     corner.y += ((i & 2) ? lengths.y : 0.0);
  176.     corner.z += ((i & 4) ? lengths.z : 0.0);
  177.     MTransPoint(&corner, &corner, trans);
  178.     if (corner.x < mins.x) mins.x = corner.x;
  179.     if (corner.x > maxs.x) maxs.x = corner.x;
  180.     if (corner.y < mins.y) mins.y = corner.y;
  181.     if (corner.y > maxs.y) maxs.y = corner.y;
  182.     if (corner.z < mins.z) mins.z = corner.z;
  183.     if (corner.z > maxs.z) maxs.z = corner.z;
  184.     }
  185.   bbox->Lower_Left = mins;
  186.   VSub(bbox->Lengths, maxs, mins);
  187.   }
  188.  
  189. void
  190. Recompute_Inverse_BBox(bbox, trans)
  191. BBOX *bbox;
  192. TRANSFORM *trans;
  193.   {
  194.   VECTOR lower_left, lengths, corner;
  195.   VECTOR mins, maxs;
  196.   int i;
  197.  
  198.   lower_left = bbox->Lower_Left;
  199.   lengths = bbox->Lengths;
  200.   Make_Vector(&mins,  BOUND_HUGE,  BOUND_HUGE,  BOUND_HUGE);
  201.   Make_Vector(&maxs, -BOUND_HUGE, -BOUND_HUGE, -BOUND_HUGE);
  202.   for (i=1;i<=8;i++)
  203.     {
  204.     corner = lower_left;
  205.     corner.x += ((i & 1) ? lengths.x : 0.0);
  206.     corner.y += ((i & 2) ? lengths.y : 0.0);
  207.     corner.z += ((i & 4) ? lengths.z : 0.0);
  208.     MInvTransPoint(&corner, &corner, trans);
  209.     if (corner.x < mins.x) mins.x = corner.x;
  210.     if (corner.x > maxs.x) maxs.x = corner.x;
  211.     if (corner.y < mins.y) mins.y = corner.y;
  212.     if (corner.y > maxs.y) maxs.y = corner.y;
  213.     if (corner.z < mins.z) mins.z = corner.z;
  214.     if (corner.z > maxs.z) maxs.z = corner.z;
  215.     }
  216.   bbox->Lower_Left = mins;
  217.   VSub(bbox->Lengths, maxs, mins);
  218.   }
  219.  
  220. QSORT_FUNCT_RET compslabs(in_a, in_b)
  221. QSORT_FUNCT_PARAM in_a;
  222. QSORT_FUNCT_PARAM in_b;
  223.   {
  224.  
  225.   OBJECT **a, **b;
  226.   DBL am, bm;
  227.  
  228.   a = (OBJECT **)in_a;
  229.   b = (OBJECT **)in_b;
  230.  
  231.   switch (Axis) 
  232.   {
  233.   case 0:
  234.     am = 2.0 * (*a)->Bounds.Lower_Left.x + (*a)->Bounds.Lengths.x;
  235.     bm = 2.0 * (*b)->Bounds.Lower_Left.x + (*b)->Bounds.Lengths.x;
  236.     break;
  237.   case 1:
  238.     am = 2.0 * (*a)->Bounds.Lower_Left.y + (*a)->Bounds.Lengths.y;
  239.     bm = 2.0 * (*b)->Bounds.Lower_Left.y + (*b)->Bounds.Lengths.y;
  240.     break;
  241.   case 2:
  242.     am = 2.0 * (*a)->Bounds.Lower_Left.z + (*a)->Bounds.Lengths.z;
  243.     bm = 2.0 * (*b)->Bounds.Lower_Left.z + (*b)->Bounds.Lengths.z;
  244.     break;
  245.   default:
  246.     Error("Bad axis in compslabs\n");
  247.   }
  248.  
  249.   if (am < bm)
  250.     return -1;
  251.   else if (am == bm)
  252.     return 0;
  253.   else
  254.     return 1;
  255.   }
  256.  
  257. static int
  258. FindAxis(Prims, first, last)
  259. OBJECT **Prims;
  260. unsigned long first, last;
  261.   {
  262.   BBOX *bbox;
  263.   VECTOR mins, maxs;
  264.   unsigned long i;
  265.   int which;
  266.   DBL d = -BOUND_HUGE, e;
  267.  
  268.   Make_Vector(&mins, BOUND_HUGE, BOUND_HUGE, BOUND_HUGE);
  269.   Make_Vector(&maxs, -BOUND_HUGE, -BOUND_HUGE, -BOUND_HUGE);
  270.  
  271.   for (i=first;i<last;i++)
  272.     {
  273.     bbox = &(Prims[i]->Bounds);
  274.     if (bbox->Lower_Left.x < mins.x)
  275.       mins.x = bbox->Lower_Left.x;
  276.     if (bbox->Lower_Left.x + bbox->Lengths.x > maxs.x)
  277.       maxs.x = bbox->Lower_Left.x;
  278.     if (bbox->Lower_Left.y < mins.y)
  279.       mins.y = bbox->Lower_Left.y;
  280.     if (bbox->Lower_Left.y + bbox->Lengths.y > maxs.y)
  281.       maxs.y = bbox->Lower_Left.y;
  282.     if (bbox->Lower_Left.z < mins.z)
  283.       mins.z = bbox->Lower_Left.z;
  284.     if (bbox->Lower_Left.z + bbox->Lengths.z > maxs.z)
  285.       maxs.z = bbox->Lower_Left.z;
  286.     }
  287.  
  288.   e = maxs.x - mins.x;
  289.   if (e > d)
  290.     {
  291.     d = e; which = 0;
  292.   }
  293.   e = maxs.y - mins.y;
  294.   if (e > d)
  295.     {
  296.     d = e; which = 1;
  297.   }
  298.   e = maxs.z - mins.z;
  299.   if (e > d) 
  300.     { 
  301.     d = e; which = 2; 
  302.   }
  303.  
  304.   return which;
  305.   }
  306.  
  307. static int
  308. SortAndSplit(Root, Prims, nPrims, first, last)
  309. OBJECT **Root;
  310. OBJECT **Prims;
  311. unsigned long *nPrims;
  312. unsigned long first;
  313. unsigned long last;
  314.   {
  315.   COMPOSITE *cd;
  316.   unsigned long size, i, j, m;
  317.   DBL dmin, dmax, tmin, tmax;
  318.  
  319.   Axis = FindAxis(Prims, first, last);
  320.   size = last - first;
  321.  
  322.   /* Actually, we could do this faster in several ways. we could use a
  323.       logn algorithm to find the median along the given axis, and then a
  324.       linear algorithm to partition along the axis. Oh well. */
  325.  
  326.   qsort((char *) (Prims + first), (int)size, sizeof(OBJECT *), compslabs);
  327.  
  328.   if (size <= BUNCHING_FACTOR) 
  329.     {
  330.     cd = Create_Composite();
  331.     cd->Entries = (unsigned short)size;
  332.  
  333.     for (i=0;i<size;i++) 
  334.       {
  335.       cd->Objects[i] = Prims[first+i];
  336. /*
  337. printf("Extent of object %ld/%d: <%g, %g, %g> -> <%g, %g, %g>\n",
  338.        first+i, cd->Objects[i]->Type,
  339.        cd->Objects[i]->Bounds.Lower_Left.x,
  340.        cd->Objects[i]->Bounds.Lower_Left.y,
  341.        cd->Objects[i]->Bounds.Lower_Left.z,
  342.        cd->Objects[i]->Bounds.Lower_Left.x + cd->Objects[i]->Bounds.Lengths.x,
  343.        cd->Objects[i]->Bounds.Lower_Left.y + cd->Objects[i]->Bounds.Lengths.y,
  344.        cd->Objects[i]->Bounds.Lower_Left.z + cd->Objects[i]->Bounds.Lengths.z);
  345. */
  346.       }
  347.  
  348.     /* Check bounds in each direction */
  349.     /* First along the x axis */
  350.     dmin = BOUND_HUGE; dmax = -BOUND_HUGE;
  351.     for (j=0;j<size;j++) 
  352.       {
  353.       tmin = cd->Objects[j]->Bounds.Lower_Left.x;
  354.       tmax = tmin + cd->Objects[j]->Bounds.Lengths.x;
  355.       if (tmin < dmin) dmin = tmin;
  356.       if (tmax > dmax) dmax = tmax;
  357.       }
  358.     cd->Bounds.Lower_Left.x = dmin;
  359.     cd->Bounds.Lengths.x = dmax - dmin;
  360.  
  361.     /* Now along the y axis */
  362.     dmin = BOUND_HUGE; dmax = -BOUND_HUGE;
  363.     for (j=0;j<size;j++)
  364.       {
  365.       tmin = cd->Objects[j]->Bounds.Lower_Left.y;
  366.       tmax = tmin + cd->Objects[j]->Bounds.Lengths.y;
  367.       if (tmin < dmin) dmin = tmin;
  368.       if (tmax > dmax) dmax = tmax;
  369.       }
  370.     cd->Bounds.Lower_Left.y = dmin;
  371.     cd->Bounds.Lengths.y = dmax - dmin;
  372.  
  373.     /* Lastly along the z axis */
  374.     dmin = BOUND_HUGE; dmax = -BOUND_HUGE;
  375.     for (j=0;j<size;j++) 
  376.       {
  377.       tmin = cd->Objects[j]->Bounds.Lower_Left.z;
  378.       tmax = tmin + cd->Objects[j]->Bounds.Lengths.z;
  379.       if (tmin < dmin) dmin = tmin;
  380.       if (tmax > dmax) dmax = tmax;
  381.       }
  382.     cd->Bounds.Lower_Left.z = dmin;
  383.     cd->Bounds.Lengths.z = dmax - dmin;
  384.  
  385.     *Root = (OBJECT *)cd;
  386.     if (*nPrims <= maxprimcount) 
  387.       {
  388.       Prims[*nPrims] = (OBJECT *)cd;
  389.       *nPrims += 1;
  390.       return 1;
  391.       }
  392.     else
  393.       Error("Too many primitives\n");
  394.     }
  395.   else 
  396.     {
  397.     m = (first + last) / 2;
  398.     SortAndSplit(Root, Prims, nPrims, first, m);
  399.     SortAndSplit(Root, Prims, nPrims, m , last);
  400.     return 0;
  401.     }
  402.   return -1;
  403.   }
  404.  
  405.   void
  406.   BuildBoundingSlabs(Root)
  407.     OBJECT **Root;
  408.   {
  409.   OBJECT **Prims, **prim, *head;
  410.   unsigned long nPrims;
  411.   unsigned long low, high;
  412.  
  413.   /* We have to start by counting how many frame level object there are */
  414.   head   = Frame.Objects;
  415.   nPrims = 0;
  416.   while (head != NULL)
  417.     {
  418.     nPrims++;
  419.     head = head->Sibling;
  420.     }
  421.  
  422.   /* The total # of prims inflates around 150% when bounding objects are
  423.       generated.  If the 1.8 below proves to be too small, use 2.0.  The
  424.       inflation is never 200%. */
  425.   maxprimcount = (unsigned long)(1.8 * (nPrims + 1));
  426.  
  427.   /* Now allocate an array to hold references to these prims & any new
  428.      composite objects we may generate */
  429.   Prims = (OBJECT **)malloc((unsigned)maxprimcount * sizeof(OBJECT *));
  430.   if (Prims == NULL)
  431.     Error("Failed to allocate bounding slab reference information\n");
  432.  
  433.   /* Copy pointers to the objects into the array */
  434.   prim = Prims;
  435.   for (head=Frame.Objects;head!=NULL;head=head->Sibling)
  436.     {
  437.     if (head->Type & LIGHT_SOURCE_OBJECT)
  438.       {
  439.       /* Only bother with lights if they have an attached shape */
  440.       if (((LIGHT_SOURCE *)head)->Children != NULL)
  441.     *prim++ = ((LIGHT_SOURCE *)head)->Children;
  442.       else
  443.     nPrims--;
  444.       }
  445.     else
  446.       /* Normal sort of object - add it to the list */
  447.       *prim++ = head;
  448.     }
  449.  
  450.   /* Now do a sort on the objects, with the end result being a tree of
  451.       objects sorted along the x, y, and z axes */
  452.   low  = 0;
  453.   high = nPrims;
  454.   while (SortAndSplit(Root, Prims, &nPrims, low, high) == 0)
  455.     {
  456.     low  = high;
  457.     high = nPrims;
  458.     }
  459.  
  460.   Use_Slabs = (nPrims >= Bounds_Threshold);
  461.  
  462.   /* Test */
  463. /*
  464. printf("Extent of scene: <%g, %g, %g> -> <%g, %g, %g>\n",
  465.        (*Root)->Bounds.Lower_Left.x,
  466.        (*Root)->Bounds.Lower_Left.y,
  467.        (*Root)->Bounds.Lower_Left.z,
  468.        (*Root)->Bounds.Lower_Left.x + (*Root)->Bounds.Lengths.x,
  469.        (*Root)->Bounds.Lower_Left.y + (*Root)->Bounds.Lengths.y,
  470.        (*Root)->Bounds.Lower_Left.z + (*Root)->Bounds.Lengths.z);
  471. */
  472.  
  473.   /* Now we can get rid of the Prim array, and just use Root */
  474.   free(Prims);
  475.   }
  476.  
  477. #ifdef DB_CODE
  478. /* Changes necessary for the vista/light buffer. */
  479. /* This has to be global */
  480. void PriorityQueueInsert(Queue, Qsize, key, obj)
  481. #else
  482. static void
  483. PriorityQueueInsert(Queue, Qsize, key, obj)
  484. #endif
  485. Qelem *Queue;
  486. unsigned *Qsize;
  487. DBL key;
  488. OBJECT *obj;
  489.   {
  490.   unsigned size;
  491.   int i;
  492.   Qelem tmp;
  493.  
  494.   totalQueues++;
  495.   (*Qsize)++;
  496.   size = *Qsize;
  497.   /* if (size > maxQueueSize) maxQueueSize = size; */
  498.   if (size >= MAXQUEUE)
  499.     Error("Priority queue overflow");
  500.   Queue[size].q_key = key;
  501.   Queue[size].q_obj = obj;
  502.  
  503.   i = size;
  504.   while (i > 1 && Queue[i].q_key < Queue[i/2].q_key)
  505.     {
  506.     tmp = Queue[i];
  507.     Queue[i] = Queue[i/2];
  508.     Queue[i/2] = tmp;
  509.     i = i / 2;
  510.     }
  511.   }
  512.  
  513. #ifdef DB_CODE
  514. /* Changes necessary for the vista/light buffer. */
  515. /* This has to be global */
  516. void PriorityQueueDelete(Queue, Qsize, key, obj)
  517. #else
  518. static void
  519. PriorityQueueDelete(Queue, Qsize, key, obj)
  520. #endif
  521. Qelem *Queue;
  522. unsigned *Qsize;
  523. DBL *key;
  524. OBJECT **obj;
  525.   {
  526.   Qelem tmp;
  527.   int i, j;
  528.   unsigned size;
  529.  
  530.   if (*Qsize == 0)
  531.     Error("priority queue is empty");
  532.  
  533.   *key = Queue[1].q_key;
  534.   *obj = Queue[1].q_obj;
  535.   Queue[1] = Queue[*Qsize];
  536.   (*Qsize)--;
  537.   size = *Qsize;
  538.  
  539.   i = 1 ;
  540.  
  541.   while (2 * i <= (int)size)
  542.     {
  543.     if (2 * i == (int)size)
  544.       j = 2 * i;
  545.     else if (Queue[2*i].q_key < Queue[2*i+1].q_key)
  546.       j = 2 * i;
  547.     else
  548.       j = 2 * i + 1;
  549.  
  550.     if (Queue[i].q_key > Queue[j].q_key)
  551.       {
  552.       tmp = Queue[i];
  553.       Queue[i] = Queue[j];
  554.       Queue[j] = tmp;
  555.       i = j;
  556.       }
  557.     else
  558.       break;
  559.     }
  560.   }
  561.  
  562. #ifdef DB_CODE
  563. /* Changes necessary for the vista/light buffer. */
  564. /* I have to pass the bouding slab seperatly, otherwise I couldn't use
  565.    this function in addon1.c */
  566. void CheckAndEnqueue(Queue, Qsize, obj, Bounds, rayinfo)
  567. #else
  568. static void
  569. CheckAndEnqueue(Queue, Qsize, obj, rayinfo)
  570. #endif
  571. Qelem *Queue;
  572. unsigned *Qsize;
  573. OBJECT *obj;
  574. #ifdef DB_CODE
  575. /* Changes necessary for the vista/light buffer. */
  576. BBOX *Bounds;
  577. #endif
  578. RAYINFO *rayinfo;
  579.   {
  580.   DBL tmin, tmax;
  581.   DBL dmin, dmax ;
  582.  
  583.   nChecked++;
  584.  
  585. #ifdef DB_CODE
  586. /* Changes necessary for the vista/light buffer. */
  587. /* Just changed obj->Bounds to Bounds */
  588.   if (rayinfo->nonzero.x )
  589.     {
  590.     if (rayinfo->positive.x )
  591.       {
  592.       dmin = (Bounds->Lower_Left.x - rayinfo->slab_num.x) *
  593.       rayinfo->slab_den.x;
  594.       dmax = dmin + (Bounds->Lengths.x  * rayinfo->slab_den.x);
  595.       if ( dmax < EPSILON ) return ;
  596.       }
  597.     else
  598.       {
  599.       dmax = (Bounds->Lower_Left.x - rayinfo->slab_num.x) *
  600.     rayinfo->slab_den.x;
  601.       if ( dmax < EPSILON ) return ;
  602.       dmin = dmax + (Bounds->Lengths.x  * rayinfo->slab_den.x);
  603.       }
  604.     if ( dmin > dmax ) return ;
  605.     }
  606.   else
  607.     {
  608.     if ( ( rayinfo->slab_num.x < Bounds->Lower_Left.x ) ||
  609.       ( rayinfo->slab_num.x >
  610.       Bounds->Lengths.x + Bounds->Lower_Left.x ) ) return ;
  611.     dmin = -BOUND_HUGE; dmax = BOUND_HUGE;
  612.     }
  613.  
  614.   if (rayinfo->nonzero.y )
  615.     {
  616.     if (rayinfo->positive.y )
  617.       {
  618.       tmin = (Bounds->Lower_Left.y - rayinfo->slab_num.y) *
  619.       rayinfo->slab_den.y;
  620.       tmax = tmin + (Bounds->Lengths.y  * rayinfo->slab_den.y);
  621.       }
  622.     else
  623.       {
  624.       tmax = (Bounds->Lower_Left.y - rayinfo->slab_num.y) *
  625.     rayinfo->slab_den.y;
  626.       tmin = tmax + (Bounds->Lengths.y  * rayinfo->slab_den.y);
  627.       }
  628.     /* unwrap the logic - do the dmin and dmax checks only when tmin and
  629.       tmax actually affect anything, also try to escape ASAP.  Better
  630.       yet, fold the logic below into the two branches above so as to
  631.       compute only what is needed.
  632.     */
  633.     /* you might even try tmax < EPSILON first (instead of second) for an
  634.       early quick out
  635.     */
  636.     if ( tmax < dmax )
  637.       {
  638.       if ( tmax < EPSILON ) return;
  639.       /* check bounds only if tmax changes dmax */
  640.       if ( tmin > dmin )
  641.     {
  642.     if ( tmin > tmax ) return ;
  643.     /* do this last in case it's not needed! */
  644.     dmin = tmin ;
  645.     }
  646.       else
  647.     {
  648.     if ( dmin > tmax ) return ;
  649.     }
  650.       /* do this last in case it's not needed! */
  651.       dmax = tmax ;
  652.       }
  653.     else
  654.       {
  655.       if ( tmin > dmin )
  656.     {
  657.     if ( tmin > dmax ) return ;
  658.     /* do this last in case it's not needed! */
  659.     dmin = tmin ;
  660.     } /* else nothing needs to happen, since dmin and dmax did not
  661.            change! */
  662.  
  663.       }
  664.     }
  665.   else
  666.     {
  667.     if (rayinfo->slab_num.y < Bounds->Lower_Left.y ||
  668.       rayinfo->slab_num.y >
  669.       Bounds->Lengths.y + Bounds->Lower_Left.y ) return ;
  670.     }
  671.  
  672.   if (rayinfo->nonzero.z )
  673.     {
  674.     if (rayinfo->positive.z )
  675.       {
  676.       tmin = (Bounds->Lower_Left.z - rayinfo->slab_num.z) *
  677.       rayinfo->slab_den.z;
  678.       tmax = tmin + (Bounds->Lengths.z  * rayinfo->slab_den.z);
  679.       }
  680.     else
  681.       {
  682.       tmax = (Bounds->Lower_Left.z - rayinfo->slab_num.z) *
  683.     rayinfo->slab_den.z;
  684.       tmin = tmax + (Bounds->Lengths.z  * rayinfo->slab_den.z);
  685.       }
  686.     if ( tmax < dmax )
  687.       {
  688.       if ( tmax < EPSILON ) return;
  689.       /* check bounds only if tmax changes dmax */
  690.       if ( tmin > dmin )
  691.     {
  692.     if ( tmin > tmax ) return ;
  693.     /* do this last in case it's not needed! */
  694.     dmin = tmin ;
  695.     }
  696.       else
  697.     {
  698.     if ( dmin > tmax ) return ;
  699.     }
  700.       /* do this last in case it's not needed! */
  701.       dmax = tmax ;
  702.       }
  703.     else
  704.       {
  705.       if ( tmin > dmin )
  706.     {
  707.     if ( tmin > dmax ) return ;
  708.     /* do this last in case it's not needed! */
  709.     dmin = tmin ;
  710.     } /* else nothing needs to happen, since dmin and dmax did not
  711.            change! */
  712.  
  713.       }
  714.     }
  715.   else
  716.     {
  717.     if (rayinfo->slab_num.z < Bounds->Lower_Left.z ||
  718.       rayinfo->slab_num.z >
  719.       Bounds->Lengths.z + Bounds->Lower_Left.z ) return ;
  720.     }
  721. #else
  722.   if (rayinfo->nonzero.x )
  723.     {
  724.     if (rayinfo->positive.x )
  725.       {
  726.       dmin = (obj->Bounds.Lower_Left.x - rayinfo->slab_num.x) *
  727.       rayinfo->slab_den.x;
  728.       dmax = dmin + (obj->Bounds.Lengths.x  * rayinfo->slab_den.x);
  729.       if ( dmax < EPSILON ) return ;
  730.       }
  731.     else
  732.       {
  733.       dmax = (obj->Bounds.Lower_Left.x - rayinfo->slab_num.x) *
  734.     rayinfo->slab_den.x;
  735.       if ( dmax < EPSILON ) return ;
  736.       dmin = dmax + (obj->Bounds.Lengths.x  * rayinfo->slab_den.x);
  737.       }
  738.     if ( dmin > dmax ) return ;
  739.     }
  740.   else
  741.     {
  742.     if ( ( rayinfo->slab_num.x < obj->Bounds.Lower_Left.x ) ||
  743.       ( rayinfo->slab_num.x >
  744.       obj->Bounds.Lengths.x + obj->Bounds.Lower_Left.x ) ) return ;
  745.     dmin = -BOUND_HUGE; dmax = BOUND_HUGE;
  746.     }
  747.  
  748.   if (rayinfo->nonzero.y )
  749.     {
  750.     if (rayinfo->positive.y )
  751.       {
  752.       tmin = (obj->Bounds.Lower_Left.y - rayinfo->slab_num.y) *
  753.       rayinfo->slab_den.y;
  754.       tmax = tmin + (obj->Bounds.Lengths.y  * rayinfo->slab_den.y);
  755.       }
  756.     else
  757.       {
  758.       tmax = (obj->Bounds.Lower_Left.y - rayinfo->slab_num.y) *
  759.     rayinfo->slab_den.y;
  760.       tmin = tmax + (obj->Bounds.Lengths.y  * rayinfo->slab_den.y);
  761.       }
  762.     /* unwrap the logic - do the dmin and dmax checks only when tmin and
  763.       tmax actually affect anything, also try to escape ASAP.  Better
  764.       yet, fold the logic below into the two branches above so as to
  765.       compute only what is needed.
  766.     */
  767.     /* you might even try tmax < EPSILON first (instead of second) for an
  768.       early quick out
  769.     */
  770.     if ( tmax < dmax )
  771.       {
  772.       if ( tmax < EPSILON ) return;
  773.       /* check bounds only if tmax changes dmax */
  774.       if ( tmin > dmin )
  775.     {
  776.     if ( tmin > tmax ) return ;
  777.     /* do this last in case it's not needed! */
  778.     dmin = tmin ;
  779.     }
  780.       else
  781.     {
  782.     if ( dmin > tmax ) return ;
  783.     }
  784.       /* do this last in case it's not needed! */
  785.       dmax = tmax ;
  786.       }
  787.     else
  788.       {
  789.       if ( tmin > dmin )
  790.     {
  791.     if ( tmin > dmax ) return ;
  792.     /* do this last in case it's not needed! */
  793.     dmin = tmin ;
  794.     } /* else nothing needs to happen, since dmin and dmax did not
  795.            change! */
  796.  
  797.       }
  798.     }
  799.   else
  800.     {
  801.     if (rayinfo->slab_num.y < obj->Bounds.Lower_Left.y ||
  802.       rayinfo->slab_num.y >
  803.       obj->Bounds.Lengths.y + obj->Bounds.Lower_Left.y ) return ;
  804.     }
  805.  
  806.   if (rayinfo->nonzero.z )
  807.     {
  808.     if (rayinfo->positive.z )
  809.       {
  810.       tmin = (obj->Bounds.Lower_Left.z - rayinfo->slab_num.z) *
  811.       rayinfo->slab_den.z;
  812.       tmax = tmin + (obj->Bounds.Lengths.z  * rayinfo->slab_den.z);
  813.       }
  814.     else
  815.       {
  816.       tmax = (obj->Bounds.Lower_Left.z - rayinfo->slab_num.z) *
  817.     rayinfo->slab_den.z;
  818.       tmin = tmax + (obj->Bounds.Lengths.z  * rayinfo->slab_den.z);
  819.       }
  820.     if ( tmax < dmax )
  821.       {
  822.       if ( tmax < EPSILON ) return;
  823.       /* check bounds only if tmax changes dmax */
  824.       if ( tmin > dmin )
  825.     {
  826.     if ( tmin > tmax ) return ;
  827.     /* do this last in case it's not needed! */
  828.     dmin = tmin ;
  829.     }
  830.       else
  831.     {
  832.     if ( dmin > tmax ) return ;
  833.     }
  834.       /* do this last in case it's not needed! */
  835.       dmax = tmax ;
  836.       }
  837.     else
  838.       {
  839.       if ( tmin > dmin )
  840.     {
  841.     if ( tmin > dmax ) return ;
  842.     /* do this last in case it's not needed! */
  843.     dmin = tmin ;
  844.     } /* else nothing needs to happen, since dmin and dmax did not
  845.            change! */
  846.  
  847.       }
  848.     }
  849.   else
  850.     {
  851.     if (rayinfo->slab_num.z < obj->Bounds.Lower_Left.z ||
  852.       rayinfo->slab_num.z >
  853.       obj->Bounds.Lengths.z + obj->Bounds.Lower_Left.z ) return ;
  854.     }
  855. #endif
  856.  
  857.   PriorityQueueInsert(Queue, Qsize, dmin, obj);
  858.   nEnqueued++;
  859.   }
  860.  
  861. int
  862. Bounds_Intersect(Root, ray, Best_Intersection, Best_Object)
  863. OBJECT *Root;
  864. RAY *ray;
  865. INTERSECTION *Best_Intersection;
  866. OBJECT **Best_Object;
  867.   {
  868.   Qelem *Queue;
  869.   unsigned Qsize = 0;
  870.   int i;
  871.   OBJECT *cobj;
  872.   COMPOSITE *cdp;
  873.   RAYINFO rayinfo;
  874.   DBL t, key;
  875.   INTERSECTION New_Intersection;
  876.   int Intersection_Found = 0;
  877.  
  878.   Queue = (Qelem *)malloc(MAXQUEUE * sizeof(Qelem));
  879.   if (Queue == NULL)
  880.     Error("Failed to allocate priority queue\n");
  881.  
  882.   /* Create the direction vectors for this ray */
  883.   rayinfo.slab_num.x = ray->Initial.x;
  884.   rayinfo.slab_num.y = ray->Initial.y;
  885.   rayinfo.slab_num.z = ray->Initial.z;
  886.   if ( rayinfo.nonzero.x = ((t = ray->Direction.x) != 0.0) )
  887.     {
  888.     rayinfo.slab_den.x = 1.0 / t;
  889.     rayinfo.positive.x = ( ray->Direction.x > 0.0 ) ;
  890.     }
  891.   if ( rayinfo.nonzero.y = ((t = ray->Direction.y) != 0.0) )
  892.     {
  893.     rayinfo.slab_den.y = 1.0 / t;
  894.     rayinfo.positive.y = ( ray->Direction.y > 0.0 ) ;
  895.     }
  896.   if ( rayinfo.nonzero.z = ((t = ray->Direction.z) != 0.0) )
  897.     {
  898.     rayinfo.slab_den.z = 1.0 / t;
  899.     rayinfo.positive.z = ( ray->Direction.z > 0.0 ) ;
  900.     }
  901.  
  902.   /* start with an empty priority queue */
  903.   Qsize = 0;
  904.   totalQueueResets++;
  905.  
  906. #ifdef DB_CODE
  907.   /* Changes necessary for the vista/light buffer. */
  908.   /* Pass bounds seperatly */
  909.   CheckAndEnqueue(Queue, &Qsize, Root, &Root->Bounds, &rayinfo);
  910. #else
  911.   CheckAndEnqueue(Queue, &Qsize, Root, &rayinfo);
  912. #endif
  913.  
  914.   for (;;)
  915.     {
  916.     if (Qsize == 0)
  917.       break;
  918.  
  919.     PriorityQueueDelete(Queue, &Qsize, &key, &cobj);
  920.  
  921.     if (key > Best_Intersection->Depth)
  922.       break;
  923.     else
  924.       if (cobj->Type & BOUNDING_OBJECT)
  925.     {
  926.     cdp = (COMPOSITE *)cobj;
  927.     for (i=0;(unsigned short)i < cdp->Entries;i++)
  928. #ifdef DB_CODE
  929.       /* Changes necessary for the vista/light buffer. */
  930.       /* Pass bounds seperatly */
  931.       CheckAndEnqueue(Queue, &Qsize, cdp->Objects[i], &cdp->Objects[i]->Bounds, &rayinfo);
  932. #else
  933.       CheckAndEnqueue(Queue, &Qsize, cdp->Objects[i],
  934.         &rayinfo) ;
  935. #endif
  936.     }
  937.       else
  938.     {
  939.     if (Intersection(&New_Intersection, cobj, ray))
  940.       if (New_Intersection.Depth < Best_Intersection->Depth)
  941.         {
  942.         *Best_Intersection = New_Intersection;
  943.         *Best_Object = cobj;
  944.         Intersection_Found = TRUE;
  945.         }
  946.     }
  947.     }
  948.  
  949.   free(Queue);
  950.   return Intersection_Found;
  951.   }
  952.