如何在Qt中使用vtkImplicitPlaneWidget2

时间:2017-01-08 04:18:19

标签: c++ qt vtk

我将Qt与vtkImplicitPlaneWidget2结合起来时遇到了麻烦。 没有Qt,我可以得到这样的结果。 Without Qt

但是对于Qt,我无法显示vtkImplicitPlaneWidget2的基本功能,换句话说,它只显示体积数据,但无法显示平面的限幅器,如下图所示。 With Qt

我试图找到结果,似乎永远不会在Qt中调用回调函数。我认为这是因为我没有将Qt事件连接到回调函数,但我不知道如何处理它。

以下是代码。

void planeWidgetPara::showLabelPlane()
{
this->setupUi(this);

#pragma region image_process
    vtkSmartPointer<vtkImageData> imageData =
        vtkSmartPointer<vtkImageData>::New();
    imageData->SetExtent(0, dims[0] / scaledown - 1,
        0, dims[1] / scaledown - 1,
        0, dims[2]);
    imageData->SetSpacing(this->getSpacing());
    imageData->AllocateScalars(VTK_UNSIGNED_CHAR, 3);

    int scaledown = this->getScaledown();

    for (int i = 0;i < dims[0] / scaledown;i++)
    {
        for (int j = 0;j < dims[1] / scaledown;j++)
        {
            for (int k = 0;k < dims[2];k++)
            {
                imageData->SetScalarComponentFromDouble(i, j, k, 0, getLabelScaledownColor(i, j, k, 0, scaledown));
            imageData->SetScalarComponentFromDouble(i, j, k, 1, getLabelScaledownColor(i, j, k, 1, scaledown));
            imageData->SetScalarComponentFromDouble(i, j, k, 2, getLabelScaledownColor(i, j, k, 2, scaledown));
            }
        }
    }

    vtkSmartPointer<vtkImageDataGeometryFilter> imageDataGeometryFilter =
        vtkSmartPointer<vtkImageDataGeometryFilter>::New();

    imageDataGeometryFilter->SetInputData(imageData);
    imageDataGeometryFilter->Update();

    // Setup a visualization pipeline
    vtkSmartPointer<vtkPlane> plane =
        vtkSmartPointer<vtkPlane>::New();
    plane->SetOrigin(this->getRawOrigin());
    vtkSmartPointer<vtkClipPolyData> clipper =
        vtkSmartPointer<vtkClipPolyData>::New();
    clipper->SetClipFunction(plane);
    clipper->InsideOutOn();

    clipper->SetInputConnection(imageDataGeometryFilter->GetOutputPort());

    // Create a mapper and actor
    vtkSmartPointer<vtkPolyDataMapper> mapper =
        vtkSmartPointer<vtkPolyDataMapper>::New();
    mapper->SetInputConnection(clipper->GetOutputPort());
    vtkSmartPointer<vtkActor> actor =
        vtkSmartPointer<vtkActor>::New();
    actor->SetMapper(mapper);

    vtkSmartPointer<vtkProperty> backFaces =
        vtkSmartPointer<vtkProperty>::New();
    backFaces->SetDiffuseColor(.8, .8, .4);

    actor->SetBackfaceProperty(backFaces);
#pragma endregion


    // A renderer and render window
    vtkSmartPointer<vtkRenderer> renderer =
        vtkSmartPointer<vtkRenderer>::New();
    vtkSmartPointer<vtkRenderWindow> renderWindow =
        this->qvtkWidget->GetRenderWindow();

    renderWindow->AddRenderer(renderer);
    renderer->AddActor(actor);

    //// An interactor
    vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
        this->qvtkWidget->GetRenderWindow()->GetInteractor();
    renderWindowInteractor->SetRenderWindow(renderWindow);
    renderWindow->Render();

    // The callback will do the work
    vtkSmartPointer<vtkIPWCallback> myCallback =
        vtkSmartPointer<vtkIPWCallback>::New();
    myCallback->Plane = plane;
    myCallback->Actor = actor;

    vtkSmartPointer<vtkImplicitPlaneRepresentation> rep =
        vtkSmartPointer<vtkImplicitPlaneRepresentation>::New();
    rep->SetPlaceFactor(1.25); // This must be set prior to placing the widget
    rep->PlaceWidget(actor->GetBounds());
    rep->SetNormal(plane->GetNormal());

    vtkSmartPointer<vtkImplicitPlaneWidget2> planeWidget =
        vtkSmartPointer<vtkImplicitPlaneWidget2>::New();
    planeWidget->SetInteractor(renderWindowInteractor);
    planeWidget->SetRepresentation(rep);
    planeWidget->AddObserver(vtkCommand::InteractionEvent, myCallback);

    // Render
    renderWindowInteractor->Initialize();
    renderWindow->Render();
    planeWidget->On();
    // Begin mouse interaction
    //renderWindowInteractor->Start();

    // VTK/Qt wedded
    this->qvtkWidget->GetRenderWindow()->AddRenderer(renderer);



    // Set up action signals and slots
    connect(this->actionExit, SIGNAL(triggered()), this, SLOT(slotExit()));
}

0 个答案:

没有答案