Beispiel #1
0
    def test_scaling(self):

        transform = scaling(2, 3, 4)

        p = point(-4, 6, 8)

        p_out = point(-8, 18, 32)

        self.assertEqual(transform * p, p_out, 'The scaling of point fail')
Beispiel #2
0
    def test_reflection(self):

        transform = scaling(-1, 1, 1)

        p = point(2, 3, 4)

        p_out = point(-2, 3, 4)

        self.assertEqual(transform * p, p_out, 'The reflection failed')
Beispiel #3
0
    def test_translation(self):

        transform = translation(5, -3, 2)

        p = point(-3, 4, 5)

        p_out = point(2, 1, 7)

        self.assertEqual(transform * p, p_out, 'The translation failed')
Beispiel #4
0
    def test_position(self):

        r = ray(point(2, 3, 4), vector(1, 0, 0))

        self.assertEqual(position(r, 0), point(2, 3, 4), 'Ray position failed')
        self.assertEqual(position(r, 1), point(3, 3, 4), 'Ray position failed')
        self.assertEqual(position(r, -1), point(1, 3, 4),
                         'Ray position failed')
        self.assertEqual(position(r, 2.5), point(4.5, 3, 4),
                         'Ray position failed')
Beispiel #5
0
    def test_shearing_zy(self):

        transform = shearing(zy=1)

        p = point(2, 3, 4)

        p_out = point(2, 3, 7)

        self.assertEqual(transform * p, p_out,
                         'Shearing in zy direction failed')
Beispiel #6
0
    def test_shearing_yz(self):

        transform = shearing(yz=1)

        p = point(2, 3, 4)

        p_out = point(2, 7, 4)

        self.assertEqual(transform * p, p_out,
                         'Shearing in yz direction failed')
Beispiel #7
0
    def test_shearing_xy(self):

        transform = shearing(xy=1)

        p = point(2, 3, 4)

        p_out = point(5, 3, 4)

        self.assertEqual(transform * p, p_out,
                         'shearing in xy direction failed')
Beispiel #8
0
    def test_inv_translation(self):

        transform = translation(5, -3, 2)

        inv = inverse(transform)

        p = point(-3, 4, 5)

        p_out = point(-8, 7, 3)

        self.assertEqual(inv * p, p_out, 'The inverse translation failed')
Beispiel #9
0
    def test_rotation_z(self):

        p = point(0, 1, 0)

        half_quarter = rotation_z(pi / 4)
        full_quarter = rotation_z(pi / 2)

        p_out_half = point(-sqrt(2) / 2, sqrt(2) / 2, 0)
        p_out_full = point(-1, 0, 0)

        self.assertEqual(half_quarter * p, p_out_half, 'z rotation failed')
        self.assertEqual(full_quarter * p, p_out_full, 'z rotation failed')
Beispiel #10
0
    def test_rotation_x(self):

        p = point(0, 1, 0)

        half_quarter = rotation_x(pi / 4)
        full_quarter = rotation_x(pi / 2)
        half_quarter_inv = inverse(half_quarter)

        p_out_half = point(0, sqrt(2) / 2, sqrt(2) / 2)
        p_out_full = point(0, 0, 1)
        p_out_inv = point(0, sqrt(2) / 2, -sqrt(2) / 2)

        self.assertEqual(half_quarter * p, p_out_half, 'x rotation failed')
        self.assertEqual(full_quarter * p, p_out_full, 'x rotation failed')
        self.assertEqual(half_quarter_inv * p, p_out_inv,
                         'x inverse rotation failed')
Beispiel #11
0
    def test_sphere_z(self):
        """Test sphere normal in z direction"""

        s_var = sphere()
        n_var = normal_at(s_var, point(0, 0, 1))

        self.assertEqual(n_var, vector(0, 0, 1),
                         "Sphere normal in z direction failed.")
Beispiel #12
0
    def test_sphere_nonaxial(self):
        """Test sphere normal in nonaxial direction"""

        s_var = sphere()
        n_var = normal_at(s_var, point(sqrt(3) / 3, sqrt(3) / 3, sqrt(3) / 3))

        self.assertEqual(n_var, vector(sqrt(3)/3, sqrt(3)/3, sqrt(3)/3),\
                "Sphere normal in z direction failed.")
Beispiel #13
0
    def test_sphere_normal_vector_is_normal(self):
        """Tests if the normal vector is normal in a sphere"""

        s_var = sphere()
        n_var = normal_at(s_var, point(sqrt(3) / 3, sqrt(3) / 3, sqrt(3) / 3))

        self.assertEqual(n_var, normalize(n_var),
                         'Normal in a sphere is not normalized.')
Beispiel #14
0
    def test_sphere_miss(self):

        r = ray(point(0, 2, -5), vector(0, 0, 1))

        s = sphere()

        xs = intersect(s, r)

        self.assertEqual(len(xs), 0, 'There should be no intersections ')
Beispiel #15
0
    def test_sphere_transformed2(self):

        r = ray(point(0, 0, -5), vector(0, 0, 1))

        s = sphere()

        s.set_transform(translation(5, 0, 0))
        xs = intersect(s, r)

        self.assertEqual(len(xs), 0, 'There sould be no intersection')
Beispiel #16
0
    def test_sphere_translated(self):
        """Tests a sphere normal vector under translation"""

        s_var = sphere()
        s_var.set_transform(translation(0, 1, 0))

        n_var = normal_at(s_var, point(0, 1.70711, -0.70711))

        self.assertEqual(n_var, vector(0, 0.70711, -0.70711),\
                'The normal in the translated sphere is wrong.')
Beispiel #17
0
    def test_ray(self):

        origin = point(1, 2, 3)

        direction = vector(4, 5, 6)

        r = ray(origin, direction)

        self.assertEqual(r.origin, origin, 'The origin of ray failed')
        self.assertEqual(r.direction, direction, 'The direction of ray failed')
    def test_lightning4(self):
        """Lighting with the light and the eye 45 degrees from the suface line"""

        eyev = vector(0, -sqrt(2) / 2, -sqrt(2) / 2)
        normalv = vector(0, 0, -1)
        light = PointLight(point(0, 10, -10), color(1, 1, 1))

        result = lighting(self.m_var, light, self.position, eyev, normalv)

        self.assertEqual(result, color(1.6364, 1.6364, 1.6364), \
                'The lighting function failed.')
    def test_lightning1(self):
        """Lighting with the eye between the light and the surface."""

        eyev = vector(0, 0, -1)
        normalv = vector(0, 0, -1)
        light = PointLight(point(0, 0, -10), color(1, 1, 1))

        result = lighting(self.m_var, light, self.position, eyev, normalv)

        self.assertEqual(result, color(1.9, 1.9, 1.9), \
                'The lighting function failed.')
Beispiel #20
0
    def test_sphere_intersect_group(self):

        r = ray(point(0, 0, -5), vector(0, 0, 1))

        s = sphere()

        xs = intersect(s, r)

        self.assertEqual(len(xs), 2, 'There should be 2 intersections')
        self.assertEqual(xs[0].object, s, 'The intersection object failed')
        self.assertEqual(xs[1].object, s, 'The intersection object failed')
    def test_lightning5(self):
        """Lighting with the light hidden behind the surfice"""

        eyev = vector(0, 0, -1)
        normalv = vector(0, 0, -1)
        light = PointLight(point(0, 0, 10), color(1, 1, 1))

        result = lighting(self.m_var, light, self.position, eyev, normalv)

        self.assertEqual(result, color(0.1, 0.1, 0.1), \
                'The lighting function failed.')
Beispiel #22
0
    def test_sphere(self):

        r = ray(point(0, 0, -5), vector(0, 0, 1))

        s = sphere()

        xs = intersect(s, r)

        self.assertEqual(len(xs), 2, 'There should be 2 intersections')
        self.assertEqual(xs[0].t, 4.0, 't=4 should intersect')
        self.assertEqual(xs[1].t, 6.0, 't=6 should intersect')
Beispiel #23
0
    def test_transformation(self):

        r = ray(point(1, 2, 3), vector(0, 1, 0))

        m = translation(3, 4, 5)

        r2 = transform(r, m)

        self.assertEqual(r2.origin, point(4, 6, 8),
                         'The ray translation failed')
        self.assertEqual(r2.direction, vector(0, 1, 0),
                         'The ray translation failed')

        m2 = scaling(2, 3, 4)

        r3 = transform(r, m2)

        self.assertEqual(r3.origin, point(2, 6, 12), 'The ray scaling failed')
        self.assertEqual(r3.direction, vector(0, 3, 0),
                         'The ray scaling failed')
Beispiel #24
0
    def test_sphere_transformed(self):
        "Tests the sphere normal vector under a transformation"

        s_var = sphere()
        m_var = scaling(1, 0.5, 1) * rotation_z(pi / 5)
        s_var.set_transform(m_var)

        n_var = normal_at(s_var, point(0, sqrt(2) / 2, -sqrt(2) / 2))

        self.assertEqual(n_var, vector(0, 0.97014, -0.24254),\
                'The normal of an sphere transformed is wrong.')
Beispiel #25
0
    def test_sphere_inner(self):

        r = ray(point(0, 0, 0), vector(0, 0, 1))

        s = sphere()

        xs = intersect(s, r)

        self.assertEqual(len(xs), 2,
                         'There should be 2 intersections (virtual)')
        self.assertEqual(xs[0].t, -1.0, 't=-1 should be an intersection time')
        self.assertEqual(xs[1].t, 1.0, 't=1 should be an intersection time')
Beispiel #26
0
    def test_chain_transform1(self):

        p = point(1, 0, 1)

        A = rotation_x(pi / 2)
        B = scaling(5, 5, 5)
        C = translation(10, 5, 7)

        p2 = point(1, -1, 0)

        self.assertEqual(A * p, p2, 'The rotation failed')

        p3 = point(5, -5, 0)

        self.assertEqual(B * p2, p3, 'The chain scaling failed')

        p4 = point(15, 0, 7)

        self.assertEqual(C * p3, p4, 'The chain translation failed')

        self.assertEqual(C * B * A * p, p4, 'The chain transformations failed')
Beispiel #27
0
    def test_sphere_behind(self):

        r = ray(point(0, 0, 5), vector(0, 0, 1))

        s = sphere()

        xs = intersect(s, r)

        self.assertEqual(len(xs), 2,
                         'There should be 2 intersections (virtual)')
        self.assertEqual(xs[0].t, -6.0, 't=-6 should be an intersection time')
        self.assertEqual(xs[1].t, -4.0, 't=-4 should be an intersection time')
Beispiel #28
0
    def test_tuple_general(self):

        ap = point(1.1, 4.5, 4.6)
        av = vector(0, 2.1, 3.3)
        ap2 = point(1.1, 6.6, 7.9)
        av2 = vector(1, 2, 3)
        av3 = vector(1, -0.1, -0.3)
        ap3 = point(-1.1, -4.5, -4.6)

        self.assertEqual(av + av3, av2, 'These shoud be equal')
        self.assertEqual(ap + av, ap2, 'These shoud be equal')
        self.assertEqual(ap2 - ap, av, 'These should be equal')
        self.assertEqual(ap2 - av, ap, 'These should be equal')
        self.assertEqual(av2 - av, av3, 'These should be equal')
        self.assertEqual(-ap, ap3, 'These should be equal')

        #multiplication and division
        ascalar = 3.5
        ascalar2 = 0.5
        scalar3 = 2
        ap = point(1, 2, 4.4)
        ap2 = point(3.5, 7, 15.4)
        ap3 = point(0.5, 1, 2.2)

        self.assertEqual(ascalar * ap, ap2,
                         'This multiplication should be equal')
        self.assertEqual(ascalar2 * ap, ap3,
                         'This multiplication should be equal')
        self.assertEqual(ap / scalar3, ap3, 'This division shouldbe equal')
Beispiel #29
0
    def test_pointlight(self):
        """
        Tests the creation of a light source.
        """

        intensity = color(1, 1, 1)
        position = point(0, 0, 0)

        light = PointLight(position, intensity)

        self.assertEqual(light.position, position, "Light position is wrong.")
        self.assertEqual(light.intensity, intensity,
                         "Light intensity is wrong")
Beispiel #30
0
    def test_sphere_transformed(self):

        r = ray(point(0, 0, -5), vector(0, 0, 1))

        s = sphere()

        s.set_transform(scaling(2, 2, 2))

        xs = intersect(s, r)

        self.assertEqual(len(xs), 2, 'There should have been 2 intersections')
        self.assertEqual(xs[0].t, 3, 't=3 should have been present')
        self.assertEqual(xs[1].t, 7, 't=4 should have been present')