Ejemplo n.º 1
0
 def __init__(self, object_to_world, world_to_object, reverse_orientation, radius, z0, z1, phi_max):
     """Default constructor for Sphere."""
     super(Sphere, self).__init__(object_to_world, world_to_object, reverse_orientation)
     self.radius = float(radius)
     self.z_min = clamp(min(z0, z1), -radius, radius)
     self.z_max = clamp(max(z0, z1), -radius, radius)
     self.theta_min = math.acos(clamp(self.z_min / radius, -1.0, 1.0))
     self.theta_max = math.acos(clamp(self.z_max / radius, -1.0, 1.0))
     self.phi_max = math.radians(clamp(phi_max, 0.0, 360.0))
Ejemplo n.º 2
0
def slerp(t, q1, q2):
    """Spherical Linear Interpolation between two quaternions."""
    cos_theta = dot_quaternions(q1, q2)
    if cos_theta > 0.9995:
        return normalize((1.0 - t)*q1 + t*q2)
    else:
        theta = math.acos(clamp(cos_theta, -1.0, 1.0))
        theta_p = theta * t
        q_perp = normalize(q2 - q1*cos_theta)
        return q1*math.cos(theta_p) + q_perp*math.sin(theta_p)
Ejemplo n.º 3
0
 def test_clamp(self):
     low = -10.0
     high = 10.0
     self.assertEqual(clamp(3.4, low, high), 3.4)
     self.assertEqual(clamp(-100.0, low, high), low)
     self.assertEqual(clamp(100.0, low, high), high)
Ejemplo n.º 4
0
    def __init__(self, primitives, refine_immediately):
        """Default constructor for GridAccel."""
        # initialize self.primitives with primitives for grid
        if refine_immediately:
            self.primitives = []
            for primitive in primitives:
                primitive.fully_refine(self.primitives)
        else:
            self.primitives = list(primitives)

        # compute bounds and choose grid resolution
        self.bounds = BBox()
        for primitive in self.primitives:
            self.bounds = union(self.bounds, primitive.world_bound())
        delta = self.bounds.p_max - self.bounds.p_min
        
        # find voxels_per_unit_dist for grid
        max_axis = self.bounds.maximum_extent()
        inv_max_width = 1.0 / delta[max_axis]
        cube_root = 3.0 * pow(len(self.primitives), 1.0/3.0)
        voxels_per_unit_dist = cube_root * inv_max_width
        self.n_voxels = []
        for axis in range(3):
            self.n_voxels.append(clamp(
                round_to_int(delta[axis] * voxels_per_unit_dist), 1, 64))

        # compute voxel widths and allocate voxels
        self.width = Vector()
        self.inv_width = Vector()
        for axis in range(3):
            self.width[axis] = delta[axis] / self.n_voxels[axis]
            if self.width[axis] == 0.0:
                self.inv_width[axis] = 0.0
            else:
                self.inv_width[axis] = 1.0 / self.width[axis]
        nv = self.n_voxels[0] * self.n_voxels[1] * self.n_voxels[2]

        # array of voxels, initialized at None
        self.voxels = [None] * nv
        
        # add primitives to grid voxels
        for primitive in self.primitives:
            # find voxel extent of primitive
            primitive_bound = primitive.world_bound()
            v_min = []
            v_max = []
            for axis in range(3):
                v_min.append(self._pos_to_voxel(primitive_bound.p_min, axis))
                v_max.append(self._pos_to_voxel(primitive_bound.p_max, axis))

            # add primitive to overlapping voxels
            for z in range(v_min[2], v_max[2]+1):
                for y in range(v_min[1], v_max[1]+1):
                    for x in range(v_min[0], v_max[0]+1):
                        index = self._offset(x, y, z)
                        if self.voxels[index] is None:
                            self.voxels[index] = Voxel(primitive)
                        else:
                            self.voxels[index].add_primitive(primitive)

        # create reader-writer mutex for grid
        self.rw_lock = DummyRWLock()
Ejemplo n.º 5
0
 def _pos_to_voxel(self, point, axis):
     """Convert a 1D position into a voxel index."""
     v = int((point[axis] - self.bounds.p_min[axis]) *
             self.inv_width[axis])
     return clamp(v, 0, self.n_voxels[axis]-1)
Ejemplo n.º 6
0
    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