fixed Box::Extend(Vec3f&)
[MicroTrace.git] / Triangle.cxx
index 456c6d8..873f440 100644 (file)
@@ -7,8 +7,10 @@ Triangle::Triangle(const Vec3f& a,
   : Primitive(shader),
     m_a(a),
     m_b(b),
-    m_c(c)
+    m_c(c),
+    m_n( (b-a).cross(c-a) )
 {
+  m_n.normalize();
 }
 
 Triangle::~Triangle()
@@ -44,10 +46,11 @@ Triangle::Intersect(Ray& ray)
 
   float f = edge2.dot(qvec);
   f *= inv_det;
-  if (ray.t() <= f || f <  1e-4  )
+  if (ray.t() <= f || f < Epsilon )
     return false;
 
   ray.setT(f);
+  ray.setHit(this);
 
   return true;
 }
@@ -55,8 +58,21 @@ Triangle::Intersect(Ray& ray)
 Vec3f
 Triangle::GetNormal(Ray& ray)
 {
-  // normal is cross product of spanning vectors
-  Vec3f n = (m_c - m_a) % (m_c - m_b);
-  n.normalize();
-  return n;
+  return m_n;
+}
+
+Box
+Triangle::CalcBounds()
+{
+  Box bounds;
+  bounds.Extend(m_a);
+  bounds.Extend(m_b);
+  bounds.Extend(m_c);
+  return bounds;
+}
+
+bool
+Triangle::InVoxel(const Box& box)
+{
+  return CalcBounds().Overlaps(box);
 }
This page took 0.025656 seconds and 4 git commands to generate.