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 generate_ray(self, sample): """Generate a Ray from the camera.""" # Generate raster and camera samples p_ras = Point(sample.image_x, sample.image_y, 0) p_camera = self.raster_to_camera(p_ras) ray = Ray(Point(0, 0, 0), normalize(Vector.from_point(p_camera)), 0.0, float('inf')) # Modify ray for depth of field if self.lens_radius > 0.0: # Sample point on lens lens_u, lens_v = concentric_sample_disk(sample.lens_u, sample.lens_v) lens_u *= self.lens_radius lens_v *= self.lens_radius # Compute point on plane of focus ft = self.focal_distance / ray.d.z p_focus = ray(ft) # Update ray for effect of lens ray.o = Point(lens_u, lens_v, 0.0) ray.d = normalize(p_focus - ray.o) ray.time = sample.time ray = self.camera_to_world(ray) return 1.0, ray