home *** CD-ROM | disk | FTP | other *** search
/ Photo CD Demo 1 / Demo.bin / gems / gemsii / rotate.c < prev    next >
C/C++ Source or Header  |  1991-09-22  |  3KB  |  87 lines

  1.  
  2. #include <math.h>
  3. #include "GraphicsGems.h"
  4.  
  5. /*=========================================================================*
  6.  *  Author: Jim Arvo                                                       *
  7.  *                                                                         *
  8.  *  This routine maps three values (x[0], x[1], x[2]) to a 3x3 rotation    *
  9.  *  matrix, M.  If x0, x1, and x2 are uniformly distributed random numbers *
  10.  *  in [0,1], then M will be a random rotation matrix.                     *
  11.  *                                                                         *
  12.  *  NOTE: This function will not produce UNIFORMLY distributed rotation    *
  13.  *  matrices as claimed in Gems II.  Watch for a better version in         *
  14.  *  Gems III.                                                              *
  15.  *                                                                         *
  16.  *=========================================================================*/
  17. void Rand_rotation( x, M )
  18. float x[];
  19. Matrix3 *M;
  20.     {
  21.     float  a, b, c, d, s;
  22.     float  z, r, theta, omega;       
  23.     float  bb, cc, dd;
  24.     float  ab, ac, ad;
  25.     float  bc, bd, cd;
  26.  
  27.     /* Use the random variables x[0] and x[1] to determine the axis of  */
  28.     /* rotation in cylindrical coordinates and the random variable x[2] */
  29.     /* to determine the amount of rotation, omega, about this axis.     */
  30.  
  31.     z = x[0];
  32.     r = sqrt( 1 - z * z );
  33.     theta = 2.0 * PI * x[1];
  34.     omega = PI * x[2];
  35.  
  36.     /* Compute the unit quaternion (a,b,c,d) where a is the cosine of    */
  37.     /* half the rotation angle and the axis vector (b,c,d) is determined */
  38.     /* by "r", "theta" and "z" computed above.                           */
  39.  
  40.     s = sin( omega );
  41.     a = cos( omega );
  42.     b = s * cos( theta ) * r;
  43.     c = s * sin( theta ) * r;
  44.     d = s * z;
  45.  
  46.     /* Compute all the pairwise products of a, b, c, and d, except a * a. */
  47.  
  48.     bb = b * b;   cc = c * c;   dd = d * d;
  49.     ab = a * b;   ac = a * c;   ad = a * d;
  50.     bc = b * c;   bd = b * d;   cd = c * d;
  51.  
  52.     /* Construct an orthogonal matrix corresponding to  */
  53.     /* the unit quaternion (a,b,c,d).                   */
  54.  
  55.     M->element[0][0] = 1.0 - 2.0 * ( cc + dd );
  56.     M->element[0][1] =       2.0 * ( bc + ad );
  57.     M->element[0][2] =       2.0 * ( bd - ac );
  58.  
  59.     M->element[1][0] =       2.0 * ( bc - ad );
  60.     M->element[1][1] = 1.0 - 2.0 * ( bb + dd );
  61.     M->element[1][2] =       2.0 * ( cd + ab );
  62.  
  63.     M->element[2][0] =       2.0 * ( bd + ac );
  64.     M->element[2][1] =       2.0 * ( cd - ab );
  65.     M->element[2][2] = 1.0 - 2.0 * ( bb + cc );
  66.  
  67.     } /* Rand_rotation */
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.