Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions slam/geodesics.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,25 @@ def shortest_path(mesh, start_idx, end_idx):
return path


def compute_gdist(mesh, vert_id):
def compute_gdist(mesh, source_id, target_id=None):
"""
This func computes the geodesic distance from one point to all vertices on
mesh by using the gdist.compute_gdist().
Actually, you can get the geo-distances that you want by changing the
source and target vertices set.
:param mesh: trimesh object
:param vert_id: the point index
This func computes the geodesic distance between a set of sources and targets
on a mesh surface by using the gdist.compute_gdist().
If no target_id are provided, all vertices of the mesh are considered as targets.

:param mesh: (trimesh object) the mesh surface
:param source_id: (list) the sources index
:param target_id: (list) the targets index
:return:
"""
vert = mesh.vertices
poly = mesh.faces.astype(np.int32)

source_index = np.array([vert_id], dtype=np.int32)
target_index = np.linspace(0, len(vert) - 1, len(vert)).astype(np.int32)
source_index = np.array([source_id], dtype=np.int32)
if target_id:
target_index = np.array([target_id], dtype=np.int32)
else:
target_index = np.linspace(0, len(vert) - 1, len(vert)).astype(np.int32)

return gdist.compute_gdist(vert, poly, source_index, target_index)

Expand Down
48 changes: 29 additions & 19 deletions tests/test_geodesics.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import unittest

import numpy as np
from scipy.spatial import Delaunay
import trimesh
# import slam.geodesics as sg
import slam.geodesics as sg


TOL = 1e-10

Expand All @@ -30,7 +30,7 @@ def create_rectangular_grid(rows=3, cols=4):
return trimesh.Trimesh(faces=tri.simplices, vertices=coords, process=False)


"""

class TestGeodesics(unittest.TestCase):

def test_compute_gdist(self):
Expand All @@ -46,26 +46,36 @@ def test_compute_gdist(self):
self.assertTrue(np.logical_and(distance[m * n - 1] >= low_bound,
distance[-1] <= up_bound))

def test_dijkstra_length(self):
# Check if the max distance to one side of the rectangle
# equals the length of the other side
def test_compute_gdist_to_target(self):
m = 4
n = 5
rectangle = create_rectangular_grid(m, n)
set_of_points = range(m) # boundary with y = 0
distance = sg.dijkstra_length(rectangle, set_of_points)
self.assertTrue(np.abs(np.max(distance) - (n - 1) < TOL))
print(rectangle)
target = 10 #rectangle.vertices.shape[0]-1
distance = sg.compute_gdist(rectangle, 0)
distance_to_target = sg.compute_gdist(rectangle, 0, target)
self.assertTrue(distance[target]==distance_to_target)

# def test_dijkstra_length(self):
# # Check if the max distance to one side of the rectangle
# # equals the length of the other side
# m = 4
# n = 5
# rectangle = create_rectangular_grid(m, n)
# set_of_points = range(m) # boundary with y = 0
# distance = sg.dijkstra_length(rectangle, set_of_points)
# self.assertTrue(np.abs(np.max(distance) - (n - 1) < TOL))
#
# def test_gdist_length(self):
# # Check if the max distance to one side of the rectangle
# # equals the length of the other side
# m = 4
# n = 5
# rectangle = create_rectangular_grid(m, n)
# set_of_points = range(m) # boundary with y = 0
# distance = sg.gdist_length(rectangle, set_of_points)
# self.assertTrue(np.abs(np.max(distance) - (n - 1) < TOL))

def test_gdist_length(self):
# Check if the max distance to one side of the rectangle
# equals the length of the other side
m = 4
n = 5
rectangle = create_rectangular_grid(m, n)
set_of_points = range(m) # boundary with y = 0
distance = sg.gdist_length(rectangle, set_of_points)
self.assertTrue(np.abs(np.max(distance) - (n - 1) < TOL))
"""

if __name__ == "__main__":
unittest.main()
Loading