InVoxel() for all Primitives, including fix for Triangle::CalcBounds()
[MicroTrace.git] / Sphere.cxx
index 77d4320..137ce8f 100644 (file)
@@ -26,33 +26,41 @@ Sphere::Intersect(Ray& ray)
   if(t > ray.t())
     return false;
 
-  if( t < 1e-6 )
+  if( t < Epsilon )
     {
       t = (-B+root)/(2.0f*A);
-      if( t < 1e-6 || t > ray.t())
+      if( t < Epsilon || t > ray.t())
        return false;
     }
+
   ray.setT(t);
+  ray.setHit(this);
+
   return true;
 }
 
 Vec3f
 Sphere::GetNormal(Ray& ray)
 {
-  // We don't want to modify the ray (probably this is not needed, but I am
-  // too lazy to think about it now ...)
-  Ray tempRay = ray;
-
-  // Surface normal is the difference between intersection and center point
-  if(Intersect(tempRay)) {
-    // intersection point
-    Vec3f i = tempRay.origin() + tempRay.direction() * (tempRay.t() - Epsilon);
-    // normal
-    Vec3f n = (i - m_center);
-    n.normalize();
-    return n;
-  } else {
-    // no intersection with ray, so no surface normal
-    return Vec3f(0,0,0);
-  }
+  Vec3f p = ray.origin()+ray.direction()*ray.t();
+  Vec3f N = (p-m_center);
+  N.normalize();
+
+  return N;
+}
+
+Box
+Sphere::CalcBounds()
+{
+  Vec3f min(m_center[0] - m_radius/2, m_center[1] - m_radius/2,
+    m_center[2] - m_radius/2);
+  Vec3f max(m_center[0] + m_radius/2, m_center[1] + m_radius/2,
+    m_center[2] + m_radius/2);
+  return Box(min, max);
+}
+
+bool
+Sphere::InVoxel(const Box& box)
+{
+  return CalcBounds().Overlaps(box);
 }
This page took 0.023851 seconds and 4 git commands to generate.