예제 #1
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())
예제 #2
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)
예제 #3
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)
예제 #4
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))
예제 #5
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))
예제 #6
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_)
예제 #7
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)
예제 #8
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
예제 #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 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)
예제 #11
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
예제 #12
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())
예제 #13
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)