Example #1
0
 def when_triangle_created(self):
     self.triangle = SphericalTriangle(self.vertices)
Example #2
0
class TestSphericalTriangle(unittest.TestCase):
    """
    Test SphericalTriangle Class
    """

    def clean_state(self):
        self.triangle = None
        self.vertices = None
        self.center_ray = None
        self.test_direction = None

    def setUp(self):
        self.clean_state()
        self.vertices = (Ray.right, Ray.forward, Ray.up)

    def tearDown(self):
        self.clean_state()

    def test_spherical_triangle_normal_constrcution(self):
        self.when_triangle_created()
        self.then_triangle_has_normal_values()

    def when_triangle_created(self):
        self.triangle = SphericalTriangle(self.vertices)

    def then_triangle_has_normal_values(self):
        self.assertEqual(self.triangle.vertices, self.vertices)
        self.assertAlmostEqual(1.0 / 6.0, self.triangle.volume)
        self.assertAlmostEqual(sqrt(2.0), self.triangle.diameter)

    def test_spherical_triangle_constructor_requires_tuple(self):
        with self.assertRaises(TypeError):
            SphericalTriangle("wrong stuff")

    def test_spherical_triangle_constructor_requires_3_tuple(self):
        with self.assertRaises(ValueError):
            SphericalTriangle((Ray.right, Ray.forward))

    def test_spherical_triangle_constructor_requires_ray_vertices(self):
        with self.assertRaises(TypeError):
            SphericalTriangle((Ray.right, "wrong stuff", Ray.forward))

    def test_spherical_triangle_orientation(self):
        self.when_triangle_created()
        self.then_triangle_has_positive_orientation()

    def then_triangle_has_positive_orientation(self):
        self.assertTrue(self.triangle.has_positive_orientation)

    def test_spherical_triangle_opposite_orientation(self):
        self.when_triangle_created()
        self.when_triangle_inverted()
        self.then_triangle_has_negative_orientation()

    def when_triangle_inverted(self):
        self.triangle = self.triangle.opposite_orientation()

    def then_triangle_has_negative_orientation(self):
        self.assertFalse(self.triangle.has_positive_orientation)

    def test_spherical_triangle_center_ray(self):
        self.when_triangle_created()
        self.when_center_ray_extracted()
        self.then_center_ray_has_correct_values()

    def when_center_ray_extracted(self):
        self.center_ray = self.triangle.center_ray

    def then_center_ray_has_correct_values(self):
        root_third = sqrt(1.0 / 3.0)
        expected_direction_xyz = (root_third, root_third, root_third)
        self.assertTupleEqual(expected_direction_xyz, self.center_ray.direction.xyz)

    def test_spherical_triangle_normal_constrcution(self):
        self.when_triangle_created()
        self.when_triangle_repositioned()
        self.then_triangle_has_repositioned_values()

    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)

    def then_triangle_has_repositioned_values(self):
        expected_results = [vertex.add(self.test_direction) for vertex in self.vertices]
        actual_results = [vertex for vertex in self.triangle]
        for index in range(3):
            expected_vertex = expected_results[index]
            actual_vertex = actual_results[index]
            self.assertTupleEqual(expected_vertex.position.xyz, actual_vertex.position.xyz)
            self.assertTupleEqual(expected_vertex.direction.xyz, actual_vertex.direction.xyz)