Exemple #1
0
 def test_ray_equality_2(self):
     p1 = data.Point(0.1, 1.0, 2.0)
     v1 = data.Vector(3.0, 4.0, 5.0)
     r1 = data.Ray(p1, v1)
     p2 = data.Point(0.0, 1.0, 2.0)
     v2 = data.Vector(3.0, 4.0, 5.0)
     r2 = data.Ray(p2, v2)
     self.assertFalse(r1 == r2)
Exemple #2
0
def true_color(list, eye_point, color, light, sphere_list, point):
   mindex = 0
   for i in range(1, len(list)):
      n = dist_from_eye(list[mindex][1], eye_point)
      m = dist_from_eye(list[i][1], eye_point)
      if m < n:
         mindex = i
   
   first = list[mindex][0].color
   finish = list[mindex][0].finish
   color_with_finish = default_color(first, finish, color)

   M = scale_vector(sphere_normal_at_point(list[mindex][0], list[mindex][1])\
       , 0.01)
   pe = translate_point(list[mindex][1], M)
   N = scale_vector(M, 100)
   ldir = normalize_vector(vector_from_to(pe, light.pt))

   dot = dot_vector(N, ldir)
   if dot > 0: #light is visible
      lray = data.Ray(pe, ldir)
      if is_closer_sphere(sphere_list, lray, light):
         rvec = difference_vector(ldir, scale_vector(N, 2 * dot))
         vdir = normalize_vector(vector_from_to(point, pe))
         spec = dot_vector(rvec, vdir)

         if spec > 0: #specular intensity contributes
            return spec_color(color_with_finish, first, dot, finish, light, spec)

         else: #specular intensity does not contribute
            return diff_color(color_with_finish, first, dot, finish, light)

   return color_with_finish
Exemple #3
0
def cast_all_rays(min_x, max_x, min_y, max_y, width, height, eye_point,
                  sphere_list, color, light):
    w = math.fabs(min_x) + math.fabs(max_x)
    changeX = w / width
    xval = [(min_x + (vals * changeX)) for vals in range(width)]
    yheight = math.fabs(min_y) + math.fabs(max_y)
    changeY = yheight / height
    yval = [(max_y - (vals * changeY)) for vals in range(height)]

    xstep = (max_x - min_x) / float(width)
    ystep = (max_y - min_y) / float(height)
    print 'P3'
    print str(width) + ' ' + str(height)
    print '255'

    for y in yval:
        for x in xval:
            ray = data.Ray(
                eye_point,
                vector_math.difference_point(data.Point(x, y, 0), eye_point))
            finalColors = cast_ray(ray, sphere_list, color, light, eye_point)
            if finalColors:
                print str(int(finalColors.r * 255)) + ' ' + str(
                    int(finalColors.g * 255)) + ' ' + str(
                        int(finalColors.b * 255))
            else:
                print '255 255 255'
Exemple #4
0
 def test_ray(self):
     point = data.Point(1, 2, 3)
     vector = data.Vector(4, 5, 6)
     ray = data.Ray(point, vector)
     self.assertAlmostEqual(ray.pt.x, 1)
     self.assertAlmostEqual(ray.pt.y, 2)
     self.assertAlmostEqual(ray.pt.z, 3)
     self.assertAlmostEqual(ray.dir.x, 4)
     self.assertAlmostEqual(ray.dir.y, 5)
     self.assertAlmostEqual(ray.dir.z, 6)
     self.assertEqual(ray.pt, point)
     self.assertEqual(ray.dir, vector)
     self.assertEqual(ray.pt, data.Point(1, 2, 3))
     self.assertEqual(ray.dir, data.Vector(4, 5, 6))
     self.assertEqual(ray,
                      data.Ray(data.Point(1, 2, 3), data.Vector(4, 5, 6)))
Exemple #5
0
 def test_sphere_intersection_point1(self):
     theRay = data.Ray(data.Point(0, 0, 0), data.Vector(1, 0, 0))
     theSphere = data.Sphere(data.Point(0, 0, 0), 1)
     pointt = collisions.sphere_intersection_point(theRay, theSphere)
     self.assertAlmostEqual(pointt.x, 1.0)
     self.assertAlmostEqual(pointt.y, 0.0)
     self.assertAlmostEqual(pointt.z, 0.0)
Exemple #6
0
 def test_sphere_intersection_point_2(self):
     #ray intersects at two points
     sphere = data.Sphere(data.Point(0, 0, 0), 5)
     ray = data.Ray(data.Point(0, -10, 0), data.Vector(0, 5, 0))
     outcome = data.Point(0, -5, 0)
     self.assertEqual(collisions.sphere_intersection_point(ray, sphere),
                      outcome)
Exemple #7
0
 def test_cast_ray_4(self):
     ray = data.Ray(data.Point(0.0, -3.0, -3.0), data.Vector(2.0, 1.0, 1.0))
     sphere1 = data.Sphere(data.Point(0.0, 2.0, 2.0), 6.0, data.Color(1.0, 0.0, 0.0))
     sphere2 = data.Sphere(data.Point(-10.0, -15.0, -20.0), 2.0, data.Color(0.0, 0.0, 1.0))
     sphere_list = [sphere1, sphere2]
     casted = cast.cast_ray(ray, sphere_list)
     self.assertEqual(casted, data.Color(1.0, 0.0, 0.0))
Exemple #8
0
def cast_all_rays(min_x, max_x, min_y, max_y, width, height, eye_point,
                  sphere_list, color, light):
    dx = (max_x - min_x) / float(width)
    dy = (max_y - min_y) / float(height)
    print_header(width, height)
    for y in range(height):
        for x in range(width):
            valx = min_x + (x * dx)
            valy = max_y - (y * dy)
            pixpt = data.Point(valx, valy, 0)
            edir = vector_from_to(eye_point, pixpt)
            eray = data.Ray(eye_point, edir)

            pcolor = cast_ray(eray, sphere_list, color, light, eye_point)
            r = int(pcolor.r * 255)
            if r > 255:
                r = 255

            g = int(pcolor.g * 255)
            if g > 255:
                g = 255

            b = int(pcolor.b * 255)
            if b > 255:
                b = 255
            print r, g, b
def cast_all_rays(min_x, max_x, min_y, max_y, width, height, eye_point, sphere_list, ambient, light, file_name):

    # not certain about the file type of the image file here, I put .txt for now
    image_file = open(file_name, "w")

    num_of_pixel_x = (max_x - min_x) / float(width)
    num_of_pixel_y = (max_y - min_y) / float(height)

    #print("P3")
    #print(str(width), str(height))
    #print("255")
    image_file.write("P3\n")
    image_file.write(str(width) + " " + str(height) + "\n")
    image_file.write("255\n")

    #print("before going in double for loop in cast_all_rays")
    count = 0

    for row in range(height):
        #print("in row loop")
        y = max_y - row * num_of_pixel_y
        for col in range(width):
            x = min_x + col * num_of_pixel_x
            # z-coordinate is constant there for z = 0.0 for all point on image plane
            point_on_image_plane = data.Point(x, y, 0.0)
            ray_direction_vector = vector_math.difference_point(point_on_image_plane, eye_point)
            constructed_ray = data.Ray(eye_point, ray_direction_vector)
            pixel_color = cast_ray(constructed_ray, sphere_list, light, ambient, eye_point)
            #print('{:4d}{:4d}{:4d}'.format(min(int(pixel_color.r * 255), 255), min(int(pixel_color.g * 255), 255), min(int(pixel_color.b * 255), 255)), end='   ')
            image_file.write('{0:4d}{1:4d}{2:4d}'.format(min(int(pixel_color.r * 255), 255), min(int(pixel_color.g * 255), 255), min(int(pixel_color.b * 255), 255)))
            count += 1
            print("loop time " + str(count))
        image_file.write("\n")
        #print("loop time " + str(count))
    image_file.close()
Exemple #10
0
 def test_cast_ray_5(self):
     ray = data.Ray(data.Point(0.0, 0.0, 0.0), data.Vector(5.0, 0.0, 0.0))
     sphere1 = data.Sphere(data.Point(5.0, 0.0, -5.0), 5.0, data.Color(1.0, 0.0, 0.0))
     sphere2 = data.Sphere(data.Point(17.0, 0.0, 5.0), 5.0, data.Color(0.0, 0.0, 1.0))
     sphere_list = [sphere1, sphere2]
     casted = cast.cast_ray(ray, sphere_list)
     self.assertEqual(casted, data.Color(1.0, 0.0, 0.0))
Exemple #11
0
def cast_all_rays(min_x, max_x, min_y, max_y, width, height, eye_point,
                  sphere_list, ambient_color, light):

    f = open("image.ppm", 'w')

    f.write("P3\n")
    f.write("%d %d\n" % (width, height))
    f.write("255\n")

    w = max_x - min_x
    h = max_y - min_y
    scale_x = w / width
    scale_y = h / height

    total = width * height
    percent = 0

    for i in range(height):
        print int(percent)
        for j in range(width):
            position = width * i + j
            percent = float(position) / total * 100
            x = j * scale_x + min_x
            y = max_y - i * scale_y
            dir = vector_math.vector_from_to(eye_point, data.Point(x, y, 0))
            ray = data.Ray(eye_point, dir)
            b = cast_ray(ray, sphere_list, ambient_color, light, eye_point)
            print_color(f, b)
Exemple #12
0
 def test_ray_2(self):
     point2 = data.Point(0, -1, -2.0)
     vector2 = data.Vector(1, 2, 3.0)
     ray2 = data.Ray(point2, vector2)
     self.assertAlmostEqual(ray2.pt.x, 0)
     self.assertAlmostEqual(ray2.pt.y, -1)
     self.assertAlmostEqual(ray2.pt.z, -2)
     self.assertAlmostEqual(ray2.dir.x, 1)
     self.assertAlmostEqual(ray2.dir.y, 2)
     self.assertAlmostEqual(ray2.dir.z, 3)
     self.assertEqual(ray2.pt, point2)
     self.assertEqual(ray2.dir, vector2)
     self.assertEqual(ray2.pt, data.Point(0, -1, -2))
     self.assertEqual(ray2.dir, data.Vector(1, 2, 3))
     self.assertEqual(ray2,
                      data.Ray(data.Point(0, -1, -2), data.Vector(1, 2, 3)))
Exemple #13
0
 def test_sphere_intersection_point(self):
     #no intersection with sphere
     sphere = data.Sphere(data.Point(0, 0, 0), 1)
     ray = data.Ray(data.Point(5, 0, 0), data.Vector(10, 0, 0))
     outcome = None
     self.assertEqual(collisions.sphere_intersection_point(ray, sphere),
                      outcome)
def calculate_light_components(
        sphere_point: typing.Tuple[data.Sphere, data.Point],
        spheres_list: typing.List[data.Sphere], eye_point: data.Point,
        light: data.Light) -> typing.Tuple[float, float]:
    Pe = compute_Pe(sphere_point)
    normal = collisions.sphere_normal_at_point(sphere_point[0],
                                               sphere_point[1])
    L_dir = v_math.normalize_vector(v_math.vector_from_to(Pe, light.pt))
    L_ray = data.Ray(Pe, L_dir)

    for other_sphere in spheres_list:
        if other_sphere is not sphere_point[0]:
            L_ray_intersection = collisions.sphere_intersection_point(
                L_ray, other_sphere)
            if L_ray_intersection:
                distance_sphere = distance_between_points(
                    Pe, L_ray_intersection)
                distance_light = distance_between_points(Pe, light.pt)
                if distance_sphere < distance_light:
                    return (0, 0)

    normal_Ldir = v_math.dot_vector(normal, L_dir)

    if normal_Ldir > 0:
        specular_intensity = compute_specular_intensity(
            Pe, normal, L_dir, normal_Ldir, eye_point)
        return (normal_Ldir, specular_intensity)
    else:
        return (0, 0)
Exemple #15
0
 def test_sphere_intersection_1(self):
     point_ray = data.Point(-10.0, 0.0, 0.0)
     direction = data.Vector(1.0, 0.0, 0.0)
     ray = data.Ray(point_ray, direction)
     point1_sphere = data.Point(0.0, 1.0, 0.0)
     sphere1 = data.Sphere(point1_sphere, 1.0)
     collision_point = collisions.sphere_intersection_point(ray, sphere1)
     self.assertEqual(collision_point, data.Point(0.0, 0.0, 0.0))
Exemple #16
0
 def test_ray_1(self):
     ray1 = data.Ray(data.Point(0, 0, 0), data.Vector(1, 0, 0))
     self.assertAlmostEqual(ray1.pt.x, 0)
     self.assertAlmostEqual(ray1.pt.y, 0)
     self.assertAlmostEqual(ray1.pt.z, 0)
     self.assertAlmostEqual(ray1.dir.x, 1)
     self.assertAlmostEqual(ray1.dir.y, 0)
     self.assertAlmostEqual(ray1.dir.z, 0)
Exemple #17
0
 def test_cast_ray1(self):
     ray = data.Ray(data.Point(0, 0, 0), data.Vector(1, 0, 0))
     sphere_list = [
         data.Sphere(data.Point(1, 1, 1), 3),
         data.Sphere(data.Point(3, 3, 3), 1)
     ]
     boolean = cast.cast_ray(ray, sphere_list)
     self.assertEqual(boolean, True)
Exemple #18
0
 def test_sphere_intersection_3(self):
     point_ray = data.Point(-2581.5873, 7307.182, -4513.9069)
     direction = data.Vector(2669.2004, -7638.7545, -83.06)
     ray = data.Ray(point_ray, direction)
     point_sphere = data.Point(-188.86, -50.360, -300.360)
     sphere = data.Sphere(point_sphere, 400)
     collision_point = collisions.sphere_intersection_point(ray, sphere)
     self.assertEqual(collision_point, None)
Exemple #19
0
 def test_sphere_intersection_0(self):
     point_ray = data.Point(-10.0, 0.0, 0.0)
     direction = data.Vector(1.0, 0.0, 0.0)
     ray = data.Ray(point_ray, direction)
     point0_sphere = data.Point(0.0, 2.0, 0.0)
     sphere0 = data.Sphere(point0_sphere, 1.0)
     self.assertEqual(collisions.sphere_intersection_point(ray, sphere0),
                      None)
Exemple #20
0
 def test_cast_ray(self):
     r = data.Ray(data.Point(0, 0, 0), data.Vector(0, 50, 0))
     sphere_list = [
         data.Sphere(data.Point(0, 50, 0), 10, data.Color(0.0, 1.0, 1.0)),
         data.Sphere(data.Point(0, 100, 0), 10, data.Color(1.0, 1.0, 0.0))
     ]
     self.assertEqual(cast.cast_ray(r, sphere_list),
                      data.Color(0.0, 1.0, 1.0))
Exemple #21
0
 def test_find_sphere_intersections_0(self):
     point_ray = data.Point(-10.0, 0.0, 0.0)
     direction = data.Vector(1.0, 0.0, 0.0)
     ray = data.Ray(point_ray, direction)
     point0_sphere = data.Point(0.0, 2.0, 0.0)
     sphere0 = data.Sphere(point0_sphere, 1.0)
     collision_points = collisions.find_intersection_points([sphere0], ray)
     self.assertListAlmostEqual(collision_points, [])
Exemple #22
0
 def test_find_intersection_2(self):
     sphere_1 = data.Sphere(data.Point(20, 20, 20), 2)
     sphere_2 = data.Sphere(data.Point(60, 60, 60), 1)
     s_list = [sphere_1, sphere_2]
     new_list = []
     r = data.Ray(data.Point(0, 0, 0), data.Vector(0, 0, 1))
     self.assertEqual(collisions.find_intersection_points(s_list, r),
                      new_list)
Exemple #23
0
 def test_find_intersection(self):
     sphere_1 = data.Sphere(data.Point(1, 1, 0), 1)
     sphere_2 = data.Sphere(data.Point(200, 200, 200), 1)
     s_list = [sphere_1, sphere_2]
     new_list = [(data.Sphere(data.Point(1, 1, 0), 1), data.Point(0, 1, 0))]
     r = data.Ray(data.Point(-1, 1, 0), data.Vector(20, 0, 0))
     self.assertEqual(collisions.find_intersection_points(s_list, r),
                      new_list)
Exemple #24
0
 def test_cast_ray(self):
     ray = data.Ray(data.Point(0, 0, 0), data.Vector(1, 0, 0))
     sphere_list = [
         data.Sphere(data.Point(1, 1, 1), 1),
         data.Sphere(data.Point(3, 3, 3), 1)
     ]
     color = data.Color
     boolean = cast.cast_ray(ray, sphere_list, color)
     self.assertEqual(boolean, False)
Exemple #25
0
    def test_cast_ray(self):
        sph1 = data.Sphere(data.Point(0, 3, 3), 5, data.Color(1.0, 1.0, 0.0), \
        data.Finish(0.5, 0.4, 0.5, 0.05))
        sph2 = data.Sphere(data.Point(5, 1, 2), 2, data.Color(0.0, 1.0, 1.0), \
        data.Finish(0.9, 0.4, 0.5, 0.05))
        sph3 = data.Sphere(data.Point(2, 2, 2), 4, data.Color(1.0, 0.0, 1.0), \
        data.Finish(0.8, 0.4, 0.5, 0.05))
        N = [sph1, sph2, sph3]

        ray1 = data.Ray(data.Point(4, 4, 4), data.Vector(-2, -2, -2))
        self.assertTrue(cast.cast_ray(ray1, N, 0.8, data.Light(data.Point(-100.0, \
        100.0, -100.0), data.Color(1.5, 1.5, 1.5)), data.Point(0.0, 0.0, -14.0)), \
        data.Color(0.8, 0.8, 0.8))

        ray2 = data.Ray(data.Point(3, 5, 1), data.Vector(-3, 1, 4))
        self.assertTrue(cast.cast_ray(ray2, N, 0.5, data.Light(data.Point(-100.0, \
        100.0, -100.0), data.Color(1.5, 1.5, 1.5)), data.Point(0.0, 0.0, -14.0)), \
        data.Color(1.0, 1.0, 1.0))
    def test_ray2(self):
        rt2 = data.Ray(data.Point(0, 0, 0), data.Vector(0, 0, 0))
        self.assertAlmostEqual(rt2.pt.x, 0)
        self.assertAlmostEqual(rt2.pt.y, 0)
        self.assertAlmostEqual(rt2.pt.z, 0)

        self.assertAlmostEqual(rt2.dir.x, 0)
        self.assertAlmostEqual(rt2.dir.y, 0)
        self.assertAlmostEqual(rt2.dir.z, 0)
    def test_ray1(self):
        rt1 = data.Ray(data.Point(-9, 7, 3.55), data.Vector(8, -6, 888))
        self.assertAlmostEqual(rt1.pt.x, -9)
        self.assertAlmostEqual(rt1.pt.y, 7)
        self.assertAlmostEqual(rt1.pt.z, 3.55)

        self.assertAlmostEqual(rt1.dir.x, 8)
        self.assertAlmostEqual(rt1.dir.y, -6)
        self.assertAlmostEqual(rt1.dir.z, 888)
Exemple #28
0
 def test_ray_2(self):
     ray2 = data.Ray(data.Point(1, 2, 3), data.Vector(2.0, 3, 3))
     self.assertAlmostEqual(ray2.pt.x, 1)
     self.assertAlmostEqual(ray2.pt.y, 2)
     self.assertAlmostEqual(ray2.pt.z, 3)
     self.assertAlmostEqual(ray2.dir.x, 2.0)
     self.assertAlmostEqual(ray2.dir.y, 3)
     self.assertAlmostEqual(ray2.dir.z, 3)
     pass
Exemple #29
0
 def test_ray_2(self):
     pt = data.Point(6.8, 6.9, 8.1)
     dir = data.Vector(4.4, 3.2, 2.2)
     my_ray = data.Ray(pt, dir)
     self.assertAlmostEqual(my_ray.pt.x, 6.8)
     self.assertAlmostEqual(my_ray.pt.y, 6.9)
     self.assertAlmostEqual(my_ray.pt.z, 8.1)
     self.assertAlmostEqual(my_ray.dir.x, 4.4)
     self.assertAlmostEqual(my_ray.dir.y, 3.2)
     self.assertAlmostEqual(my_ray.dir.z, 2.2)
Exemple #30
0
 def test_ray_1(self):
     pt = data.Point(9.9, 10.1, 11.1)
     dir = data.Vector(1.1, 5.6, 7.9)
     my_ray = data.Ray(pt, dir)
     self.assertAlmostEqual(my_ray.pt.x, 9.9)
     self.assertAlmostEqual(my_ray.pt.y, 10.1)
     self.assertAlmostEqual(my_ray.pt.z, 11.1)
     self.assertAlmostEqual(my_ray.dir.x, 1.1)
     self.assertAlmostEqual(my_ray.dir.y, 5.6)
     self.assertAlmostEqual(my_ray.dir.z, 7.9)