title: Ray-Triangle Intersection C++
body: <p>I am testing if a ray intersects a triangle. For the time being, I'm using the following code to test if there is an intersection between a triangle, and the ray from origin to the midpoint of the triangle:</p>
  <pre><code>Ray&lt;float&gt; *ray = new Ray&lt;float&gt;(Vec3&lt;float&gt;(0), chosenTriangle-&gt;GetTriangleMidpoint()); 
  </code></pre>
  <p>Along side is the <code>Vec3</code> object which I'm using to store the vector operations:</p>
  <pre><code>template&lt;typename T&gt;
  class Vec3
  {
  public:
      T x, y, z;
      Vec3() : x(T(0)), y(T(0)), z(T(0)) { }
      Vec3(T xx) : x(xx), y(xx), z(xx) { }
  
      Vec3(T xx, T yy, T zz) : x(xx), y(yy), z(zz) {}
      Vec3&amp; normalize()
      {
          T nor2 = length2();
          if (nor2 &gt; 0) {
              T invNor = 1 / sqrt(nor2);
              x *= invNor, y *= invNor, z *= invNor;
          }
          return *this;
      }
  
      Vec3&lt;T&gt; operator * (const T &amp;f) const { return Vec3&lt;T&gt;(x * f, y * f, z * f); }
      Vec3&lt;T&gt; operator * (const Vec3&lt;T&gt; &amp;v) const { return Vec3&lt;T&gt;(x * v.x, y * v.y, z * v.z); }
      T dot(const Vec3&lt;T&gt; &amp;v) const { return x * v.x + y * v.y + z * v.z; }
      Vec3&lt;T&gt; operator - (const Vec3&lt;T&gt; &amp;v) const { return Vec3&lt;T&gt;(x - v.x, y - v.y, z - v.z); }
      Vec3&lt;T&gt; operator + (const Vec3&lt;T&gt; &amp;v) const { return Vec3&lt;T&gt;(x + v.x, y + v.y, z + v.z); }
      bool operator == (const Vec3&lt;T&gt; &amp;v) { return x == v.x &amp;&amp; y == v.y &amp;&amp; z == v.z; }
      Vec3&lt;T&gt; operator - () const { return Vec3&lt;T&gt;(-x, -y, -z); }
      T length2() const { return x * x + y * y + z * z; }
      T length() const { return sqrt(length2()); }
      Vec3&lt;T&gt; CrossProduct(Vec3&lt;T&gt; other)
      { 
          return Vec3&lt;T&gt;(y*other.z - other.y*z, x*other.z - z*other.x, x*other.y - y*other.x); 
      }
      friend std::ostream &amp; operator &lt;&lt; (std::ostream &amp;os, const Vec3&lt;T&gt; &amp;v)
      {
          os &lt;&lt; &quot;[&quot; &lt;&lt; v.x &lt;&lt; &quot; &quot; &lt;&lt; v.y &lt;&lt; &quot; &quot; &lt;&lt; v.z &lt;&lt; &quot;]&quot;;
          return os;
      }
  </code></pre>
  <p>The chosen triangle and the ray have the following values, where <code>vertA</code>, <code>vertB</code> and <code>vertC</code> are the vertices of the triangle and are found in an object which represents a triangle.</p>
  <p><img src="https://i.stack.imgur.com/pElgI.png" alt="enter image description here" /></p>
  <p>The code which computes if there is an intersection between a specified ray and an intersection is the following. This code is found inside the triangle object method where <code>vertA</code>, <code>vertB</code> and <code>vertC</code> are global variables.</p>
  <pre><code>bool CheckRayIntersection(Vec3&lt;T&gt; &amp;o, Vec3&lt;T&gt; &amp;d)
  {
      Vec3&lt;T&gt; e1 = vertB - vertA;
      Vec3&lt;T&gt; e2 = vertC - vertA;
      Vec3&lt;T&gt; p = d.CrossProduct(e2);
      T a = e1.dot(p);
  
      if(a == 0)
          return false;
  
      float f = 1.0f/a;
  
      Vec3&lt;T&gt; s = o - vertA;
      T u = f * s.dot(p);
      if(u &lt; 0.0f || u &gt; 1.0f)
          return false;
  
      Vec3&lt;T&gt; q = s.CrossProduct(e1);
      T v = f * d.dot(q);
  
      if(v &lt; 0.0f || u+v &gt; 1.0f)
          return false;
  
      T t = f * e2.dot(q);
  
      return (t &gt;= 0);
      
  }
  </code></pre>
  <p>I still get a false returned from the function, but I'm presuming it should return a true since a vector passing through the midpoint of the triangle should intersect the triangle at the midpoint. Can anybody enlighten me what's wrong in my code? Or is the returned false actually correct?</p>