faster trimesh.proximity.closest_point for large query point sets
See original GitHub issueThank you for this amazing library.
I would like to compute the distance of a large number of point to a relatively simple mesh.
I am using trimesh.proximity.closest_point
, but unfortunately it is quit slow : it takes 30 second when querying 500000 points on my machine.
One major bottleneck seem to be the line candidates = [list(rtree.intersection(b)) for b in bounds]
in proximity.py
.
I believe this could run much faster if the loop was implemented in c++
. Ideally rtree.intersection would accept a list of boxes and implement the loop in c++ and return the list of list of candidates (similarly to scipy.spatial.KDTree.query_ball_point
), but that does not seem supported by rtree.
Here is the code I use to get some timings
import trimesh
import time
import numpy as np
import matplotlib.pyplot as plt
vertices = np.random.rand(20, 3)
faces = (np.random.rand(10, 3) * 20).astype(np.int)
mesh = trimesh.Trimesh(faces=faces, vertices=vertices)
nb_points_list = [1000, 10000, 100000, 500000]
durations_list = []
for nb_points in nb_points_list:
points = np.random.rand(nb_points, 3)
start = time.time()
trimesh.proximity.closest_point(mesh, points)
duration = time.time()-start
durations_list.append(duration)
print(f"trimesh.proximity.closest_point wit {nb_points} points took {duration} seconds")
plt.plot(nb_points_list, durations_list)
plt.xlabel("nb qurey points")
plt.xlabel("duration in seconds")
plt.show()
I get this timings trimesh.proximity.closest_point wit 1000 points took 0.11966729164123535 seconds trimesh.proximity.closest_point wit 10000 points took 0.6350014209747314 seconds trimesh.proximity.closest_point wit 100000 points took 6.338748455047607 seconds trimesh.proximity.closest_point wit 500000 points took 32.05847191810608 seconds
It would be great if we could list the potential ways we could accelerate the function. I looked at alternative libararies available from python to do the nearest points query but did not find a good alternative yet.
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (2 by maintainers)
Top GitHub Comments
The module python-prtree has been recently improved to implements a rtree on 3d data . The code is in C++ with pybind11 binding that provide a vectorized interface . It could be good alternative to Toblerity/rtree.
tried https://pypi.org/project/pysdf/ that is amazingly fast. with 500000 points I get
pysdf does not return the nearest face index for each point but could probably be modified to do that, in which case it could maybe be used a a dependency to accelerate trimesh’s function