def test_best_fit(points, circle_expected): points = Points(points) circle_fit = Circle.best_fit(points) assert circle_fit.point.is_close(circle_expected.point, abs_tol=1e-9) assert math.isclose(circle_fit.radius, circle_expected.radius)
def circles(draw): """ Return a strategy which generates circles. Returns ------- LazyStrategy Hypothesis strategy. Examples -------- >>> from hypothesis import find >>> from tests.property.strategies import circles >>> circle = find(circles(), lambda x: x.radius >= 1) >>> round(circle.radius) 1 """ return Circle(draw(arrays_fixed(2)), draw(radii))
(Plane([0, 0, 0], [0, 0, 1]), [1, 1, 0], [1, 1, 0]), (Plane([0, 0, 0], [0, 0, 1]), [1, 1, 1], [1, 1, 0]), (Plane([0, 0, 0], [0, 0, 1]), [7, -5, 20], [7, -5, 0]), (Plane([0, 0, 0], [0, 0, -10]), [7, -5, 20], [7, -5, 0]), ], ) def test_project_vector_plane(plane, vector, vector_expected): vector_projected = plane.project_vector(vector) assert vector_projected.is_close(vector_expected) @pytest.mark.parametrize( "circle, point, point_expected", [ (Circle([0, 0], 1), [1, 0], [1, 0]), (Circle([0, 0], 1), [2, 0], [1, 0]), (Circle([0, 0], 1), [-2, 0], [-1, 0]), (Circle([0, 0], 1), [0, 2], [0, 1]), (Circle([0, 0], 1), [0, -2], [0, -1]), (Circle([0, 0], 5), [0, -2], [0, -5]), (Circle([0, 1], 5), [0, -2], [0, -4]), (Circle([0, 0], 1), [1, 1], math.sqrt(2) / 2 * np.ones(2)), (Circle([0, 0], 2), [1, 1], math.sqrt(2) * np.ones(2)), ], ) def test_project_point_circle(circle, point, point_expected): point_projected = circle.project_point(point) assert point_projected.is_close(point_expected)
(Plane([0, 0, 0], [1, 0, 0]), Plane([0, 0, 0], [1, 0, 0])), (Plane([1, 0, 0], [1, 0, 0]), Plane([0, 0, 0], [1, 0, 0])), (Plane([0, 0, 5], [0, 0, 1]), Plane([4, 2, 4], [0, 0, 3])), (Plane([0, 0, -5], [0, 0, 1]), Plane([4, 2, 4], [0, 0, 3])), ], ) def test_intersect_planes_failure(plane_a, plane_b): with pytest.raises(Exception): plane_a.intersect_plane(plane_b) @pytest.mark.parametrize( "circle, line, point_a_expected, point_b_expected", [ (Circle([0, 0], 1), Line([0, 0], [1, 0]), [-1, 0], [1, 0]), (Circle([0, 0], 1), Line([0, 0], [0, 1]), [0, -1], [0, 1]), (Circle([0, 0], 1), Line([0, 1], [1, 0]), [0, 1], [0, 1]), (Circle([0, 0], 1), Line([0, 0.5], [1, 0]), [-math.sqrt(3) / 2, 0.5], [math.sqrt(3) / 2, 0.5]), (Circle([1, 0], 1), Line([0, 0], [1, 0]), [0, 0], [2, 0]), ], ) def test_intersect_circle_line(circle, line, point_a_expected, point_b_expected): point_a, point_b = circle.intersect_line(line) assert point_a.is_close(point_a_expected) assert point_b.is_close(point_b_expected)
@pytest.mark.parametrize( ("obj_spatial", "repr_expected"), [ (Point([0]), "Point([0])"), (Point([0, 0]), "Point([0, 0])"), (Point([0.5, 0]), "Point([0.5, 0. ])"), (Point([-11, 0]), "Point([-11, 0])"), (Vector([-11, 0]), "Vector([-11, 0])"), (Vector([-11.0, 0.0]), "Vector([-11., 0.])"), (Vector([0, 0]), "Vector([0, 0])"), (Vector([0.5, 0]), "Vector([0.5, 0. ])"), (Points([[1.5, 2], [5, 3]]), "Points([[1.5, 2. ],\n [5. , 3. ]])"), (Line([0, 0], [1, 0]), "Line(point=Point([0, 0]), direction=Vector([1, 0]))"), (Line([-1, 2, 3], [5, 4, 2]), "Line(point=Point([-1, 2, 3]), direction=Vector([5, 4, 2]))"), (Line(np.zeros(2), [1, 0]), "Line(point=Point([0., 0.]), direction=Vector([1, 0]))"), (Plane([0, 0], [1, 0]), "Plane(point=Point([0, 0]), normal=Vector([1, 0]))"), (Plane([-1, 2, 3], [5, 4, 2]), "Plane(point=Point([-1, 2, 3]), normal=Vector([5, 4, 2]))"), (Circle([0, 0], 1), "Circle(point=Point([0, 0]), radius=1)"), (Circle([0, 0], 2.5), "Circle(point=Point([0, 0]), radius=2.5)"), (Sphere([0, 0, 0], 1), "Sphere(point=Point([0, 0, 0]), radius=1)"), ( Triangle([0, 0], [0, 1], [1, 0]), "Triangle(point_a=Point([0, 0]), point_b=Point([0, 1]), point_c=Point([1, 0]))", ), (Cylinder([0, 0, 0], [0, 0, 1], 1), "Cylinder(point=Point([0, 0, 0]), vector=Vector([0, 0, 1]), radius=1)"), ], ) def test_repr(obj_spatial, repr_expected): assert repr(obj_spatial) == repr_expected
) def test_is_coplanar(line_a, line_b, bool_expected): """Test checking if two lines are coplanar.""" if bool_expected is None: with pytest.raises(TypeError, match="The input must also be a line."): line_a.is_coplanar(line_b) else: assert line_a.is_coplanar(line_b) == bool_expected @pytest.mark.parametrize( "circle, point, bool_expected", [ (Circle([0, 0], 1), [1, 0], True), (Circle([0, 0], 1), [0, 1], True), (Circle([0, 0], 1), [-1, 0], True), (Circle([0, 0], 1), [0, -1], True), (Circle([0, 0], 1), [0, 0], False), (Circle([0, 0], 1), [1, 1], False), (Circle([0, 0], 2), [1, 0], False), (Circle([1, 0], 1), [1, 0], False), (Circle([0, 0], math.sqrt(2)), [1, 1], True), ], ) def test_circle_contains_point(circle, point, bool_expected): assert circle.contains_point(point) == bool_expected
def test_circumference_area(radius, circumference_expected, area_expected): circle = Circle([0, 0], radius) assert math.isclose(circle.circumference(), circumference_expected) assert math.isclose(circle.area(), area_expected)
def test_failure(point, radius): with pytest.raises(Exception): Circle(point, radius)
""" Circle-Line Intersection ======================== """ from skspatial.objects import Circle, Line from skspatial.plotting import plot_2d circle = Circle([0, 0], 5) line = Line([0, 0], [1, 1]) point_a, point_b = circle.intersect_line(line) _, ax = plot_2d( circle.plotter(fill=False), line.plotter(t_1=-5, t_2=5, c='k'), point_a.plotter(c='r', s=100, edgecolor='k', zorder=3), point_b.plotter(c='r', s=100, edgecolor='k', zorder=3), ) ax.axis('equal')
def test_failure(point, radius, message_expected): with pytest.raises(ValueError, match=message_expected): Circle(point, radius)
def test_best_fit_failure(points, message_expected): with pytest.raises(ValueError, match=message_expected): Circle.best_fit(points)
(4.5, 9 * np.pi, 20.25 * np.pi), (10, 20 * np.pi, 100 * np.pi), ], ) def test_circumference_area(radius, circumference_expected, area_expected): circle = Circle([0, 0], radius) assert math.isclose(circle.circumference(), circumference_expected) assert math.isclose(circle.area(), area_expected) @pytest.mark.parametrize( ("circle", "point", "dist_expected"), [ (Circle([0, 0], 1), [0, 0], 1), (Circle([0, 0], 1), [0.5, 0], 0.5), (Circle([0, 0], 1), [1, 0], 0), (Circle([0, 0], 1), [0, 1], 0), (Circle([0, 0], 1), [-1, 0], 0), (Circle([0, 0], 1), [0, -1], 0), (Circle([0, 0], 1), [2, 0], 1), (Circle([0, 0], 1), [1, 1], math.sqrt(2) - 1), (Circle([1, 1], 1), [0, 0], math.sqrt(2) - 1), (Circle([0, 0], 2), [0, 5], 3), ], ) def test_distance_point(circle, point, dist_expected): assert math.isclose(circle.distance_point(point), dist_expected)
(Line([0, 0], [1, 0]), Line([0, 0], [1, 0]), 0), (Line([24, 0], [0, 1]), Line([3, 0], [0, -5]), 21), (Line([0, 0], [1, 1]), Line([1, 0], [1, 1]), math.sqrt(2) / 2), # The lines are skew. (Line([0, 0, 0], [0, 1, 0]), Line([1, 0, 0], [0, -4, 13]), 1), ], ) def test_distance_lines(line_a, line_b, dist_expected): assert math.isclose(line_a.distance_line(line_b), dist_expected) @pytest.mark.parametrize( "circle, point, dist_expected", [ (Circle([0, 0], 1), [0, 0], 1), (Circle([0, 0], 1), [0.5, 0], 0.5), (Circle([0, 0], 1), [1, 0], 0), (Circle([0, 0], 1), [0, 1], 0), (Circle([0, 0], 1), [-1, 0], 0), (Circle([0, 0], 1), [0, -1], 0), (Circle([0, 0], 1), [2, 0], 1), (Circle([0, 0], 1), [1, 1], math.sqrt(2) - 1), (Circle([1, 1], 1), [0, 0], math.sqrt(2) - 1), (Circle([0, 0], 2), [0, 5], 3), ], ) def test_distance_circle_point(circle, point, dist_expected): assert math.isclose(circle.distance_point(point), dist_expected)