home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / oct93 / graphics / graphtal.lha / Graphtal / transform.C < prev    next >
C/C++ Source or Header  |  1992-11-19  |  2KB  |  86 lines

  1. /*
  2.  * transform.C
  3.  *
  4.  * Copyright (C) 1992, Christoph Streit (streit@iam.unibe.ch)
  5.  *                     University of Berne, Switzerland
  6.  * Copyright (C) 1989, 1991, Craig E. Kolb
  7.  * All rights reserved.
  8.  *
  9.  * This software may be freely copied, modified, and redistributed
  10.  * provided that this copyright notice is preserved on all copies.
  11.  *
  12.  * You may not distribute this software, in whole or in part, as part of
  13.  * any commercial product without the express consent of the authors.
  14.  *
  15.  * There is no warranty or other guarantee of fitness of this software
  16.  * for any purpose.  It is provided solely "as is".
  17.  *
  18.  */
  19.  
  20. #include "transform.h"
  21.  
  22. /*
  23.  * Transform normal with the transpose of the given transformation
  24.  * matrix, not regarding the translations. The normalized Vector is
  25.  * returned.
  26.  */
  27. Vector normalTransform(const Vector& normal, const TransMatrix& tmat)
  28. {
  29.   Vector n = 
  30.     Vector(normal[0]*tmat(0,0) + normal[1]*tmat(0,1) + normal[2]*tmat(0,2),
  31.        normal[0]*tmat(1,0) + normal[1]*tmat(1,1) + normal[2]*tmat(1,2),
  32.        normal[0]*tmat(2,0) + normal[1]*tmat(2,1) + normal[2]*tmat(2,2));
  33.   return n.normalized();
  34. }
  35.  
  36. /*
  37.  * Transform vector with the given transformation matrix, 
  38.  * not regarding the translations.
  39.  */
  40. Vector vectorTransform(const Vector& v, const TransMatrix& tmat)
  41. {
  42.   return Vector(v[0]*tmat(0,0) + v[1]*tmat(1,0) + v[2]*tmat(2,0),
  43.         v[0]*tmat(0,1) + v[1]*tmat(1,1) + v[2]*tmat(2,1),
  44.         v[0]*tmat(0,2) + v[1]*tmat(1,2) + v[2]*tmat(2,2));
  45. }
  46.  
  47. Vector pointTransform(const Vector& p, const TransMatrix& tmat)
  48. {
  49.   return p*tmat;
  50. }
  51.  
  52. /*
  53.  * This code is adapted from Craig Kolbs rayshade.
  54.  *
  55.  * Return transformation information to map the "coordinate system"
  56.  * with the given origin, "up" vector, radius, and up axis lengths to
  57.  * one in which the "up" vector is the Z axis and the x/y/up axes
  58.  * have unit length.  This is useful for transforming a general
  59.  * form of a primitive into a canonical, Z-axis aligned, unit size
  60.  * primitive, facilitating intersection testing.
  61.  * Assumes that "up" is normalized.
  62.  */
  63.  
  64. TransMatrix coordSystemTransform(const Vector& orig, Vector up, real r, real len)
  65. {
  66.   TransMatrix result;
  67.   Vector rotvec;
  68.  
  69.   result.scale(r, r, len);
  70.  
  71.   if (1 - fabs(up[2]) < EPSILON) {
  72.     rotvec[0] = 1;
  73.     rotvec[1] = rotvec[2] = 0;
  74.   }
  75.   else {
  76.     rotvec[0] = up[1];
  77.     rotvec[1] = -up[0];
  78.     rotvec[2] = 0;
  79.   }
  80.   
  81.   result.rotate(rotvec, -acos(up[2]));
  82.   result.translate(orig);
  83.  
  84.   return result;
  85. }
  86.