question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

faster trimesh.proximity.closest_point for large query point sets

See original GitHub issue

Thank 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 image

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:open
  • Created 3 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
martinResearchcommented, May 31, 2021

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.

1reaction
martinResearchcommented, Jan 11, 2021

tried https://pypi.org/project/pysdf/ that is amazingly fast. with 500000 points I get

trimesh.proximity.closest_point took 31.534400939941406 seconds
kdtree.query   took 0.25 seconds
pysdf   took 0.058998 seconds

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

trimesh.proximity — trimesh 3.17.1 documentation
For each point find nearby faces relatively quickly. The closest point on the mesh to the queried point is guaranteed to be on...
Read more >
CUBIT™ 15.9 User Documentation
Introduction - A quick overview of some of the main features and goals of the CUBIT. Mesh Generation Toolkit, licensing and distribution, hardware...
Read more >
trimesh/base.py · master · ynic-debian / trimesh - GitLab
Permutator(self) # convenience class for nearest point queries self.nearest = proximity.ProximityQuery(self) # store metadata about the mesh ...
Read more >
Level Set Methods for Visualization
J. Sethian, Level Set Methods and Fast Marching Methods, Cambridge University Press, ... the query point to the closest point with the surface...
Read more >
AStar — Godot Engine (stable) documentation in English
An implementation of A* to find the shortest paths among connected points in space. A* (A star) is a computer algorithm that is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found