Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trimesh for terrain representation #2351

Open
mllobera opened this issue Feb 5, 2025 · 3 comments
Open

Trimesh for terrain representation #2351

mllobera opened this issue Feb 5, 2025 · 3 comments

Comments

@mllobera
Copy link

mllobera commented Feb 5, 2025

I generated a regular triangulated mesh in order to represent a terrain (ideally this would be using a delaunay triangulation). I was wondering if trimesh has any functionality built in that would allow me to get the vertices of the triangle (simplex) that contains a 2D point (though the triangulation is 3D). I needed so that I can interpolate the elevation at that point. Does anyone have any suggestions as to how this would be achieved using trimesh?

Also forgive me but when you specify "points ((n, 3) float)" is (n.3) a numpy array representing 3d coordinates?

Thank you and apologize for any dumb question

@mllobera
Copy link
Author

mllobera commented Feb 6, 2025

So... I found that within trimesh you can create a 'triangle_ray' and this can be used to find intersections.

I wanted to test whether I could use this to determine the triangle containing the point I want to interpolate. Here is my code,

terrain = tm.Trimesh(vertices = trn.vertices,
           faces = trn.indices)
intersections= tm.ray.ray_triangle.RayMeshIntersector(terrain)
origin = np.array([[531306., 4387340.,0.]])
direction = np.array([[0.,0.,1.]])
intersections.intersects_first(origin, direction)

Unfortunately I got the following error,

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Cell In[34], [line 4](vscode-notebook-cell:?execution_count=34&line=4)
      [2](vscode-notebook-cell:?execution_count=34&line=2) origin = np.array([[531306., 4387340.,0.]])
      [3](vscode-notebook-cell:?execution_count=34&line=3) direction = np.array([[0.,0.,1.]])
----> [4](vscode-notebook-cell:?execution_count=34&line=4) intersections.intersects_first(origin, direction)

File c:\Python\Miniconda3\envs\opengl\Lib\site-packages\trimesh\ray\ray_triangle.py:119, in RayMeshIntersector.intersects_first(self, ray_origins, ray_directions, **kwargs)
    [101](file:///C:/Python/Miniconda3/envs/opengl/Lib/site-packages/trimesh/ray/ray_triangle.py:101) def intersects_first(self, ray_origins, ray_directions, **kwargs):
    [102](file:///C:/Python/Miniconda3/envs/opengl/Lib/site-packages/trimesh/ray/ray_triangle.py:102)     """
    [103](file:///C:/Python/Miniconda3/envs/opengl/Lib/site-packages/trimesh/ray/ray_triangle.py:103)     Find the index of the first triangle a ray hits.
    [104](file:///C:/Python/Miniconda3/envs/opengl/Lib/site-packages/trimesh/ray/ray_triangle.py:104) 
   (...)
    [116](file:///C:/Python/Miniconda3/envs/opengl/Lib/site-packages/trimesh/ray/ray_triangle.py:116)       Index of triangle ray hit, or -1 if not hit
    [117](file:///C:/Python/Miniconda3/envs/opengl/Lib/site-packages/trimesh/ray/ray_triangle.py:117)     """
--> [119](file:///C:/Python/Miniconda3/envs/opengl/Lib/site-packages/trimesh/ray/ray_triangle.py:119)     (index_tri, index_ray) = self.intersects_id(
    [120](file:///C:/Python/Miniconda3/envs/opengl/Lib/site-packages/trimesh/ray/ray_triangle.py:120)         ray_origins=ray_origins,
    [121](file:///C:/Python/Miniconda3/envs/opengl/Lib/site-packages/trimesh/ray/ray_triangle.py:121)         ray_directions=ray_directions,
    [122](file:///C:/Python/Miniconda3/envs/opengl/Lib/site-packages/trimesh/ray/ray_triangle.py:122)         return_locations=False,
    [123](file:///C:/Python/Miniconda3/envs/opengl/Lib/site-packages/trimesh/ray/ray_triangle.py:123)         multiple_hits=False,
    [124](file:///C:/Python/Miniconda3/envs/opengl/Lib/site-packages/trimesh/ray/ray_triangle.py:124)         **kwargs,
    [125](file:///C:/Python/Miniconda3/envs/opengl/Lib/site-packages/trimesh/ray/ray_triangle.py:125)     )
    [127](file:///C:/Python/Miniconda3/envs/opengl/Lib/site-packages/trimesh/ray/ray_triangle.py:127)     # put the result into the form of "one triangle index per ray"
    [128](file:///C:/Python/Miniconda3/envs/opengl/Lib/site-packages/trimesh/ray/ray_triangle.py:128)     result = np.ones(len(ray_origins), dtype=np.int64) * -1
...
-> [1724](file:///C:/Python/Miniconda3/envs/opengl/Lib/site-packages/trimesh/util.py:1724)     import rtree
   [1726](file:///C:/Python/Miniconda3/envs/opengl/Lib/site-packages/trimesh/util.py:1726)     # make sure we've copied bounds
   [1727](file:///C:/Python/Miniconda3/envs/opengl/Lib/site-packages/trimesh/util.py:1727)     bounds = np.array(bounds, dtype=np.float64, copy=True)

ModuleNotFoundError: No module named 'rtree'

@liberostelios
Copy link

You need to have rtree installed. Either install it manually (pip install rtree) or install a trimesh with all its dependencies (like pip install 'trimesh[easy]').

@mllobera
Copy link
Author

mllobera commented Feb 6, 2025

Thanks you @liberostelios

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants