added transform_normal() to util/Transform

git-svn-id: svn://anubis/fart/trunk@34 7f9b0f55-74a9-4bce-be96-3c2cd072584d
This commit is contained in:
Josh Holtrop 2009-01-23 04:15:25 +00:00
parent af0afe1631
commit bdb648b84f
4 changed files with 9 additions and 2 deletions

View File

@ -64,6 +64,11 @@ Vector Transform::transform_direction(Transform & t, const Vector & v)
return t.getMatrix() % v; return t.getMatrix() % v;
} }
Vector Transform::transform_normal(Transform & t, const Vector & v)
{
return (t.getMatrix() % v).normalize();
}
Ray Transform::transform_ray(Transform & t, const Ray & r) Ray Transform::transform_ray(Transform & t, const Ray & r)
{ {
Vector newPosition = t.getMatrix() * r.getOrigin(); Vector newPosition = t.getMatrix() * r.getOrigin();

View File

@ -17,6 +17,7 @@ class Transform
Matrix & getMatrix() { return m_matrix; } Matrix & getMatrix() { return m_matrix; }
Vector transform_point(Transform & t, const Vector & v); Vector transform_point(Transform & t, const Vector & v);
Vector transform_direction(Transform & t, const Vector & v); Vector transform_direction(Transform & t, const Vector & v);
Vector transform_normal(Transform & t, const Vector & v);
Ray transform_ray(Transform & t, const Ray & r); Ray transform_ray(Transform & t, const Ray & r);
protected: protected:

View File

@ -17,7 +17,7 @@ Vector::Vector(double x, double y, double z)
m_array[2] = z; m_array[2] = z;
} }
void Vector::normalize() Vector & Vector::normalize()
{ {
double length = sqrt(m_array[0] * m_array[0] double length = sqrt(m_array[0] * m_array[0]
+ m_array[1] * m_array[1] + m_array[1] * m_array[1]
@ -25,6 +25,7 @@ void Vector::normalize()
m_array[0] /= length; m_array[0] /= length;
m_array[1] /= length; m_array[1] /= length;
m_array[2] /= length; m_array[2] /= length;
return *this;
} }
std::ostream & operator<<(std::ostream & out, const Vector & v) std::ostream & operator<<(std::ostream & out, const Vector & v)

View File

@ -11,7 +11,7 @@ class Vector
Vector(double x, double y, double z); Vector(double x, double y, double z);
double & operator[](int idx) { return m_array[idx]; } double & operator[](int idx) { return m_array[idx]; }
double operator[](int idx) const { return m_array[idx]; } double operator[](int idx) const { return m_array[idx]; }
void normalize(); Vector & normalize();
private: private:
double m_array[3]; double m_array[3];