コード例 #1
0
ファイル: test_light.py プロジェクト: orwonthe/scopes
 def test_cross_product(self):
     a = Direction.from_xyz((2.0, 3.0, 4.0))
     b = Direction.from_xyz((10.0, 100.0, 1000.0))
     expected_direction = Direction.from_xyz((
         3.0 * 1000.0 - 4.0 * 100.0,
         4.0 * 10.0 - 2.0 * 1000.0,
         2.0 * 100.0 - 3.0 * 10.0
     ))
     actual_direction = a.cross(b)
     self.assertTupleEqual(expected_direction.coordinates, actual_direction.coordinates)
     # cross product is orthoganal to arguments
     self.assertAlmostEqual(0.0, a.dot(actual_direction))
     self.assertAlmostEqual(0.0, b.dot(actual_direction))
コード例 #2
0
ファイル: test_instruments.py プロジェクト: orwonthe/scopes
 def setUp(self):
     self.clean_state()
     self.diameter = 0.1
     self.aperture_origin = Position.from_xyz((10.0, 20.0, 30.0))
     self.aperture_direction = Direction.from_xyz((1.0, 2.0, 3.0))
     self.placement = Ray(self.aperture_origin, self.aperture_direction.normalized())
     self.aperture = Aperture(placement=self.placement, diameter=self.diameter)
     self.initial_wavefront = WaveFront.unit_sphere
     self.orthogonal_directions = [
         self.aperture_direction.cross(Direction.right).normalized(),
         self.aperture_direction.cross(Direction.forward).normalized(),
         self.aperture_direction.cross(Direction.backward).normalized(),
     ]
コード例 #3
0
ファイル: test_light.py プロジェクト: orwonthe/scopes
 def setUp(self):
     self.clean_state()
     self.vector = (2.0, 3.0, 4.0)
     vector_length = vector_norm(self.vector)
     self.unit_vector = tuple(component / vector_length for component in self.vector)
     self.position = Position.from_xyz((10.0, 20.0, 30.0))
     self.direction = Direction.from_xyz(self.vector)
     self.unit_direction = Direction.from_xyz(self.unit_vector)
     self.ray = Ray(self.position, self.direction)
     self.unit_ray = Ray(self.position, self.unit_direction)
     self.other_position = Position.from_xyz((-9.0, -88.0, 12.34))
     self.other_direction = Direction.from_xyz((19.0, 8.0, -0.03))
     self.other_ray = Ray(self.other_position, self.other_direction)
     offset = self.position.dot(self.unit_direction)
     plane_projector = (-offset,) + self.unit_vector
     self.plane = Plane(plane_projector)
     self.orthogonal_directions = [
         Direction.from_xyz((0.2, 0.2, -0.25)),
         Direction.from_xyz((-7.0, 2.0, 2.0))
     ]
     self.orthogonal_rays = [Ray(self.position, direction)
                             for direction in self.orthogonal_directions]
コード例 #4
0
ファイル: test_instruments.py プロジェクト: orwonthe/scopes
 def setUp(self):
     self.clean_state()
     self.diameter = 10.0
     self.focal_length = 50.0
     self.aperture_origin = Position.from_xyz((10.0, 20.0, 30.0))
     self.aperture_direction = Direction.from_xyz((1.0, 2.0, 3.0))
     self.aperture_normal = self.aperture_direction.normalized()
     self.placement = Ray(self.aperture_origin, self.aperture_normal)
     self.orthogonal_directions = [
         self.aperture_direction.cross(Direction.right).normalized(),
         self.aperture_direction.cross(Direction.forward).normalized(),
         self.aperture_direction.cross(Direction.backward).normalized(),
     ]
     self.test_directions = [self.aperture_normal.add(
         orthogonal_direction.scale(0.1)).normalized() for orthogonal_direction in self.orthogonal_directions]
コード例 #5
0
ファイル: test_light.py プロジェクト: orwonthe/scopes
 def when_triangle_repositioned(self):
     self.test_direction = Direction.from_xyz((1000.0, 2000.0, 3000.0))
     self.triangle = self.triangle.add(self.test_direction)
コード例 #6
0
ファイル: test_light.py プロジェクト: orwonthe/scopes
 def setUp(self):
     self.clean_state()
     self.direction = Direction.from_xyz((2.0, 3.0, 4.0))
     self.volume = 10.0
コード例 #7
0
ファイル: test_light.py プロジェクト: orwonthe/scopes
 def when_direction_added_to_ray(self):
     self.ray = self.ray.add(Direction.from_xyz((100, 200, 300)))
コード例 #8
0
ファイル: test_light.py プロジェクト: orwonthe/scopes
 def test_ray_has_cardinal_rays(self):
     for direction_name in ['up', 'down', 'left', 'right', 'forward', 'backward', 'unknown']:
         target = Ray.get_named_cardinal_ray(direction_name)
         expected_direction = Direction.get_named_cardinal_direction(direction_name)
         self.assertEqual(expected_direction.xyz, target.direction.xyz)
         self.assertEqual(Position.origin.xyz, target.position.xyz)
コード例 #9
0
ファイル: test_light.py プロジェクト: orwonthe/scopes
 def setUp(self):
     self.clean_state()
     self.expected_position = Position.from_xyz((10.0, 20.0, 30.0))
     self.expected_direction = Direction.from_xyz((0.5, 0.4, 0.3))
     self.endpoint_xyz = (10.5, 20.4, 30.3)
     self.ten_scale_endpoint_xyz = (15.0, 24.0, 33.0)
コード例 #10
0
ファイル: test_light.py プロジェクト: orwonthe/scopes
 def test_direction_from_xyz_requires_3_tuple(self):
     with self.assertRaises(ValueError):
         Direction.from_xyz((0.0, 2.2, 3.3, 4.4))
コード例 #11
0
ファイル: test_light.py プロジェクト: orwonthe/scopes
 def test_direction_from_xyz_requires_tuple(self):
     with self.assertRaises(TypeError):
         Direction.from_xyz("wrong stuff")
コード例 #12
0
ファイル: test_light.py プロジェクト: orwonthe/scopes
 def when_direction_created_from_xyz(self):
     self.direction = Direction.from_xyz(self.xyz)
コード例 #13
0
ファイル: test_light.py プロジェクト: orwonthe/scopes
 def when_direction_created(self):
     self.direction = Direction(self.wxyz)
コード例 #14
0
ファイル: test_light.py プロジェクト: orwonthe/scopes
class TestDirection(unittest.TestCase):
    """
    Test Direction Class
    """

    def clean_state(self):
        self.direction = None
        self.w = None
        self.x = None
        self.y = None
        self.z = None
        self.wxyz = None
        self.xyz = None

    def setUp(self):
        self.clean_state()
        self.w = 0.0
        self.x = 3.0
        self.y = 4.0
        self.z = 5.0
        self.wxyz = (self.w, self.x, self.y, self.z)
        self.xyz = (self.x, self.y, self.z)

    def tearDown(self):
        self.clean_state()

    def test_direction_constructor_requires_tuple(self):
        with self.assertRaises(TypeError):
            Direction("wrong stuff")

    def test_direction_constructor_requires_4_tuple(self):
        with self.assertRaises(ValueError):
            Direction((2.2, 3.3, 4.4))

    def test_direction_constructor_requires_zeroed_w(self):
        with self.assertRaises(ValueError):
            Direction((0.0001, 2.2, 3.3, 4.4))

    def test_direction_create_normal(self):
        self.when_direction_created()
        self.then_direction_has_expected_values()

    def when_direction_created(self):
        self.direction = Direction(self.wxyz)

    def then_direction_has_expected_values(self):
        self.assertFalse(self.direction.is_position)
        self.assertTrue(self.direction.is_direction)
        self.assertEqual(self.xyz, self.direction.xyz)
        expected_length = sqrt(50)
        self.assertAlmostEqual(expected_length, self.direction.length())

    def test_direction_from_xyz_normal(self):
        self.when_direction_created_from_xyz()
        self.then_direction_has_expected_values()

    def when_direction_created_from_xyz(self):
        self.direction = Direction.from_xyz(self.xyz)

    def test_direction_from_xyz_requires_tuple(self):
        with self.assertRaises(TypeError):
            Direction.from_xyz("wrong stuff")

    def test_direction_from_xyz_requires_3_tuple(self):
        with self.assertRaises(ValueError):
            Direction.from_xyz((0.0, 2.2, 3.3, 4.4))

    def test_direction_cardinal_directions(self):
        self.assertTupleEqual((0.0, 0.0, 1.0), Direction.up.xyz)
        self.assertTupleEqual((0.0, 0.0, -1.0), Direction.down.xyz)
        self.assertTupleEqual((-1.0, 0.0, 0.0), Direction.left.xyz)
        self.assertTupleEqual((1.0, 0.0, 0.0), Direction.right.xyz)
        self.assertTupleEqual((0.0, 1.0, 0.0), Direction.forward.xyz)
        self.assertTupleEqual((0.0, -1.0, 0.0), Direction.backward.xyz)
        self.assertTupleEqual((0.0, 0.0, 0.0), Direction.unknown.xyz)

    def test_direction_scales_by_one(self):
        self.when_direction_created_from_xyz()
        self.direction = self.direction.scale(1.0)
        self.then_direction_has_expected_values()

    def test_direction_scales(self):
        self.when_direction_created_from_xyz()
        self.direction = self.direction.scale(2.0)
        self.assertTrue(self.direction.is_direction)
        self.assertTupleEqual((6.0, 8.0, 10.0), self.direction.xyz)

    def test_direction_normalized(self):
        self.when_direction_created_from_xyz()
        self.direction = self.direction.normalized()
        self.assertAlmostEqual(1.0, vector_norm(self.direction))

    def test_cross_product(self):
        a = Direction.from_xyz((2.0, 3.0, 4.0))
        b = Direction.from_xyz((10.0, 100.0, 1000.0))
        expected_direction = Direction.from_xyz((
            3.0 * 1000.0 - 4.0 * 100.0,
            4.0 * 10.0 - 2.0 * 1000.0,
            2.0 * 100.0 - 3.0 * 10.0
        ))
        actual_direction = a.cross(b)
        self.assertTupleEqual(expected_direction.coordinates, actual_direction.coordinates)
        # cross product is orthoganal to arguments
        self.assertAlmostEqual(0.0, a.dot(actual_direction))
        self.assertAlmostEqual(0.0, b.dot(actual_direction))