home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: Graphics / Graphics.zip / pov22f.zip / source / bound.c < prev    next >
C/C++ Source or Header  |  1993-11-03  |  20KB  |  726 lines

  1. /****************************************************************************
  2. *                bound.c
  3. *
  4. *  This module implements the bounding slab calculations.
  5. *  This file was written by Alexander Enzmann.    He wrote the code for
  6. *  POV-Ray's bounding slabs and generously provided us these enhancements.
  7. *  The slab intersection code was further hacked by Eric Haines to speed it up.
  8. *
  9. *  Just so everyone knows where this came from, the code is VERY heavily
  10. *  based on the slab code from Mark VandeWettering's MTV raytracer.
  11. *  POV-Ray is just joining the crowd of admirers of Mark's contribution to
  12. *  the public domain. [ARE]
  13. *
  14. *  from Persistence of Vision Raytracer
  15. *  Copyright 1993 Persistence of Vision Team
  16. *---------------------------------------------------------------------------
  17. *  NOTICE: This source code file is provided so that users may experiment
  18. *  with enhancements to POV-Ray and to port the software to platforms other
  19. *  than those supported by the POV-Ray Team.  There are strict rules under
  20. *  which you are permitted to use this file.  The rules are in the file
  21. *  named POVLEGAL.DOC which should be distributed with this file. If
  22. *  POVLEGAL.DOC is not available or for more info please contact the POV-Ray
  23. *  Team Coordinator by leaving a message in CompuServe's Graphics Developer's
  24. *  Forum.  The latest version of POV-Ray may be found there as well.
  25. *
  26. * This program is based on the popular DKB raytracer version 2.12.
  27. * DKBTrace was originally written by David K. Buck.
  28. * DKBTrace Ver 2.0-2.12 were written by David K. Buck & Aaron A. Collins.
  29. *
  30. *****************************************************************************/
  31.  
  32. #include "frame.h"
  33. #include "vector.h"
  34. #include "povproto.h"
  35.  
  36.   typedef struct {
  37.   int x,y,z ;
  38.   } 
  39. VECTORI, *pVECTORI ;
  40.  
  41.   typedef struct {
  42.   VECTOR slab_num ;
  43.   VECTOR slab_den ;
  44.   VECTORI nonzero ;
  45.   VECTORI positive ;
  46.   } 
  47. RAYINFO, *pRAYINFO ;
  48.  
  49.  
  50. extern FRAME Frame;
  51. extern long Bounds_Threshold;
  52. extern int Use_Slabs;
  53. static int Axis = 0;
  54. static unsigned long maxprimcount = 0;
  55.   typedef struct t_qelem {
  56.   DBL     q_key;
  57.   OBJECT *q_obj;
  58.   } 
  59. Qelem;
  60.  
  61. static int FindAxis PARAMS((OBJECT **Prims, unsigned long first,
  62. unsigned long last));
  63. static COMPOSITE *Create_Composite PARAMS((void));
  64. static int SortAndSplit PARAMS((OBJECT **Root, OBJECT **Prims,
  65. unsigned long *nPrims, unsigned long first, unsigned long last));
  66. static void PriorityQueueInsert PARAMS((Qelem *Queue, unsigned *Qsize,
  67. DBL key, OBJECT *obj));
  68. static void CheckAndEnqueue PARAMS((Qelem *Queue, unsigned *Qsize,
  69. OBJECT *obj, RAYINFO *rayinfo));
  70. static void PriorityQueueDelete PARAMS((Qelem *Queue, unsigned *Qsize,
  71. DBL *key, OBJECT **obj));
  72. /* QSORT_FUNCT_RET compslabs PARAMS((QSORT_FUNCT_PARAM in_a,
  73. QSORT_FUNCT_PARAM in_b)); */
  74.  
  75. /* Should move these out of here... */
  76. unsigned long totalQueues = 0;
  77. unsigned long totalQueueResets = 0;
  78. unsigned long nChecked = 0;
  79. unsigned long nEnqueued = 0;
  80.  
  81. unsigned MAXQUEUE = 512;
  82.  
  83. METHODS Composite_Methods =
  84.   { 
  85.   NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, Destroy_Composite 
  86. };
  87.  
  88. void Destroy_Composite (Object)
  89. OBJECT *Object;
  90.   {
  91.   free (Object);
  92.   }
  93.  
  94. static COMPOSITE *Create_Composite ()
  95.   {
  96.   COMPOSITE *New;
  97.  
  98.   if ((New = (COMPOSITE *) malloc (sizeof (COMPOSITE))) == NULL)
  99.     MAError ("composite");
  100.  
  101.   INIT_OBJECT_FIELDS(New, COMPOSITE_OBJECT, &Composite_Methods)
  102.     return New;
  103.   }
  104.  
  105. void
  106. recompute_bbox(bbox, trans)
  107. BBOX *bbox;
  108. TRANSFORM *trans;
  109.   {
  110.   VECTOR lower_left, lengths, corner;
  111.   VECTOR mins, maxs;
  112.   int i;
  113.  
  114.   lower_left = bbox->Lower_Left;
  115.   lengths    = bbox->Lengths;
  116.   Make_Vector(&mins,  BOUND_HUGE,  BOUND_HUGE,  BOUND_HUGE);
  117.   Make_Vector(&maxs, -BOUND_HUGE, -BOUND_HUGE, -BOUND_HUGE);
  118.   for (i=1;i<=8;i++) 
  119.     {
  120.     corner = lower_left;
  121.     corner.x += ((i & 1) ? lengths.x : 0.0);
  122.     corner.y += ((i & 2) ? lengths.y : 0.0);
  123.     corner.z += ((i & 4) ? lengths.z : 0.0);
  124.     MTransPoint(&corner, &corner, trans);
  125.     if (corner.x < mins.x) mins.x = corner.x;
  126.     if (corner.x > maxs.x) maxs.x = corner.x;
  127.     if (corner.y < mins.y) mins.y = corner.y;
  128.     if (corner.y > maxs.y) maxs.y = corner.y;
  129.     if (corner.z < mins.z) mins.z = corner.z;
  130.     if (corner.z > maxs.z) maxs.z = corner.z;
  131.     }
  132.   bbox->Lower_Left = mins;
  133.   VSub(bbox->Lengths, maxs, mins);
  134.   }
  135.  
  136. void
  137. Recompute_Inverse_BBox(bbox, trans)
  138. BBOX *bbox;
  139. TRANSFORM *trans;
  140.   {
  141.   VECTOR lower_left, lengths, corner;
  142.   VECTOR mins, maxs;
  143.   int i;
  144.  
  145.   lower_left = bbox->Lower_Left;
  146.   lengths = bbox->Lengths;
  147.   Make_Vector(&mins,  BOUND_HUGE,  BOUND_HUGE,  BOUND_HUGE);
  148.   Make_Vector(&maxs, -BOUND_HUGE, -BOUND_HUGE, -BOUND_HUGE);
  149.   for (i=1;i<=8;i++) 
  150.     {
  151.     corner = lower_left;
  152.     corner.x += ((i & 1) ? lengths.x : 0.0);
  153.     corner.y += ((i & 2) ? lengths.y : 0.0);
  154.     corner.z += ((i & 4) ? lengths.z : 0.0);
  155.     MInvTransPoint(&corner, &corner, trans);
  156.     if (corner.x < mins.x) mins.x = corner.x;
  157.     if (corner.x > maxs.x) maxs.x = corner.x;
  158.     if (corner.y < mins.y) mins.y = corner.y;
  159.     if (corner.y > maxs.y) maxs.y = corner.y;
  160.     if (corner.z < mins.z) mins.z = corner.z;
  161.     if (corner.z > maxs.z) maxs.z = corner.z;
  162.     }
  163.   bbox->Lower_Left = mins;
  164.   VSub(bbox->Lengths, maxs, mins);
  165.   }
  166.  
  167. QSORT_FUNCT_RET compslabs(in_a, in_b)
  168. QSORT_FUNCT_PARAM in_a;
  169. QSORT_FUNCT_PARAM in_b;
  170.   {
  171.  
  172.   OBJECT **a, **b;
  173.   DBL am, bm;
  174.  
  175.   a = (OBJECT **)in_a;
  176.   b = (OBJECT **)in_b;
  177.  
  178.   switch (Axis) 
  179.   {
  180.   case 0:
  181.     am = 2.0 * (*a)->Bounds.Lower_Left.x + (*a)->Bounds.Lengths.x;
  182.     bm = 2.0 * (*b)->Bounds.Lower_Left.x + (*b)->Bounds.Lengths.x;
  183.     break;
  184.   case 1:
  185.     am = 2.0 * (*a)->Bounds.Lower_Left.y + (*a)->Bounds.Lengths.y;
  186.     bm = 2.0 * (*b)->Bounds.Lower_Left.y + (*b)->Bounds.Lengths.y;
  187.     break;
  188.   case 2:
  189.     am = 2.0 * (*a)->Bounds.Lower_Left.z + (*a)->Bounds.Lengths.z;
  190.     bm = 2.0 * (*b)->Bounds.Lower_Left.z + (*b)->Bounds.Lengths.z;
  191.     break;
  192.   default:
  193.     Error("Bad axis in compslabs\n");
  194.   }
  195.  
  196.   if (am < bm)
  197.     return -1;
  198.   else if (am == bm)
  199.     return 0;
  200.   else
  201.     return 1;
  202.   }
  203.  
  204. static int
  205. FindAxis(Prims, first, last)
  206. OBJECT **Prims;
  207. unsigned long first, last;
  208.   {
  209.   BBOX *bbox;
  210.   VECTOR mins, maxs;
  211.   unsigned long i;
  212.   int which;
  213.   DBL d = -BOUND_HUGE, e;
  214.  
  215.   Make_Vector(&mins, BOUND_HUGE, BOUND_HUGE, BOUND_HUGE);
  216.   Make_Vector(&maxs, -BOUND_HUGE, -BOUND_HUGE, -BOUND_HUGE);
  217.  
  218.   for (i=first;i<last;i++) 
  219.     {
  220.     bbox = &(Prims[i]->Bounds);
  221.     if (bbox->Lower_Left.x < mins.x)
  222.       mins.x = bbox->Lower_Left.x;
  223.     if (bbox->Lower_Left.x + bbox->Lengths.x > maxs.x)
  224.       maxs.x = bbox->Lower_Left.x;
  225.     if (bbox->Lower_Left.y < mins.y)
  226.       mins.y = bbox->Lower_Left.y;
  227.     if (bbox->Lower_Left.y + bbox->Lengths.y > maxs.y)
  228.       maxs.y = bbox->Lower_Left.y;
  229.     if (bbox->Lower_Left.z < mins.z)
  230.       mins.z = bbox->Lower_Left.z;
  231.     if (bbox->Lower_Left.z + bbox->Lengths.z > maxs.z)
  232.       maxs.z = bbox->Lower_Left.z;
  233.     }
  234.  
  235.   e = maxs.x - mins.x;
  236.   if (e > d) 
  237.     { 
  238.     d = e; which = 0; 
  239.   }
  240.   e = maxs.y - mins.y;
  241.   if (e > d) 
  242.     { 
  243.     d = e; which = 1; 
  244.   }
  245.   e = maxs.z - mins.z;
  246.   if (e > d) 
  247.     { 
  248.     d = e; which = 2; 
  249.   }
  250.  
  251.   return which;
  252.   }
  253.  
  254. static int
  255. SortAndSplit(Root, Prims, nPrims, first, last)
  256. OBJECT **Root;
  257. OBJECT **Prims;
  258. unsigned long *nPrims;
  259. unsigned long first;
  260. unsigned long last;
  261.   {
  262.   COMPOSITE *cd;
  263.   unsigned long size, i, j, m;
  264.   DBL dmin, dmax, tmin, tmax;
  265.  
  266.   Axis = FindAxis(Prims, first, last);
  267.   size = last - first;
  268.  
  269.   /* Actually, we could do this faster in several ways. we could use a
  270.       logn algorithm to find the median along the given axis, and then a
  271.       linear algorithm to partition along the axis. Oh well. */
  272.  
  273.   qsort((char *) (Prims + first), (int)size, sizeof(OBJECT *), compslabs);
  274.  
  275.   if (size <= BUNCHING_FACTOR) 
  276.     {
  277.     cd = Create_Composite();
  278.     cd->Entries = (unsigned short)size;
  279.  
  280.     for (i=0;i<size;i++) 
  281.       {
  282.       cd->Objects[i] = Prims[first+i];
  283.       /*
  284. printf("Extent of object %ld/%d: <%g, %g, %g> -> <%g, %g, %g>\n",
  285.        first+i, cd->Objects[i]->Type,
  286.        cd->Objects[i]->Bounds.Lower_Left.x,
  287.        cd->Objects[i]->Bounds.Lower_Left.y,
  288.        cd->Objects[i]->Bounds.Lower_Left.z,
  289.        cd->Objects[i]->Bounds.Lower_Left.x + cd->Objects[i]->Bounds.Lengths.x,
  290.        cd->Objects[i]->Bounds.Lower_Left.y + cd->Objects[i]->Bounds.Lengths.y,
  291.        cd->Objects[i]->Bounds.Lower_Left.z + cd->Objects[i]->Bounds.Lengths.z);
  292. */
  293.       }
  294.  
  295.     /* Check bounds in each direction */
  296.     /* First along the x axis */
  297.     dmin = BOUND_HUGE; dmax = -BOUND_HUGE;
  298.     for (j=0;j<size;j++) 
  299.       {
  300.       tmin = cd->Objects[j]->Bounds.Lower_Left.x;
  301.       tmax = tmin + cd->Objects[j]->Bounds.Lengths.x;
  302.       if (tmin < dmin) dmin = tmin;
  303.       if (tmax > dmax) dmax = tmax;
  304.       }
  305.     cd->Bounds.Lower_Left.x = dmin;
  306.     cd->Bounds.Lengths.x = dmax - dmin;
  307.  
  308.     /* Now along the y axis */
  309.     dmin = BOUND_HUGE; dmax = -BOUND_HUGE;
  310.     for (j=0;j<size;j++) 
  311.       {
  312.       tmin = cd->Objects[j]->Bounds.Lower_Left.y;
  313.       tmax = tmin + cd->Objects[j]->Bounds.Lengths.y;
  314.       if (tmin < dmin) dmin = tmin;
  315.       if (tmax > dmax) dmax = tmax;
  316.       }
  317.     cd->Bounds.Lower_Left.y = dmin;
  318.     cd->Bounds.Lengths.y = dmax - dmin;
  319.  
  320.     /* Lastly along the z axis */
  321.     dmin = BOUND_HUGE; dmax = -BOUND_HUGE;
  322.     for (j=0;j<size;j++) 
  323.       {
  324.       tmin = cd->Objects[j]->Bounds.Lower_Left.z;
  325.       tmax = tmin + cd->Objects[j]->Bounds.Lengths.z;
  326.       if (tmin < dmin) dmin = tmin;
  327.       if (tmax > dmax) dmax = tmax;
  328.       }
  329.     cd->Bounds.Lower_Left.z = dmin;
  330.     cd->Bounds.Lengths.z = dmax - dmin;
  331.  
  332.     *Root = (OBJECT *)cd;
  333.     if (*nPrims <= maxprimcount) 
  334.       {
  335.       Prims[*nPrims] = (OBJECT *)cd;
  336.       *nPrims += 1;
  337.       return 1;
  338.       }
  339.     else
  340.       Error("Too many primitives\n");
  341.     }
  342.   else 
  343.     {
  344.     m = (first + last) / 2;
  345.     SortAndSplit(Root, Prims, nPrims, first, m);
  346.     SortAndSplit(Root, Prims, nPrims, m , last);
  347.     return 0;
  348.     }
  349.   return -1;
  350.   }
  351.  
  352.   void
  353.   BuildBoundingSlabs(Root)
  354.     OBJECT **Root;
  355.   {
  356.   OBJECT **Prims, **prim, *head;
  357.   unsigned long nPrims;
  358.   unsigned long low, high;
  359.  
  360.   /* We have to start by counting how many frame level object there are */
  361.   head   = Frame.Objects;
  362.   nPrims = 0;
  363.   while (head != NULL) 
  364.     {
  365.     nPrims++;
  366.     head = head->Sibling;
  367.     }
  368.  
  369.   /* The total # of prims inflates around 150% when bounding objects are
  370.       generated.  If the 1.8 below proves to be too small, use 2.0.  The
  371.       inflation is never 200%. */
  372.   maxprimcount = (unsigned long)(1.8 * (nPrims + 1));
  373.  
  374.   /* Now allocate an array to hold references to these prims & any new
  375.      composite objects we may generate */
  376.   Prims = (OBJECT **)malloc((unsigned)maxprimcount * sizeof(OBJECT *));
  377.   if (Prims == NULL)
  378.     Error("Failed to allocate bounding slab reference information\n");
  379.  
  380.   /* Copy pointers to the objects into the array */
  381.   prim = Prims;
  382.   for (head=Frame.Objects;head!=NULL;head=head->Sibling) 
  383.     {
  384.     if (head->Type & LIGHT_SOURCE_OBJECT) 
  385.       {
  386.       /* Only bother with lights if they have an attached shape */
  387.       if (((LIGHT_SOURCE *)head)->Children != NULL)
  388.         *prim++ = ((LIGHT_SOURCE *)head)->Children;
  389.       else
  390.         nPrims--;
  391.       }
  392.     else
  393.       /* Normal sort of object - add it to the list */
  394.       *prim++ = head;
  395.     }
  396.  
  397.   /* Now do a sort on the objects, with the end result being a tree of
  398.       objects sorted along the x, y, and z axes */
  399.   low  = 0;
  400.   high = nPrims;
  401.   while (SortAndSplit(Root, Prims, &nPrims, low, high) == 0) 
  402.     {
  403.     low  = high;
  404.     high = nPrims;
  405.     }
  406.  
  407.   Use_Slabs = (nPrims >= Bounds_Threshold);
  408.  
  409.   /* Test */
  410.   /*
  411. printf("Extent of scene: <%g, %g, %g> -> <%g, %g, %g>\n",
  412.        (*Root)->Bounds.Lower_Left.x,
  413.        (*Root)->Bounds.Lower_Left.y,
  414.        (*Root)->Bounds.Lower_Left.z,
  415.        (*Root)->Bounds.Lower_Left.x + (*Root)->Bounds.Lengths.x,
  416.        (*Root)->Bounds.Lower_Left.y + (*Root)->Bounds.Lengths.y,
  417.        (*Root)->Bounds.Lower_Left.z + (*Root)->Bounds.Lengths.z);
  418. */
  419.  
  420.   /* Now we can get rid of the Prim array, and just use Root */
  421.   free(Prims);
  422.   }
  423.  
  424. static void
  425. PriorityQueueInsert(Queue, Qsize, key, obj)
  426. Qelem *Queue;
  427. unsigned *Qsize;
  428. DBL key;
  429. OBJECT *obj;
  430.   {
  431.   unsigned size;
  432.   int i;
  433.   Qelem tmp;
  434.  
  435.   totalQueues++;
  436.   (*Qsize)++;
  437.   size = *Qsize;
  438.   /* if (size > maxQueueSize) maxQueueSize = size; */
  439.   if (size >= MAXQUEUE)
  440.     Error("Priority queue overflow");
  441.   Queue[size].q_key = key;
  442.   Queue[size].q_obj = obj;
  443.  
  444.   i = size;
  445.   while (i > 1 && Queue[i].q_key < Queue[i/2].q_key) 
  446.     {
  447.     tmp = Queue[i];
  448.     Queue[i] = Queue[i/2];
  449.     Queue[i/2] = tmp;
  450.     i = i / 2;
  451.     }
  452.   }
  453.  
  454. static void
  455. PriorityQueueDelete(Queue, Qsize, key, obj)
  456. Qelem *Queue;
  457. unsigned *Qsize;
  458. DBL *key;
  459. OBJECT **obj;
  460.   {
  461.   Qelem tmp;
  462.   int i, j;
  463.   unsigned size;
  464.  
  465.   if (*Qsize == 0)
  466.     Error("priority queue is empty");
  467.  
  468.   *key = Queue[1].q_key;
  469.   *obj = Queue[1].q_obj;
  470.   Queue[1] = Queue[*Qsize];
  471.   (*Qsize)--;
  472.   size = *Qsize;
  473.  
  474.   i = 1 ;
  475.  
  476.   while (2 * i <= (int)size) 
  477.     {
  478.     if (2 * i == (int)size)
  479.       j = 2 * i;
  480.     else if (Queue[2*i].q_key < Queue[2*i+1].q_key)
  481.       j = 2 * i;
  482.     else
  483.       j = 2 * i + 1;
  484.  
  485.     if (Queue[i].q_key > Queue[j].q_key) 
  486.       {
  487.       tmp = Queue[i];
  488.       Queue[i] = Queue[j];
  489.       Queue[j] = tmp;
  490.       i = j;
  491.       }
  492.     else
  493.       break;
  494.     }
  495.   }
  496.  
  497. static void
  498. CheckAndEnqueue(Queue, Qsize, obj, rayinfo)
  499. Qelem *Queue;
  500. unsigned *Qsize;
  501. OBJECT *obj;
  502. RAYINFO *rayinfo;
  503.   {
  504.   DBL tmin, tmax;
  505.   DBL dmin, dmax ;
  506.  
  507.   nChecked++;
  508.  
  509.   if (rayinfo->nonzero.x ) 
  510.     {
  511.     if (rayinfo->positive.x ) 
  512.       {
  513.       dmin = (obj->Bounds.Lower_Left.x - rayinfo->slab_num.x) *
  514.       rayinfo->slab_den.x;
  515.       dmax = dmin + (obj->Bounds.Lengths.x  * rayinfo->slab_den.x);
  516.       if ( dmax < EPSILON ) return ;
  517.       } 
  518.     else 
  519.       {
  520.       dmax = (obj->Bounds.Lower_Left.x - rayinfo->slab_num.x) *
  521.         rayinfo->slab_den.x;
  522.       if ( dmax < EPSILON ) return ;
  523.       dmin = dmax + (obj->Bounds.Lengths.x  * rayinfo->slab_den.x);
  524.       }
  525.     if ( dmin > dmax ) return ;
  526.     }
  527.   else 
  528.     {
  529.     if ( ( rayinfo->slab_num.x < obj->Bounds.Lower_Left.x ) ||
  530.       ( rayinfo->slab_num.x >
  531.       obj->Bounds.Lengths.x + obj->Bounds.Lower_Left.x ) ) return ;
  532.     dmin = -BOUND_HUGE; dmax = BOUND_HUGE;
  533.     }
  534.  
  535.   if (rayinfo->nonzero.y ) 
  536.     {
  537.     if (rayinfo->positive.y ) 
  538.       {
  539.       tmin = (obj->Bounds.Lower_Left.y - rayinfo->slab_num.y) *
  540.       rayinfo->slab_den.y;
  541.       tmax = tmin + (obj->Bounds.Lengths.y  * rayinfo->slab_den.y);
  542.       } 
  543.     else 
  544.       {
  545.       tmax = (obj->Bounds.Lower_Left.y - rayinfo->slab_num.y) *
  546.         rayinfo->slab_den.y;
  547.       tmin = tmax + (obj->Bounds.Lengths.y  * rayinfo->slab_den.y);
  548.       }
  549.     /* unwrap the logic - do the dmin and dmax checks only when tmin and
  550.       tmax actually affect anything, also try to escape ASAP.  Better
  551.       yet, fold the logic below into the two branches above so as to
  552.       compute only what is needed.
  553.     */
  554.     /* you might even try tmax < EPSILON first (instead of second) for an
  555.           early quick out
  556.     */
  557.     if ( tmax < dmax ) 
  558.       {
  559.       if ( tmax < EPSILON ) return;
  560.       /* check bounds only if tmax changes dmax */
  561.       if ( tmin > dmin ) 
  562.         {
  563.         if ( tmin > tmax ) return ;
  564.         /* do this last in case it's not needed! */
  565.         dmin = tmin ;
  566.         } 
  567.       else 
  568.         {
  569.         if ( dmin > tmax ) return ;
  570.         }
  571.       /* do this last in case it's not needed! */
  572.       dmax = tmax ;
  573.       } 
  574.     else 
  575.       {
  576.       if ( tmin > dmin ) 
  577.         {
  578.         if ( tmin > dmax ) return ;
  579.         /* do this last in case it's not needed! */
  580.         dmin = tmin ;
  581.         } /* else nothing needs to happen, since dmin and dmax did not
  582.            change! */
  583.              
  584.       }
  585.     } 
  586.   else 
  587.     {
  588.     if (rayinfo->slab_num.y < obj->Bounds.Lower_Left.y ||
  589.       rayinfo->slab_num.y >
  590.       obj->Bounds.Lengths.y + obj->Bounds.Lower_Left.y ) return ;
  591.     }
  592.  
  593.   if (rayinfo->nonzero.z ) 
  594.     {
  595.     if (rayinfo->positive.z ) 
  596.       {
  597.       tmin = (obj->Bounds.Lower_Left.z - rayinfo->slab_num.z) *
  598.       rayinfo->slab_den.z;
  599.       tmax = tmin + (obj->Bounds.Lengths.z  * rayinfo->slab_den.z);
  600.       } 
  601.     else 
  602.       {
  603.       tmax = (obj->Bounds.Lower_Left.z - rayinfo->slab_num.z) *
  604.         rayinfo->slab_den.z;
  605.       tmin = tmax + (obj->Bounds.Lengths.z  * rayinfo->slab_den.z);
  606.       }
  607.     if ( tmax < dmax ) 
  608.       {
  609.       if ( tmax < EPSILON ) return;
  610.       /* check bounds only if tmax changes dmax */
  611.       if ( tmin > dmin ) 
  612.         {
  613.         if ( tmin > tmax ) return ;
  614.         /* do this last in case it's not needed! */
  615.         dmin = tmin ;
  616.         } 
  617.       else 
  618.         {
  619.         if ( dmin > tmax ) return ;
  620.         }
  621.       /* do this last in case it's not needed! */
  622.       dmax = tmax ;
  623.       } 
  624.     else 
  625.       {
  626.       if ( tmin > dmin ) 
  627.         {
  628.         if ( tmin > dmax ) return ;
  629.         /* do this last in case it's not needed! */
  630.         dmin = tmin ;
  631.         } /* else nothing needs to happen, since dmin and dmax did not
  632.            change! */
  633.              
  634.       }
  635.     } 
  636.   else 
  637.     {
  638.     if (rayinfo->slab_num.z < obj->Bounds.Lower_Left.z ||
  639.       rayinfo->slab_num.z >
  640.       obj->Bounds.Lengths.z + obj->Bounds.Lower_Left.z ) return ;
  641.     }
  642.  
  643.   PriorityQueueInsert(Queue, Qsize, dmin, obj);
  644.   nEnqueued++;
  645.   }
  646.  
  647. int
  648. Bounds_Intersect(Root, ray, Best_Intersection, Best_Object)
  649. OBJECT *Root;
  650. RAY *ray;
  651. INTERSECTION *Best_Intersection;
  652. OBJECT **Best_Object;
  653.   {
  654.   Qelem *Queue;
  655.   unsigned Qsize = 0;
  656.   int i;
  657.   OBJECT *cobj;
  658.   COMPOSITE *cdp;
  659.   RAYINFO rayinfo;
  660.   DBL t, key;
  661.   INTERSECTION New_Intersection;
  662.   int Intersection_Found = 0;
  663.  
  664.   Queue = (Qelem *)malloc(MAXQUEUE * sizeof(Qelem));
  665.   if (Queue == NULL)
  666.     Error("Failed to allocate priority queue\n");
  667.  
  668.   /* Create the direction vectors for this ray */
  669.   rayinfo.slab_num.x = ray->Initial.x;
  670.   rayinfo.slab_num.y = ray->Initial.y;
  671.   rayinfo.slab_num.z = ray->Initial.z;
  672.   if ( rayinfo.nonzero.x = ((t = ray->Direction.x) != 0.0) ) 
  673.     {
  674.     rayinfo.slab_den.x = 1.0 / t;
  675.     rayinfo.positive.x = ( ray->Direction.x > 0.0 ) ;
  676.     }
  677.   if ( rayinfo.nonzero.y = ((t = ray->Direction.y) != 0.0) ) 
  678.     {
  679.     rayinfo.slab_den.y = 1.0 / t;
  680.     rayinfo.positive.y = ( ray->Direction.y > 0.0 ) ;
  681.     }
  682.   if ( rayinfo.nonzero.z = ((t = ray->Direction.z) != 0.0) ) 
  683.     {
  684.     rayinfo.slab_den.z = 1.0 / t;
  685.     rayinfo.positive.z = ( ray->Direction.z > 0.0 ) ;
  686.     }
  687.  
  688.   /* start with an empty priority queue */
  689.   Qsize = 0;
  690.   totalQueueResets++;
  691.  
  692.   CheckAndEnqueue(Queue, &Qsize, Root, &rayinfo );
  693.  
  694.   for (;;)
  695.     {
  696.     if (Qsize == 0)
  697.       break;
  698.  
  699.     PriorityQueueDelete(Queue, &Qsize, &key, &cobj);
  700.  
  701.     if (key > Best_Intersection->Depth)
  702.       break;
  703.     else
  704.       if (cobj->Type & BOUNDING_OBJECT)
  705.         {
  706.         cdp = (COMPOSITE *)cobj;
  707.         for (i=0;(unsigned short)i < cdp->Entries;i++)
  708.           CheckAndEnqueue(Queue, &Qsize, cdp->Objects[i],
  709.             &rayinfo) ;
  710.         }
  711.       else
  712.         {
  713.         if (Intersection(&New_Intersection, cobj, ray))
  714.           if (New_Intersection.Depth < Best_Intersection->Depth)
  715.             {
  716.             *Best_Intersection = New_Intersection;
  717.             *Best_Object = cobj;
  718.             Intersection_Found = TRUE;
  719.             }
  720.         }
  721.     }
  722.  
  723.   free(Queue);
  724.   return Intersection_Found;
  725.   }
  726.