fart/util/Transform.cc
Josh Holtrop 63d56c5e43 renamed transformation operations in util/Transform for clarity
git-svn-id: svn://anubis/fart/trunk@32 7f9b0f55-74a9-4bce-be96-3c2cd072584d
2009-01-23 02:13:17 +00:00

74 lines
1.6 KiB
C++

#include "Transform.h"
#include <math.h>
Transform::Transform()
{
m_matrix = Matrix::identity();
}
void Transform::translate(double x, double y, double z)
{
Matrix t = Matrix::identity();
t[0][3] = x;
t[1][3] = y;
t[2][3] = z;
m_matrix *= t;
}
void Transform::rotate(double angle, double xv, double yv, double zv)
{
/* formula from http://en.wikipedia.org/wiki/Rotation_matrix */
Vector l(xv, yv, zv);
l.normalize();
double c = cos(angle);
double s = sin(angle);
double lx2 = l[0] * l[0];
double ly2 = l[1] * l[1];
double lz2 = l[2] * l[2];
Matrix t = Matrix::identity();
t[0][0] = lx2 + (1 - lx2) * c;
t[0][1] = l[0] * l[1] * (1 - c) - l[2] * s;
t[0][2] = l[0] * l[2] * (1 - c) + l[1] * s;
t[1][0] = l[0] * l[1] * (1 - c) + l[2] * s;
t[1][1] = ly2 + (1 - ly2) * c;
t[1][2] = l[1] * l[2] * (1 - c) - l[0] * s;
t[2][0] = l[0] * l[2] * (1 - c) - l[1] * s;
t[2][1] = l[1] * l[2] * (1 - c) + l[0] * s;
t[2][2] = lz2 + (1 - lz2) * c;
m_matrix *= t;
}
void Transform::scale(double xs, double ys, double zs)
{
Matrix t = Matrix::identity();
t[0][0] = xs;
t[1][1] = ys;
t[2][2] = zs;
m_matrix *= t;
}
Vector Transform::transform_point(Transform & t, const Vector & v)
{
return t.getMatrix() * v;
}
Vector Transform::transform_direction(Transform & t, const Vector & v)
{
return t.getMatrix() % v;
}
Ray Transform::transform_ray(Transform & t, const Ray & r)
{
Vector newPosition = t.getMatrix() * r.getOrigin();
Vector newDirection = t.getMatrix() % r.getDirection();
Ray res(newPosition, newDirection);
return res;
}