From 19dfb66301a6ea5ddd6e69c654980efc45d9a728 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Sat, 9 Aug 2014 11:04:17 -0700 Subject: [PATCH] Cleanup for Spheres --- src/objectSphere.cc | 70 ++++++++++++++++++--------------------------- src/objectSphere.hh | 13 ++++----- 2 files changed, 34 insertions(+), 49 deletions(-) diff --git a/src/objectSphere.cc b/src/objectSphere.cc index d0ea008..a6229df 100644 --- a/src/objectSphere.cc +++ b/src/objectSphere.cc @@ -1,7 +1,5 @@ -/* object_sphere.h - * - * Spheres are Scene objects defined by a center point and a radius. - * +/* objectSphere.cc + * vim: set tw=80: * Eryn Wells */ @@ -10,11 +8,10 @@ #include #include -#include "basics.h" -#include "object.h" #include "objectSphere.hh" +using charles::basics::Ray; using charles::basics::Vector4; @@ -30,6 +27,9 @@ Sphere::Sphere(const Vector4& origin, { } +/* + * charles::Sphere::GetRadius -- + */ Double Sphere::GetRadius() const @@ -38,28 +38,31 @@ Sphere::GetRadius() } +/* + * charles::Sphere::SetRadius -- + */ void -Sphere::SetRadius(Double r) +Sphere::SetRadius(const Double& r) { mRadius = std::fabs(r); } /* - * Sphere::DoesIntersect -- + * charles::Sphere::DoesIntersect -- */ bool -Sphere::DoesIntersect(const Ray& ray, - TVector& t, - Stats& stats) +Sphere::DoIntersect(const Ray& ray, + TVector& t, + Stats& stats) const { stats.sphereIntersectionTests++; /* Coefficients for quadratic equation. */ - Double a = ray.direction.dot(ray.direction); - Double b = ray.direction.dot(ray.origin) * 2.0; - Double c = ray.origin.dot(ray.origin) - (mRadius * mRadius); + Double a = ray.direction.Dot(ray.direction); + Double b = ray.direction.Dot(ray.origin) * 2.0; + Double c = ray.origin.Dot(ray.origin) - (mRadius * mRadius); /* Discriminant for the quadratic equation. */ Double discrim = (b * b) - (4.0 * a * c); @@ -76,7 +79,7 @@ Sphere::DoesIntersect(const Ray& ray, * Compute the intersections, the roots of the quadratic equation. Spheres * have at most two intersections. */ - Double sqrtDiscrim = sqrt(discrim); + Double sqrtDiscrim = std::sqrt(discrim); Double t0 = (-b - sqrtDiscrim) / (2.0 * a); Double t1 = (-b + sqrtDiscrim) / (2.0 * a); @@ -109,45 +112,28 @@ Sphere::DoesIntersect(const Ray& ray, /* - * Sphere::point_is_on_surface -- - * - * Determine if a point lies on the surface of this Sphere. + * charles::Sphere::DoNormal -- */ -bool -Sphere::point_is_on_surface(const Vector3 &p) +Vector4 +Sphere::DoNormal(const Vector4& p) const { - Vector3 o = GetOrigin(); - Double x = p.x - o.x; - Double y = p.y - o.y; - Double z = p.z - o.z; - - return x*x + y*y + z*z == mRadius*mRadius; + /* + * The fun thing about sphere is the normal to any point on the sphere is + * the point itself. Woo! + */ + return Normalized(p); } /* - * Sphere::compute_normal -- - * - * Compute the normal for this Sphere at the given point. If the point does not lie on the surface of the sphere, a zero - * vector is returned. + * charles::Sphere::Write -- */ -Vector3 -Sphere::compute_normal(const Vector3 &p) - const -{ - // The fun thing about sphere is the normal to any point on the sphere is the point itself. Woo! - Vector3 normal = p - GetOrigin(); - normal.normalize(); - return normal; -} - - void Sphere::Write(std::ostream& ost) const { - ost << "[Sphere origin=" << GetOrigin() << " r=" << mRadius << "]"; + ost << "[Sphere r=" << mRadius << "]"; } } /* namespace charles */ diff --git a/src/objectSphere.hh b/src/objectSphere.hh index 9597868..7ad62d1 100644 --- a/src/objectSphere.hh +++ b/src/objectSphere.hh @@ -8,8 +8,7 @@ #ifndef __OBJECTSPHERE_HH__ #define __OBJECTSPHERE_HH__ -#include "basics.h" -#include "object.h" +#include "object.hh" #include "basics/basics.hh" @@ -22,15 +21,15 @@ public: Sphere(const basics::Vector4& origin = basics::Vector4(), Double radius = 1.0); Double GetRadius() const; - void SetRadius(Double r); - - bool DoesIntersect(const Ray& ray, TVector& t, Stats& stats) const; - bool point_is_on_surface(const Vector3 &p) const; - Vector3 compute_normal(const Vector3 &p) const; + void SetRadius(const Double& r); /** @see charles::Object::Write */ void Write(std::ostream& ost) const; +protected: + bool DoIntersect(const basics::Ray& ray, TVector& t, Stats& stats) const; + basics::Vector4 DoNormal(const basics::Vector4& p) const; + private: Double mRadius; };