home *** CD-ROM | disk | FTP | other *** search
/ Photo CD Demo 1 / Demo.bin / gems / graphics / boxspher.c < prev    next >
C/C++ Source or Header  |  1992-04-09  |  4KB  |  107 lines

  1. /*
  2. A Simple Method for Box-Sphere Intersection Testing
  3. by Jim Arvo
  4. from "Graphics Gems", Academic Press, 1990
  5. */
  6.  
  7. #include "GraphicsGems.h"
  8.  
  9. /*
  10.  *  This routine tests for intersection between an n-dimensional             
  11.  *  axis-aligned box and an n-dimensional sphere.  The mode argument       
  12.  *  indicates whether the objects are to be regarded as surfaces or        
  13.  *  solids.  The values are:                                               
  14.  *                                                                         
  15.  *     mode                                                                
  16.  *                                                                         
  17.  *       0   Hollow Box, Hollow Sphere                                     
  18.  *       1   Hollow Box, Solid  Sphere
  19.  *       2   Solid  Box, Hollow Sphere                                     
  20.  *       3   Solid  Box, Solid  Sphere                                     
  21.  *                                                                         
  22. */
  23. int Box_Sphere_Intersect( n, Bmin, Bmax, C, r, mode )
  24. int    n;       /* The dimension of the space.           */
  25. float  Bmin[];  /* The minium of the box for each axis.  */
  26. float  Bmax[];  /* The maximum of the box for each axis. */
  27. float  C[];     /* The sphere center in n-space.         */
  28. float  r;       /* The radius of the sphere.             */
  29. int    mode;    /* Selects hollow or solid.              */
  30.     {
  31.     float  a, b;
  32.     float  dmin, dmax;
  33.     float  r2 = SQR( r );
  34.     int    i, face;
  35.  
  36.  
  37.     switch( mode )
  38.         {
  39.         case 0: /* Hollow Box - Hollow Sphere */
  40.             dmin = 0;
  41.             dmax = 0;
  42.             face = FALSE;
  43.             for( i = 0; i < n; i++ ) {
  44.                 a = SQR( C[i] - Bmin[i] );
  45.                 b = SQR( C[i] - Bmax[i] );
  46.                 dmax += MAX( a, b );
  47.                 if( C[i] < Bmin[i] ) {
  48.                     face = TRUE;
  49.                     dmin += a;
  50.                     }
  51.                 else if( C[i] > Bmax[i] ) {
  52.                     face = TRUE;
  53.                     dmin += b;
  54.                     }
  55.                 else if( MIN( a, b ) <= r2 ) face = TRUE;
  56.                 }
  57.             if(face && ( dmin <= r2 ) && ( r2 <= dmax)) return(TRUE);
  58.             break;
  59.  
  60.         case 1: /* Hollow Box - Solid Sphere */
  61.             dmin = 0;
  62.             face = FALSE;
  63.             for( i = 0; i < n; i++ ) {
  64.                 if( C[i] < Bmin[i] ) {
  65.                     face = TRUE;
  66.                     dmin += SQR( C[i] - Bmin[i] );
  67.                     }
  68.                 else if( C[i] > Bmax[i] ) {
  69.                     face = TRUE;
  70.                     dmin += SQR( C[i] - Bmax[i] );     
  71.                     }
  72.                 else if( C[i] - Bmin[i] <= r ) face = TRUE;
  73.                 else if( Bmax[i] - C[i] <= r ) face = TRUE;
  74.                 }
  75.             if( face && ( dmin <= r2 ) ) return( TRUE );
  76.             break;
  77.  
  78.  
  79.         case 2: /* Solid Box - Hollow Sphere */
  80.             dmax = 0;
  81.             dmin = 0;
  82.             for( i = 0; i < n; i++ ) {
  83.                 a = SQR( C[i] - Bmin[i] );
  84.                 b = SQR( C[i] - Bmax[i] );
  85.                 dmax += MAX( a, b );
  86.                 if( C[i] < Bmin[i] ) dmin += a; else
  87.                 if( C[i] > Bmax[i] ) dmin += b;
  88.                 }
  89.             if( dmin <= r2 && r2 <= dmax ) return( TRUE );
  90.             break;
  91.  
  92.         case 3: /* Solid Box - Solid Sphere */
  93.             dmin = 0;
  94.             for( i = 0; i < n; i++ ) {
  95.                 if( C[i] < Bmin[i] ) dmin += SQR(C[i] - Bmin[i] ); else
  96.                 if( C[i] > Bmax[i] ) dmin += SQR( C[i] - Bmax[i] );     
  97.                 }
  98.             if( dmin <= r2 ) return( TRUE );
  99.             break;
  100.   
  101.         } /* end switch */
  102.  
  103.     return( FALSE );
  104.     } 
  105.  
  106.  
  107.