neka-nat/cupoch

Check collision between a mesh and a set of points.

ambitious-octopus opened this issue ยท 0 comments

Good job the package you are developing is very interesting, I appreciate your work. ๐Ÿ‘

I am looking for a fast way to compute the collision between a mesh and a set of points. I am currently using the RaycastingScene() class from open3d. Unfortunately, the computation turns out to be quite slow for what I need. I was wondering if your package offers a more efficient system for the computation I need. I welcome suggestions on how to make this computation more efficient.
This is a minimal example with open3d:

import open3d as o3d
import numpy as np

mesh = o3d.geometry.TriangleMesh.create_sphere(0.2)
frame = o3d.geometry.TriangleMesh.create_coordinate_frame(0.3)

points = np.array([[-0.1, -0.1, -0.1],
                   [0.05, 0.05, 0.05],
                   [0.01, 0.01, 0.01],
                   [0.1, 0.1, 0.1],
                   [0.2, 0.2, 0.2]])

pcd = o3d.geometry.PointCloud(o3d.utility.Vector3dVector(points))
pcd.paint_uniform_color([1, 0, 0])

scene = o3d.t.geometry.RaycastingScene()
mesh_legacy = o3d.t.geometry.TriangleMesh.from_legacy(mesh)
_ = scene.add_triangles(mesh_legacy)  # we do not need the geometry ID for mesh

query_points = points.astype(np.float32)
occupancy = scene.compute_occupancy(query_points, 16)
print(occupancy.numpy())

o3d.visualization.draw_geometries([mesh, frame, pcd])

I have seen this script before, but unfortunately I get an error (issue #118):

import cupoch as cph
import copy
import time
import numpy as np
import os
import sys
dir_path = os.path.dirname(os.path.realpath(__file__))
sys.path.append(os.path.join(dir_path, "../misc"))
import meshes
cubic_size = 1.0
voxel_resolution = 128.0
mesh = meshes.bunny()
target = cph.geometry.VoxelGrid.create_from_triangle_mesh_within_bounds(
mesh,
voxel_size=cubic_size / voxel_resolution,
min_bound=(-cubic_size / 2, -cubic_size / 2, -cubic_size / 2),
max_bound=(cubic_size / 2, cubic_size / 2, cubic_size / 2),
)
vis = cph.visualization.Visualizer()
vis.create_window()
vis.add_geometry(target)
query = copy.deepcopy(target)
query.translate([-0.15, 0.0, 0.0])
dh = 0.02
for i in range(15):
query.translate([dh, 0.0, 0.0])
ans = cph.collision.compute_intersection(target, query, 0.0)
print(ans)
target.paint_uniform_color([1.0, 1.0, 1.0])
query.paint_uniform_color([1.0, 1.0, 1.0])
target.paint_indexed_color(ans.get_first_collision_indices(), [1.0, 0.0, 0.0])
query.paint_indexed_color(ans.get_second_collision_indices(), [0.0, 1.0, 0.0])
if i == 0:
vis.add_geometry(query)
vis.update_geometry(target)
vis.update_geometry(query)
vis.poll_events()
vis.update_renderer()
time.sleep(0.1)
vis.destroy_window()

Thank you for your support.