Skip to content
This repository has been archived by the owner on Feb 3, 2023. It is now read-only.

Commit

Permalink
Polyline: Add reindex() method (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmelnikow authored Mar 22, 2019
1 parent 36765de commit c35c0af
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
25 changes: 25 additions & 0 deletions blmath/geometry/primitives/polyline.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,31 @@ def flip(self):
'''
self.v = np.flipud(self.v)

def reindexed(self, index, ret_edge_mapping=False):
'''
Return a new Polyline which reindexes the callee polyline, which much
be closed, so the vertex with the given index becomes vertex 0.
ret_edge_mapping: if True, return an array that maps from old edge
indices to new.
'''
if not self.closed:
raise ValueError("Can't rotate an open polyline")

rotated_v = np.append(
self.v[index:], self.v[0:index], axis=0
)
result = Polyline(v=rotated_v, closed=True)
if ret_edge_mapping:
edge_mapping = np.append(
np.arange(index, len(self.v)),
np.arange(0, index),
axis=0
)
return result, edge_mapping
else:
return result

def partition_by_length(self, max_length, ret_indices=False):
'''
Subdivide each line segment longer than max_length with
Expand Down
24 changes: 24 additions & 0 deletions blmath/geometry/primitives/test_polyline.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,3 +271,27 @@ def test_flip(self):
original.flip()

np.testing.assert_array_almost_equal(original.v, expected.v)

def test_reindexed(self):
original = Polyline(np.array([
[0., 0., 0.],
[1., 0., 0.],
[1., 1., 0.],
[1., 7., 0.],
[1., 8., 0.],
[0., 8., 0.],
]), closed=True)

reindexed, edge_mapping = original.reindexed(5, ret_edge_mapping=True)

expected = Polyline(np.array([
[0., 8., 0.],
[0., 0., 0.],
[1., 0., 0.],
[1., 1., 0.],
[1., 7., 0.],
[1., 8., 0.],
]), closed=True)

np.testing.assert_array_almost_equal(reindexed.v, expected.v)
np.testing.assert_array_equal(original.segments[edge_mapping], reindexed.segments)

0 comments on commit c35c0af

Please sign in to comment.