home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / developm / source / macraysh.sit / Code / Source / rotate.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-11-04  |  2.4 KB  |  95 lines

  1. /*
  2.  * rotate.c
  3.  *
  4.  * Copyright (C) 1989, 1991, Craig E. Kolb
  5.  * All rights reserved.
  6.  *
  7.  * This software may be freely copied, modified, and redistributed
  8.  * provided that this copyright notice is preserved on all copies.
  9.  *
  10.  * You may not distribute this software, in whole or in part, as part of
  11.  * any commercial product without the express consent of the authors.
  12.  * 
  13.  * There is no warranty or other guarantee of fitness of this software
  14.  * for any purpose.  It is provided solely "as is".
  15.  *
  16.  * $Id: rotate.c,v 4.0 91/07/17 14:31:18 kolb Exp Locker: kolb $
  17.  *
  18.  * $Log:    rotate.c,v $
  19.  * Revision 4.0  91/07/17  14:31:18  kolb
  20.  * Initial version.
  21.  * 
  22.  */
  23. #include "common.h"
  24. #include "rotate.h"
  25.  
  26. TransMethods *iRotateMethods;
  27. void RotationMatrix();
  28.  
  29. Rotate *
  30. RotateCreate()
  31. {
  32.     Rotate *res;
  33.  
  34.     res = (Rotate *)Malloc(sizeof(Rotate));
  35.     res->x = res->y = res->theta = 0.;
  36.     res->z = 1.;
  37.     return res;
  38. }
  39.  
  40. TransMethods *
  41. RotateMethods()
  42. {
  43.     if (iRotateMethods == (TransMethods *)NULL) {
  44.         iRotateMethods = (TransMethods *)Malloc(sizeof(TransMethods));
  45.         iRotateMethods->create = (TransCreateFunc *)RotateCreate;
  46.         iRotateMethods->propagate = RotatePropagate;
  47.     }
  48.     return iRotateMethods;    
  49. }
  50.  
  51. void
  52. RotatePropagate(rotate, trans, itrans)
  53. Rotate *rotate;
  54. RSMatrix *trans, *itrans;
  55. {
  56.     Vector axis;
  57.  
  58.     RotationMatrix(rotate->x, rotate->y, rotate->z, deg2rad(rotate->theta), trans);
  59.     /*
  60.      * Build the inverse...
  61.      */
  62.     MatrixInvert(trans, itrans);
  63. }
  64.  
  65. void
  66. RotationMatrix(x, y, z, theta, trans)
  67. Float x, y, z, theta;
  68. RSMatrix *trans;
  69. {
  70.     Float n1, n2, n3, sintheta, costheta;
  71.     Vector vector;
  72.  
  73.     MatrixInit(trans);
  74.     vector.x = x;
  75.     vector.y = y;
  76.     vector.z = z;
  77.  
  78.     if (VecNormalize(&vector) == 0.)
  79.         RLerror(RL_WARN, "Degenerate rotation axis.\n",0,0,0);
  80.  
  81.     sintheta = sin(theta);
  82.     costheta = cos(theta);
  83.  
  84.     n1 = vector.x; n2 = vector.y; n3 = vector.z;
  85.     trans->matrix[0][0] = (Float)(n1*n1 + (1. - n1*n1)*costheta);
  86.     trans->matrix[0][1] = (Float)(n1*n2*(1 - costheta) + n3*sintheta);
  87.     trans->matrix[0][2] = (Float)(n1*n3*(1 - costheta) - n2*sintheta);
  88.     trans->matrix[1][0] = (Float)(n1*n2*(1 - costheta) - n3*sintheta);
  89.     trans->matrix[1][1] = (Float)(n2*n2 + (1 - n2*n2)*costheta);
  90.     trans->matrix[1][2] = (Float)(n2*n3*(1 - costheta) + n1*sintheta);
  91.     trans->matrix[2][0] = (Float)(n1*n3*(1 - costheta) + n2*sintheta);
  92.     trans->matrix[2][1] = (Float)(n2*n3*(1 - costheta) - n1*sintheta);
  93.     trans->matrix[2][2] = (Float)(n3*n3 + (1 - n3*n3)*costheta);
  94. }
  95.