def test_rotation_signed_area(random_quat): """Ensure that rotating does not change the signed area.""" assume(not np.all(random_quat == 0)) random_quat = rowan.normalize(random_quat) rotated_points = rowan.rotate(random_quat, get_square_points()) poly = Polygon(rotated_points) assert np.isclose(poly.signed_area, 1) poly.reorder_verts(clockwise=True) assert np.isclose(poly.signed_area, -1)
def test_area(square_points): """Test area calculation.""" # Shift to ensure that the negative areas are subtracted as needed. points = np.asarray(square_points) + 2 square = Polygon(points) assert square.signed_area == 1 assert square.area == 1 # Ensure that area is signed. square.reorder_verts(True) assert square.signed_area == -1 assert square.area == 1
def test_bounding_circle_radius_random_hull_rotation(points, rotation): """Test that rotating vertices does not change the bounding radius.""" assume(not np.all(rotation == 0)) hull = ConvexHull(points) poly = Polygon(points[hull.vertices]) rotation = rowan.normalize(rotation) rotated_vertices = rowan.rotate(rotation, poly.vertices) poly_rotated = Polygon(rotated_vertices) circle = poly.bounding_circle rotated_circle = poly_rotated.bounding_circle assert np.isclose(circle.radius, rotated_circle.radius)
def test_bounding_circle_radius_random_hull(points): hull = ConvexHull(points) poly = Polygon(points[hull.vertices]) # For an arbitrary convex polygon, the furthest vertex from the origin is # an upper bound on the bounding sphere radius, but need not be the radius # because the ball need not be centered at the centroid. rmax = np.max(np.linalg.norm(poly.vertices, axis=-1)) circle = poly.bounding_circle assert circle.radius <= rmax + 1e-6 poly.center = [0, 0, 0] circle = poly.bounding_circle assert circle.radius <= rmax + 1e-6
def test_form_factor(square): """Validate the form factor of a polygon. At the moment this is primarily a regression test, and should be expanded for more rigorous validation. """ square.center = (0, 0, 0) ks = np.array( [ [0, 0, 0], [1, 0, 0], [-1, 0, 0], [2, 0, 0], [0, 1, 0], [0, 0, 1], [1, 2, 3], [-2, 4, -5.2], ], dtype=np.float, ) ampl = [ 1.0, 0.95885108, 0.95885108, 0.84147098, 0.95885108, 1.0, 0.80684536, 0.3825737, ] np.testing.assert_allclose(square.compute_form_factor_amplitude(ks), ampl) # Form factor should be invariant to shifts along the normal. square.center = [0, 0, -1] np.testing.assert_allclose(square.compute_form_factor_amplitude(ks), ampl) # Form factor should be invariant to polygon "direction" (the line integral # direction change should cause a sign flip that cancels the normal flip). new_square = Polygon(square.vertices[::-1, :]) np.testing.assert_allclose(new_square.compute_form_factor_amplitude(ks), ampl)
def test_circumcircle(): family = RegularNGonFamily() for i in range(3, 10): vertices = family.make_vertices(i) rmax = np.max(np.linalg.norm(vertices, axis=-1)) poly = Polygon(vertices) circle = poly.circumcircle assert np.isclose(rmax, circle.radius) assert np.allclose(circle.center, 0)
def polygon_from_hull(verts): """Generate a polygon from a hull if possible. This function tries to generate a polygon from a hull, and returns False if it fails so that Hypothesis can simply assume(False) if the hull is nearly degenerate. """ try: poly = Polygon(verts) except AssertionError: # Don't worry about failures caused by bad hulls that fail the simple # polygon test. return False return poly
def test_identical_points(ones): """Ensure that running with identical points produces an error.""" with pytest.raises(ValueError): Polygon(ones)
def test_duplicate_points(square_points): """Ensure that running with any duplicate points produces a warning.""" square_points = np.vstack((square_points, square_points[[0]])) with pytest.raises(ValueError): Polygon(square_points)
def test_2d_verts(square_points): """Try creating object with 2D vertices.""" square_points = square_points[:, :2] Polygon(square_points)
def square(): return Polygon(get_square_points())
def test_nonplanar(square_points): """Ensure that nonplanar vertices raise an error.""" with pytest.raises(ValueError): square_points[0, 2] += 1 Polygon(square_points)