# Intersection points between Line and Mesh

Revised for version 12. Now, viewportLayout1 is of type Model. To check if a Line collides with a Mesh, you need to intersect all the Mesh triangles with the Line, by using the Segment3D.IntersectWith() method.
The function below avoids to intersect line and triangles that lie on the same plane, neither adds the duplicated points.

``````
private void SegmentMeshIntersection(Mesh mesh, Segment3D segment, double tolerance, out List<Point3D> intersectedPoints, out List<IndexTriangle> intersectedTriangles)
{
Point3D intersection;
intersectedPoints = new List<Point3D>();
intersectedTriangles = new List<IndexTriangle>();

for (int i = 0; i < mesh.Triangles.Length; i++)
{
IndexTriangle it = mesh.Triangles[i];

// if the segment lies on the triangle plane, then avoid the intersection
if (!segment.IsInPlane(new Plane(mesh.Vertices[it.V1], mesh.Vertices[it.V2], mesh.Vertices[it.V3]), tolerance))
{
if (segment.IntersectWith(mesh.Vertices[it.V1], mesh.Vertices[it.V2], mesh.Vertices[it.V3], out intersection))
{

// checks for duplicated points
}
}
}
}

{
int count = 0;

if (listToCheck.Count == 0)
{
return;
}

for (int i = 0; i < listToCheck.Count; i++)
{
{
count++;
}
}

if (count == 0)
{
}
}
``````

Usage example:

``````List<Point3D> intersectedPoints;
List<IndexTriangle> intersectedTriangles;

Line line = new Line(new Point3D(-80, -15, 40), new Point3D(80, -15, 30));

Segment3D segment = new Segment3D(line.StartPoint, line.EndPoint);
SegmentMeshIntersection(bunnyMesh, segment, 0.01, out intersectedPoints, out intersectedTriangles);
``````
1 out of 1 found this helpful
Have more questions? Submit a request