Updates for Box

This commit is contained in:
Eryn Wells 2014-08-09 10:54:00 -07:00
parent 70576c382a
commit 392871a1ee
2 changed files with 66 additions and 56 deletions

View file

@ -8,87 +8,95 @@
#include "objectBox.hh" #include "objectBox.hh"
using charles::basics::Ray;
using charles::basics::Vector4;
namespace charles { namespace charles {
/*
* charles::Box::Box --
*/
Box::Box() Box::Box()
/* A unit box centered on the origin. */ /* A unit box centered on the origin. */
: Box(Vector3(-0.5, -0.5, -0.5), Vector3(0.5, 0.5, 0.5)) : Box(Vector4(-0.5, -0.5, -0.5), Vector4(0.5, 0.5, 0.5))
{ } { }
Box::Box(const Vector3& near, const Vector3& far) /*
* charles::Box::Box --
*/
Box::Box(const Vector4& near,
const Vector4& far)
: Object(), : Object(),
mNear(near), mNear(near),
mFar(far) mFar(far)
{ } { }
Vector3& /*
* charles::Box::GetNear --
*/
Vector4&
Box::GetNear() Box::GetNear()
{ {
return mNear; return mNear;
} }
/*
* charles::Box::SetNear --
*/
void void
Box::SetNear(const Vector3& near) Box::SetNear(const Vector4& near)
{ {
mNear = near; mNear = near;
} }
Vector3& /*
* charles::Box::GetFar --
*/
Vector4&
Box::GetFar() Box::GetFar()
{ {
return mFar; return mFar;
} }
/*
* charles::Box::SetFar --
*/
void void
Box::SetFar(const Vector3& far) Box::SetFar(const Vector4& far)
{ {
mFar = far; mFar = far;
} }
bool /*
Box::point_is_on_surface(const Vector3& p) * charles::Box::DoNormal --
*/
Vector4
Box::DoNormal(const Vector4& p)
const const
{ {
if (p.x == mNear.x || p.x == mFar.x) { if (NearlyEqual(p.X(), mNear.X())) {
return (p.y > mNear.y && p.y < mFar.y) return Vector4(-1, 0, 0);
&& (p.z > mNear.z && p.z < mFar.z); } else if (NearlyEqual(p.X(), mFar.X())) {
} else if (p.y == mNear.y || p.y == mFar.y) { return Vector4(1, 0, 0);
return (p.x > mNear.x && p.x < mFar.x) } else if (NearlyEqual(p.Y(), mNear.Y())) {
&& (p.z > mNear.z && p.z < mFar.z); return Vector4(0, -1, 0);
} else if (p.z == mNear.z || p.z == mFar.z) { } else if (NearlyEqual(p.Y(), mFar.Y())) {
return (p.x > mNear.x && p.x < mFar.x) return Vector4(0, 1, 0);
&& (p.y > mNear.y && p.y < mFar.y); } else if (NearlyEqual(p.Z(), mNear.Z())) {
} return Vector4(0, 0, -1);
return false; } else if (NearlyEqual(p.Z(), mFar.Z())) {
} return Vector4(0, 0, 1);
Vector3
Box::compute_normal(const Vector3& p)
const
{
if (NearlyEqual(p.x, mNear.x)) {
return Vector3(-1, 0, 0);
} else if (NearlyEqual(p.x, mFar.x)) {
return Vector3(1, 0, 0);
} else if (NearlyEqual(p.y, mNear.y)) {
return Vector3(0, -1, 0);
} else if (NearlyEqual(p.y, mFar.y)) {
return Vector3(0, 1, 0);
} else if (NearlyEqual(p.z, mNear.z)) {
return Vector3(0, 0, -1);
} else if (NearlyEqual(p.z, mFar.z)) {
return Vector3(0, 0, 1);
} }
/* TODO: Eventually, I might want to raise an error here. */ /* TODO: Eventually, I might want to raise an error here. */
return Vector3(); return Vector4();
} }
@ -96,7 +104,7 @@ Box::compute_normal(const Vector3& p)
* charles::Box::DoIntersect -- * charles::Box::DoIntersect --
*/ */
bool bool
Box::DoIntersect(const basics::Ray& ray, Box::DoIntersect(const Ray& ray,
TVector& t, TVector& t,
Stats& stats) Stats& stats)
const const
@ -108,17 +116,17 @@ Box::DoIntersect(const basics::Ray& ray,
Double tNear = -std::numeric_limits<Double>::infinity(); Double tNear = -std::numeric_limits<Double>::infinity();
Double tFar = std::numeric_limits<Double>::infinity(); Double tFar = std::numeric_limits<Double>::infinity();
if (!IntersectSlab(mNear.x, mFar.x, if (!IntersectSlab(mNear.X(), mFar.X(),
ray.origin.X(), ray.direction.X(), ray.origin.X(), ray.direction.X(),
tNear, tFar)) { tNear, tFar)) {
return false; return false;
} }
if (!IntersectSlab(mNear.y, mFar.y, if (!IntersectSlab(mNear.Y(), mFar.Y(),
ray.origin.Y(), ray.direction.Y(), ray.origin.Y(), ray.direction.Y(),
tNear, tFar)) { tNear, tFar)) {
return false; return false;
} }
if (!IntersectSlab(mNear.z, mFar.z, if (!IntersectSlab(mNear.Z(), mFar.Z(),
ray.origin.Z(), ray.direction.Z(), ray.origin.Z(), ray.direction.Z(),
tNear, tFar)) { tNear, tFar)) {
return false; return false;

View file

@ -3,9 +3,11 @@
* Eryn Wells <eryn@erynwells.me> * Eryn Wells <eryn@erynwells.me>
*/ */
#include "basics.h" #ifndef __OBJECTBOX_HH__
#include "object.h" #define __OBJECTBOX_HH__
#include "types.hh"
#include "object.hh"
#include "basics/basics.hh"
namespace charles { namespace charles {
@ -14,22 +16,20 @@ struct Box
: public Object : public Object
{ {
Box(); Box();
Box(const Vector3& near, const Vector3& far); Box(const basics::Vector4& near, const basics::Vector4& far);
Vector3& GetNear(); basics::Vector4& GetNear();
void SetNear(const Vector3& near); void SetNear(const basics::Vector4& near);
Vector3& GetFar();
void SetFar(const Vector3& far);
bool DoesIntersect(const Ray& ray, TVector& t, Stats& stats) const; basics::Vector4& GetFar();
bool point_is_on_surface(const Vector3 &p) const; void SetFar(const basics::Vector4& far);
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: protected:
bool DoIntersect(const basics::Ray& ray, TVector& t, Stats& stats) const; bool DoIntersect(const basics::Ray& ray, TVector& t, Stats& stats) const;
basics::Vector4 DoNormal(const basics::Vector4& p) const;
private: private:
/** /**
@ -52,10 +52,12 @@ private:
Double& tFar) const; Double& tFar) const;
/** The near, lower left corner. */ /** The near, lower left corner. */
Vector3 mNear; basics::Vector4 mNear;
/** The far, upper right corner. */ /** The far, upper right corner. */
Vector3 mFar; basics::Vector4 mFar;
}; };
} /* namespace charles */ } /* namespace charles */
#endif /* __OBJECTBOX_HH__ */