Esempio n. 1
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
Esempio n. 2
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
Esempio n. 3
0
    def test_intersect(self):
        # test an intersection
        ray = Ray(Point(0, 0, 10), Vector(0, 0, -1))
        intersection = Intersection()
        intersected = self.grid_accel.intersect(ray, intersection)
        self.assertFalse(intersected)
        ray.maxt = float('inf')
        intersected = self.grid_accel.intersect_p(ray)
        self.assertFalse(intersected)

        # test an intersection
        ray2 = Ray(Point(10, 0, 10), Vector(0, 0, -1))
        intersection = Intersection()
        intersected = self.grid_accel.intersect(ray2, intersection)
        self.assertTrue(intersected)
        self.assertEqual(intersection.primitive_id,
                         self.primitive_sphere1.primitive_id)
        self.assertEqual(intersection.shape_id, self.sphere1.shape_id)
        ray2.maxt = float('inf')
        intersected = self.grid_accel.intersect_p(ray2)
        self.assertTrue(intersected)

        # test an intersection
        ray3 = Ray(Point(-10, 0, 10), Vector(0, 0, -1))
        intersection = Intersection()
        intersected = self.grid_accel.intersect(ray3, intersection)
        self.assertTrue(intersected)
        self.assertEqual(intersection.primitive_id,
                         self.primitive_sphere2.primitive_id)
        self.assertEqual(intersection.shape_id, self.sphere2.shape_id)
        ray3.maxt = float('inf')
        intersected = self.grid_accel.intersect_p(ray3)
        self.assertTrue(intersected)
Esempio n. 4
0
 def pdf_wi(self, p, wi):
     """Intersect sample ray with area light geometry."""
     dg_light = DifferentialGeometry()
     ray = Ray(p, wi, 1e-3)
     ray.depth = -1 # temp hack to ignore alpha mask
     intersect, t_hit, ray_epsilon, dg_light = self.intersect(ray)
     if not intersect:
         return 0.0
     # convert light sample weight to solid angle measure
     pdf = distance_squared(p, ray(t_hit)) / \
           (abs_dot(dg_light.nnm -wi) * self.area())
     if pdf == float('inf'):
         return 0.0
     return pdf
Esempio n. 5
0
    def test_bounding_box_9(self):
        bbox = BBox(Point(-1, -1, -1), Point(1, 1, 1))
        ray1 = Ray(Point(10, 10, 10), Vector(-1, -1, -1))
        intersect, hit0, hit1 = bbox.intersect_p(ray1)
        self.assertTrue(intersect)

        ray2 = Ray(Point(10, 10, 10), Vector(-1, 1, -1))
        intersect, hit0, hit1 = bbox.intersect_p(ray2)
        self.assertFalse(intersect)

        ray3 = Ray(Point(0, 0, 10), Vector(0, 0, -1))
        intersect, hit0, hit1 = bbox.intersect_p(ray3)
        self.assertTrue(intersect)
        self.assertEqual(hit0, 9.0)
        self.assertEqual(hit1, 11.0)
Esempio n. 6
0
    def sample_p(self, p, u1, u2):
        """Sample at point p."""
        # Compute coordinate system for sphere sampling
        p_center = self.object_to_world(Point(0, 0, 0))
        wc = normalize(p_center - p)
        wc_x, wc_y = coordinate_system(wc)

        # Sample uniformly on sphere if $\pt{}$ is inside it
        if (distance_squared(p, p_center) - self.radius * self.radius) < 1e-4:
            return self.sample(u1, u2)

        # Sample sphere uniformly inside subtended cone
        sin_theta_max2 = self.radius * self.radius / distance_squared(
            p, p_center)
        cos_theta_max = math.sqrt(max(0.0, 1.0 - sin_theta_max2))
        raise Exception("next_line")
        # r = Ray(p, uniform_sample_cone(u1, u2, cos_theta_max, wcX, wcY, wc), 1e-3)
        r = Ray(p)
        intersect, t_hit, ray_epsilon, dg_sphere = self.intersect(r)
        if not intersect:
            t_hit = dot(p_center - p, normalize(r.d))
        ps = r(t_hit)
        ns = Normal(normalize(ps - p_center))
        if (self.reverse_orientation):
            ns *= -1.0
        return ps, ns
Esempio n. 7
0
    def test_ray(self):
        r = Ray(Point(0, 0, 0), Vector(1, 2, 3))
        
        # test copy constructor
        r2 = Ray.from_ray(r)
        self.assertTrue(isinstance(r2, Ray))
        self.assertEqual(r2.d, r.d)

        # test constructor from parent ray
        r3 = Ray.from_ray_parent(r.o, r.d, r, r.mint)
        self.assertTrue(isinstance(r3, Ray))
        self.assertEqual(r3.depth, r.depth+1)

        # test operator()
        p = r(1.7)        
        self.assertEqual(p, Point(1.7, 3.4, 5.1))        
Esempio n. 8
0
    def test_ray_differential(self):
        r = Ray(Point(0, 0, 0), Vector(1, 2, 3))
        rd = RayDifferential(Point(0, 0, 0), Vector(1, 2, 3))

        # test copy constructor from Ray
        rd.has_differentials = True
        rd1 = RayDifferential.from_ray_differential(rd)
        self.assertTrue(isinstance(rd1, RayDifferential))
        self.assertEqual(rd1.o, rd.o)
        self.assertEqual(rd1.d, rd.d)
        self.assertEqual(rd1.rx_origin, rd.rx_origin)
        self.assertEqual(rd1.ry_origin, rd.ry_origin)
        self.assertEqual(rd1.rx_direction, rd.rx_direction)
        self.assertEqual(rd1.ry_direction, rd.ry_direction)
        self.assertEqual(rd1.has_differentials, rd.has_differentials)

        # test copy constructor from Ray
        rd2 = RayDifferential.from_ray(r)
        self.assertTrue(isinstance(rd2, RayDifferential))
        self.assertEqual(rd2.d, r.d)
        self.assertEqual(rd2.has_differentials, False)

        # test constructor from parent ray
        rd3 = RayDifferential.from_ray_parent(r.o, r.d, r, r.mint)
        self.assertTrue(isinstance(rd3, RayDifferential))
        self.assertEqual(rd3.depth, r.depth + 1)

        # test operator()
        p = rd(1.7)
        self.assertEqual(p, Point(1.7, 3.4, 5.1))
Esempio n. 9
0
    def test_ray(self):
        r = Ray(Point(0, 0, 0), Vector(1, 2, 3))

        # test copy constructor
        r2 = Ray.from_ray(r)
        self.assertTrue(isinstance(r2, Ray))
        self.assertEqual(r2.d, r.d)

        # test constructor from parent ray
        r3 = Ray.from_ray_parent(r.o, r.d, r, r.mint)
        self.assertTrue(isinstance(r3, Ray))
        self.assertEqual(r3.depth, r.depth + 1)

        # test operator()
        p = r(1.7)
        self.assertEqual(p, Point(1.7, 3.4, 5.1))
Esempio n. 10
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))
Esempio n. 11
0
    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)
Esempio n. 12
0
    def test_intersect(self):
        # test an intersection
        ray = Ray(Point(0, 0, 10), Vector(0, 0, -1))
        intersection = Intersection()
        intersected = self.grid_accel.intersect(ray, intersection)
        self.assertFalse(intersected)
        ray.maxt = float('inf')
        intersected = self.grid_accel.intersect_p(ray)
        self.assertFalse(intersected)

        # test an intersection
        ray2 = Ray(Point(10, 0, 10), Vector(0, 0, -1))
        intersection = Intersection()
        intersected = self.grid_accel.intersect(ray2, intersection)
        self.assertTrue(intersected)
        self.assertEqual(intersection.primitive_id,
                        self.primitive_sphere1.primitive_id)
        self.assertEqual(intersection.shape_id,
                        self.sphere1.shape_id)
        ray2.maxt = float('inf')
        intersected = self.grid_accel.intersect_p(ray2)
        self.assertTrue(intersected)

        # test an intersection
        ray3 = Ray(Point(-10, 0, 10), Vector(0, 0, -1))
        intersection = Intersection()
        intersected = self.grid_accel.intersect(ray3, intersection)
        self.assertTrue(intersected)
        self.assertEqual(intersection.primitive_id,
                        self.primitive_sphere2.primitive_id)
        self.assertEqual(intersection.shape_id,
                        self.sphere2.shape_id)
        ray3.maxt = float('inf')
        intersected = self.grid_accel.intersect_p(ray3)
        self.assertTrue(intersected)
Esempio n. 13
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)
Esempio n. 14
0
    def __call__(self, elt):
        """Overload the operator().

        Supported operations:
        * Transform(Point)
        * Transform(Vector)
        * Transform(Normal)
        * Transform(Ray)
        * Transform(RayDifferential)
        * Transform(BBox)
        
        """
        if isinstance(elt, Point):
            x = elt.x
            y = elt.y
            z = elt.z
            xp = self.m.m[0][0] * x + self.m.m[0][1] * y + self.m.m[0][
                2] * z + self.m.m[0][3]
            yp = self.m.m[1][0] * x + self.m.m[1][1] * y + self.m.m[1][
                2] * z + self.m.m[1][3]
            zp = self.m.m[2][0] * x + self.m.m[2][1] * y + self.m.m[2][
                2] * z + self.m.m[2][3]
            wp = self.m.m[3][0] * x + self.m.m[3][1] * y + self.m.m[3][
                2] * z + self.m.m[3][3]
            if wp == 1.0:
                return Point(xp, yp, zp)
            else:
                return Point(xp, yp, zp) / wp
        elif isinstance(elt, Vector):
            x = elt.x
            y = elt.y
            z = elt.z
            xp = self.m.m[0][0] * x + self.m.m[0][1] * y + self.m.m[0][2] * z
            yp = self.m.m[1][0] * x + self.m.m[1][1] * y + self.m.m[1][2] * z
            zp = self.m.m[2][0] * x + self.m.m[2][1] * y + self.m.m[2][2] * z
            return Vector(xp, yp, zp)
        elif isinstance(elt, Normal):
            x = elt.x
            y = elt.y
            z = elt.z
            return Normal(
                self.m_inv.m[0][0] * x + self.m_inv.m[1][0] * y +
                self.m_inv.m[2][0] * z, self.m_inv.m[0][1] * x +
                self.m_inv.m[1][1] * y + self.m_inv.m[2][1] * z,
                self.m_inv.m[0][2] * x + self.m_inv.m[1][2] * y +
                self.m_inv.m[2][2] * z)
        elif isinstance(elt, RayDifferential):
            ray = RayDifferential.from_ray_differential(elt)
            ray.o = self(ray.o)
            ray.d = self(ray.d)
            ray.rx_origin = self(ray.rx_origin)
            ray.ry_origin = self(ray.ry_origin)
            ray.rx_direction = self(ray.rx_direction)
            ray.ry_direction = self(ray.ry_direction)
            return ray
        elif isinstance(elt, Ray):
            ray = Ray.from_ray(elt)
            ray.o = self(ray.o)
            ray.d = self(ray.d)
            return ray
        elif isinstance(elt, BBox):
            ret = BBox(self(Point(elt.p_min.x, elt.p_min.y, elt.p_min.z)))
            ret = union(ret, self(Point(elt.p_max.x, elt.p_min.y,
                                        elt.p_min.z)))
            ret = union(ret, self(Point(elt.p_min.x, elt.p_max.y,
                                        elt.p_min.z)))
            ret = union(ret, self(Point(elt.p_min.x, elt.p_min.y,
                                        elt.p_max.z)))
            ret = union(ret, self(Point(elt.p_min.x, elt.p_max.y,
                                        elt.p_max.z)))
            ret = union(ret, self(Point(elt.p_max.x, elt.p_max.y,
                                        elt.p_min.z)))
            ret = union(ret, self(Point(elt.p_max.x, elt.p_min.y,
                                        elt.p_max.z)))
            ret = union(ret, self(Point(elt.p_max.x, elt.p_max.y,
                                        elt.p_max.z)))
            return ret
Esempio n. 15
0
    def __call__(self, elt):
        """Overload the operator().

        Supported operations:
        * Transform(Point)
        * Transform(Vector)
        * Transform(Normal)
        * Transform(Ray)
        * Transform(RayDifferential)
        * Transform(BBox)
        
        """
        if isinstance(elt, Point):
            x = elt.x
            y = elt.y
            z = elt.z
            xp = self.m.m[0][0]*x + self.m.m[0][1]*y + self.m.m[0][2]*z + self.m.m[0][3]
            yp = self.m.m[1][0]*x + self.m.m[1][1]*y + self.m.m[1][2]*z + self.m.m[1][3]
            zp = self.m.m[2][0]*x + self.m.m[2][1]*y + self.m.m[2][2]*z + self.m.m[2][3]
            wp = self.m.m[3][0]*x + self.m.m[3][1]*y + self.m.m[3][2]*z + self.m.m[3][3]
            if wp == 1.0:
                return Point(xp, yp, zp)
            else:
                return Point(xp, yp, zp)/wp
        elif isinstance(elt, Vector):
            x = elt.x
            y = elt.y
            z = elt.z
            xp = self.m.m[0][0]*x + self.m.m[0][1]*y + self.m.m[0][2]*z
            yp = self.m.m[1][0]*x + self.m.m[1][1]*y + self.m.m[1][2]*z
            zp = self.m.m[2][0]*x + self.m.m[2][1]*y + self.m.m[2][2]*z
            return Vector(xp, yp, zp)
        elif isinstance(elt, Normal):
            x = elt.x
            y = elt.y
            z = elt.z
            return Normal(self.m_inv.m[0][0]*x + self.m_inv.m[1][0]*y + self.m_inv.m[2][0]*z,
                          self.m_inv.m[0][1]*x + self.m_inv.m[1][1]*y + self.m_inv.m[2][1]*z,
                          self.m_inv.m[0][2]*x + self.m_inv.m[1][2]*y + self.m_inv.m[2][2]*z)
        elif isinstance(elt, RayDifferential):
            ray = RayDifferential.from_ray_differential(elt)
            ray.o = self(ray.o)
            ray.d = self(ray.d)
            ray.rx_origin = self(ray.rx_origin)
            ray.ry_origin = self(ray.ry_origin)
            ray.rx_direction = self(ray.rx_direction)
            ray.ry_direction = self(ray.ry_direction)
            return ray
        elif isinstance(elt, Ray):
            ray = Ray.from_ray(elt)
            ray.o = self(ray.o)
            ray.d = self(ray.d)
            return ray
        elif isinstance(elt, BBox):
            ret = BBox(self(Point(elt.p_min.x, elt.p_min.y, elt.p_min.z)))
            ret = union(ret, self(Point(elt.p_max.x, elt.p_min.y, elt.p_min.z)))
            ret = union(ret, self(Point(elt.p_min.x, elt.p_max.y, elt.p_min.z)))
            ret = union(ret, self(Point(elt.p_min.x, elt.p_min.y, elt.p_max.z)))
            ret = union(ret, self(Point(elt.p_min.x, elt.p_max.y, elt.p_max.z)))
            ret = union(ret, self(Point(elt.p_max.x, elt.p_max.y, elt.p_min.z)))
            ret = union(ret, self(Point(elt.p_max.x, elt.p_min.y, elt.p_max.z)))
            ret = union(ret, self(Point(elt.p_max.x, elt.p_max.y, elt.p_max.z)))
            return ret