示例#1
0
bool SDFRenderable::intersect(RayIntersection &intersection)
{
    intersection.object = NULL;

    Vector3dd walk = intersection.ray.p;
    double l = 0.0;

    static const int LIMIT  = 100;

    for (int i = 0; i < LIMIT; i++)
    {
        double d = F(walk);
        l += fabs(d);

        if (fabs(d) < 0.00000001) {

            double delta = 1e-7;
            Vector3dd normal;
            normal.x() = F(walk + Vector3dd::OrtX() * delta) - d;
            normal.y() = F(walk + Vector3dd::OrtY() * delta) - d;
            normal.z() = F(walk + Vector3dd::OrtZ() * delta) - d;
            normal /= delta;
            normal.normalise();
            intersection.object = this;
            intersection.normal = normal;
            intersection.t = l;

            //SYNC_PRINT(("d=%lf (%lf) (%lf %lf %lf)\n", l, d, normal.x(), normal.y(), normal.z()));
            return true;
        }
        if (fabs(d) > 1e10) {
            return false;
        }
        walk = intersection.ray.getPoint(l);
    }

    return false;
}
void TestbedMainWindow::preprocessImage(void)
{
    if (mImage == NULL) {
        return;
    }

    L_INFO_P("Starting to preprocess");
    PointScene *scene = new PointScene();
    //scene->scene.push_back(PointScene::Point(Vector3dd(1.0, 1.0, 1.0)));
    scene->showBuffer(mImage);
    m3DHist->setNewScenePointer(QSharedPointer<Scene3D>(scene), CloudViewDialog::MAIN_SCENE);

    Vector3dd mean(0.0);

    for (int i = 0; i < mImage->h; i++)
    {
        for (int j = 0; j < mImage->w; j++)
        {
            mean += mImage->element(i,j).toDouble();
        }
    }
    mean /= (mImage->h * mImage->w);

    EllipticalApproximationUnified<Vector3dd> ellip;
    for (int i = 0; i < mImage->h; i++)
    {
        for (int j = 0; j < mImage->w; j++)
        {
            ellip.addPoint(mImage->element(i,j).toDouble() - mean);
        }
    }
    ellip.getEllipseParameters();
    qDebug() << "Size is: "<< ellip.mAxes.size();

    EllApproxScene *sceneEl = new EllApproxScene(mean, ellip);
    m3DHist->setNewScenePointer(QSharedPointer<Scene3D>(sceneEl), CloudViewDialog::ADDITIONAL_SCENE);
    L_INFO_P("Color distribution analyzed");
    L_INFO_P("Preparing HSV presentation");

    delete_safe(mHComp);
    delete_safe(mSComp);
    delete_safe(mVComp);
    mHComp = new G8Buffer(mImage->getSize(), false);
    mSComp = new G8Buffer(mImage->getSize(), false);
    mVComp = new G8Buffer(mImage->getSize(), false);

    for (int i = 0; i < mImage->h; i++)
    {
        for (int j = 0; j < mImage->w; j++)
        {
            RGBColor &color = mImage->element(i,j);
            mHComp->element(i,j) = color.hue() * 255 / 360;
            mSComp->element(i,j) = color.saturation();
            mVComp->element(i,j) = color.value();
        }
    }
    L_INFO_P("Preparing edges");
    G12Buffer *tempBuffer = G8Buffer::toG12Buffer(mVComp);
    delete_safe(mEdges);

    delete_safe(mCannyEdges);
    delete_safe(mEdges);

    CannyParameters *cannyParams = mUi->cannyParametersWidget->createParameters();

    DerivativeBuffer *derivativeBuffer = NULL;
    mCannyEdges = CannyFilter::doFilter(tempBuffer, *cannyParams, &derivativeBuffer);
    mEdges = derivativeBuffer->gradientMagnitudeBuffer(10.0);

    delete_safe(derivativeBuffer);

    delete_safe(cannyParams);
    delete_safe(tempBuffer);


    L_INFO_P("Preparing projected buffer");
    Vector3dd mainDirection = ellip.mAxes[0];
    mainDirection.normalise();
    L_INFO << "Principal component is:" << mainDirection;
    delete_safe(mPrincipal);
    mPrincipal = projectToDirection(mImage, mainDirection);

    Vector3dd secondaryDirection = ellip.mAxes[1];
    secondaryDirection.normalise();
    L_INFO << "Secondary component is:" << secondaryDirection;
    delete_safe(mPrincipal2);
    mPrincipal2 = projectToDirection(mImage, secondaryDirection);

    Vector3dd thirdDirection = ellip.mAxes[2];
    thirdDirection.normalise();
    L_INFO << "Third component is:" << thirdDirection;
    delete_safe(mPrincipal3);
    mPrincipal3 = projectToDirection(mImage, thirdDirection);

    PreciseTimer timer;
    L_INFO_P("Preparing local histogram buffer");
    timer = PreciseTimer::currentTime();

    delete_safe(mHistBuffer);
    mHistBuffer = new AbstractBuffer<LocalHistogram>(mPrincipal->getSize());
    int bound = mUi->histRadiusSpinBox->value();
    for (int i = bound; i < mPrincipal->h - bound; i++)
    {
        for (int j = bound; j < mPrincipal->w - bound; j++)
        {
            for (int dy = -bound; dy <= bound; dy++)
            {
                for (int dx = -bound; dx <= bound; dx++)
                {
                    mHistBuffer->element(i, j).inc(mPrincipal->element(i + dy, j + dx));
                }
            }

        }
    }
    L_INFO_P("  Done in %d", timer.usecsToNow());


    L_INFO_P("Preparing local histogram2D buffer");
    timer = PreciseTimer::currentTime();

    delete_safe(mHist2DBuffer);
    mHist2DBuffer = new Histogram2DBuffer(mPrincipal->getSize());
    bound = mUi->histRadiusSpinBox->value();
    for (int i = bound; i < mPrincipal->h - bound; i++)
    {
        for (int j = bound; j < mPrincipal->w - bound; j++)
        {
            LocalHistogram2D *hist = &mHist2DBuffer->element(i, j);
            hist->isSet = true;
            for (int dy = -bound; dy <= bound; dy++)
            {
                for (int dx = -bound; dx <= bound; dx++)
                {
                    hist->inc(mPrincipal->element(i + dy, j + dx), mPrincipal2->element(i + dy, j + dx));
                }
            }

        }
    }
    L_INFO_P("  Done in %d", timer.usecsToNow());


    updateViewImage();

}