#include "Box.hxx"
+#include "InfinitePlane.hxx"
Box::Box() : m_min(Infinity, Infinity, Infinity),
m_max(-Infinity, -Infinity, -Infinity)
void
Box::Clip(const Ray& ray, float& tnear, float& tfar) const
{
+ // Note: equations here are the same as in InfinitePlane::Intersect()
+ // t = ((o-a)*n) / (d*n)
+ // o = ray.origin(), d = ray.direction(),
+ // n = surface normal of plane, a = anchor point of plane
+ Vec3f diff_min = m_min - ray.origin(); // o-a
+ Vec3f diff_max = m_max - ray.origin();
+
+ float cos_theta;
+ float tx_near, tx_far;
+
+ // clip along x axis
+ if(cos_theta = ray.direction().dot(Vec3f(1,0,0)) != 0) // if not parallel...
+ tx_near = diff_min.dot(Vec3f(1,0,0)) / cos_theta;
+ if(cos_theta = ray.direction().dot(Vec3f(1,0,0)) != 0)
+ tx_far = diff_max.dot(Vec3f(1,0,0)) / cos_theta;
+
+ // clip along y axis
+ if(cos_theta = ray.direction().dot(Vec3f(0,1,0)) != 0)
+ ty_near = diff_min.dot(Vec3f(0,1,0)) / cos_theta;
+ if(cos_theta = ray.direction().dot(Vec3f(0,0,1)) != 0)
+ ty_far = diff_max.dot(Vec3f(0,1,0)) / cos_theta;
+
+ // ray intersects box iff it intersects projection on xy plane
+ // in this case: tx_near <= ty_near <= tx_far <= ty_far
+ // or: tx_far <= ty_near <= tx_near <= ty_far
+ // or: tx_far <= ty_far <= tx_near <= ty_near
+ // or: tx_near <= ty_far <= tx_far <= ty_near
+ if(tx_near <= ty_near && tx_far <= ty_near &&
+ tx_near <= ty_far && tx_far <= ty_far) {
+
+ // clip along z axis
+ if (cos_theta = ray.direction().dot(Vec3f(0, 1, 0)) != 0)
+ tz_near = diff_min.dot(Vec3f(0, 1, 0)) / cos_theta;
+ if (cos_theta = ray.direction().dot(Vec3f(0, 0, 1)) != 0)
+ tz_far = diff_max.dot(Vec3f(0, 1, 0)) / cos_theta;
+
+ } else {
+
+ }
+
+ //////////////////////////
+
}
const Vec3f&