示例#1
0
    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)
示例#2
0
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
示例#3
0
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
示例#4
0
    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)
示例#5
0
 def test_transform_ray(self):
     ray = Ray(origin=Point(1, 2, 3),
               direction=Vector(10, 20, 30))
     ray_transformed = translate(Point(10, 20, 30))(ray)
     
     self.assertTrue(isinstance(ray_transformed, Ray))
     self.assertEqual(ray_transformed.o, Point(11, 22, 33))
     self.assertEqual(ray_transformed.d, Vector(10, 20, 30))
示例#6
0
    def test_transform(self):
        p = Point(1, 2, 3)
        p2 = translate(Point(10, 20, 30))(p)
        self.assertTrue(isinstance(p2, Point))
        self.assertEqual(p2, Point(11, 22, 33))
        
        v = Vector(1, 2, 3)
        v2 = translate(Point(10, 20, 30))(v)
        self.assertTrue(isinstance(v2, Vector))
        self.assertEqual(v2, Vector(1, 2, 3))

        self.assertEqual(scale(2, 3, 4)(Point(1, 2, 3)),
                         Point(2, 6, 12))
        self.assertEqual(scale(2, 3, 4)(Vector(1, 2, 3)),
                         Vector(2, 6, 12))
        self.assertEqual(rotate(90, Vector(0, 1, 0))(Normal(1, 0, 0)),
                         Normal(0, 0, -1))
示例#7
0
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
示例#8
0
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
示例#9
0
    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)
示例#10
0
 def test_transform_bbox(self):
     box = BBox(Point(-1, -2, 0), Point(0, 3, -4))
     box_transformed = translate(Point(10, 20, 30))(box)
     self.assertTrue(isinstance(box_transformed, BBox))
     self.assertEqual(box_transformed.p_min, Point(9, 18, 26))
     self.assertEqual(box_transformed.p_max, Point(10, 23, 30))
     
     box_transformed2 = scale(2, 3, 4)(box)
     self.assertTrue(isinstance(box_transformed2, BBox))
     self.assertEqual(box_transformed2.p_min, Point(-2, -6, -16))
     self.assertEqual(box_transformed2.p_max, Point(0, 9, 0))
示例#11
0
 def test_decompose(self):
     vector_translate = Vector(10.0, 20.0, 30.0)
     matrix_translate = translate(vector_translate)
     matrix_rotation = rotate(35.0, Vector(1.0, 2.0, 3.0))
     vector_scale = Vector(1.2, 3.4, 3.2)
     matrix_scale = scale(vector_scale.x, vector_scale.y, vector_scale.z)
     transform = matrix_translate * matrix_rotation * matrix_scale
     vector_, quaternion_, scale_ = decompose(transform.m)
     self.assertEqual(vector_translate, vector_)
     self.assertEqual(matrix_rotation, quaternion_.to_transform())
     self.assertEqual(matrix_scale.m, scale_)
示例#12
0
    def test_inverse(self):
        m1 = scale(2.0, 3.0, 4.0)
        m2 = scale(1.0/2.0, 1.0/3.0, 1.0/4.0)
        self.assertEqual(m1.inverse(), m2)
        self.assertEqual(m1.m_inv, m2.m)
        self.assertEqual(m2.m_inv, m1.m)

        m3 = translate(Point(5, 6, 7)) * scale(2, -3 , 4) * rotate(17, Vector(-1, 4, -2))
        m4 = m3.inverse()
        identity = Transform()
        self.assertTrue((m3*m4).is_identity())
        self.assertTrue((m4*m3).is_identity())
示例#13
0
    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)
示例#14
0
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)
示例#15
0
    def test_transform_ray(self):
        ray_differential = RayDifferential(origin=Point(1,2,3),
                                           direction=Vector(10,20,30))
        ray_differential.rx_origin = Point(4,5,6)
        ray_differential.ry_origin = Point(5,6,7)
        ray_differential.rx_direction = Vector(2,3,4)
        ray_differential.ry_direction = Vector(3,4,5)
        ray_transformed = translate(Point(10,20,30))(ray_differential)

        self.assertTrue(isinstance(ray_transformed, RayDifferential))
        self.assertEqual(ray_transformed.o, Point(11,22,33))
        self.assertEqual(ray_transformed.d, Vector(10,20,30))
        self.assertEqual(ray_transformed.rx_origin, Point(14,25,36))
        self.assertEqual(ray_transformed.ry_origin, Point(15,26,37))
        self.assertEqual(ray_transformed.rx_direction, Vector(2,3,4))
        self.assertEqual(ray_transformed.ry_direction, Vector(3,4,5))
示例#16
0
    def __init__(self, camera_to_world, projection, screen_window, s_open,
                 s_close, lens_radius, focal_distance, film):
        """Default constructor for ProjectiveCamera."""
        super(ProjectiveCamera, self).__init__(camera_to_world, s_open,
                                               s_close, film)
        # initialize depth of field parameters
        self.lens_radius = lens_radius
        self.focal_distance = focal_distance

        # compute projective camera transformations
        self.camera_to_screen = projection

        # compute projective camera screen transformations
        sc1 = scale(float(film.x_resolution), float(film.y_resolution), 1.0)
        sc2 = scale(1.0 / (screen_window[1] - screen_window[0]),
                    1.0 / (screen_window[2] - screen_window[3]), 1.0)
        tr = translate(Point(-screen_window[0], -screen_window[3], 0.0))
        self.screen_to_raster = sc1 * sc2 * tr
        self.raster_to_screen = inverse(self.screen_to_raster)
        self.raster_to_camera = inverse(self.camera_to_screen) * \
                                self.raster_to_screen
示例#17
0
    def __init__(self, camera_to_world, projection, screen_window,
                 s_open, s_close, lens_radius, focal_distance,
                 film):
        """Default constructor for ProjectiveCamera."""
        super(ProjectiveCamera, self).__init__(camera_to_world,
                                               s_open, s_close,
                                               film)
        # initialize depth of field parameters
        self.lens_radius = lens_radius
        self.focal_distance = focal_distance

        # compute projective camera transformations
        self.camera_to_screen = projection

        # compute projective camera screen transformations
        sc1 = scale(float(film.x_resolution), float(film.y_resolution), 1.0)
        sc2 = scale(1.0 / (screen_window[1] - screen_window[0]),
                    1.0 / (screen_window[2] - screen_window[3]),
                    1.0)
        tr = translate(Point(-screen_window[0], -screen_window[3], 0.0))
        self.screen_to_raster = sc1 * sc2 * tr
        self.raster_to_screen = inverse(self.screen_to_raster)
        self.raster_to_camera = inverse(self.camera_to_screen) * \
                                self.raster_to_screen
示例#18
0
    def test_transform_handedness(self):
        m1 = translate(Point(-17, 2, 31)) * scale(0.5, 6 , 1.4) * rotate(35, Vector(-15, 20, 0.2))
        self.assertFalse(m1.swap_handedness())

        m2 = translate(Point(5, 6, 7)) * scale(2, -3 , 4) * rotate(17, Vector(-1, 4, -2))
        self.assertTrue(m2.swap_handedness())
示例#19
0
 def test_transform_transform(self):
     m1 = scale(2, 3, 4) * translate(Point(10, 20, 30))
     m2 = translate(Point(20, 60, 120)) * scale(2, 3, 4)
     self.assertEqual(m1, m2)