From 0f891a251f7b9c9fab5f2162a4770676b38c5732 Mon Sep 17 00:00:00 2001 From: je-cook <81617086+je-cook@users.noreply.github.com> Date: Fri, 26 Jul 2024 12:16:00 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Enforce=20use=20of=20ordered=20e?= =?UTF-8?q?dges=20(#3095)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 🐛 Enforce use of ordered edges * Made a test that fails on Edges but passes on OrderedEdges! * Updated docstring * Updated spelling to merge. --------- Co-authored-by: ocean --- bluemira/codes/_freecadapi.py | 4 +-- tests/codes/test_freecadapi.py | 4 +-- tests/geometry/test_geometry.py | 2 +- tests/geometry/test_tools.py | 4 +-- tests/geometry/test_wire.py | 53 ++++++++++++++++++++++++++++++++- 5 files changed, 59 insertions(+), 8 deletions(-) diff --git a/bluemira/codes/_freecadapi.py b/bluemira/codes/_freecadapi.py index e020f5caff..9d9b8b886b 100644 --- a/bluemira/codes/_freecadapi.py +++ b/bluemira/codes/_freecadapi.py @@ -769,13 +769,13 @@ def tessellate(obj: apiShape, tolerance: float) -> tuple[np.ndarray, np.ndarray] def start_point(obj: apiShape) -> np.ndarray: """The start point of the object""" - point = obj.Edges[0].firstVertex().Point + point = obj.OrderedEdges[0].firstVertex().Point return vector_to_numpy(point) def end_point(obj: apiShape) -> np.ndarray: """The end point of the object""" - point = obj.Edges[-1].lastVertex().Point + point = obj.OrderedEdges[-1].lastVertex().Point return vector_to_numpy(point) diff --git a/tests/codes/test_freecadapi.py b/tests/codes/test_freecadapi.py index fe44088f58..1b104dda67 100644 --- a/tests/codes/test_freecadapi.py +++ b/tests/codes/test_freecadapi.py @@ -135,13 +135,13 @@ def test_make_polygon(self): def test_make_bezier(self): bezier: Part.Wire = cadapi.make_bezier(self.square_points) - curve = bezier.Edges[0].Curve + curve = bezier.OrderedEdges[0].Curve assert type(curve) is Part.BezierCurve def test_interpolate_bspline(self): pntslist = self.square_points bspline: Part.Wire = cadapi.interpolate_bspline(pntslist) - curve = bspline.Edges[0].Curve + curve = bspline.OrderedEdges[0].Curve assert type(curve) is Part.BSplineCurve # assert that the bspline pass through the points # get the points parameter diff --git a/tests/geometry/test_geometry.py b/tests/geometry/test_geometry.py index 3162c8330d..f8d12c14f2 100644 --- a/tests/geometry/test_geometry.py +++ b/tests/geometry/test_geometry.py @@ -103,7 +103,7 @@ def test_make_ellipse(self): major_radius=major_radius, minor_radius=minor_radius, ) - edge = bm_ellipse.boundary[0].Edges[0] + edge = bm_ellipse.boundary[0].OrderedEdges[0] # ellispe eccentricity eccentricity = math.sqrt(1 - (minor_radius / major_radius) ** 2) diff --git a/tests/geometry/test_tools.py b/tests/geometry/test_tools.py index 5a67577847..03824407c6 100644 --- a/tests/geometry/test_tools.py +++ b/tests/geometry/test_tools.py @@ -493,7 +493,7 @@ def test_tangencies_open(self, st, et): # np.testing.assert_allclose(spline.length, expected_length) if st and et: assert spline.length > 1.0 - e = spline.shape.Edges[0] + e = spline.shape.OrderedEdges[0] np.testing.assert_allclose( e.tangentAt(e.FirstParameter), np.array(st) / norm(st) ) @@ -510,7 +510,7 @@ def test_tangencies_closed(self, st, et): points, closed=True, start_tangent=st, end_tangent=et ) if st and et: - e = spline.shape.Edges[0] + e = spline.shape.OrderedEdges[0] np.testing.assert_allclose( e.tangentAt(e.FirstParameter), np.array(st) / norm(st) ) diff --git a/tests/geometry/test_wire.py b/tests/geometry/test_wire.py index 167218fbb6..1b5b2d4acc 100644 --- a/tests/geometry/test_wire.py +++ b/tests/geometry/test_wire.py @@ -3,14 +3,23 @@ # SPDX-FileCopyrightText: 2021-present J. Morris, D. Short # # SPDX-License-Identifier: LGPL-2.1-or-later + import numpy as np import pytest +from bluemira.base.file import get_bluemira_path from bluemira.geometry.coordinates import Coordinates from bluemira.geometry.error import GeometryError -from bluemira.geometry.tools import make_bezier, make_circle, make_polygon +from bluemira.geometry.tools import ( + deserialise_shape, + make_bezier, + make_circle, + make_polygon, +) from bluemira.geometry.wire import BluemiraWire +TEST_PATH = get_bluemira_path("geometry/test_data", subfolder="tests") + class TestWire: def test_start_point_given_polygon(self): @@ -92,6 +101,48 @@ def test_3_vertices_correct(self): np.testing.assert_allclose(vertexes[:, 1], p2) np.testing.assert_allclose(vertexes[:, 2], p3) + def test_end_points(self): + """ + Test to make sure that the .end_point() is actually the end point, rather than + somewhere in the middle of the wire. + Potentially needs to be removed after the next API-breaking change where wires + are only allowed to be joined when they're oriented correctly. + """ + # Toughest case + rightmost_point = [10.538050403959396, 0.0, -5.974513810642086] + straight_line_end = { + "LineSegment": { + "StartPoint": [9.598817369491535, 0.0, -5.72284707755179], + "EndPoint": rightmost_point, + } + } + arc_start = { + "ArcOfCircle": { + "Radius": 0.8508213264708893, + "Center": [9.558782242701866, 0.0, -6.572725961982168], + "Axis": [-0.0, 1.0, -0.0], + "StartAngle": 180.0, + "EndAngle": 272.69703056741383, + "StartPoint": [8.707960916230977, 0.0, -6.572725961982168], + "EndPoint": [9.598817369491535, 0.0, -5.722847077551793], + } + } + + def wrap_as_BMWdict(cadapi_item, label=""): + return { + "BluemiraWire": {"label": label, "boundary": [{"Wire": [cadapi_item]}]} + } + + w1 = deserialise_shape(wrap_as_BMWdict(straight_line_end)) + w2 = deserialise_shape(wrap_as_BMWdict(arc_start)) + wrong_wire = BluemiraWire([ + w1, + w2, + ]) # we expect this line to potentially throw an error in future versions?? + right_wire = BluemiraWire([w2, w1]) + np.testing.assert_allclose(wrong_wire.end_point(), Coordinates(rightmost_point)) + np.testing.assert_allclose(right_wire.end_point(), Coordinates(rightmost_point)) + class ValueParameterBase: @classmethod