def test_center(): """Test getting and setting the center.""" circle = Circle(1) assert all(circle.center == (0, 0, 0)) center = (1, 1, 1) circle.center = center assert all(circle.center == center)
def test_moment_inertia(r, center): circle = Circle(r) assert np.all(np.asarray(circle.planar_moments_inertia) >= 0) circle.center = center area = np.pi * r ** 2 expected = [np.pi / 4 * r ** 4] * 3 expected[0] += area * center[0] ** 2 expected[1] += area * center[1] ** 2 expected[2] = area * center[0] * center[1] np.testing.assert_allclose(circle.planar_moments_inertia[:2], expected[:2]) np.testing.assert_allclose(circle.planar_moments_inertia[2], expected[2]) assert circle.polar_moment_inertia == pytest.approx(sum(expected[:2]))
def test_perimeter_setter(perimeter): """Test setting the perimeter.""" circle = Circle(1) circle.perimeter = perimeter assert circle.radius == perimeter / (2 * np.pi) assert circle.perimeter == approx(perimeter) circle.perimeter = perimeter + 1 assert circle.radius == (perimeter + 1) / (2 * np.pi) assert circle.perimeter == approx(perimeter + 1) circle.circumference = perimeter assert circle.radius == perimeter / (2 * np.pi) assert circle.circumference == approx(perimeter) circle.circumference = perimeter + 1 assert circle.radius == (perimeter + 1) / (2 * np.pi) assert circle.circumference == approx(perimeter + 1)
def test_repr(): circle = Circle(1, [1, 2, 0]) assert str(circle), str(eval(repr(circle)))
def test_inertia_tensor(): """Test the inertia tensor calculation.""" circle = Circle(1) circle.center = (0, 0, 0) assert np.sum(circle.inertia_tensor > 1e-6) == 1 assert circle.inertia_tensor[2, 2] == np.pi / 2
def test_perimeter(r): circle = Circle(1) circle.radius = r assert circle.perimeter == 2 * np.pi * r assert circle.circumference == 2 * np.pi * r
def test_minimal_bounding_circle(a, b, center): ellipse = Ellipse(a, b, center) bounding_circle = ellipse.minimal_bounding_circle assert sphere_isclose(bounding_circle, Circle(max(a, b), center))
def test_iq(r): circle = Circle(r) assert circle.iq == 1
def test_minimal_centered_bounding_circle(r, center): circ = Circle(r, center) assert sphere_isclose(circ.minimal_centered_bounding_circle, circ)
def test_distance_to_surface(r): """Test calculating the shape distance.""" theta = np.linspace(0, 2 * np.pi, 10000) circle = Circle(r) distance = circle.distance_to_surface(theta) assert_distance_to_surface_2d(circle, theta, distance)
def test_invalid_radius_setter(): circle = Circle(1) with pytest.raises(ValueError): circle.radius = -1
def test_invalid_radius(): with pytest.raises(ValueError): Circle(-1)
def test_radius_getter_setter(r): """Test getting and setting the radius.""" circle = Circle(r) assert circle.radius == r circle.radius = r + 1 assert circle.radius == r + 1
def test_maximal_centered_bounded_circle(a, b, center): ellipse = Ellipse(a, b, center) bounded_circle = ellipse.maximal_centered_bounded_circle assert sphere_isclose(bounded_circle, Circle(min(a, b), center))
def test_area(r): circle = Circle(1) circle.radius = r assert circle.area == np.pi * r ** 2
def test_set_area(area): """Test setting the area.""" circle = Circle(1) circle.area = area assert circle.area == approx(area) assert circle.radius == approx((area / np.pi) ** 0.5)
def test_get_set_minimal_centered_bounding_circle_radius(r, center): _test_get_set_minimal_bounding_sphere_radius(Circle(r, center), True)
def test_eccentricity(r): circle = Circle(r) assert circle.eccentricity == 0
def test_maximal_centered_bounded_circle(r, center): circ = Circle(r, center) assert sphere_isclose(circ.maximal_centered_bounded_circle, circ)
def test_is_inside(x, center): circle = Circle(1, center) assert circle.is_inside([x, 0, 0] + center).squeeze() == (x <= 1)
def test_minimal_centered_bounding_circle(poly): assert sphere_isclose( poly.minimal_centered_bounding_circle, Circle(np.linalg.norm(poly.vertices, axis=-1).max()), )