code: def ray_triangle_intersection(v1, v2, v3, o, d):
      '''Computes the distance from a point to a triangle given a ray.
      '''
      eps = 1e-6
      e1 = sub(v2, v1)
      e2 = sub(v3, v1)
      p = cross(d, e2)
      det = dot(e1, p)
      if abs(det) < eps:
          return None
      inv = 1.0 / det
      t = sub(o, v1)
      u = dot(t, p) * inv
      if u < 0 or u > 1:
          return None
      q = cross(t, e1)
      v = dot(d, q) * inv
      if v < 0 or v > 1:
          return None
      t = dot(e2, q) * inv
      if t > eps:
          return t
      return Nonf ray_triangle_intersection(v1, v2, v3, o, d):
      '''Computes the distance from a point to a triangle given a ray.
      '''
      eps = 1e-6
      e1 = sub(v2, v1)
      e2 = sub(v3, v1)
      p = cross(d, e2)
      det = dot(e1, p)
      if abs(det) < eps:
          return None
      inv = 1.0 / det
      t = sub(o, v1)
      u = dot(t, p) * inv
      if u < 0 or u > 1:
          return None
      q = cross(t, e1)
      v = dot(d, q) * inv
      if v < 0 or v > 1:
          return None
      t = dot(e2, q) * inv
      if t > eps:
          return t
      return None
docs: Computes the distance from a point to a triangle given a ray.