src/util/src/geometry.js
function findPoiBetweenSegmentAndTriangle(poi, segment, triangleVertex1, triangleVertex2,
                                          triangleVertex3) {
  //
  // Find the point of intersection between the segment and the triangle's plane.
  //

  // First triangle edge.
  vec3.subtract(_tmpVec1, triangleVertex2, triangleVertex1);
  // Second triangle edge.
  vec3.subtract(_tmpVec2, triangleVertex3, triangleVertex1);
  // Triangle normal.
  vec3.cross(_tmpVec3, _tmpVec1, _tmpVec2);
  // Triangle to segment.
  vec3.subtract(_tmpVec4, segment.start, triangleVertex1);

  const normalToSegmentProj = vec3.dot(_tmpVec3, segment.dir);

  if (normalToSegmentProj < EPSILON && normalToSegmentProj > -EPSILON) {
    // The line segment is parallel to the triangle.
    return false;
  }

  const normalToDiffProj = -vec3.dot(_tmpVec3, _tmpVec4);
  const segmentNormalizedDistance = normalToDiffProj / normalToSegmentProj;

  if (segmentNormalizedDistance < 0 || segmentNormalizedDistance > 1) {
    // The line segment ends before intersecting the plane.
    return false;
  }

  vec3.scaleAndAdd(poi, segment.start, segment.dir, segmentNormalizedDistance);

  //
  // Determine whether the point of intersection lies within the triangle.
  //

  const edge1DotEdge1 = vec3.dot(_tmpVec1, _tmpVec1);
  const edge1DotEdge2 = vec3.dot(_tmpVec1, _tmpVec2);
  const edge2DotEdge2 = vec3.dot(_tmpVec2, _tmpVec2);
  // Triangle to point of intersection.
  vec3.subtract(_tmpVec3, poi, triangleVertex1);
  const diffDotEdge1 = vec3.dot(_tmpVec3, _tmpVec1);
  const diffDotEdge2 = vec3.dot(_tmpVec3, _tmpVec2);
  const denominator = edge1DotEdge2 * edge1DotEdge2 - edge1DotEdge1 * edge2DotEdge2;

  // Check the triangle's parametric coordinates.
  const s = (edge1DotEdge2 * diffDotEdge2 - edge2DotEdge2 * diffDotEdge1) / denominator;
  if (s < 0 || s > 1) {
    return false;
  }
  const t = (edge1DotEdge2 * diffDotEdge1 - edge1DotEdge1 * diffDotEdge2) / denominator;
  if (t < 0 || s + t > 1) {
    return false;
  }

  return true;
}
function findPoiBetweenSegmentAndTriangle(poi, segment, triangleVertex1, triangleVertex2,
                                          triangleVertex3) {
  //
  // Find the point of intersection between the segment and the triangle's plane.
  //

  // First triangle edge.
  vec3.subtract(_tmpVec1, triangleVertex2, triangleVertex1);
  // Second triangle edge.
  vec3.subtract(_tmpVec2, triangleVertex3, triangleVertex1);
  // Triangle normal.
  vec3.cross(_tmpVec3, _tmpVec1, _tmpVec2);
  // Triangle to segment.
  vec3.subtract(_tmpVec4, segment.start, triangleVertex1);

  const normalToSegmentProj = vec3.dot(_tmpVec3, segment.dir);

  if (normalToSegmentProj < EPSILON && normalToSegmentProj > -EPSILON) {
    // The line segment is parallel to the triangle.
    return false;
  }

  const normalToDiffProj = -vec3.dot(_tmpVec3, _tmpVec4);
  const segmentNormalizedDistance = normalToDiffProj / normalToSegmentProj;

  if (segmentNormalizedDistance < 0 || segmentNormalizedDistance > 1) {
    // The line segment ends before intersecting the plane.
    return false;
  }

  vec3.scaleAndAdd(poi, segment.start, segment.dir, segmentNormalizedDistance);

  //
  // Determine whether the point of intersection lies within the triangle.
  //

  const edge1DotEdge1 = vec3.dot(_tmpVec1, _tmpVec1);
  const edge1DotEdge2 = vec3.dot(_tmpVec1, _tmpVec2);
  const edge2DotEdge2 = vec3.dot(_tmpVec2, _tmpVec2);
  // Triangle to point of intersection.
  vec3.subtract(_tmpVec3, poi, triangleVertex1);
  const diffDotEdge1 = vec3.dot(_tmpVec3, _tmpVec1);
  const diffDotEdge2 = vec3.dot(_tmpVec3, _tmpVec2);
  const denominator = edge1DotEdge2 * edge1DotEdge2 - edge1DotEdge1 * edge2DotEdge2;

  // Check the triangle's parametric coordinates.
  const s = (edge1DotEdge2 * diffDotEdge2 - edge2DotEdge2 * diffDotEdge1) / denominator;
  if (s < 0 || s > 1) {
    return false;
  }
  const t = (edge1DotEdge2 * diffDotEdge1 - edge1DotEdge1 * diffDotEdge2) / denominator;
  if (t < 0 || s + t > 1) {
    return false;
  }

  return true;
}
function findPoiBetweenSegmentAndTriangle ( poi , segment , triangleVertex1 , triangleVertex2 , triangleVertex3 ) { // // Find the point of intersection between the segment and the triangle's plane. // // First triangle edge. vec3 . subtract ( _tmpVec1 , triangleVertex2 , triangleVertex1 ) ; // Second triangle edge. vec3 . subtract ( _tmpVec2 , triangleVertex3 , triangleVertex1 ) ; // Triangle normal. vec3 . cross ( _tmpVec3 , _tmpVec1 , _tmpVec2 ) ; // Triangle to segment. vec3 . subtract ( _tmpVec4 , segment . start , triangleVertex1 ) ; const normalToSegmentProj = vec3 . dot ( _tmpVec3 , segment . dir ) ; if ( normalToSegmentProj < EPSILON && normalToSegmentProj > - EPSILON ) { // The line segment is parallel to the triangle. return false ; } const normalToDiffProj = - vec3 . dot ( _tmpVec3 , _tmpVec4 ) ; const segmentNormalizedDistance = normalToDiffProj / normalToSegmentProj ; if ( segmentNormalizedDistance < 0 || segmentNormalizedDistance > 1 ) { // The line segment ends before intersecting the plane. return false ; } vec3 . scaleAndAdd ( poi , segment . start , segment . dir , segmentNormalizedDistance ) ; // // Determine whether the point of intersection lies within the triangle. // const edge1DotEdge1 = vec3 . dot ( _tmpVec1 , _tmpVec1 ) ; const edge1DotEdge2 = vec3 . dot ( _tmpVec1 , _tmpVec2 ) ; const edge2DotEdge2 = vec3 . dot ( _tmpVec2 , _tmpVec2 ) ; // Triangle to point of intersection. vec3 . subtract ( _tmpVec3 , poi , triangleVertex1 ) ; const diffDotEdge1 = vec3 . dot ( _tmpVec3 , _tmpVec1 ) ; const diffDotEdge2 = vec3 . dot ( _tmpVec3 , _tmpVec2 ) ; const denominator = edge1DotEdge2 * edge1DotEdge2 - edge1DotEdge1 * edge2DotEdge2 ; // Check the triangle's parametric coordinates. const s = ( edge1DotEdge2 * diffDotEdge2 - edge2DotEdge2 * diffDotEdge1 ) / denominator ; if ( s < 0 || s > 1 ) { return false ; } const t = ( edge1DotEdge2 * diffDotEdge1 - edge1DotEdge1 * diffDotEdge2 ) / denominator ; if ( t < 0 || s + t > 1 ) { return false ; } return true ; }
Finds the point of intersection between a line segment and a triangle.

This assumes the triangle is not degenerate (has non-zero side lengths).

----------------------------------------------------------------------------
Originally based on Dan Sunday's algorithms at http://geomalgorithms.com/a06-_intersect-2.html.

Copyright 2001 softSurfer, 2012 Dan Sunday
This code may be freely used and modified for any purpose
providing that this copyright notice is included with it.
SoftSurfer makes no warranty for this code, and cannot be held
liable for any real or imagined damage resulting from its use.
Users of this code must verify correctness for their application.
----------------------------------------------------------------------------

@param {vec3} poi Output param. Null if there is no intersection.
@param {LineSegment} segment
@param {vec3} triangleVertex1
@param {vec3} triangleVertex2
@param {vec3} triangleVertex3
@returns {boolean} True if there is an intersection.
Finds the point of intersection between a line segment and a triangle .