2
$\begingroup$

I'm trying to generate a point cloud representing the volume of an arbitrary mesh. My approach is to create a uniform grid of sample points within the object bounding box, and then call closest_point_on_mesh for each of them discard the ones outside.

Here's my function for working out if a sample point is within the mesh or not:

def IsPointInsideObject(point, object): (success, closestPoint, closestFaceNormal, closestFaceIndex) = object.closest_point_on_mesh(point) result = True if success and (closestPoint - point).dot(closestFaceNormal) > 0 else False return result 

It does a good job mostly, but I always have these extra points which falsely report true:

Incorrect points

Looking into it, I think it's because the closest point sits right on an edge between faces, and so closest_point_on_mesh returns a face whose normal is pointing in the wrong direction.

Has anyone done something similar and found a way around this? I could maybe add a random jitter to my sample points in the hope of picking the right face, but I'd prefer to keep the grid uniform.

Or is there a better way to work out if a point's inside a mesh?

$\endgroup$
1
  • 1
    $\begingroup$ The only part of this question that's strictly related to Blender is "Or is there a better way to work out if a point's inside a mesh?" That being said, be aware that the Suzanne mesh is not manifold. $\endgroup$ Commented Sep 1, 2024 at 20:33

1 Answer 1

3
$\begingroup$

Actually just found a solution myself, by using a tolerance on the dot product check (and making sure the vector between the points is normalised) - so it looks like this:

def IsPointInsideObject(point, object): (success, closestPoint, closestFaceNormal, _) = object.closest_point_on_mesh(point) result = True if success and (closestPoint - point).normalized().dot(closestFaceNormal) > 0.4 else False return result 
$\endgroup$

You must log in to answer this question.