Ejemplo n.º 1
0
void Grid::initializeRayMarch(MarchingInfo &mi, const Ray &r, float tmin) const
{
    Vec3f rayOrigin = r.getOrigin();
    Vec3f rayDir = r.getDirection();

    Vec3f boxMin = mpBox->getMin();
    Vec3f boxMax = mpBox->getMax();

    float rayT;
    if (mpBox->Inside(rayOrigin))
        rayT = 0.0;
    else if (!mpBox->IntersectP(r, &rayT))
    {
        return;
    }
    
    printf("rayT=%f\n", rayT);
    Vec3f gridIntersect = r.pointAtParameter(rayT);
    cout << "gridIntersect=" << gridIntersect << endl;
    cout << "Box min=" << mpBox->getMin() << endl;
    printf("mVoxel:%d, %d, %d \n", mVoxel[0], mVoxel[1], mVoxel[2]);
    //MarchingInfo for indices
    int pos[3];
    int axis;
    int sign[3];
    float delta[3];
    float next[3];
    int indices[3];
    for (axis = 0; axis < 3; axis++) {
        pos[axis] = posToVoxel(gridIntersect, axis);
        if (rayDir[axis] != 0)
            sign[axis] = rayDir[axis] > 0 ? 1 : -1;

        if (sign[axis] >= 0) {
            next[axis] = rayT +
                (voxelToPos(pos[axis]+1, axis) - gridIntersect[axis])/rayDir[axis];
            delta[axis] = mVoxel[axis] / rayDir[axis];
        }
        else {
            next[axis] = rayT +
                (voxelToPos(pos[axis], axis) - gridIntersect[axis])/rayDir[axis];
            delta[axis] = mVoxel[axis] / rayDir[axis];
        }
    }
    printf("Indices:%d, %d, %d \n", pos[0], pos[1], pos[2]);
    mi.setIndices(pos[0], pos[1], pos[2]);
    mi.setSign(sign[0], sign[1], sign[2]);
    mi.setDelta(delta[0], delta[1], delta[2]);
    mi.setNext(next[0], next[1], next[2]);
    mi.set_tmin(rayT);
}
Ejemplo n.º 2
0
bool GridAccel::IntersectP(const Ray &ray) const {
    PBRT_GRID_INTERSECTIONP_TEST(const_cast<GridAccel *>(this), const_cast<Ray *>(&ray));
    RWMutexLock lock(*rwMutex, READ);
    // Check ray against overall grid bounds
    float rayT;
    if (bounds.Inside(ray(ray.mint)))
        rayT = ray.mint;
    else if (!bounds.IntersectP(ray, &rayT)) {
        PBRT_GRID_RAY_MISSED_BOUNDS();
        return false;
    }
    Point gridIntersect = ray(rayT);

    // Set up 3D DDA for ray
    float NextCrossingT[3], DeltaT[3];
    int Step[3], Out[3], Pos[3];
    for (int axis = 0; axis < 3; ++axis) {
        // Compute current voxel for axis
        Pos[axis] = posToVoxel(gridIntersect, axis);
        if (ray.d[axis] >= 0) {
            // Handle ray with positive direction for voxel stepping
            NextCrossingT[axis] = rayT +
                (voxelToPos(Pos[axis]+1, axis) - gridIntersect[axis]) / ray.d[axis];
            DeltaT[axis] = Width[axis] / ray.d[axis];
            Step[axis] = 1;
            Out[axis] = NVoxels[axis];
        }
        else {
            // Handle ray with negative direction for voxel stepping
            NextCrossingT[axis] = rayT +
                (voxelToPos(Pos[axis], axis) - gridIntersect[axis]) / ray.d[axis];
            DeltaT[axis] = -Width[axis] / ray.d[axis];
            Step[axis] = -1;
            Out[axis] = -1;
        }
    }

    // Walk grid for shadow ray
    for (;;) {
        int o = offset(Pos[0], Pos[1], Pos[2]);
        Voxel *voxel = voxels[o];
        PBRT_GRID_RAY_TRAVERSED_VOXEL(Pos, voxel ? voxel->size() : 0);
        if (voxel && voxel->IntersectP(ray, lock))
            return true;
        // Advance to next voxel

        // Find _stepAxis_ for stepping to next voxel
        int bits = ((NextCrossingT[0] < NextCrossingT[1]) << 2) +
                   ((NextCrossingT[0] < NextCrossingT[2]) << 1) +
                   ((NextCrossingT[1] < NextCrossingT[2]));
        const int cmpToAxis[8] = { 2, 1, 2, 1, 2, 2, 0, 0 };
        int stepAxis = cmpToAxis[bits];
        if (ray.maxt < NextCrossingT[stepAxis])
            break;
        Pos[stepAxis] += Step[stepAxis];
        if (Pos[stepAxis] == Out[stepAxis])
            break;
        NextCrossingT[stepAxis] += DeltaT[stepAxis];
    }
    return false;
}
Ejemplo n.º 3
0
bool Heightfield2::IntersectP(const Ray &r) const{
    Ray ray;
    (*WorldToObject)(r, &ray);

    float rayT;
    if(bounds.Inside(ray(ray.mint)))
        rayT = ray.mint;
    else if(!bounds.IntersectP(ray, &rayT))
        return false;
    Point gridIntersect = ray(rayT);
    
    // Set up 2D DDA for ray
    float NextCrossingT[2], DeltaT[2];
    int Step[2], Out[2], Pos[2];
    for (int axis = 0; axis < 2; ++axis) {
        // Compute current voxel for axis
        Pos[axis] = posToVoxel(gridIntersect, axis);
        if (ray.d[axis] >= 0) {
            // Handle ray with positive direction for voxel stepping
            NextCrossingT[axis] = rayT +
                (voxelToPos(Pos[axis]+1, axis) - gridIntersect[axis]) / ray.d[axis];
            DeltaT[axis] = width[axis] / ray.d[axis];
            Step[axis] = 1;
            Out[axis] = nVoxels[axis];
        }
        else {
            // Handle ray with negative direction for voxel stepping
            NextCrossingT[axis] = rayT +
                (voxelToPos(Pos[axis], axis) - gridIntersect[axis]) / ray.d[axis];
            DeltaT[axis] = -width[axis] / ray.d[axis];
            Step[axis] = -1;
            Out[axis] = -1;
        }
    }

    // Walk ray through voxel grid
    bool hitSomething = false;
    int index[3];
    for (;;) {

        // Check for intersection in current voxel and advance to next
        index[0] = Pos[0] + Pos[1]*nx;
        index[1] = Pos[0]+1 + Pos[1]*nx;
        index[2] = Pos[0]+1 + (Pos[1]+1)*nx;
        hitSomething |= TriangleIntersectP(r, index);
        if(hitSomething) return hitSomething;

        index[1] = Pos[0]+1 + (Pos[1]+1)*nx;
        index[2] = Pos[0] + (Pos[1]+1)*nx;
        hitSomething |= TriangleIntersectP(r, index);
        if(hitSomething) return hitSomething;

        // Advance to next voxel
        // Find _stepAxis_ for stepping to next voxel
        int stepAxis = (NextCrossingT[0] < NextCrossingT[1])? 0 : 1;
        Pos[stepAxis] += Step[stepAxis];
        if (Pos[stepAxis] == Out[stepAxis])
            break;
        NextCrossingT[stepAxis] += DeltaT[stepAxis];
    }
    return hitSomething;
}