title: How to find the intersection point of a ray and a triangle?
body: <p>I find the trilinear coordinates of the coordinate of the point of intersection through the barycentric coordinates. Barycentric coordinates are correct (seemingly).</p>
  
  <pre><code>    private const double Epsilon = 0.000001d;
  
      public static Vector3? GetPointIntersectionRayAndTriangle(Vector3 rayOrigin, Vector3 rayDirection, Vector3 vert0, Vector3 vert1, Vector3 vert2)
      {
          Vector3 edge1 = new Vector3();
          Vector3 edge2 = new Vector3();
  
          Vector3 tvec = new Vector3();
          Vector3 pvec = new Vector3();
          Vector3 qvec = new Vector3();
  
          double det, invDet;
  
          edge1 = vert1 - vert0;
          edge2 = vert2 - vert0;
  
          pvec = Cross(rayDirection, edge2);
  
          det = Dot(edge1, pvec);
  
          if (det &gt; -Epsilon &amp;&amp; det &lt; Epsilon)
          {
              return null;
          }
  
          invDet = 1d / det;
  
          tvec = rayOrigin - vert0;
  
          double t, u, v;
  
          u = Dot(tvec, pvec) * invDet;
  
          if (u &lt; 0 || u &gt; 1)
          {
              return null;
          }
  
          qvec = Cross(tvec, edge1);
  
          v = Dot(rayDirection, qvec) * invDet;
  
          if (v &lt; 0 || u + v &gt; 1)
          {
              return null;
          }
  
          t = Dot(edge2, qvec) * invDet;
  
          return GetTrilinearCoordinates(t, u, v, vert0, vert1, vert2);
      }
  
      private static double Dot(Vector3 v1, Vector3 v2)
      {
          return v1.X * v2.X + v1.Y * v2.Y + v1.Z * v2.Z;
      }
  
      private static Vector3 Cross(Vector3 v1, Vector3 v2)
      {
          Vector3 dest;
  
          dest.X = v1.Y * v2.Z - v1.Z * v2.Y;
          dest.Y = v1.Z * v2.X - v1.X * v2.Z;
          dest.Z = v1.X * v2.Y - v1.Y * v2.X;
  
          return dest;
      }
  
      private static Vector3 GetTrilinearCoordinates(double t, double u, double v, Vector3 vert0, Vector3 vert1, Vector3 vert2)
      {
          float a = (vert0 - vert1).Length();
          float b = (vert1 - vert2).Length();
          float c = (vert2 - vert0).Length();
  
          return new Vector3((float)t / a, (float)u / b, (float)v / c);
      }
  </code></pre>
  
  <ul>
  <li>rayOrigin - beginning of the ray. </li>
  <li>vert0, vert1, vert2 - coordinates
  of the triangle.</li>
  </ul>
  
  <p>I use this unit test to check:</p>
  
  <pre><code>    [TestMethod]
      public void GetPointIntersectionRayAndTriangleCheckOnResult()
      {
          Vector3? vector1 = ComputationsInThreeDimensionalSpace.GetPointIntersectionRayAndTriangle(
              new Vector3(1, 1, 2),
              new Vector3(0, 0, -4),
              new Vector3(0, 0, 0),
              new Vector3(4, -1, 0),
              new Vector3(0, 5, 0));
  
          if (!vector1.HasValue)
          {
              Assert.Fail();
          }
  
          Assert.AreEqual(new Vector3(1, 1, 0), vector1.Value);
      }
  </code></pre>
  
  <p>Are there other ways to find the point of intersection of a ray with a triangle? It is desirable without barycentric coordinates.</p>