ffddbc8a2643f5f0f1cbac14b2dcf6bf3fe4def3
[MicroTrace.git] / Triangle.cxx
1 #include "Triangle.hxx"
2
3 Triangle::Triangle(const Vec3f& a,
4 const Vec3f& b,
5 const Vec3f& c)
6 : m_a(a),
7 m_b(b),
8 m_c(c)
9 {
10 }
11
12 Triangle::~Triangle()
13 {
14 }
15
16 bool
17 Triangle::Intersect(Ray& ray)
18 {
19 const Vec3f edge1 = m_b-m_a;
20 const Vec3f edge2 = m_c-m_a;
21
22 const Vec3f pvec = ray.direction().cross(edge2);
23
24 const float det = edge1.dot(pvec);
25 if (fabs(det) < Epsilon) return false;
26
27 const float inv_det = 1.0f / det;
28
29 const Vec3f tvec = ray.origin()-m_a;
30 float lambda = tvec.dot( pvec );
31 lambda *= inv_det;
32
33 if (lambda < 0.0f || lambda > 1.0f)
34 return false;
35
36 const Vec3f qvec = tvec.cross(edge1);
37 float mue = ray.direction().dot(qvec);
38 mue *= inv_det;
39
40 if (mue < 0.0f || mue+lambda > 1.0f)
41 return false;
42
43 float f = edge2.dot(qvec);
44 f *= inv_det;
45 if (ray.t() <= f || f < 1e-4 )
46 return false;
47
48 ray.setT(f);
49
50 return true;
51 }
This page took 0.042403 seconds and 3 git commands to generate.