def test_maps_the_perimeter_to_itself(self): # Make a rectangle, with enough support points to get a decent approximation perimeter = np.array([ [-2, -1], [-2, -0.9], [-2, -0.8], [-2, -0.7], [-2, -0.5], [-2, 0], [-2, 1], [0, 1], [1, 1], [1, 0], [1, -1], [0, -1], ]) + np.array([[-10, 5]]) warp_coords = poly.warp_to_circle(perimeter, perimeter, i_max=200, r_max=-1) # Warp perimeter should always be (almost) on the unit circle warp_r = np.sqrt(warp_coords[:, 0]**2 + warp_coords[:, 1]**2) np.testing.assert_almost_equal(warp_r, np.ones_like(warp_r), decimal=2) # Inverse should project exactly back to the perimeter inv_warp_coords = poly.inv_warp_to_circle(warp_coords, perimeter, i_max=200) np.testing.assert_almost_equal(inv_warp_coords, perimeter, decimal=4)
def test_can_warp_and_inv_warp_beyond_the_edge(self): # Make a circle with radius 5 theta = np.linspace(0, 2*np.pi, 50) perimeter = np.stack([np.cos(theta) * 5 + 2.0, np.sin(theta)*5 - 1.0], axis=1) # Ray along x == -y coords = np.array([ [0, 0], [1, -1], [2, -2], [3, -3], [4, -4], [5, -5], [6, -6], [7, -7], [8, -8], [9, -9], [10, -10], ]) + np.array([ [2.0, -1.0], ]) warp_coords = poly.warp_to_circle(coords, perimeter, i_max=200, r_max=-1) # Warping in this case is just centering and scaling by 5 exp_warp_coords = (coords - np.array([[2.0, -1.0]])) / 5 np.testing.assert_almost_equal(warp_coords, exp_warp_coords, decimal=4) inv_warp_coords = poly.inv_warp_to_circle(warp_coords, perimeter, i_max=200) np.testing.assert_almost_equal(coords, inv_warp_coords, decimal=4)
def test_warp_inv_warp_oversampled_square(self): # Make a square, with enough support points to get a decent approximation perimeter = np.array([ [-1, -1], [-1, -0.9], [-1, -0.8], [-1, -0.7], [-1, -0.5], [-1, 0], [-1, 1], [0, 1], [1, 1], [1, 0], [1, -1], [0, -1], ]) assert perimeter.shape[1] == 2 # Make an X inside the square coords = np.array([ [-1, -1], [-0.5, -0.5], [0, 0], [0.5, 0.5], [1, 1], [-0.5, 0.5], [-1, 1], [0.5, -0.5], [1, -1], ]) warp_coords = poly.warp_to_circle(coords, perimeter, i_max=200) inv_warp_coords = poly.inv_warp_to_circle(warp_coords, perimeter, i_max=200) np.testing.assert_almost_equal(coords, inv_warp_coords)
def test_project_circle_onto_circle(self): theta = np.arange(0, 2*np.pi, np.pi/100) perimeter = np.stack([2*np.cos(theta), 2*np.sin(theta)], axis=1) coords = np.random.rand(100, 2) * 4 - 2 coords = coords[(coords[:, 0]**2 + coords[:, 1]**2) < 4] warp_coords = poly.warp_to_circle(coords, perimeter) np.testing.assert_almost_equal(warp_coords, coords/2)
def test_project_square_onto_circle_oversampled(self): # Make a square, with enough support points to get a decent approximation perimeter = np.array([ [-1, -1], [-1, -0.9], [-1, -0.8], [-1, -0.7], [-1, -0.5], [-1, 0], [-1, 1], [0, 1], [1, 1], [1, 0], [1, -1], [0, -1], ]) assert perimeter.shape[1] == 2 # Make an X inside the square coords = np.array([ [-1, -1], [-0.5, -0.5], [0, 0], [0.5, 0.5], [1, 1], [-0.5, 0.5], [-1, 1], [0.5, -0.5], [1, -1], ]) warp_coords = poly.warp_to_circle(coords, perimeter, i_max=200) # Projects to a smaller X inside the circle exp_coords = np.array([ [-0.70710678, -0.70710678], [-0.35355339, -0.35355339], [0.0, 0.0], [0.35355339, 0.35355339], [0.70710678, 0.70710678], [-0.35355339, 0.35355339], [-0.70710678, 0.70710678], [0.35355339, -0.35355339], [0.70710678, -0.70710678], ]) np.testing.assert_almost_equal(warp_coords, exp_coords)