Search code examples
python-3.xopen3d

How to obtain the mesh triangle the point in the point cloud belongs to?


I am converting a mesh to point cloud using open3d "sample_points_uniformly" function. The catch is I also need to know which mesh triangle the point in the point cloud belongs to. Is there a way to get this information?

o3d_mesh = o3d.geometry.TriangleMesh()
o3d_mesh.vertices = o3d.utility.Vector3dVector(vert_data)
o3d_mesh.triangles = o3d.utility.Vector3iVector(face_data)
pcd = o3d_mesh.sample_points_poisson_disk(number_of_points=500, init_factor=5)

I need to know which triangle (index), to which a point in pcd stems from.


Solution

  • I have found the answer to my own question. Since I already know the point, I simply perform ray casting, which gives the primitive_id for the intersection point. (Note: I perturb the point by a small amount along the normal)

    # sample point
    point = np.asarray(pcd.points)[200]
    normal =  np.asarray(pcd.normals)[200]
    epsilon=1e-6
    ray_d = np.hstack((point+epsilon*normal, -normal))
    ray = o3d.core.Tensor([ray_d], dtype=o3d.core.Dtype.Float32)
    
    # do ray casting with the ray
    primitive_id = scene.cast_rays(ray)["primitive_ids"].item()