STriangle3.h
Go to the documentation of this file.
1 
2 #pragma once
3 
4 #include <ionCore.h>
5 
6 
7 namespace ion
8 {
9 
12  template <typename T>
13  class STriangle3
14  {
15 
16  public:
17 
19 
21  {}
22 
23  STriangle3(vec3<T> const & v1, vec3<T> const & v2, vec3<T> const & v3)
24  {
25  Vertices[0] = v1;
26  Vertices[1] = v2;
27  Vertices[2] = v3;
28  }
29 
30  bool intersect(vec3<T> const & p, vec3<T> const & d)
31  {
32  vec3<T> v0, v1, v2;
33  v0 = Vertices[0];
34  v1 = Vertices[1];
35  v2 = Vertices[2];
36 
37  vec3<T> e1, e2, h, s, q;
38  T a, f, u, v;
39  e1 = v1 - v0;
40  e2 = v2 - v0;
41  h = Cross(d, e2);
42  a = Dot(e1, h);
43 
44  if (Equals(a, 0.f))
45  return false;
46 
47  f = 1 / a;
48  s = p - v0;
49  u = f * (Dot(s, h));
50 
51  if (u < 0.0 || u > 1.0)
52  return false;
53 
54  q = Cross(s, e1);
55  v = f * Dot(d, q);
56 
57  if (v < 0.0 || u + v > 1.0)
58  return false;
59 
60  // at this stage we can compute t to find out where
61  // the intersection point is on the line
62  float t = f * Dot(e2, q);
63 
64  if (t > 0.00001) // ray intersection
65  return true;
66 
67  else // this means that there is a line intersection
68  // but not a ray intersection
69  return false;
70  }
71 
72  };
73 
78 
79  typedef STriangle3f tri3f;
80  typedef STriangle3d tri3d;
81  typedef STriangle3i tri3i;
82  typedef STriangle3u tri3u;
83 
84 }
STriangle3< float > STriangle3f
Definition: STriangle3.h:74
3D vector
Definition: vec3.h:15
STriangle3f tri3f
Definition: STriangle3.h:79
STriangle3u tri3u
Definition: STriangle3.h:82
STriangle3< ion::uint > STriangle3u
Definition: STriangle3.h:77
3D triangle
Definition: STriangle3.h:13
STriangle3d tri3d
Definition: STriangle3.h:80
STriangle3(vec3< T > const &v1, vec3< T > const &v2, vec3< T > const &v3)
Definition: STriangle3.h:23
STriangle3< double > STriangle3d
Definition: STriangle3.h:75
STriangle3()
Definition: STriangle3.h:20
vec3< T > Vertices[3]
Definition: STriangle3.h:18
Definition: CCatmullRomAdvancedSplineInterpolator.h:7
STriangle3i tri3i
Definition: STriangle3.h:81
bool intersect(vec3< T > const &p, vec3< T > const &d)
Definition: STriangle3.h:30
STriangle3< int > STriangle3i
Definition: STriangle3.h:76