Skip to content

Commit

Permalink
fixed bug in polyline shorten and extend not reseting the lines attri…
Browse files Browse the repository at this point in the history
…bute
  • Loading branch information
nmaslarinos committed Sep 22, 2023
1 parent d0083cd commit 05fa4e0
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 44 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Fixed bug in `compas.datastructures.Mesh.insert_vertex`.
* Fixed bug in `compas.geometry.angle_vectors_signed`.
* Changed `compas.artists.MeshArtist` default colors.
* Fixed bug in `compas.geometry.curves.Polyline` shorten and extend methods.

### Removed

Expand Down
5 changes: 3 additions & 2 deletions src/compas/geometry/curves/polyline.py
Original file line number Diff line number Diff line change
Expand Up @@ -578,10 +578,10 @@ def extend(self, length):
"""
try:
start, end = length
self.points[0] = self.points[0] + self.lines[0].vector.unitized().scaled(-start)
self[0] = self[0] + self.lines[0].vector.unitized().scaled(-start)
except TypeError:
start = end = length
self.points[-1] = self.points[-1] + self.lines[-1].vector.unitized().scaled(end)
self[-1] = self[-1] + self.lines[-1].vector.unitized().scaled(end)

def extended(self, length):
"""Returns a copy of this polyline extended by a given length.
Expand Down Expand Up @@ -643,6 +643,7 @@ def shorten(self, length):
else:
self.points[-1] = line.start + line.vector.unitized().scaled(total_length - end)
break
self._lines = None

def shortened(self, length):
"""Returns a copy of this polyline shortened by a given length.
Expand Down
58 changes: 16 additions & 42 deletions tests/compas/geometry/test_curves_polyline.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,54 +432,28 @@ def test_polyline_tangent_at_point(coords, input, expected):


@pytest.mark.parametrize(
"coords,input,expected",
"coords,input,expected,length",
[
(
[[0, 0, 0], [1, 0, 0], [2, 0, 0], [2, 2, 0]],
1.5,
[[0, 0, 0], [1, 0, 0], [2, 0, 0], [2, 3.5, 0]],
),
(
[[0, 0, 0], [1, 0, 0], [2, 0, 0], [2, 2, 0]],
-2.5,
[[0, 0, 0], [1, 0, 0], [2, 0, 0], [2, -0.5, 0]],
),
(
[[0, 0, 0], [1, 0, 0], [2, 0, 0], [2, 2, 0]],
(2, 2),
[[-2, 0, 0], [1, 0, 0], [2, 0, 0], [2, 4, 0]],
),
(
[[0, 0, 0], [1, 0, 0], [2, 0, 0], [2, 2, 0]],
(2, 0),
[[-2, 0, 0], [1, 0, 0], [2, 0, 0], [2, 2, 0]],
),
([[0, 0, 0], [1, 0, 0], [2, 0, 0], [2, 2, 0]], 1.5, [[0, 0, 0], [1, 0, 0], [2, 0, 0], [2, 3.5, 0]], 5.5),
([[0, 0, 0], [1, 0, 0], [2, 0, 0], [2, 2, 0]], -2.5, [[0, 0, 0], [1, 0, 0], [2, 0, 0], [2, -0.5, 0]], 2.5),
([[0, 0, 0], [1, 0, 0], [2, 0, 0], [2, 2, 0]], (2, 2), [[-2, 0, 0], [1, 0, 0], [2, 0, 0], [2, 4, 0]], 8),
([[0, 0, 0], [1, 0, 0], [2, 0, 0], [2, 2, 0]], (2, 0), [[-2, 0, 0], [1, 0, 0], [2, 0, 0], [2, 2, 0]], 6),
],
)
def test_polyline_extend(coords, input, expected):
assert expected == Polyline(coords).extended(input)
def test_polyline_extend(coords, input, expected, length):
polyline = Polyline(coords).extended(input)
assert expected == polyline and length == polyline.length


@pytest.mark.parametrize(
"coords,input,expected",
"coords,input,expected,length",
[
(
[[0, 0, 0], [1, 0, 0], [2, 0, 0], [2, 2, 0]],
0.5,
[[0, 0, 0], [1, 0, 0], [2, 0, 0], [2, 1.5, 0]],
),
(
[[0, 0, 0], [1, 0, 0], [2, 0, 0], [2, 2, 0]],
2,
[[0, 0, 0], [1, 0, 0], [2, 0, 0]],
),
(
[[0, 0, 0], [1, 0, 0], [2, 0, 0], [2, 2, 0]],
(0.5, 2.5),
[[0.5, 0, 0], [1, 0, 0], [1.5, 0, 0]],
),
([[0, 0, 0], [1, 0, 0], [2, 0, 0], [2, 2, 0]], (1, 2), [[1, 0, 0], [2, 0, 0]]),
([[0, 0, 0], [1, 0, 0], [2, 0, 0], [2, 2, 0]], 0.5, [[0, 0, 0], [1, 0, 0], [2, 0, 0], [2, 1.5, 0]], 3.5),
([[0, 0, 0], [1, 0, 0], [2, 0, 0], [2, 2, 0]], 2, [[0, 0, 0], [1, 0, 0], [2, 0, 0]], 2),
([[0, 0, 0], [1, 0, 0], [2, 0, 0], [2, 2, 0]], (0.5, 2.5), [[0.5, 0, 0], [1, 0, 0], [1.5, 0, 0]], 1),
([[0, 0, 0], [1, 0, 0], [2, 0, 0], [2, 2, 0]], (1, 2), [[1, 0, 0], [2, 0, 0]], 1),
],
)
def test_polyline_shortened(coords, input, expected):
assert expected == Polyline(coords).shortened(input)
def test_polyline_shortened(coords, input, expected, length):
polyline = Polyline(coords).shortened(input)
assert expected == polyline and length == polyline.length

0 comments on commit 05fa4e0

Please sign in to comment.