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

Commit

Permalink
Polyline: Add convenience attrs segment and segment_vectors (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmelnikow authored Jan 31, 2019
1 parent 9116f49 commit 26255b8
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
15 changes: 15 additions & 0 deletions blmath/geometry/primitives/polyline.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ def e(self):
'''
return self.__dict__['e']

@property
def segments(self):
'''
Coordinate pairs for each segment.
'''
return self.v[self.e]

@property
def segment_lengths(self):
'''
Expand All @@ -129,6 +136,14 @@ def total_length(self):
'''
return np.sum(self.segment_lengths)

@property
def segment_vectors(self):
'''
Vectors spanning each segment.
'''
segments = self.segments
return segments[:, 1] - segments[:, 0]

def flip(self):
'''
Flip the polyline from end to end.
Expand Down
34 changes: 34 additions & 0 deletions blmath/geometry/primitives/test_polyline.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,40 @@ def test_edges(self):

np.testing.assert_array_equal(Polyline(v, closed=True).e, expected_closed)

def test_segments(self):
polyline = Polyline(np.array([
[0., 0., 0.],
[1., 0., 0.],
[1., 1., 0.],
[1., 2., 0.],
]), closed=True)

expected = np.array([
[[0., 0., 0.], [1., 0., 0.]],
[[1., 0., 0.], [1., 1., 0.]],
[[1., 1., 0.], [1., 2., 0.]],
[[1., 2., 0.], [0., 0., 0.]],
])

np.testing.assert_array_equal(polyline.segments, expected)

def test_segment_vectors(self):
polyline = Polyline(np.array([
[0., 0., 0.],
[1., 0., 0.],
[1., 1., 0.],
[1., 2., 0.],
]), closed=True)

expected = np.array([
[1., 0., 0.],
[0., 1., 0.],
[0., 1., 0.],
[-1., -2., 0.],
])

np.testing.assert_array_equal(polyline.segment_vectors, expected)

def test_length_of_empty_polyline(self):
polyline = Polyline(None)
self.assertEqual(polyline.total_length, 0)
Expand Down

0 comments on commit 26255b8

Please sign in to comment.