def test_best_fit(points, sphere_expected): points = Points(points) sphere_fit = Sphere.best_fit(points) assert sphere_fit.point.is_close(sphere_expected.point, abs_tol=1e-9) assert math.isclose(sphere_fit.radius, sphere_expected.radius)
def spheres(draw): """ Return a strategy which generates Circle objects. Returns ------- LazyStrategy Hypothesis strategy. Examples -------- >>> from hypothesis import find >>> from tests.property.strategies import spheres >>> sphere = find(spheres(), lambda x: x.radius >= 1) >>> round(sphere.radius) 1 """ return Sphere(draw(arrays_fixed(3)), draw(radii))
(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) @pytest.mark.parametrize( "sphere, point, point_expected", [ (Sphere([0, 0, 0], 1), [1, 0, 0], [1, 0, 0]), (Sphere([0, 0, 0], 2), [1, 0, 0], [2, 0, 0]), (Sphere([0, 0, 0], 0.1), [1, 0, 0], [0.1, 0, 0]), (Sphere([-1, 0, 0], 1), [1, 0, 0], [0, 0, 0]), (Sphere([0, 0, 0], 1), [1, 1, 1], math.sqrt(3) / 3 * np.ones(3)), (Sphere([0, 0, 0], 3), [1, 1, 1], math.sqrt(3) * np.ones(3)), ], ) def test_project_point_sphere(sphere, point, point_expected): point_projected = sphere.project_point(point) assert point_projected.is_close(point_expected) @pytest.mark.parametrize( "circle_or_sphere, point",
""" Sphere-Line Intersection ======================== """ from skspatial.objects import Sphere, Line from skspatial.plotting import plot_3d sphere = Sphere([0, 0, 0], 1) line = Line([0, 0, 0], [1, 1, 1]) point_a, point_b = sphere.intersect_line(line) plot_3d( line.plotter(t_1=-1, c='k'), sphere.plotter(alpha=0.2), point_a.plotter(c='r', s=100), point_b.plotter(c='r', s=100), )
(Circle([0, 0], 1), Line([0, -2], [1, 0])), (Circle([0, 0], 1), Line([2, 0], [0, 1])), (Circle([0, 0], 1), Line([3, 0], [1, 1])), (Circle([1.5, 0], 1), Line([0, 0], [1, 0])), ], ) def test_intersect_circle_line_failure(circle, line): with pytest.raises(Exception): circle.intersect_line(line) @pytest.mark.parametrize( "sphere, line, point_a_expected, point_b_expected", [ (Sphere([0, 0, 0], 1), Line([0, 0, 0], [1, 0, 0]), [-1, 0, 0 ], [1, 0, 0]), ( Sphere([0, 0, 0], 1), Line([0, 0, 0], [1, 1, 0]), -math.sqrt(2) / 2 * np.array([1, 1, 0]), math.sqrt(2) / 2 * np.array([1, 1, 0]), ), ( Sphere([0, 0, 0], 1), Line([0, 0, 0], [1, 1, 1]), -math.sqrt(3) / 3 * np.ones(3), math.sqrt(3) / 3 * np.ones(3), ), (Sphere([1, 0, 0], 1), Line([0, 0, 0], [1, 0, 0]), [0, 0, 0 ], [2, 0, 0]),
@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
[0, 1, 1]]), (Plane([0, 0, 0], [1, 1, 0]), [[-1, 1, -1], [1, -1, -1], [-1, 1, 1], [1, -1, 1]]), ], ) def test_plane_points(plane, points_expected): points = plane.to_points() assert points.is_close(points_expected) @pytest.mark.parametrize( "sphere, n_angles, points_expected", [ (Sphere([0, 0, 0], 1), 1, [[0, 0, 1]]), (Sphere([0, 0, 0], 1), 2, [[0, 0, -1], [0, 0, 1]]), (Sphere([0, 0, 0], 1), 3, [[0, -1, 0], [0, 0, -1], [0, 0, 1], [0, 1, 0]]), (Sphere([0, 0, 0], 2), 3, [[0, -2, 0], [0, 0, -2], [0, 0, 2], [0, 2, 0]]), (Sphere([1, 0, 0], 1), 3, [[1, -1, 0], [1, 0, -1], [1, 0, 1], [1, 1, 0]]), (Sphere([1, 1, 1], 1), 3, [[1, 0, 1], [1, 1, 0], [1, 1, 2], [1, 2, 1] ]), ], ) def test_sphere_points(sphere, n_angles, points_expected): array_rounded = sphere.to_points(n_angles).round(3) points_unique = Points(array_rounded).unique()
(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 @pytest.mark.parametrize( "sphere, point, bool_expected", [ (Sphere([0, 0, 0], 1), [1, 0, 0], True), (Sphere([0, 0, 0], 1), [0, 1, 0], True), (Sphere([0, 0, 0], 1), [0, 0, 1], True), (Sphere([0, 0, 0], 1), [-1, 0, 0], True), (Sphere([0, 0, 0], 1), [0, -1, 0], True), (Sphere([0, 0, 0], 1), [0, 0, -1], True), (Sphere([0, 0, 0], 1), [1, 1, 0], False), (Sphere([1, 0, 0], 1), [1, 0, 0], False), (Sphere([1, 0, 0], 1), [2, 0, 0], True), (Sphere([0, 0, 0], 2), [0, 2, 0], True), (Sphere([0, 0, 0], math.sqrt(3)), [1, 1, 1], True), ], ) def test_sphere_contains_point(sphere, point, bool_expected): assert sphere.contains_point(point) == bool_expected
def test_surface_area_volume(radius, surface_area_expected, volume_expected): sphere = Sphere([0, 0, 0], radius) assert math.isclose(sphere.surface_area(), surface_area_expected) assert math.isclose(sphere.volume(), volume_expected)
def test_failure(point, radius, message_expected): with pytest.raises(ValueError, match=message_expected): Sphere(point, radius)
def test_best_fit_failure(points, message_expected): with pytest.raises(ValueError, match=message_expected): Sphere.best_fit(points)
(4.5, 81 * np.pi, 121.5 * np.pi), (10, 400 * np.pi, 4000 / 3 * np.pi), ], ) def test_surface_area_volume(radius, surface_area_expected, volume_expected): sphere = Sphere([0, 0, 0], radius) assert math.isclose(sphere.surface_area(), surface_area_expected) assert math.isclose(sphere.volume(), volume_expected) @pytest.mark.parametrize( ("sphere", "point", "dist_expected"), [ (Sphere([0, 0, 0], 1), [0, 0, 0], 1), (Sphere([0, 0, 0], 1), [1, 0, 0], 0), (Sphere([0, 0, 0], 1), [0, -1, 0], 0), (Sphere([0, 0, 0], 2), [0, 0, 0], 2), (Sphere([0, 0, 0], 1), [1, 1, 1], math.sqrt(3) - 1), (Sphere([0, 0, 0], 2), [1, 1, 1], 2 - math.sqrt(3)), (Sphere([1, 0, 0], 2), [0, 0, 0], 1), ], ) def test_distance_point(sphere, point, dist_expected): assert math.isclose(sphere.distance_point(point), dist_expected) @pytest.mark.parametrize( ("sphere", "point", "bool_expected"),
def test_failure(point, radius): with pytest.raises(Exception): Sphere(point, radius)
(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) @pytest.mark.parametrize( "sphere, point, dist_expected", [ (Sphere([0, 0, 0], 1), [0, 0, 0], 1), (Sphere([0, 0, 0], 1), [1, 0, 0], 0), (Sphere([0, 0, 0], 1), [0, -1, 0], 0), (Sphere([0, 0, 0], 2), [0, 0, 0], 2), (Sphere([0, 0, 0], 1), [1, 1, 1], math.sqrt(3) - 1), (Sphere([0, 0, 0], 2), [1, 1, 1], 2 - math.sqrt(3)), (Sphere([1, 0, 0], 2), [0, 0, 0], 1), ], ) def test_distance_plane_point(sphere, point, dist_expected): assert math.isclose(sphere.distance_point(point), dist_expected) @pytest.mark.parametrize( "array_a, array_b, array_c, area_expected",