I am using the trimesh library, to check this. It has mesh.contains for performing these checks. I am trying to truncate an array of lines using the mesh.contains, such that the new lines only contain the points from inside the mesh.
Although this works fine for in some cases, it fails in multiple other cases. I am attaching a few pictures, the blue is the truncated line and the red is the original line. The gray shaded region is the mesh. In one case, it is able to detect points outside and truncate properly. However, there are issues in case of outside regions that are sandwiched between the domains. The points are outside, but this function is unable to eliminate them.
The code chunk is given below:
# create an empty array to store truncated lines
truncated_arr = []
# loop over each line in the array
for line in arr:
new_line = []
# loop over each point in the line and check if it's inside the mesh
for point in line:
if ((mesh1.contains([point])) or (mesh2.contains([point])) or (mesh3.contains([point]))):
new_line.append(point)
# append the truncated line to the new array
if len(new_line) > 1:
truncated_arr.append(new_line)
I am looking for ideas to overcome this. Is there a way to make this algorithm detect points that are outside the domain but is sandwinched between two wings of the domain, as seen in the picture?



A possible solution would be to use the mesh.ray.intersects_location() method to perform a ray casting for each point, and count the number of intersections to determine whether the point is inside or outside the mesh.
Here's a modified version of your code that implements this approach:
Given a point, the
is_inside()function ray-casts the point along an axis in the positive X direction, then it counts the number of times the ray intersects the mesh. If the number of intersections is odd, the point is assumed to be inside the mesh. You can change the direction of the ray by changing thedirectionparameter. Thetolparameter is a small tolerance value to avoid starting the ray intersection exactly on the surface of the mesh.Note that this approach might be slower than using the mesh.contains() method due to the additional ray intersection calculations. However, it should better handle points outside the domain but sandwiched between two wings of the domain.