def test_ObscRectangle(): import random random.seed(577) for i in range(100): cx = random.gauss(0.0, 1.0) cy = random.gauss(0.0, 1.0) w = random.uniform(0.5, 2.5) h = random.uniform(0.5, 2.5) obsc = batoid.ObscRectangle(w, h, cx, cy, 0.0) for i in range(100): x = random.gauss(0.0, 2.0) y = random.gauss(0.0, 2.0) assert obsc.contains(x, y) == (x > cx - w / 2 and x < cx + w / 2 and y > cy - h / 2 and y < cy + h / 2) th = random.uniform(0.0, np.pi / 2) obsc = batoid.ObscRectangle(w, h, cx, cy, th) for i in range(100): x = random.gauss(0.0, 2.0) y = random.gauss(0.0, 2.0) xp = (x - cx) * np.cos(-th) - (y - cy) * np.sin(-th) + cx yp = (x - cx) * np.sin(-th) + (y - cy) * np.cos(-th) + cy assert obsc.contains(x, y) == (xp > cx - w / 2 and xp < cx + w / 2 and yp > cy - h / 2 and yp < cy + h / 2) do_pickle(obsc)
def test_ObscCompound(): import random random.seed(57721) for i in range(100): rx = random.gauss(0.0, 1.0) ry = random.gauss(0.0, 1.0) w = random.uniform(0.5, 2.5) h = random.uniform(0.5, 2.5) th = random.uniform(0.0, np.pi) rect = batoid.ObscRectangle(w, h, rx, ry, th) cx = random.gauss(0.0, 1.0) cy = random.gauss(0.0, 1.0) r = random.uniform(0.5, 1.5) circ = batoid.ObscCircle(r, cx, cy) union = batoid.ObscUnion([rect, circ]) do_pickle(union) union2 = batoid.ObscUnion([circ, rect]) assert union == union2 # commutative! assert hash(union) == hash(union2) intersection = batoid.ObscIntersection([rect, circ]) do_pickle(intersection) intersection2 = batoid.ObscIntersection([circ, rect]) assert intersection == intersection2 assert hash(intersection) == hash(intersection2) for i in range(100): x = random.gauss(0.0, 2.0) y = random.gauss(0.0, 2.0) assert (union.contains(x, y) == union2.contains(x, y) == (rect.contains(x, y) or circ.contains(x, y))) assert (intersection.contains(x, y) == intersection2.contains( x, y) == (rect.contains(x, y) and circ.contains(x, y)))
def test_ObscPolygon(): rng = np.random.default_rng(57721) # size = 10_000 size = 10 # Test equivalency with ObscRectangle for i in range(100): cx = rng.normal(0.0, 1.0) cy = rng.normal(0.0, 1.0) w = rng.uniform(0.5, 2.5) h = rng.uniform(0.5, 2.5) xs = [cx - w / 2, cx + w / 2, cx + w / 2, cx - w / 2] ys = [cy - h / 2, cy - h / 2, cy + h / 2, cy + h / 2] obscPoly = batoid.ObscPolygon(xs, ys) obscRect = batoid.ObscRectangle(w, h, cx, cy, 0.0) for i in range(100): x = rng.normal(0.0, 2.0) y = rng.normal(0.0, 2.0) assert obscPoly.contains(x, y) == obscRect.contains(x, y) x = rng.normal(0.0, 2.0, size=size) y = rng.normal(0.0, 2.0, size=size) np.testing.assert_array_equal(obscPoly.contains(x, y), obscRect.contains(x, y)) do_pickle(obscPoly) rv = batoid.RayVector(x, y, 0.0, 0.0, 0.0, 0.0) batoid.obscure(obscPoly, rv) np.testing.assert_array_equal(obscPoly.contains(x, y), rv.vignetted) # Try Union of two Rectangles equal to Polygon. # Center of both rectangles at (1, 2) # One is width=4, height=2 # Other is width=2, height=4 r1 = batoid.ObscRectangle(4, 2, 1, 2) r2 = batoid.ObscRectangle(2, 4, 1, 2) o1 = batoid.ObscUnion([r1, r2]) xs = [-2, -1, -1, 1, 1, 2, 2, 1, 1, -1, -1, -2, -2] ys = [1, 1, 2, 2, 1, 1, -1, -1, -2, -2, -1, -1, 1] o2 = batoid.ObscPolygon(np.array(xs) + 1, np.array(ys) + 2) x = rng.normal(0.0, 2.0, size=size) y = rng.normal(0.0, 2.0, size=size) np.testing.assert_array_equal(o1.contains(x, y), o2.contains(x, y))
def test_ne(): objs = [ batoid.ObscCircle(1.0), batoid.ObscCircle(2.0), batoid.ObscCircle(1.0, 0.1, 0.1), batoid.ObscAnnulus(0.0, 1.0), batoid.ObscAnnulus(0.1, 1.0), batoid.ObscAnnulus(0.1, 1.0, 0.1, 0.1), batoid.ObscRectangle(1.0, 2.0), batoid.ObscRectangle(1.0, 2.0, 0.1, 0.1), batoid.ObscRectangle(1.0, 2.0, 0.1, 0.1, 1.0), batoid.ObscRay(1.0, 2.0), batoid.ObscRay(1.0, 2.0, 0.1, 0.1), batoid.ObscNegation(batoid.ObscCircle(1.0)), batoid.ObscPolygon([0,1,1,0],[0,0,1,1]), batoid.ObscUnion([batoid.ObscCircle(1.0)]), batoid.ObscUnion([ batoid.ObscCircle(1.0), batoid.ObscCircle(2.0) ]), batoid.ObscUnion([ batoid.ObscCircle(1.0), batoid.ObscCircle(2.2) ]), batoid.ObscUnion([ batoid.ObscCircle(1.0), batoid.ObscCircle(2.2), batoid.ObscAnnulus(1.0, 2.0) ]), batoid.ObscIntersection([batoid.ObscCircle(1.0)]), batoid.ObscIntersection([ batoid.ObscCircle(1.0), batoid.ObscCircle(2.0) ]), batoid.ObscIntersection([ batoid.ObscCircle(1.0), batoid.ObscCircle(2.2) ]), batoid.ObscIntersection([ batoid.ObscCircle(1.0), batoid.ObscCircle(2.2), batoid.ObscAnnulus(1.0, 2.0) ]), ] all_obj_diff(objs)
def test_ObscRectangle(): rng = np.random.default_rng(577) size = 10_000 for i in range(100): cx = rng.normal(0.0, 1.0) cy = rng.normal(0.0, 1.0) w = rng.uniform(0.5, 2.5) h = rng.uniform(0.5, 2.5) obsc = batoid.ObscRectangle(w, h, cx, cy) for i in range(100): x = rng.normal(0.0, 2.0) y = rng.normal(0.0, 2.0) assert obsc.contains(x, y) == (x > cx - w / 2 and x < cx + w / 2 and y > cy - h / 2 and y < cy + h / 2) th = rng.uniform(0.0, np.pi / 2) obsc = batoid.ObscRectangle(w, h, cx, cy, th) for i in range(100): x = rng.normal(0.0, 2.0) y = rng.normal(0.0, 2.0) xp = (x - cx) * np.cos(-th) - (y - cy) * np.sin(-th) + cx yp = (x - cx) * np.sin(-th) + (y - cy) * np.cos(-th) + cy assert obsc.contains(x, y) == (xp > cx - w / 2 and xp < cx + w / 2 and yp > cy - h / 2 and yp < cy + h / 2) x = rng.normal(0.0, 2.0, size=size) y = rng.normal(0.0, 2.0, size=size) xp = (x - cx) * np.cos(-th) - (y - cy) * np.sin(-th) + cx yp = (x - cx) * np.sin(-th) + (y - cy) * np.cos(-th) + cy np.testing.assert_array_equal(obsc.contains(x, y), (xp > cx - w / 2) & (xp < cx + w / 2) & (yp > cy - h / 2) & (yp < cy + h / 2)) do_pickle(obsc) rv = batoid.RayVector(x, y, 0.0, 0.0, 0.0, 0.0) batoid.obscure(obsc, rv) np.testing.assert_array_equal(obsc.contains(x, y), rv.vignetted)
def test_ObscPolygon(): import random random.seed(577215) # Test equivalency with ObscRectangle for i in range(100): cx = random.gauss(0.0, 1.0) cy = random.gauss(0.0, 1.0) w = random.uniform(0.5, 2.5) h = random.uniform(0.5, 2.5) xs = [cx - w / 2, cx + w / 2, cx + w / 2, cx - w / 2, cx - w / 2] ys = [cy - h / 2, cy - h / 2, cy + h / 2, cy + h / 2, cy - h / 2] obscPoly = batoid.ObscPolygon(xs, ys) obscRect = batoid.ObscRectangle(w, h, cx, cy, 0.0) for i in range(100): x = random.gauss(0.0, 2.0) y = random.gauss(0.0, 2.0) assert obscPoly.contains(x, y) == obscRect.contains(x, y) # Try Union of two Rectangles equal to Polygon. # Center of both rectangles at (1, 2) # One is width=4, height=2 # Other is width=2, height=4 r1 = batoid.ObscRectangle(4, 2, 1, 2) r2 = batoid.ObscRectangle(2, 4, 1, 2) o1 = batoid.ObscUnion([r1, r2]) xs = [-2, -1, -1, 1, 1, 2, 2, 1, 1, -1, -1, -2, -2] ys = [1, 1, 2, 2, 1, 1, -1, -1, -2, -2, -1, -1, 1] o2 = batoid.ObscPolygon(np.array(xs) + 1, np.array(ys) + 2) for i in range(10000): x = random.gauss(0.0, 2.0) y = random.gauss(0.0, 2.0) assert o1.contains(x, y) == o2.contains(x, y) do_pickle(o2)
def test_ObscCompound(): rng = np.random.default_rng(577215) size = 10_000 for i in range(100): rx = rng.normal(0.0, 1.0) ry = rng.normal(0.0, 1.0) w = rng.uniform(0.5, 2.5) h = rng.uniform(0.5, 2.5) th = rng.uniform(0.0, np.pi) rect = batoid.ObscRectangle(w, h, rx, ry, th) cx = rng.normal(0.0, 1.0) cy = rng.normal(0.0, 1.0) r = rng.uniform(0.5, 1.5) circ = batoid.ObscCircle(r, cx, cy) union = batoid.ObscUnion([rect, circ]) do_pickle(union) union2 = batoid.ObscUnion([circ, rect]) assert union == union2 # commutative! assert hash(union) == hash(union2) intersection = batoid.ObscIntersection([rect, circ]) do_pickle(intersection) intersection2 = batoid.ObscIntersection([circ, rect]) assert intersection == intersection2 assert hash(intersection) == hash(intersection2) for i in range(100): x = rng.normal(0.0, 2.0) y = rng.normal(0.0, 2.0) assert (union.contains(x, y) == union2.contains(x, y) == (rect.contains(x, y) or circ.contains(x, y))) assert (intersection.contains(x, y) == intersection2.contains( x, y) == (rect.contains(x, y) and circ.contains(x, y))) x = rng.normal(0.0, 2.0, size=size) y = rng.normal(0.0, 2.0, size=size) np.testing.assert_array_equal(union.contains(x, y), union2.contains(x, y)) np.testing.assert_array_equal( union.contains(x, y), rect.contains(x, y) | circ.contains(x, y)) np.testing.assert_array_equal(intersection.contains(x, y), intersection2.contains(x, y)) np.testing.assert_array_equal( intersection.contains(x, y), rect.contains(x, y) & circ.contains(x, y)) rv = batoid.RayVector(x, y, 0.0, 0.0, 0.0, 0.0) batoid.obscure(union, rv) np.testing.assert_array_equal(union.contains(x, y), rv.vignetted) rv = batoid.RayVector(x, y, 0.0, 0.0, 0.0, 0.0) batoid.obscure(intersection, rv) np.testing.assert_array_equal(intersection.contains(x, y), rv.vignetted) with np.testing.assert_raises(ValueError): batoid.ObscUnion() with np.testing.assert_raises(ValueError): batoid.ObscIntersection()