Real closestPtLineTriangle(Line3 const & line, Vector3 const & v0, Vector3 const & edge01, Vector3 const & edge02, Real & s, Vector3 & c1, Vector3 & c2) { if (lineIntersectsTriangle(line, v0, edge01, edge02, &s)) { c1 = c2 = line.getPoint() + s * line.getDirection(); return 0; } // Either (1) the line is not parallel to the triangle and the point of // intersection of the line and the plane of the triangle is outside the // triangle or (2) the line and triangle are parallel. Regardless, the // closest point on the triangle is on an edge of the triangle. Compare // the line to all three edges of the triangle. Real min_sqdist = -1; Vector3 v[3] = { v0, v0 + edge01, v0 + edge02 }; Vector3 tmp_c1, tmp_c2; float tmp_s, t; for (int i0 = 2, i1 = 0; i1 < 3; i0 = i1++) { Real sqdist = closestPtSegmentLine(v[i0], v[i1], line.getPoint(), line.getPoint() + line.getDirection(), t, tmp_s, tmp_c2, tmp_c1); if (min_sqdist < 0 || sqdist < min_sqdist) { min_sqdist = sqdist; s = tmp_s; c1 = tmp_c1; c2 = tmp_c2; } } return min_sqdist; }
// clip this line segment against this triangle. bool clip(const Vector3d &start,Vector3d &end) const { Vector3d sect; bool hit = lineIntersectsTriangle(start.Ptr(), end.Ptr(), mP1.Ptr(), mP2.Ptr(), mP3.Ptr(), sect.Ptr() ); if ( hit ) { end = sect; } return hit; }