def setUp(self): # create a transform o2w = translate(Vector(10, 0, 0)) * scale(1.3, 1.8, 2.0) w2o = o2w.inverse() # create the sphere self.sphere = Sphere(o2w, w2o, False, 1.0, -1.0, 1.0, 360)
def create_pyramid(): primitives = [] # create the sphere primitive material = None object_to_world = Transform() world_to_object = Transform() sphere = Sphere(object_to_world, world_to_object, False, 1.0, -1.0, 1.0, 360) primitive = GeometricPrimitive(sphere, material, None) # now create the instances # create the objects, with this layout (shown in 2D) # O O O O level 0, width 4, pos_x -6 # O O O level 1, width 3, pos_x -4 # O O level 2, width 2, pos_x -2 # O level 3, width 1, pos_x 0 for level in range(4): width_array = 4 - level start_pos = Point(-2.0 * (3 - level), -2.0 * (3 - level), level * 2.0) for i in range(width_array): start_pos_i = start_pos + i * Vector(2.0, 0.0, 0.0) for j in range(width_array): pos = start_pos_i + j * Vector(0.0, 2.0, 0.0) transform = translate(pos).inverse() world_to_primitive = AnimatedTransform(transform, 0.0, transform, 1.0) instance = TransformedPrimitive(primitive, world_to_primitive) primitives.append(instance) return primitives
def setUp(self): # create a transform o2w = translate(Vector(10,0,0)) * scale(1.3, 1.8, 2.0) w2o = o2w.inverse() # create the sphere self.sphere = Sphere(o2w, w2o, False, 1.0, -1.0, 1.0, 360)
def create_simple_sphere(): primitives = [] material = None object_to_world = translate(Point(0, 1, 0)) world_to_object = object_to_world.inverse() sphere = Sphere(object_to_world, world_to_object, False, 1.0, -1.0, 1.0, 360) primitive = GeometricPrimitive(sphere, material, None) primitives.append(primitive) return primitives
def setUp(self): # create a transform o2w1 = translate(Vector(10, 0, 0)) * scale(1.3, 1.8, 2.0) w2o1 = o2w1.inverse() # create a sphere self.sphere1 = Sphere(o2w1, w2o1, False, 1.0, -1.0, 1.0, 360) self.primitive_sphere1 = GeometricPrimitive(self.sphere1, None, None) # create a transform o2w2 = translate(Vector(-10, 0, 0)) * scale(1.3, 1.8, 2.0) w2o2 = o2w2.inverse() # create a sphere self.sphere2 = Sphere(o2w2, w2o2, False, 1.0, -1.0, 1.0, 360) self.primitive_sphere2 = GeometricPrimitive(self.sphere2, None, None) primitives = [self.primitive_sphere1, self.primitive_sphere2] self.grid_accel = GridAccel(primitives, True)
class TestSphere(unittest.TestCase): def setUp(self): # create a transform o2w = translate(Vector(10, 0, 0)) * scale(1.3, 1.8, 2.0) w2o = o2w.inverse() # create the sphere self.sphere = Sphere(o2w, w2o, False, 1.0, -1.0, 1.0, 360) def test_intersect(self): # test an intersection ray = Ray(Point(20, 10, 10), Vector(-1, -1, -1)) intersect, t_hit, ray_epsilon, dg = self.sphere.intersect(ray) self.assertTrue(intersect) intersect = self.sphere.intersect_p(ray) self.assertTrue(intersect) # test an intersection ray = Ray(Point(20, 10, 10), Vector(-1, 1, -1)) intersect, t_hit, ray_epsilon, dg = self.sphere.intersect(ray) self.assertFalse(intersect) intersect = self.sphere.intersect_p(ray) self.assertFalse(intersect) # test an intersection ray = Ray(Point(10, 0, 0), Vector(3, 1, -2)) intersect, t_hit, ray_epsilon, dg = self.sphere.intersect(ray) self.assertTrue(intersect) intersect = self.sphere.intersect_p(ray) self.assertTrue(intersect)
class TestSphere(unittest.TestCase): def setUp(self): # create a transform o2w = translate(Vector(10,0,0)) * scale(1.3, 1.8, 2.0) w2o = o2w.inverse() # create the sphere self.sphere = Sphere(o2w, w2o, False, 1.0, -1.0, 1.0, 360) def test_intersect(self): # test an intersection ray = Ray(Point(20, 10, 10), Vector(-1, -1, -1)) intersect, t_hit, ray_epsilon, dg = self.sphere.intersect(ray) self.assertTrue(intersect) intersect = self.sphere.intersect_p(ray) self.assertTrue(intersect) # test an intersection ray = Ray(Point(20, 10, 10), Vector(-1, 1, -1)) intersect, t_hit, ray_epsilon, dg = self.sphere.intersect(ray) self.assertFalse(intersect) intersect = self.sphere.intersect_p(ray) self.assertFalse(intersect) # test an intersection ray = Ray(Point(10, 0, 0), Vector(3, 1, -2)) intersect, t_hit, ray_epsilon, dg = self.sphere.intersect(ray) self.assertTrue(intersect) intersect = self.sphere.intersect_p(ray) self.assertTrue(intersect)
def time_sphere_intersection(): # create a transform o2w = translate(Vector(10,0,0)) * scale(1.3, 1.8, 2.0) w2o = o2w.inverse() # create the sphere sphere = Sphere(o2w, w2o, False, 1.0, -1.0, 1.0, 360) # create a large amount of rays, # choose so that half of them will intersect the ray positions = [Point(random.randint(0,100), random.randint(0,100), random.randint(0,100) ) for i in range(size)] ray = Ray(Point(0,0,0), Vector(1.0, 1.0, 1.0)) vectors = [] for i in xrange(size): position = positions[i] if i%2 == 0: # make sure this ray hit the sphere vector = sphere.object_to_world(Point(0, 0, 0)) - position vector /= float(random.randint(1,10)) else: # construct a random vector vector = Vector((random.random()-0.5)*random.randint(1,5), (random.random()-0.5)*random.randint(1,5), (random.random()-0.5*random.randint(1,5))) vectors.append(vector) intersections = 0 t1 = time.time() for i in xrange(nb_calls): ray.o = positions[i%size] ray.d = vectors[i%size] if sphere.intersect_p(ray): intersections += 1 t2 = time.time() for i in xrange(nb_calls): ray.o = positions[i%size] ray.d = vectors[i%size] sphere.intersect(ray) t3 = time.time() print "%d calls, %d intersections" % (nb_calls, intersections) print "Sphere.intersect_p() %.2fms" % ((t2-t1)/float(nb_calls)*1000.0) print "Sphere.intersect() %.2fms" % ((t3-t2)/float(nb_calls)*1000.0)
def initScene(): global scene camera = Camera() camera.position = Vector(0, 1, 5) camera.point_at = Vector(0, 1, 0) scene = Scene(window.width, window.height, camera) log.info("Initialized scene with {0}x{1} image, {2}".format(window.width, window.height, camera)) scene.ambient_light = color.scale(color.WHITE, 0.1) light = PointLight() light.position = Vector(5, 8, 8) log.info("Adding {0} to scene".format(light)) scene.lights.append(light) light2 = PointLight() light2.position = Vector(-5, 8, 8) log.info("Adding {0} to scene".format(light2)) scene.lights.append(light2) plane = Plane() plane.material.color = color.scale(color.WHITE, 0.5) plane.material.hardness = 0 plane.material.specular = 0.5 plane.material.reflection = 0.1 log.info("Adding {0} to scene".format(plane)) scene.shapes.append(plane) sphere = Sphere() sphere.position = Vector(1, 2, -3) sphere.radius = 1 sphere.material.color = color.RED sphere.material.specular = 0.5 sphere.material.hardness = 50 sphere.material.reflection = 0.5 log.info("Adding {0} to scene".format(sphere)) scene.shapes.append(sphere) sphere2 = Sphere() sphere2.position = Vector(-1, 1, -1.5) sphere2.radius = 1 sphere2.material.color = color.YELLOW sphere2.material.specular = 0.5 sphere2.material.hardness = 50 sphere2.material.reflection = 0.5 log.info("Adding {0} to scene".format(sphere2)) scene.shapes.append(sphere2)