Add Write methods to scene objects

Write methods are polymorphic, overridden by subclasses. There is a single operator<< for Objects that calls the right Write() method for the object. Neat.
This commit is contained in:
Eryn Wells 2014-08-03 17:25:00 -07:00
parent b56bbf4ced
commit 4828cb313f
8 changed files with 64 additions and 1 deletions

View file

@ -14,7 +14,7 @@
#include "object.h"
namespace charles {
#pragma mark - Objects
/*
@ -70,4 +70,21 @@ Object::SetMaterial(const Material& material)
mMaterial = material;
}
void
Object::Write(std::ostream& ost)
const
{
ost << "[Object]";
}
std::ostream&
operator<<(std::ostream& ost,
const Object& object)
{
object.Write(ost);
return ost;
}
} /* namespace charles */

View file

@ -47,6 +47,8 @@ struct Object
virtual bool point_is_on_surface(const Vector3 &p) const = 0;
virtual Vector3 compute_normal(const Vector3 &p) const = 0;
virtual void Write(std::ostream& ost) const;
private:
/** The location of this object. */
Vector3 mOrigin;
@ -55,6 +57,16 @@ private:
Material mMaterial;
};
/**
* Write a string representation of the Object to an output stream.
*
* @param [in] ost The output stream
* @param [in] object The object
* @returns The output stream
*/
std::ostream& operator<<(std::ostream& ost, const Object& object);
} /* namespace charles */
#endif

View file

@ -195,5 +195,13 @@ Box::IntersectSlab(const Double& slabLow,
return true;
}
void
Box::Write(std::ostream& ost)
const
{
ost << "[Box near=" << mNear << " far=" << mFar << "]";
}
} /* namespace charles */

View file

@ -25,6 +25,9 @@ struct Box
bool point_is_on_surface(const Vector3 &p) const;
Vector3 compute_normal(const Vector3 &p) const;
/** @see charles::Object::Write */
void Write(std::ostream& ost) const;
private:
/**
* Perform the intersection test on a slab, defined by `slabHigh` and

View file

@ -153,4 +153,12 @@ Plane::compute_normal(const Vector3 &p)
#pragma clang diagnostic pop
void
Plane::Write(std::ostream& ost)
const
{
ost << "[Plane normal=" << mNormal << " distance=" << mDistance << "]";
}
} /* namespace charles */

View file

@ -34,6 +34,9 @@ public:
bool point_is_on_surface(const Vector3 &p) const;
Vector3 compute_normal(const Vector3 &p) const;
/** @see charles::Object::Write */
void Write(std::ostream& ost) const;
private:
/** A normal vector, which specified the orientation of the plane. */
Vector3 mNormal;

View file

@ -161,4 +161,12 @@ Sphere::compute_normal(const Vector3 &p)
return normal;
}
void
Sphere::Write(std::ostream& ost)
const
{
ost << "[Sphere origin=" << GetOrigin() << " r=" << radius << "]";
}
} /* namespace charles */

View file

@ -29,6 +29,10 @@ public:
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 */
void Write(std::ostream& ost) const;
private:
float radius;
};