def test_intersect_with_transform(self): # intersect a scaled sphere r = Ray(Point(0, 0, -5), Vector(0, 0, 1)) s = Sphere() s.transform = Matrix.scale(2, 2, 2) xs = s.intersect(r) self.assertEqual(len(xs), 2) self.assertEqual(xs[0].t, 3) self.assertEqual(xs[1].t, 7) # intersect a translated sphere r = Ray(Point(0, 0, -5), Vector(0, 0, 1)) s = Sphere() s.transform = Matrix.translate(5, 0, 0) xs = s.intersect(r) self.assertEqual(len(xs), 0)
def test_intersect3(self): origin = Point(0, 2, -5) direction = Vector(0, 0, 1) r = Ray(origin, direction) s = Sphere() xs = Intersections([]) self.assertTrue(s.intersect(r) == xs)
def test_intersect(self): r = Ray(Point(0, 0, -5), Vector(0, 0, 1)) s = Sphere() xs = s.intersect(r) self.assertEqual(len(xs), 2) self.assertEqual(xs[0].object, s) self.assertEqual(xs[1].object, s)
def test_ray_originate_inside_sphere(self): r = Ray(Point(0, 0, 0), Vector(0, 0, 1)) s = Sphere() xs = s.intersect(r) self.assertEqual(len(xs), 2) self.assertEqual(xs[0].t, -1.0) self.assertEqual(xs[1].t, 1.0)
def test_sphere_behind_ray(self): r = Ray(Point(0, 0, 5), Vector(0, 0, 1)) s = Sphere() xs = s.intersect(r) self.assertEqual(len(xs), 2) self.assertEqual(xs[0].t, -6.0) self.assertEqual(xs[1].t, -4.0)
def test_ray_intersect_sphere_tangent(self): r = Ray(Point(0, 1, -5), Vector(0, 0, 1)) s = Sphere() xs = s.intersect(r) self.assertEqual(len(xs), 2) self.assertEqual(xs[0].t, 5.0) self.assertEqual(xs[1].t, 5.0)
def test_intersect_scaled_sphere_with_ray(self): r = Ray(Point(0, 0, -5), Vector(0, 0, 1)) s = Sphere() s.transform = Transformations.scaling(2, 2, 2) xs = s.intersect(r) self.assertEqual(len(xs), 2) self.assertEqual(xs[0].t, 3) self.assertEqual(xs[1].t, 7)
def test_intersect5(self): origin = Point(0, 0, 5) direction = Vector(0, 0, 1) r = Ray(origin, direction) s = Sphere() i1 = Intersection(-6, s) i2 = Intersection(-4, s) xs = Intersections([i1, i2]) self.assertTrue(s.intersect(r) == xs)
def test_transform2(self): r = Ray(Point(0, 0, -5), Vector(0, 0, 1)) s = Sphere() s.set_transform(scale(2, 2, 2)) xs = s.intersect(r) self.assertEqual(xs.count, 2) self.assertTrue(xs[0].t, 3) self.assertTrue(xs[1].t, 7)
def test_intersect_sphere(self): # through the center r = Ray(Point(0, 0, -5), Vector(0, 0, 1)) s = Sphere() xs = s.intersect(r) self.assertEqual(len(xs), 2) self.assertEqual(xs[0].t, 4) self.assertEqual(xs[1].t, 6) # at a tangent r = Ray(Point(0, 1, -5), Vector(0, 0, 1)) s = Sphere() xs = s.intersect(r) self.assertEqual(len(xs), 2) self.assertEqual(xs[0].t, 5) self.assertEqual(xs[1].t, 5) # why two points? # no intersection r = Ray(Point(0, 2, -5), Vector(0, 0, 1)) s = Sphere() xs = s.intersect(r) self.assertEqual(len(xs), 0) # starting inside the sphere r = Ray(Point(0, 0, 0), Vector(0, 0, 1)) s = Sphere() xs = s.intersect(r) self.assertEqual(len(xs), 2) self.assertEqual(xs[0].t, -1) self.assertEqual(xs[1].t, 1) # starting past the sphere r = Ray(Point(0, 0, 5), Vector(0, 0, 1)) s = Sphere() xs = s.intersect(r) self.assertEqual(len(xs), 2) self.assertEqual(xs[0].t, -6) self.assertEqual(xs[1].t, -4)
def get_hit_point_draw(combined_ray, linecolor): start = np.array([0.0, 0.0, 0.0]) rot = np.array([0.0, 1.0, 0, 0.0]) # rotate a vector by quaternion rotation dir = rotate_vec_by_quaternion(rot, combined_ray) end = start + dir #get hitting point sphere = Sphere(Point3(0, 0, 0), 1) ray = Ray3(Point3(start[0], start[1], start[2]), Vector3(dir[0], dir[1], dir[2])) hitpoint = sphere.intersect(ray) #check if hitting point is on sphere surface # check_point_on_sphere(0, 0, 0, hitpoint, 1) # convert unity coordinate to python coordinate unity_to_python_point(start) unity_to_python_point(end) unity_to_python_point(hitpoint) return hitpoint
D = 200 im = Image.new('RGB', (W, H)) pix = im.load() if False: ## this is done in image coordinates eye = Point(W / 2, H / 2, D) sphere = Sphere() ts = Matrix.scale(50, 50, 50) tt = Matrix.translate(W / 2, H / 2, D / 4) sphere.transform = tt * ts for x in range(W): for y in range(H): ray = Ray(eye, Point(x, y, 0) - eye) xs = sphere.intersect(ray) if xs: pix[x, y] = (255, 0, 0) print(x) else: ## this is done in object coordinates eye = Point(0, 0, -5) sphere = Sphere() tt = Matrix.translate(0, 0, -2) sphere.transform = tt wall = (-3, 3, -3, 3) # LRBT ms = Matrix.scale( float(wall[1] - wall[0]) / W, float(wall[3] - wall[2]) / H, 1.0)
rotate_z(pi / 6) * scale(1, 0.5, 1) * shear(1, 0, 0, 0, 0, 0)) camera = Point(0, 0, -5) wall_z = 10 wall_size = 7 canvas_pixels = 100 pixel_size = wall_size / canvas_pixels half = wall_size / 2 for j in range(HEIGHT): print(j) y = half - pixel_size * j for i in range(WIDTH): x = -half + pixel_size * i target = Point(x, y, wall_z) ray = Ray(camera, (target - camera).normalize()) intersections = sphere.intersect(ray) obj = intersections.hit() if obj is not None: c.write_pixel(i, j, Color(255, 0, 0)) # print(j, "target: ", target, obj.t) with open('test_013.ppm', 'w') as fout: c.to_ppm(fout) # while True: # orig = Point(0., 1., 0.) # p = orig # run = True # for i in range(12): # p = rotate(p)
def test_intersect_translated_sphere(self): r = Ray(Point(0, 0, -5), Vector(0, 0, 1)) s = Sphere() s.transform = Transformations.translation(5, 0, 0) xs = s.intersect(r) self.assertEqual(len(xs), 0)
def test_ray_miss_sphere(self): r = Ray(Point(0, 2, -5), Vector(0, 0, 1)) s = Sphere() xs = s.intersect(r) self.assertEqual(len(xs), 0)
for x in range(canvas_pixels): world_x = -half + pixel_size * x position = Point(world_x, world_y, wall_z) r = Ray(ray_origin, Vector.normalize(position - ray_origin)) # shrink along y-axis # r = r.transform(Transformations.scaling(1, 0.5, 1)) # shrink along x-axis # r = r.transform(Transformations.scaling(0.5, 1, 1)) # shrink and rotate # r = r.transform(Transformations.rotation_z(math.pi / 4)).transform(Transformations.scaling(0.5, 1, 1)) # shrink and skew # r = r.transform(Transformations.shearing(1, 0, 0, 0, 0, 0)).transform(Transformations.scaling(0.5, 1, 1)) xs = shape.intersect(r) hit = Intersection.hit(xs) if hit is not None: point = Ray.position(r, hit.t) normal = shape.normal_at(point) eye = -r.direction color = PointLight.lighting(hit.object.material, hit.object, light, point, eye, normal, False) canvas.write_pixel(x, y, color) canvas.canvas_to_ppm()