home *** CD-ROM | disk | FTP | other *** search
/ Photo CD Demo 1 / Demo.bin / graphtal / transfrm.c < prev    next >
C/C++ Source or Header  |  1992-10-19  |  2KB  |  84 lines

  1. /*
  2.  * transform.C
  3.  *
  4.  * Copyright (C) 1992, Christoph Streit (streit@iam.unibe.ch)
  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.  */
  17.  
  18. #include "transform.h"
  19.  
  20. /*
  21.  * Transform normal with the transpose of the given transformation
  22.  * matrix, not regarding the translations. The normalized Vector is
  23.  * returned.
  24.  */
  25. Vector normalTransform(const Vector& normal, const TransMatrix& tmat)
  26. {
  27.   Vector n = 
  28.     Vector(normal[0]*tmat(0,0) + normal[1]*tmat(0,1) + normal[2]*tmat(0,2),
  29.        normal[0]*tmat(1,0) + normal[1]*tmat(1,1) + normal[2]*tmat(1,2),
  30.        normal[0]*tmat(2,0) + normal[1]*tmat(2,1) + normal[2]*tmat(2,2));
  31.   return n.normalized();
  32. }
  33.  
  34. /*
  35.  * Transform vector with the given transformation matrix, 
  36.  * not regarding the translations.
  37.  */
  38. Vector vectorTransform(const Vector& v, const TransMatrix& tmat)
  39. {
  40.   return Vector(v[0]*tmat(0,0) + v[1]*tmat(1,0) + v[2]*tmat(2,0),
  41.         v[0]*tmat(0,1) + v[1]*tmat(1,1) + v[2]*tmat(2,1),
  42.         v[0]*tmat(0,2) + v[1]*tmat(1,2) + v[2]*tmat(2,2));
  43. }
  44.  
  45. Vector pointTransform(const Vector& p, const TransMatrix& tmat)
  46. {
  47.   return p*tmat;
  48. }
  49.  
  50. /*
  51.  * This code is adapted from Craig Kolbs rayshade.
  52.  *
  53.  * Return transformation information to map the "coordinate system"
  54.  * with the given origin, "up" vector, radius, and up axis lengths to
  55.  * one in which the "up" vector is the Z axis and the x/y/up axes
  56.  * have unit length.  This is useful for transforming a general
  57.  * form of a primitive into a canonical, Z-axis aligned, unit size
  58.  * primitive, facilitating intersection testing.
  59.  * Assumes that "up" is normalized.
  60.  */
  61.  
  62. TransMatrix coordSystemTransform(const Vector& orig, Vector up, real r, real len)
  63. {
  64.   TransMatrix result;
  65.   Vector rotvec;
  66.  
  67.   result.scale(r, r, len);
  68.  
  69.   if (1 - fabs(up[2]) < EPSILON) {
  70.     rotvec[0] = 1;
  71.     rotvec[1] = rotvec[2] = 0;
  72.   }
  73.   else {
  74.     rotvec[0] = up[1];
  75.     rotvec[1] = -up[0];
  76.     rotvec[2] = 0;
  77.   }
  78.   
  79.   result.rotate(rotvec, -acos(up[2]));
  80.   result.translate(orig);
  81.  
  82.   return result;
  83. }
  84.