예제 #1
0
    def test_translation_vector(self):

        transform = translation(5, -3, 2)

        v = vector(-3, 4, 5)

        self.assertEqual(transform * v, v,
                         'The translation of a vector failed')
예제 #2
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')
예제 #3
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.')
예제 #4
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')
예제 #5
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')
예제 #6
0
    def test_sphere_transform(self):

        s = sphere()

        self.assertEqual(
            s.transform, identity_matrix,
            'The default sphere transformation should be the identity matrix')

        t = translation(2, 3, 4)
        s.set_transform(t)

        self.assertEqual(s.transform, t,
                         'The transformation shoud be translation')
예제 #7
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')
예제 #8
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')
예제 #9
0
with open('shadow_sphere.ppm', 'w') as f:
    f.write(c.to_PPM())

print('blank canvas wrote')

pad_x = -CANVAS_W / 2
pad_y = -CANVAS_H / 2

light_position = point(-10, 10, -10)
light_color = color(1, 1, 1)
light = PointLight(light_position, light_color)

s = sphere()
s.material = Material()
s.material.color = color(1, 0.2, 1)
trans = scaling(200, 200, 1) * translation(0, 0, 800)
s.set_transform(trans)

print('{:04d},{:04d}'.format(0, 0), end='\r')
h = False
for i in range(CANVAS_W):
    #print()
    for j in range(CANVAS_H):
        print('{:04d},{:04d}'.format(i, j), end='\r')
        r = ray(point(0, 0, 0), vector(i + pad_x, -pad_y - j, CANVAS_DISTANCE))
        inter = intersect(s, r)
        if hit(inter) != None:
            c[i, j] = HIT_COLOR
            h = True
        #if h:
        #print('\033[48;2;255;0;0m  \033[0m', end='', flush=True)
예제 #10
0
 def __init__(self, center=point(0, 0, 0), r=1):
     self.center = center
     self.r = r
     self.transform = scaling(r, r, r) * translation(*center)
     self.material = Material()