Cleanup for Spheres

This commit is contained in:
Eryn Wells 2014-08-09 11:04:17 -07:00
parent 590f10a756
commit 19dfb66301
2 changed files with 34 additions and 49 deletions

View file

@ -1,7 +1,5 @@
/* object_sphere.h /* objectSphere.cc
* * vim: set tw=80:
* Spheres are Scene objects defined by a center point and a radius.
*
* Eryn Wells <eryn@erynwells.me> * Eryn Wells <eryn@erynwells.me>
*/ */
@ -10,11 +8,10 @@
#include <cstdlib> #include <cstdlib>
#include <cstdio> #include <cstdio>
#include "basics.h"
#include "object.h"
#include "objectSphere.hh" #include "objectSphere.hh"
using charles::basics::Ray;
using charles::basics::Vector4; using charles::basics::Vector4;
@ -30,6 +27,9 @@ Sphere::Sphere(const Vector4& origin,
{ } { }
/*
* charles::Sphere::GetRadius --
*/
Double Double
Sphere::GetRadius() Sphere::GetRadius()
const const
@ -38,28 +38,31 @@ Sphere::GetRadius()
} }
/*
* charles::Sphere::SetRadius --
*/
void void
Sphere::SetRadius(Double r) Sphere::SetRadius(const Double& r)
{ {
mRadius = std::fabs(r); mRadius = std::fabs(r);
} }
/* /*
* Sphere::DoesIntersect -- * charles::Sphere::DoesIntersect --
*/ */
bool bool
Sphere::DoesIntersect(const Ray& ray, Sphere::DoIntersect(const Ray& ray,
TVector& t, TVector& t,
Stats& stats) Stats& stats)
const const
{ {
stats.sphereIntersectionTests++; stats.sphereIntersectionTests++;
/* Coefficients for quadratic equation. */ /* Coefficients for quadratic equation. */
Double a = ray.direction.dot(ray.direction); Double a = ray.direction.Dot(ray.direction);
Double b = ray.direction.dot(ray.origin) * 2.0; Double b = ray.direction.Dot(ray.origin) * 2.0;
Double c = ray.origin.dot(ray.origin) - (mRadius * mRadius); Double c = ray.origin.Dot(ray.origin) - (mRadius * mRadius);
/* Discriminant for the quadratic equation. */ /* Discriminant for the quadratic equation. */
Double discrim = (b * b) - (4.0 * a * c); 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 * Compute the intersections, the roots of the quadratic equation. Spheres
* have at most two intersections. * have at most two intersections.
*/ */
Double sqrtDiscrim = sqrt(discrim); Double sqrtDiscrim = std::sqrt(discrim);
Double t0 = (-b - sqrtDiscrim) / (2.0 * a); Double t0 = (-b - sqrtDiscrim) / (2.0 * a);
Double t1 = (-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 -- * charles::Sphere::DoNormal --
*
* Determine if a point lies on the surface of this Sphere.
*/ */
bool Vector4
Sphere::point_is_on_surface(const Vector3 &p) Sphere::DoNormal(const Vector4& p)
const const
{ {
Vector3 o = GetOrigin(); /*
Double x = p.x - o.x; * The fun thing about sphere is the normal to any point on the sphere is
Double y = p.y - o.y; * the point itself. Woo!
Double z = p.z - o.z; */
return Normalized(p);
return x*x + y*y + z*z == mRadius*mRadius;
} }
/* /*
* Sphere::compute_normal -- * charles::Sphere::Write --
*
* 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.
*/ */
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 void
Sphere::Write(std::ostream& ost) Sphere::Write(std::ostream& ost)
const const
{ {
ost << "[Sphere origin=" << GetOrigin() << " r=" << mRadius << "]"; ost << "[Sphere r=" << mRadius << "]";
} }
} /* namespace charles */ } /* namespace charles */

View file

@ -8,8 +8,7 @@
#ifndef __OBJECTSPHERE_HH__ #ifndef __OBJECTSPHERE_HH__
#define __OBJECTSPHERE_HH__ #define __OBJECTSPHERE_HH__
#include "basics.h" #include "object.hh"
#include "object.h"
#include "basics/basics.hh" #include "basics/basics.hh"
@ -22,15 +21,15 @@ public:
Sphere(const basics::Vector4& origin = basics::Vector4(), Double radius = 1.0); Sphere(const basics::Vector4& origin = basics::Vector4(), Double radius = 1.0);
Double GetRadius() const; Double GetRadius() const;
void SetRadius(Double r); void SetRadius(const 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;
/** @see charles::Object::Write */ /** @see charles::Object::Write */
void Write(std::ostream& ost) const; 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: private:
Double mRadius; Double mRadius;
}; };