home *** CD-ROM | disk | FTP | other *** search
/ Stars of Shareware: Raytrace & Morphing / SOS-RAYTRACE.ISO / programm / rad386 / radiosit / src / preproce.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-21  |  7.3 KB  |  216 lines

  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <malloc.h>
  4.  
  5. #include "GraphicsGems.h"
  6. #include "data_structure.h"
  7. #include "objects.h"
  8. extern int verbose_flag;
  9. #define adjust_limits(o) {\
  10.         xlo=x2voxel(volume_grid,(o).bbox.min.x);\
  11.         ylo=y2voxel(volume_grid,(o).bbox.min.y);\
  12.         zlo=z2voxel(volume_grid,(o).bbox.min.z);\
  13.         xhi=x2voxel(volume_grid,(o).bbox.max.x);\
  14.         yhi=y2voxel(volume_grid,(o).bbox.max.y);\
  15.         zhi=z2voxel(volume_grid,(o).bbox.max.z);\
  16.         \
  17.         if (xlo >= volume_grid.x_subdivision) \
  18.             xlo = volume_grid.x_subdivision-1;\
  19.         if (ylo >= volume_grid.y_subdivision) \
  20.             ylo = volume_grid.y_subdivision-1;\
  21.         if (zlo >= volume_grid.z_subdivision) \
  22.             zlo = volume_grid.z_subdivision-1;\
  23.         if (xhi >= volume_grid.x_subdivision) \
  24.             xhi = volume_grid.x_subdivision-1;\
  25.         if (yhi >= volume_grid.y_subdivision) \
  26.             yhi = volume_grid.y_subdivision-1;\
  27.         if (zhi >= volume_grid.z_subdivision) \
  28.             zhi = volume_grid.z_subdivision-1;\
  29. }
  30. static void spacially_sub_divide()
  31. /* Compute the object list for each voxel. */
  32. {
  33.     int i;
  34.  
  35.     if (volume_grid.voxels==NULL)error("Null Voxel List.");
  36.  
  37.     for(i=0; i < number_objects; i++){
  38.         int x,y,z,xlo,ylo,zlo,xhi,yhi,zhi;
  39.         adjust_limits(object[i]);
  40.         for (x = xlo; x <= xhi; x++)
  41.            for (y = ylo; y <= yhi; y++)
  42.               for (z = zlo; z <= zhi; z++){
  43.                  Voxel *v=voxel_addr(volume_grid,x,y,z);
  44.                  if (v->nobjects==0)
  45.                     if ((v->intersecting_object_list=
  46.                      (int *)malloc(sizeof(int)*number_objects))
  47.                         ==NULL) error(
  48.                 "Cannot allocate Voxel object list");
  49.                  v->intersecting_object_list[v->nobjects]=i;
  50.                  (v->nobjects)++;
  51.               }
  52.     }
  53.  
  54. #if defined(DEBUG)
  55.     /* Check whether the object voxel classification is correct. */
  56.     /* 
  57.        Step I :
  58.         Check if the Bbox of the objects in the voxel list
  59.         really intersects the Voxel extent.
  60.     */
  61.     {
  62.     int bounds_overlap();
  63.     Voxel *vox=volume_grid.voxels;
  64.     Box3 b1;
  65.     int i,j,k,n,m;
  66.         for (i=0,b1.min.x=volume_grid.extent.min.x;
  67.         i < volume_grid.x_subdivision; i++, b1.min.x+=volume_grid.voxel_size.x)
  68.         for (j=0, b1.min.y = volume_grid.extent.min.y;
  69.         j<volume_grid.y_subdivision;j++,b1.min.y+=volume_grid.voxel_size.y)
  70.         for (k=0, b1.min.z= volume_grid.extent.min.z;
  71.         k<volume_grid.z_subdivision; k++,b1.min.z+=volume_grid.voxel_size.z,vox++){
  72.         V3Add(&(b1.min),&(volume_grid.voxel_size),&(b1.max));
  73.         for(n=0; n < vox->nobjects; n++)
  74.         if (bounds_overlap(&b1,&(object[vox->intersecting_object_list[n]].bbox))==0)
  75.             fprintf(stderr, "Step I Mismatch in Voxel[%d][%d][%d]\n", i,j,k);
  76.     }
  77.     /*
  78.        Step II :
  79.         Check for each object Bbox intersection with each Voxel.
  80.         For a valid intersection check if the Object belongs to
  81.         the Voxel list. If it does not NOTIFY.
  82.     */
  83.     for (n=0; n< number_objects;n++)
  84.         for (i=0,b1.min.x=volume_grid.extent.min.x,vox=volume_grid.voxels;
  85.         i < volume_grid.x_subdivision; i++,b1.min.x+=volume_grid.voxel_size.x)
  86.                 for (j=0, b1.min.y = volume_grid.extent.min.y;
  87.                 j<volume_grid.y_subdivision;j++,b1.min.y+=volume_grid.voxel_size.y)
  88.                 for (k=0, b1.min.z= volume_grid.extent.min.z;
  89.                 k<volume_grid.z_subdivision;k++,b1.min.z+=volume_grid.voxel_size.z,vox++){
  90.             V3Add(&(b1.min),&(volume_grid.voxel_size),&(b1.max));
  91.             /* Check if belongs to the current voxel */
  92.             if (bounds_overlap(&b1,&(object[n].bbox))){
  93.                 int exists=0;
  94.                 /* Then check if it is in the list or has been missed out.*/
  95.                 for(m=0; m < vox->nobjects; m++)
  96.                     if (n == vox->intersecting_object_list[m]){
  97.                         exists=1;
  98.                         break;
  99.                     }
  100.                 if (!exists)
  101.                     fprintf(stderr,
  102.                        "Step II Mismatch in object %d Voxel[%d][%d][%d].\n",
  103.                     n,i,j,k);
  104.             }
  105.         }
  106.     }
  107. #endif
  108. }
  109. #undef adjust_limits
  110. preprocess()
  111. /*
  112. 1. Initialising the grid data structure s.a.
  113.         area of the grid element.
  114.  
  115. 2. Parameterization and related work for all object surfaces.
  116.     For Quadrilateral :
  117.         1) Parameterize the quadrilateral from the given points.
  118.         2) Compute Plane_Normal and Plane_Constant. 
  119.         3) Compute a local co-ordinate system U,V,N and
  120.             Compute a 4 x 4 Matrix for transformation to the
  121.             local co-ordinate system.
  122.         4) Compute the Constants for inverse Mapping of a point 
  123.             on the quadrilateral to (u,v) parameter.
  124.     For Sphere :
  125.     For Other Objects :
  126.         .
  127.         .
  128.         .
  129.         .
  130. 3. Cover the whole scene with a bounding box with faces parallel to Major Planes.
  131.  
  132. 4. Divide the Bounding Box into predefined number of voxels (XDiv * YDiv * ZDiv)
  133.    and associate with each box a list of objects.
  134.  
  135. */
  136. {
  137.     int i,j,k;
  138.     Point3 min0;
  139.     Voxel *vox;
  140.  
  141.     /* Initialise the Bounding Box */
  142.     volume_grid.extent.min.x=
  143.         volume_grid.extent.min.y=
  144.             volume_grid.extent.min.z= LARGE;
  145.     volume_grid.extent.max.x=
  146.         volume_grid.extent.max.y=
  147.             volume_grid.extent.max.z= -LARGE;
  148.     for (i=0; i < number_objects; i++){
  149.         ofunc[object[i].surface_geometry_type].object_specific_preprocess(
  150.             object[i].grid,
  151.             object[i].grid_h_reso,object[i].grid_v_reso,
  152.             object[i].object_specific_structure,
  153.             &(object[i].bbox),
  154.             &(volume_grid.extent)
  155.         );
  156.         object[i].mail_box.ray_num= UNDEFINED;
  157.     }
  158.     /*
  159.         Now a tight scene volume bound is ready.
  160.         Loosen the volume extents a bit, to allow for the arithmatic errors.
  161.     */
  162.     
  163.     volume_grid.extent.min.x-=EPSILON;volume_grid.extent.max.x+=EPSILON;
  164.     volume_grid.extent.min.y-=EPSILON;volume_grid.extent.max.y+=EPSILON;
  165.     volume_grid.extent.min.z-=EPSILON;volume_grid.extent.max.z+=EPSILON;
  166.     
  167.  
  168.     volume_grid.voxel_size.x=(volume_grid.extent.max.x-volume_grid.extent.min.x)/
  169.             volume_grid.x_subdivision;
  170.     volume_grid.voxel_size.y=(volume_grid.extent.max.y-volume_grid.extent.min.y)/
  171.             volume_grid.y_subdivision;
  172.     volume_grid.voxel_size.z=(volume_grid.extent.max.z-volume_grid.extent.min.z)/
  173.             volume_grid.z_subdivision;
  174.     volume_grid.voxel_density.x=1.0/volume_grid.voxel_size.x;
  175.     volume_grid.voxel_density.y=1.0/volume_grid.voxel_size.y;
  176.     volume_grid.voxel_density.z=1.0/volume_grid.voxel_size.z;
  177.  
  178.     min0=volume_grid.extent.min;
  179.     for(i=0,vox=volume_grid.voxels;i<volume_grid.x_subdivision;
  180.         i++,min0.x+=volume_grid.voxel_size.x){
  181.         Point3 min1;
  182.         min1=min0;
  183.         for(j=0;j<volume_grid.y_subdivision;
  184.             j++,min1.y+=volume_grid.voxel_size.y){
  185.             Point3 min2;min2=min1;
  186.             for(k=0;k<volume_grid.z_subdivision;
  187.                 k++,vox++,min2.z+=volume_grid.voxel_size.z){
  188.                 vox->extent.min=min2;
  189.                 V3Add(&(vox->extent.min),
  190.                     &(volume_grid.voxel_size),
  191.                     &(vox->extent.max));
  192.             }
  193.         }
  194.     }
  195.  
  196.     spacially_sub_divide();
  197. #if defined (DEBUG)
  198.     {
  199.     int j,n=volume_grid.x_subdivision*volume_grid.y_subdivision*volume_grid.z_subdivision;
  200.     char *counter=(char *)malloc(number_objects);
  201.     if (counter==NULL) error("Cannot allocate counter.");
  202.     if(verbose_flag) fprintf(stderr,"Scene Bounding Box extents are <%g,%g,%g> to <%g,%g,%g>.\n",
  203.           volume_grid.extent.min.x,volume_grid.extent.min.y,volume_grid.extent.min.z,
  204.           volume_grid.extent.max.x,volume_grid.extent.max.y,volume_grid.extent.max.z);
  205.     
  206.     for(i=0; i<n;i++)
  207.         for(j=0;j<volume_grid.voxels[i].nobjects;j++)
  208.             counter[volume_grid.voxels[i].intersecting_object_list[j]]=1;
  209.     if(verbose_flag) fprintf(stderr,"Objects Not accounted for in Voxel list are : <");
  210.     for (i=0;i<number_objects;i++)
  211.         if(counter[i]==0) fprintf(stderr,"%d ",i);
  212.     if(verbose_flag) fprintf(stderr,">\n");
  213.     }
  214. #endif
  215. }
  216.