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

  1. /*
  2. Transforming Axis-Aligned Bounding Boxes
  3. by Jim Arvo
  4. from "Graphics Gems", Academic Press, 1990
  5. */
  6.  
  7. #include "GraphicsGems.h"                                     
  8. /* Transforms a 3D axis-aligned box via a 3x3 matrix and a translation
  9.  * vector and returns an axis-aligned box enclosing the result. */ 
  10.  
  11. void Transform_Box( M, T, A, B )
  12. Matrix3  M;      /* Transform matrix.             */
  13. Vector3  T;      /* Translation matrix.           */
  14. Box3     A;      /* The original bounding box.    */
  15. Box3    *B;      /* The transformed bounding box. */
  16.     {
  17.     float  a, b;
  18.     float  Amin[3], Amax[3];
  19.     float  Bmin[3], Bmax[3];
  20.     int    i, j;
  21.  
  22.     /*Copy box A into a min array and a max array for easy reference.*/
  23.  
  24.     Amin[0] = A.min.x;  Amax[0] = A.max.x;
  25.     Amin[1] = A.min.y;  Amax[1] = A.max.y;
  26.     Amin[2] = A.min.z;  Amax[2] = A.max.z;
  27.  
  28.     /* Take care of translation by beginning at T. */
  29.  
  30.     Bmin[0] = Bmax[0] = T.x;
  31.     Bmin[1] = Bmax[1] = T.y;
  32.     Bmin[2] = Bmax[2] = T.z;
  33.  
  34.     /* Now find the extreme points by considering the product of the */
  35.     /* min and max with each component of M.  */
  36.                      
  37.     for( i = 0; i < 3; i++ )
  38.     for( j = 0; j < 3; j++ )
  39.         {
  40.         a = M.element[i][j] * Amin[j];
  41.         b = M.element[i][j] * Amax[j];
  42.         if( a < b ) 
  43.  
  44.             { 
  45.             Bmin[i] += a; 
  46.             Bmax[i] += b;
  47.             }
  48.         else 
  49.             { 
  50.             Bmin[i] += b; 
  51.             Bmax[i] += a;
  52.             }
  53.         }
  54.  
  55.     /* Copy the result into the new box. */
  56.  
  57.     B->min.x = Bmin[0];  B->max.x = Bmax[0];
  58.     B->min.y = Bmin[1];  B->max.y = Bmax[1];
  59.     B->min.z = Bmin[2];  B->max.z = Bmax[2];
  60.  
  61.     } 
  62.