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)