-
Notifications
You must be signed in to change notification settings - Fork 43
fix (specklepy): update polyline class to use closed flag #394
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: v3-dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
from specklepy.objects.base import Base | ||
from specklepy.objects.geometry.point import Point | ||
from specklepy.objects.interfaces import ICurve, IHasUnits | ||
from specklepy.objects.models.units import Units | ||
|
||
|
||
@dataclass(kw_only=True) | ||
|
@@ -13,24 +14,28 @@ class Polyline(Base, IHasUnits, ICurve, speckle_type="Objects.Geometry.Polyline" | |
""" | ||
|
||
value: List[float] | ||
closed: bool = False | ||
|
||
def __repr__(self) -> str: | ||
return f"{self.__class__.__name__}(value: {self.value}, units: {self.units})" | ||
return ( | ||
f"{self.__class__.__name__}(" | ||
f"value: {self.value}, " | ||
f"closed: {self.closed}, " | ||
f"units: {self.units})" | ||
) | ||
|
||
def is_closed(self, tolerance: float = 1e-6) -> bool: | ||
@staticmethod | ||
def is_closed(points: List[float], tolerance: float = 1e-6) -> bool: | ||
""" | ||
check if the polyline is closed (start point equals end point within tolerance) | ||
check if the polyline is closed | ||
""" | ||
if len(self.value) < 6: # need at least 2 points to be closed | ||
if len(points) < 6: # need at least 2 points to be closed | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. at least 3 points for non-overlapping start-end coordinates to make a closed shape, and at least 4 points for start-end coordinates to possibly overlap |
||
return False | ||
|
||
# compare first and last points | ||
start = Point( | ||
x=self.value[0], y=self.value[1], z=self.value[2], units=self.units | ||
) | ||
end = Point( | ||
x=self.value[-3], y=self.value[-2], z=self.value[-1], units=self.units | ||
) | ||
start = Point(x=points[0], y=points[1], z=points[2], units=Units.m) | ||
end = Point(x=points[-3], y=points[-2], z=points[-1], units=Units.m) | ||
|
||
return start.distance_to(end) <= tolerance | ||
|
||
@property | ||
|
@@ -46,7 +51,7 @@ def calculate_length(self) -> float: | |
total_length = 0.0 | ||
for i in range(len(points) - 1): | ||
total_length += points[i].distance_to(points[i + 1]) | ||
if self.is_closed() and points: | ||
if self.closed and points: | ||
total_length += points[-1].distance_to(points[0]) | ||
return total_length | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,6 +54,7 @@ def test_polyline_creation(open_square_coords): | |
polyline = Polyline(value=open_square_coords, units=Units.m) | ||
assert polyline.value == open_square_coords | ||
assert polyline.units == Units.m.value | ||
assert polyline.closed is False | ||
|
||
|
||
def test_polyline_domain(sample_polyline): | ||
|
@@ -63,23 +64,31 @@ def test_polyline_domain(sample_polyline): | |
|
||
|
||
def test_polyline_is_closed(open_square_coords, closed_square_coords): | ||
open_poly = Polyline(value=open_square_coords, units=Units.m) | ||
closed_poly = Polyline(value=closed_square_coords, units=Units.m) | ||
# Test the static method | ||
assert not Polyline.is_closed(open_square_coords) | ||
assert Polyline.is_closed(closed_square_coords) | ||
|
||
assert not open_poly.is_closed() | ||
assert closed_poly.is_closed() | ||
# Test with closed flag | ||
open_poly = Polyline(value=open_square_coords, units=Units.m) | ||
closed_poly = Polyline(value=closed_square_coords, units=Units.m, closed=True) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. technically doesn't make difference for this test, but the "value" should be =open_square_coords, to create (what we expect to be) a valid speckle polyline (following C# class) This applies to all usages of "closed_square_coords" in the tests. It should only be used to test the static .is_closed method, but not to construct a Polyline |
||
assert not open_poly.closed | ||
assert closed_poly.closed | ||
|
||
|
||
def test_polyline_is_closed_with_tolerance(open_square_coords): | ||
almost_closed = open_square_coords + [ | ||
0.0, | ||
0.0, | ||
0.001, | ||
] # last point slightly above start | ||
poly = Polyline(value=almost_closed, units=Units.m) | ||
0.001, # last point slightly above start | ||
] | ||
# Test static method with tolerance | ||
assert not Polyline.is_closed(almost_closed, tolerance=1e-6) | ||
assert Polyline.is_closed(almost_closed, tolerance=0.01) | ||
|
||
assert not poly.is_closed(tolerance=1e-6) | ||
assert poly.is_closed(tolerance=0.01) | ||
# Also test with instance | ||
poly = Polyline(value=almost_closed, units=Units.m) | ||
# poly.closed should reflect what was passed in construction, not computed | ||
assert not poly.closed | ||
|
||
|
||
def test_polyline_length_open(sample_polyline): | ||
|
@@ -88,14 +97,13 @@ def test_polyline_length_open(sample_polyline): | |
|
||
|
||
def test_polyline_length_closed(closed_square_coords): | ||
polyline = Polyline(value=closed_square_coords, units=Units.m) | ||
polyline = Polyline(value=closed_square_coords, units=Units.m, closed=True) | ||
polyline.length = polyline.calculate_length() | ||
assert polyline.length == 4.0 | ||
|
||
|
||
def test_polyline_get_points(sample_polyline): | ||
points = sample_polyline.get_points() | ||
|
||
assert len(points) == 4 | ||
assert all(isinstance(p, Point) for p in points) | ||
assert all(p.units == Units.m.value for p in points) | ||
|
@@ -125,16 +133,25 @@ def test_polyline_invalid_coordinates(): | |
def test_polyline_units(open_square_coords): | ||
polyline = Polyline(value=open_square_coords, units=Units.m) | ||
assert polyline.units == Units.m.value | ||
|
||
polyline.units = "mm" | ||
assert polyline.units == "mm" | ||
|
||
|
||
def test_polyline_closed_flag(open_square_coords, closed_square_coords): | ||
# Test default value | ||
poly1 = Polyline(value=open_square_coords, units=Units.m) | ||
assert poly1.closed is False | ||
|
||
# Test explicit value | ||
poly2 = Polyline(value=closed_square_coords, units=Units.m, closed=True) | ||
assert poly2.closed is True | ||
|
||
|
||
def test_polyline_serialization(sample_polyline): | ||
serialized = serialize(sample_polyline) | ||
deserialized = deserialize(serialized) | ||
|
||
assert deserialized.value == sample_polyline.value | ||
assert deserialized.units == sample_polyline.units | ||
assert deserialized.domain.start == sample_polyline.domain.start | ||
assert deserialized.domain.end == sample_polyline.domain.end | ||
assert deserialized.closed == sample_polyline.closed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can add a comment here from C# to make it clear how "closed" Polyline should look like: "/// If true, do not add the last point to the value list. Polyline first and last points should be unique."