Exemple #1
0
    def test_intersect_axis(self):
        ray = rays.Ray(k=_utils.vec(0, 0, 1))
        origin = _utils.pos(1, 0, 1)
        axis = _utils.vec(0, 1, 0)

        pt_ray, pt_axi = ray.intersect_axis(origin, axis)
        self.assertApproxArray(pt_ray, _utils.pos(0, 0, 1))
        self.assertApproxArray(pt_axi, _utils.pos(1, 0, 1))
        self.assertAlmostEqual(_utils.vabs(pt_ray - pt_axi), 1)
Exemple #2
0
    def test_intersect(self):
        plane = geometry.Plane(pos=np.zeros(3),
                               width=_utils.vec(1, 0, 0),
                               height=_utils.vec(0, 1, 0))

        ray = rays.Ray(_utils.pos(.5, .5, 7), k=_utils.vec(0, 0, -1))
        self.assertApproxArray(plane.intersect(ray), _utils.pos(.5, .5, 0))

        ray = rays.Ray(_utils.pos(.5, .5, 7), k=_utils.vec(0, 0, 1))
        self.assertEqual(plane.intersect(ray), None)
Exemple #3
0
    def test_contains(self):
        plane = geometry.Plane(pos=np.zeros(3),
                               width=_utils.vec(1, 0, 0),
                               height=_utils.vec(0, 1, 0))

        self.assertTrue(plane.contains(_utils.pos(0.5, 0.5, 0)))
        self.assertTrue(plane.contains(_utils.pos(1e-20, 1e-20, 0)))
        self.assertTrue(plane.contains(_utils.pos(0.5, 0.5, 0 + 1e-30)))

        self.assertFalse(plane.contains(_utils.pos(1.5, 0.5, 0)))
        self.assertFalse(plane.contains(_utils.pos(0, -0.5, 0)))
        self.assertFalse(plane.contains(_utils.pos(0.5, 0.5, 0.1)))
Exemple #4
0
    def test_refract(self):
        screen = geometry.Screen()
        ray = rays.Ray(origin=_utils.pos(.5, .5, 1), k=_utils.vec(0, 0, -1))
        screen.refract(ray, _utils.pos(.5, .5, 0), 1)

        self.assertTrue(ray.terminated)
        self.assertSameArray(ray.pos, _utils.pos(.5, .5, 0))
Exemple #5
0
 def test_vec(self):
     vec = _utils.vec(1, 2, 3)
     self.assertTrue(type(vec) == np.ndarray)
     self.assertTrue(len(vec) == 3)
     self.assertEqual(vec[0], 1)
     self.assertEqual(vec[1], 2)
     self.assertEqual(vec[2], 3)
Exemple #6
0
    def test_properties(self):
        screen = geometry.Screen()
        ray = rays.Ray(origin=_utils.pos(.5, .5, 1), k=_utils.vec(0, 0, -1))
        screen.refract(ray, _utils.pos(.5, .5, 0), 1)

        self.assertEqual(type([]), type(screen.hits))
        self.assertEqual(1, len(screen.hits))
        self.assertEqual(ray, screen.hits[0])
Exemple #7
0
    def test_refract(self):
        # As refract in Splitter invokes
        # super, our proxy class will
        # cause runtime errors
        class MockSplitter(geometry.Splitter):
            def normal(*_):
                return _utils.vec(0, 0, 1)

        # Check it updates position
        splitter = MockSplitter()
        ray = rays.Ray(k=_utils.vec(0, 1, 1))
        new_pos = _utils.pos(10, 34.2, -70)
        splitter.refract(ray, new_pos, 1)
        self.assertSameArray(new_pos, ray.pos)

        # Check angle in is angle out
        tests = [
            (_utils.vec(5, 0, 0), 1, 2),
            (_utils.vec(1, 3, 5), 1, 3.1),
            (_utils.vec(0.246, 5.8, 24.9), 1, 1.1),
            (_utils.vec(-3, 1.8, 0), 1, 7.24),
            (_utils.vec(43.4, 9.38, -5), 2.1, 4.15),
        ]

        def angle(a, b):
            return np.arccos(abs(a.dot(b) / _utils.vabs(a) / _utils.vabs(b)))

        for k, n_prev, n_lens in tests:
            splitter = MockSplitter(n=n_lens)
            ray = rays.Ray(k=k)

            ang_1 = angle(ray.k, splitter.normal())
            splitter.refract(ray, np.zeros(3), n_prev)
            ang_2 = angle(ray.k, splitter.normal())

            self.assertAlmostEqual(ang_1, ang_2)
Exemple #8
0
 def test_normal(self):
     vec = _utils.vec(1, 4, 7)
     n, w, h = _utils.basis(vec)
     plane = geometry.Plane(normal=vec, width=w, height=h)
     self.assertApproxArray(n, plane.normal(None))
Exemple #9
0
 def test_normal(self):
     lens = geometry.Sphere(1, 1, 1, pos=_utils.pos(0, 0, 0))
     normal = _utils.vec(0, 0, 1)
     self.assertSameArray(normal, lens.normal(_utils.pos(0, 0, 0)))
Exemple #10
0
 def normal(*_):
     return _utils.vec(0, 0, 1)
Exemple #11
0
    def test_refract(self):
        # Check it updates position
        mirror = MockGeometryNormal(geometry.Mirror())
        ray = rays.Ray(k=_utils.vec(0, 1, 1))
        new_pos = _utils.pos(10, 34.2, -70)
        mirror.refract(ray, new_pos, 1)
        self.assertSameArray(new_pos, ray.pos)

        # Check angle in is angle out
        tests = [
            (_utils.vec(5, 0, 0), _utils.vec(1, 0, 0), 1, 2),
            (_utils.vec(1, 3, 5), _utils.vec(1, -2, 7), 1, 3.1),
            (_utils.vec(0.246, 5.8, 24.9), _utils.vec(45.6, -2.888,
                                                      3.1), 1, 1.1),
            (_utils.vec(-3, 1.8, 0), _utils.vec(1.7, 2.2, -1), 1, 7.24),
            (_utils.vec(43.4, 9.38, -5), _utils.vec(-1.7, 22, -1), 2.1, 4.15),
        ]

        def angle(a, b):
            return np.arccos(abs(a.dot(b) / _utils.vabs(a) / _utils.vabs(b)))

        for k, normal, n_prev, n_lens in tests:
            mirror = MockGeometryNormal(geometry.Mirror(n=n_lens), normal)
            ray = rays.Ray(k=k)

            ang_1 = angle(ray.k, normal)
            mirror.refract(ray, np.zeros(3), n_prev)
            ang_2 = angle(ray.k, normal)

            self.assertAlmostEqual(ang_1, ang_2)
Exemple #12
0
 def __init__(self, geo_obj, normal=_utils.vec(0, 0, 1)):
     self._obj = geo_obj
     self._normal = normal / _utils.vabs(normal)
Exemple #13
0
    def test_refract(self):
        # Check it updates position
        lens = MockGeometryNormal(geometry.Lens())
        ray = rays.Ray(k=_utils.vec(0, 1, 1))
        new_pos = _utils.pos(10, 34.2, -70)
        lens.refract(ray, new_pos, 1)
        self.assertSameArray(new_pos, ray.pos)

        # Check Babinet’s principle holds
        tests = [
            (_utils.vec(5, 0, 0), _utils.vec(1, 0, 0), 1, 2),
            (_utils.vec(1, 3, 5), _utils.vec(1, -2, 7), 1, 3.1),
            (_utils.vec(0.246, 5.8, 24.9), _utils.vec(45.6, -2.888,
                                                      3.1), 1, 1.1),
            (_utils.vec(-3, 1.8, 0), _utils.vec(1.7, 2.2, -1), 1, 7.24),
            (_utils.vec(43.4, 9.38, -5), _utils.vec(-1.7, 22, -1), 2.1, 4.15),
            (_utils.vec(1.4, 2.8, 3.9), _utils.vec(-1.7, 22, -1), 2, 2),
        ]

        def angle(a, b):
            return np.arccos(abs(a.dot(b) / _utils.vabs(a) / _utils.vabs(b)))

        for k, normal, n_prev, n_lens in tests:
            lens = MockGeometryNormal(geometry.Lens(n=n_lens), normal)
            ray = rays.Ray(k=k)

            n_1_sin_1 = n_prev * np.sin(angle(ray.k, normal))
            lens.refract(ray, np.zeros(3), n_prev)
            n_2_sin_2 = n_lens * np.sin(angle(ray.k, normal))

            self.assertAlmostEqual(n_1_sin_1, n_2_sin_2)