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