コード例 #1
0
ファイル: transform.py プロジェクト: BaldrLector/pyPbrt
def look_at(pos, look, up):
    """Construct a Transform corresponding to a viewpoint in world space."""
    cam_to_world = Matrix4x4()

    # initialize fourth column of the viewing matrix
    cam_to_world.m[0][3] = pos.x
    cam_to_world.m[1][3] = pos.y
    cam_to_world.m[2][3] = pos.z
    cam_to_world.m[3][3] = 1.0

    # construct the base
    dir = normalize(look - pos)
    left = normalize(cross(normalize(up), dir))
    new_up = cross(dir, left)

    # fill the other columns
    cam_to_world.m[0][0] = left.x
    cam_to_world.m[1][0] = left.y
    cam_to_world.m[2][0] = left.z
    cam_to_world.m[3][0] = 0.0
    cam_to_world.m[0][1] = new_up.x
    cam_to_world.m[1][1] = new_up.y
    cam_to_world.m[2][1] = new_up.z
    cam_to_world.m[3][1] = 0.0
    cam_to_world.m[0][2] = dir.x
    cam_to_world.m[1][2] = dir.y
    cam_to_world.m[2][2] = dir.z
    cam_to_world.m[3][2] = 0.0

    return Transform(inverse(cam_to_world), cam_to_world)
コード例 #2
0
ファイル: sphere.py プロジェクト: BaldrLector/pyPbrt
    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
コード例 #3
0
ファイル: perspective.py プロジェクト: benoit-leveau/pyPbrt
    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
コード例 #4
0
ファイル: transform.py プロジェクト: benoit-leveau/pyPbrt
def look_at(pos, look, up):
    """Construct a Transform corresponding to a viewpoint in world space."""
    cam_to_world = Matrix4x4()
    
    # initialize fourth column of the viewing matrix
    cam_to_world.m[0][3] = pos.x
    cam_to_world.m[1][3] = pos.y
    cam_to_world.m[2][3] = pos.z
    cam_to_world.m[3][3] = 1.0

    # construct the base
    dir = normalize(look-pos)
    left = normalize(cross(normalize(up), dir))
    new_up = cross(dir, left)
    
    # fill the other columns
    cam_to_world.m[0][0] = left.x
    cam_to_world.m[1][0] = left.y
    cam_to_world.m[2][0] = left.z
    cam_to_world.m[3][0] = 0.0
    cam_to_world.m[0][1] = new_up.x
    cam_to_world.m[1][1] = new_up.y
    cam_to_world.m[2][1] = new_up.z
    cam_to_world.m[3][1] = 0.0
    cam_to_world.m[0][2] = dir.x
    cam_to_world.m[1][2] = dir.y
    cam_to_world.m[2][2] = dir.z
    cam_to_world.m[3][2] = 0.0

    return Transform(inverse(cam_to_world), cam_to_world)
コード例 #5
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
コード例 #6
0
ファイル: sphere.py プロジェクト: benoit-leveau/pyPbrt
    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
コード例 #7
0
ファイル: sphere.py プロジェクト: BaldrLector/pyPbrt
 def sample(self, u1, u2):
     """Sample the shape."""
     raise Exception("check_next_line")
     p = Point(0, 0, 0) + self.radius * 1.0  # uniform_sample_sphere(u1, u2)
     ns = normalize(self.object_to_world(Normal(p.x, p.y, p.z)))
     if (self.reverse_orientation):
         ns *= -1.0
     return self.object_to_world(p), ns
コード例 #8
0
ファイル: sphere.py プロジェクト: benoit-leveau/pyPbrt
 def sample(self, u1, u2):
     """Sample the shape."""
     raise Exception("check_next_line")
     p = Point(0, 0, 0) + self.radius * 1.0  # uniform_sample_sphere(u1, u2)
     ns = normalize(self.object_to_world(Normal(p.x, p.y, p.z)))
     if self.reverse_orientation:
         ns *= -1.0
     return self.object_to_world(p), ns
コード例 #9
0
ファイル: transform.py プロジェクト: BaldrLector/pyPbrt
def rotate(angle, axis):
    """Construct a Transform representing a rotation around the specified axis."""
    a = normalize(axis)
    sin_t = math.sin(math.radians(angle))
    cos_t = math.cos(math.radians(angle))
    mat = Matrix4x4(a.x * a.x + (1.0 - a.x * a.x) * cos_t,
                    a.x * a.y * (1.0 - cos_t) - a.z * sin_t,
                    a.x * a.z * (1.0 - cos_t) + a.y * sin_t, 0.0,
                    a.x * a.y * (1.0 - cos_t) + a.z * sin_t,
                    a.y * a.y + (1.0 - a.y * a.y) * cos_t,
                    a.y * a.z * (1.0 - cos_t) - a.x * sin_t, 0.0,
                    a.x * a.z * (1.0 - cos_t) - a.y * sin_t,
                    a.y * a.z * (1.0 - cos_t) + a.x * sin_t,
                    a.z * a.z + (1.0 - a.z * a.z) * cos_t, 0.0, 0.0, 0.0, 0.0,
                    1.0)
    return Transform(mat, transpose(mat))
コード例 #10
0
ファイル: transform.py プロジェクト: benoit-leveau/pyPbrt
def rotate(angle, axis):
    """Construct a Transform representing a rotation around the specified axis."""
    a = normalize(axis)
    sin_t = math.sin(math.radians(angle))
    cos_t = math.cos(math.radians(angle))
    mat = Matrix4x4(a.x * a.x + (1.0 - a.x * a.x) * cos_t,
                    a.x * a.y * (1.0 - cos_t) - a.z * sin_t,
                    a.x * a.z * (1.0 - cos_t) + a.y * sin_t,
                    0.0,
                    a.x * a.y * (1.0 - cos_t) + a.z * sin_t,
                    a.y * a.y + (1.0 - a.y * a.y) * cos_t,
                    a.y * a.z * (1.0 - cos_t) - a.x * sin_t,
                    0.0,
                    a.x * a.z * (1.0 - cos_t) - a.y * sin_t,
                    a.y * a.z * (1.0 - cos_t) + a.x * sin_t,
                    a.z * a.z + (1.0 - a.z * a.z) * cos_t,
                    0.0,
                    0.0,
                    0.0,
                    0.0,
                    1.0)
    return Transform(mat, transpose(mat))
コード例 #11
0
ファイル: sphere.py プロジェクト: BaldrLector/pyPbrt
    def intersect(self, r):
        """Intersect the ray with the shape."""
        # Transform _Ray_ to object space
        ray = self.world_to_object(r)

        # Compute quadratic sphere coefficients
        A = ray.d.x * ray.d.x + ray.d.y * ray.d.y + ray.d.z * ray.d.z
        B = 2 * (ray.d.x * ray.o.x + ray.d.y * ray.o.y + ray.d.z * ray.o.z)
        C = ray.o.x*ray.o.x + ray.o.y*ray.o.y + \
                  ray.o.z*ray.o.z - self.radius*self.radius

        # Solve quadratic equation for _t_ values
        found, t0, t1 = quadratic(A, B, C)
        if not found:
            return False, float('inf'), 0.0, None

        # Compute intersection distance along ray
        if (t0 > ray.maxt or t1 < ray.mint):
            return False, float('inf'), 0.0, None
        t_hit = t0
        if (t0 < ray.mint):
            t_hit = t1
            if (t_hit > ray.maxt):
                return False, float('inf'), 0.0, None

        # Compute sphere hit position and $\phi$
        phi_t = ray(t_hit)
        if (phi_t.x == 0.0 and phi_t.y == 0.0):
            phi_t.x = 1e-5 * self.radius
        phi = math.atan2(phi_t.y, phi_t.x)
        if (phi < 0.0):
            phi += 2.0 * math.pi

        # Test sphere intersection against clipping parameters
        if ((self.z_min > -self.radius and phi_t.z < self.z_min) or \
            (self.z_max <  self.radius and phi_t.z > self.z_max) or \
            phi > self.phi_max):
            if (t_hit == t1):
                return False, float('inf'), 0.0, None
            if (t1 > ray.maxt):
                return False, float('inf'), 0.0, None
            t_hit = t1
            # Compute sphere hit position and $\phi$
            phi_t = ray(t_hit)
            if (phi_t.x == 0.0 and phi_t.y == 0.0):
                phi_t.x = 1e-5 * self.radius
            phi = math.atan2(phi_t.y, phi_t.x)
            if (phi < 0.0):
                phi += 2.0 * math.pi
            if ((self.z_min > -self.radius and phi_t.z < self.z_min) or \
                (self.z_max <  self.radius and phi_t.z > self.z_max) or \
                phi > self.phi_max):
                return False, float('inf'), 0.0, None

        # Find parametric representation of sphere hit
        u = phi / self.phi_max
        theta = math.acos(clamp(phi_t.z / self.radius, -1.0, 1.0))
        v = (theta - self.theta_min) / (self.theta_max - self.theta_min)

        # Compute sphere $\dpdu$ and $\dpdv$
        zradius = math.sqrt(phi_t.x * phi_t.x + phi_t.y * phi_t.y)
        inv_z_radius = 1.0 / zradius
        cos_phi = phi_t.x * inv_z_radius
        sin_phi = phi_t.y * inv_z_radius
        dpdu = Vector(-self.phi_max * phi_t.y, self.phi_max * phi_t.x, 0)
        dpdv = (self.theta_max-self.theta_min) * \
               Vector(phi_t.z * cos_phi,
                      phi_t.z * sin_phi,
                      -self.radius * math.sin(theta))

        # Compute sphere $\dndu$ and $\dndv$
        d2Pduu = -self.phi_max * self.phi_max * Vector(phi_t.x, phi_t.y, 0)
        d2Pduv = (self.theta_max - self.theta_min) * phi_t.z * self.phi_max * \
                 Vector(-sin_phi, cos_phi, 0.0)
        d2Pdvv = -(self.theta_max - self.theta_min) * \
                 (self.theta_max - self.theta_min) * \
                 Vector(phi_t.x, phi_t.y, phi_t.z)

        # Compute coefficients for fundamental forms
        E = dot(dpdu, dpdu)
        F = dot(dpdu, dpdv)
        G = dot(dpdv, dpdv)
        N = normalize(cross(dpdu, dpdv))
        e = dot(N, d2Pduu)
        f = dot(N, d2Pduv)
        g = dot(N, d2Pdvv)

        # Compute $\dndu$ and $\dndv$ from fundamental form coefficients
        invEGF2 = 1.0 / (E * G - F * F)
        dndu = Normal.from_vector((f*F - e*G) * invEGF2 * dpdu + \
                                  (e*F - f*E) * invEGF2 * dpdv)
        dndv = Normal.from_vector((g*F - f*G) * invEGF2 * dpdu + \
                                  (f*F - g*E) * invEGF2 * dpdv)

        # Initialize _DifferentialGeometry_ from parametric information
        o2w = self.object_to_world
        dg = DifferentialGeometry.from_intersection(o2w(phi_t), o2w(dpdu),
                                                    o2w(dpdv), o2w(dndu),
                                                    o2w(dndv), u, v, self)

        # Compute _rayEpsilon_ for quadric intersection
        ray_epsilon = 5e-4 * t_hit
        return True, t_hit, ray_epsilon, dg
コード例 #12
0
ファイル: sphere.py プロジェクト: benoit-leveau/pyPbrt
    def intersect(self, r):
        """Intersect the ray with the shape."""
        # Transform _Ray_ to object space
        ray = self.world_to_object(r)

        # Compute quadratic sphere coefficients
        A = ray.d.x * ray.d.x + ray.d.y * ray.d.y + ray.d.z * ray.d.z
        B = 2 * (ray.d.x * ray.o.x + ray.d.y * ray.o.y + ray.d.z * ray.o.z)
        C = ray.o.x * ray.o.x + ray.o.y * ray.o.y + ray.o.z * ray.o.z - self.radius * self.radius

        # Solve quadratic equation for _t_ values
        found, t0, t1 = quadratic(A, B, C)
        if not found:
            return False, float("inf"), 0.0, None

        # Compute intersection distance along ray
        if t0 > ray.maxt or t1 < ray.mint:
            return False, float("inf"), 0.0, None
        t_hit = t0
        if t0 < ray.mint:
            t_hit = t1
            if t_hit > ray.maxt:
                return False, float("inf"), 0.0, None

        # Compute sphere hit position and $\phi$
        phi_t = ray(t_hit)
        if phi_t.x == 0.0 and phi_t.y == 0.0:
            phi_t.x = 1e-5 * self.radius
        phi = math.atan2(phi_t.y, phi_t.x)
        if phi < 0.0:
            phi += 2.0 * math.pi

        # Test sphere intersection against clipping parameters
        if (
            (self.z_min > -self.radius and phi_t.z < self.z_min)
            or (self.z_max < self.radius and phi_t.z > self.z_max)
            or phi > self.phi_max
        ):
            if t_hit == t1:
                return False, float("inf"), 0.0, None
            if t1 > ray.maxt:
                return False, float("inf"), 0.0, None
            t_hit = t1
            # Compute sphere hit position and $\phi$
            phi_t = ray(t_hit)
            if phi_t.x == 0.0 and phi_t.y == 0.0:
                phi_t.x = 1e-5 * self.radius
            phi = math.atan2(phi_t.y, phi_t.x)
            if phi < 0.0:
                phi += 2.0 * math.pi
            if (
                (self.z_min > -self.radius and phi_t.z < self.z_min)
                or (self.z_max < self.radius and phi_t.z > self.z_max)
                or phi > self.phi_max
            ):
                return False, float("inf"), 0.0, None

        # Find parametric representation of sphere hit
        u = phi / self.phi_max
        theta = math.acos(clamp(phi_t.z / self.radius, -1.0, 1.0))
        v = (theta - self.theta_min) / (self.theta_max - self.theta_min)

        # Compute sphere $\dpdu$ and $\dpdv$
        zradius = math.sqrt(phi_t.x * phi_t.x + phi_t.y * phi_t.y)
        inv_z_radius = 1.0 / zradius
        cos_phi = phi_t.x * inv_z_radius
        sin_phi = phi_t.y * inv_z_radius
        dpdu = Vector(-self.phi_max * phi_t.y, self.phi_max * phi_t.x, 0)
        dpdv = (self.theta_max - self.theta_min) * Vector(
            phi_t.z * cos_phi, phi_t.z * sin_phi, -self.radius * math.sin(theta)
        )

        # Compute sphere $\dndu$ and $\dndv$
        d2Pduu = -self.phi_max * self.phi_max * Vector(phi_t.x, phi_t.y, 0)
        d2Pduv = (self.theta_max - self.theta_min) * phi_t.z * self.phi_max * Vector(-sin_phi, cos_phi, 0.0)
        d2Pdvv = (
            -(self.theta_max - self.theta_min) * (self.theta_max - self.theta_min) * Vector(phi_t.x, phi_t.y, phi_t.z)
        )

        # Compute coefficients for fundamental forms
        E = dot(dpdu, dpdu)
        F = dot(dpdu, dpdv)
        G = dot(dpdv, dpdv)
        N = normalize(cross(dpdu, dpdv))
        e = dot(N, d2Pduu)
        f = dot(N, d2Pduv)
        g = dot(N, d2Pdvv)

        # Compute $\dndu$ and $\dndv$ from fundamental form coefficients
        invEGF2 = 1.0 / (E * G - F * F)
        dndu = Normal.from_vector((f * F - e * G) * invEGF2 * dpdu + (e * F - f * E) * invEGF2 * dpdv)
        dndv = Normal.from_vector((g * F - f * G) * invEGF2 * dpdu + (f * F - g * E) * invEGF2 * dpdv)

        # Initialize _DifferentialGeometry_ from parametric information
        o2w = self.object_to_world
        dg = DifferentialGeometry.from_intersection(o2w(phi_t), o2w(dpdu), o2w(dpdv), o2w(dndu), o2w(dndv), u, v, self)

        # Compute _rayEpsilon_ for quadric intersection
        ray_epsilon = 5e-4 * t_hit
        return True, t_hit, ray_epsilon, dg