From bdb648b84f5e0bbb442721ed899638de46f28e6d Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Fri, 23 Jan 2009 04:15:25 +0000 Subject: [PATCH] added transform_normal() to util/Transform git-svn-id: svn://anubis/fart/trunk@34 7f9b0f55-74a9-4bce-be96-3c2cd072584d --- util/Transform.cc | 5 +++++ util/Transform.h | 1 + util/Vector.cc | 3 ++- util/Vector.h | 2 +- 4 files changed, 9 insertions(+), 2 deletions(-) diff --git a/util/Transform.cc b/util/Transform.cc index 962a52d..1ca90b3 100644 --- a/util/Transform.cc +++ b/util/Transform.cc @@ -64,6 +64,11 @@ Vector Transform::transform_direction(Transform & t, const Vector & 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) { Vector newPosition = t.getMatrix() * r.getOrigin(); diff --git a/util/Transform.h b/util/Transform.h index 11af3be..d03e7d2 100644 --- a/util/Transform.h +++ b/util/Transform.h @@ -17,6 +17,7 @@ class Transform Matrix & getMatrix() { return m_matrix; } Vector transform_point(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); protected: diff --git a/util/Vector.cc b/util/Vector.cc index 2208d11..744214c 100644 --- a/util/Vector.cc +++ b/util/Vector.cc @@ -17,7 +17,7 @@ Vector::Vector(double x, double y, double z) m_array[2] = z; } -void Vector::normalize() +Vector & Vector::normalize() { double length = sqrt(m_array[0] * m_array[0] + m_array[1] * m_array[1] @@ -25,6 +25,7 @@ void Vector::normalize() m_array[0] /= length; m_array[1] /= length; m_array[2] /= length; + return *this; } std::ostream & operator<<(std::ostream & out, const Vector & v) diff --git a/util/Vector.h b/util/Vector.h index 0704d93..390e89c 100644 --- a/util/Vector.h +++ b/util/Vector.h @@ -11,7 +11,7 @@ class Vector Vector(double x, double y, double z); double & operator[](int idx) { return m_array[idx]; } double operator[](int idx) const { return m_array[idx]; } - void normalize(); + Vector & normalize(); private: double m_array[3];