diff --git a/CHANGELOG.md b/CHANGELOG.md index 29b90f26a54..4b2afb55bca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/compas/geometry/curves/polyline.py b/src/compas/geometry/curves/polyline.py index dee9f9a19d2..9daf1e8e59d 100644 --- a/src/compas/geometry/curves/polyline.py +++ b/src/compas/geometry/curves/polyline.py @@ -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. @@ -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. diff --git a/tests/compas/geometry/test_curves_polyline.py b/tests/compas/geometry/test_curves_polyline.py index 8747759e31e..086d34a2fed 100644 --- a/tests/compas/geometry/test_curves_polyline.py +++ b/tests/compas/geometry/test_curves_polyline.py @@ -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