home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Frozen Fish 1: Amiga
/
FrozenFish-Apr94.iso
/
bbs
/
oct93
/
graphics
/
graphtal.lha
/
Graphtal
/
transform.C
< prev
next >
Wrap
C/C++ Source or Header
|
1992-11-19
|
2KB
|
86 lines
/*
* transform.C
*
* Copyright (C) 1992, Christoph Streit (streit@iam.unibe.ch)
* University of Berne, Switzerland
* Copyright (C) 1989, 1991, Craig E. Kolb
* All rights reserved.
*
* This software may be freely copied, modified, and redistributed
* provided that this copyright notice is preserved on all copies.
*
* You may not distribute this software, in whole or in part, as part of
* any commercial product without the express consent of the authors.
*
* There is no warranty or other guarantee of fitness of this software
* for any purpose. It is provided solely "as is".
*
*/
#include "transform.h"
/*
* Transform normal with the transpose of the given transformation
* matrix, not regarding the translations. The normalized Vector is
* returned.
*/
Vector normalTransform(const Vector& normal, const TransMatrix& tmat)
{
Vector n =
Vector(normal[0]*tmat(0,0) + normal[1]*tmat(0,1) + normal[2]*tmat(0,2),
normal[0]*tmat(1,0) + normal[1]*tmat(1,1) + normal[2]*tmat(1,2),
normal[0]*tmat(2,0) + normal[1]*tmat(2,1) + normal[2]*tmat(2,2));
return n.normalized();
}
/*
* Transform vector with the given transformation matrix,
* not regarding the translations.
*/
Vector vectorTransform(const Vector& v, const TransMatrix& tmat)
{
return Vector(v[0]*tmat(0,0) + v[1]*tmat(1,0) + v[2]*tmat(2,0),
v[0]*tmat(0,1) + v[1]*tmat(1,1) + v[2]*tmat(2,1),
v[0]*tmat(0,2) + v[1]*tmat(1,2) + v[2]*tmat(2,2));
}
Vector pointTransform(const Vector& p, const TransMatrix& tmat)
{
return p*tmat;
}
/*
* This code is adapted from Craig Kolbs rayshade.
*
* Return transformation information to map the "coordinate system"
* with the given origin, "up" vector, radius, and up axis lengths to
* one in which the "up" vector is the Z axis and the x/y/up axes
* have unit length. This is useful for transforming a general
* form of a primitive into a canonical, Z-axis aligned, unit size
* primitive, facilitating intersection testing.
* Assumes that "up" is normalized.
*/
TransMatrix coordSystemTransform(const Vector& orig, Vector up, real r, real len)
{
TransMatrix result;
Vector rotvec;
result.scale(r, r, len);
if (1 - fabs(up[2]) < EPSILON) {
rotvec[0] = 1;
rotvec[1] = rotvec[2] = 0;
}
else {
rotvec[0] = up[1];
rotvec[1] = -up[0];
rotvec[2] = 0;
}
result.rotate(rotvec, -acos(up[2]));
result.translate(orig);
return result;
}