Example #1
0
    def test_color_add(self):

        c1 = color(1, 0, 0.5)
        c2 = color(0.6, 0.4, 0.25)
        c12 = color(1.6, 0.4, 0.75)

        self.assertEqual(c1 + c2, c12, 'The sum of colors should be equal')
Example #2
0
    def test_color_mul(self):

        c1 = color(1, 0, 0.5)
        c2 = color(0.6, 0.4, 0.25)
        c12 = color(0.6, 0, 0.125)

        self.assertEqual(c1 * c2, c12,
                         'The multiplication of colors should be equal')
Example #3
0
    def test_color_sub(self):

        c1 = color(1, 0, 0.5)
        c2 = color(0.6, 0.4, 0.25)
        c12 = color(0.4, -0.4, 0.25)

        self.assertEqual(c1 - c2, c12,
                         'The subtraction of colors should be equal')
Example #4
0
    def test_color_scalar_mul(self):

        c1 = color(1, 0, 0.5)
        c2 = color(2, 0, 1)
        scalar = 2

        self.assertEqual(
            scalar * c1, c2,
            'The multiplication of colors and scalar should be equal')
Example #5
0
    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.')
Example #6
0
    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.')
Example #7
0
    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.')
Example #8
0
    def test_set_pixel(self):

        c = canvas(10, 20)

        red = color(1, 0, 0)
        black = color(0, 0, 0)
        c[2, 3] = red

        self.assertEqual(c[2, 3], red, 'The pixel at (2,3) should be red')
        self.assertEqual(c[0, 0], black, 'The pixel at (2,3) should be red')
        self.assertEqual(c._grid[32], red, 'The pixel at (2,3) should be red')

        with self.assertRaises(IndexError):
            c[31, 21] = 10
Example #9
0
    def canvas(self):
        canvas_list = {(round(x),round(y)) for x,y in self.tracjectory}

        max_x = max(canvas_list, key=lambda loc: loc[0])[0]+1
        max_y = max(canvas_list, key=lambda loc: loc[1])[1]+1

        c = canvas(max_x,max_y)
        background_c = color(0.9,0.9,0.9)
        c.set_one_color(background_c)

        for pt in canvas_list:
            x = pt[0]
            y = max_y-pt[1]-1
            pt2 = x,y
            c[pt2] = color(1,0,0)

        return c
Example #10
0
    def test_PPM_colors(self):

        c = canvas(5, 3)
        c1 = color(1.5, 0, 0)
        c2 = color(0, 0.5, 0)
        c3 = color(-0.5, 0, 1)

        c[0, 0] = c1
        c[2, 1] = c2
        c[4, 2] = c3
        ppm = c.to_PPM()

        ppm_last_3_lines = '\n'.join(ppm.splitlines()[-3:])
        expected = '255 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n' +\
            '0 0 0 0 0 0 0 128 0 0 0 0 0 0 0\n' +\
            '0 0 0 0 0 0 0 0 0 0 0 0 0 0 255'

        self.assertEqual(ppm_last_3_lines, expected,
                         'The generated PPM is not what expected')
Example #11
0
    def test_canvas(self):

        c = canvas(10, 20)

        self.assertEqual(c.width, 10, 'The canvas width should be 10')
        self.assertEqual(c.height, 20, 'The canvas height should be 20')

        black_color = color(0, 0, 0)

        is_black = [c_color == black_color for c_color in c]

        self.assertTrue(all(is_black), 'All canvas pixels should be black')
Example #12
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")
Example #13
0
    def test_PPM_crop(self):

        c = canvas(10, 2)
        c1 = color(1, 0.8, 0.6)
        c.set_one_color(c1)

        ppm = c.to_PPM()

        ppm_last_4_lines = '\n'.join(ppm.splitlines()[3:]) + ppm[-1]

        expected = ('255 204 153 ' * 5 + '255 204\n' + '153 255 204 ' * 4 +
                    '153\n') * 2

        self.assertEqual(ppm_last_4_lines,expected,\
            'The generated ppm has not the expected crop')
Example #14
0
    def test_material(self):
        """Tests the material class constructor"""

        m_var = Material()

        self.assertEqual(m_var.color, color(1, 1, 1),\
            'Material color failed')
        self.assertEqual(m_var.ambient, 0.1,\
            'Material ambient failed')
        self.assertEqual(m_var.diffusse, 0.9,\
            'Material diffuse failed')
        self.assertEqual(m_var.specular, 0.9,\
            'Material specular failed')
        self.assertEqual(m_var.shininess, 200.0,\
            'Material shininess failed')
Example #15
0
    def __init__(self):
        """
        Material class.

        Material()

        Material class for scene objects.

        Parameters
        ----------

        Return
        ------
        -Material: the material class object.
        """

        self.color = color(1, 1, 1)
        self.ambient = 0.1
        self.diffusse = 0.9
        self.specular = 0.9
        self.shininess = 200.0
Example #16
0
"""
The point light source library
"""
from raytrace.util import color, normalize, dot, reflect

BLACK = color(0, 0, 0)


class PointLight:
    """
    Point light class.
    """
    def __init__(self, position, intensity):
        """
        Light source class.

        PointLight(position, intensity)

        A light source located at position and with intensity intensity.

        Parametes
        ---------
        -position (point): position where the light source is located.
        -intensity (color): intensity that the light source is.

        Return
        ------
        -PointLight: a PointLight object.
        """

        self.position = position
Example #17
0
ticks = [2* n * pi/div for n in range(div) ]

points = [ rotation_z(tick) * p for tick in ticks ]

canvas_list = {(round(pos.x), round(pos.y)) for pos in points}

min_x = min(canvas_list,key=lambda loc: loc[0])[0]
pad_x = 0 if min_x >= 0 else - min_x

min_y = min(canvas_list, key= lambda loc: loc[1])[1]
pad_y = 0 if min_y >=0 else - min_y

max_x = max(canvas_list, key = lambda loc: loc[0])[0] + 1 + pad_x
max_y = max(canvas_list, key = lambda loc: loc[1])[1] +1 + pad_y

canvas_list_padded = [(x+pad_x,max_y -1-y-pad_y) for x,y in canvas_list]

c = canvas(max_x, max_y)

background_c = color(0.9,0.9,0.9)
c.set_one_color(background_c)

for pt in canvas_list_padded:
    c[pt] = color(1,0,0)

ppm = c.to_PPM()

with open('clock.ppm','w') as f:
    f.write(ppm)

Example #18
0
'''
This file draws the shadow of the sphere in the canvas
'''

from raytrace.util import color, point, vector
from raytrace.ray import ray
from raytrace.canvas import canvas
from raytrace.objects import sphere, intersect, hit
from raytrace.transformation import translation, scaling
from raytrace.light import Material, PointLight

CANVAS_DISTANCE = 100
CANVAS_H = 1000
CANVAS_W = 1000
HIT_COLOR = color(1, 0, 0)

c = canvas(CANVAS_H, CANVAS_W)

background_c = color(0.9, 0.9, 0.9)
c.set_one_color(background_c)

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)
Example #19
0
    def test_color(self):

        c = color(-0.5, 0.4, 1.7)

        equal = (c.red == -0.5) and (c.green == 0.4) and (c.blue == 1.7)
        self.assertTrue(equal, 'The definition in color class should match')
Example #20
0
 def __init__(self, width, height):
     self.width = width
     self.height = height
     self._grid = [color(0, 0, 0)] * width * height