0

I try to display a 3D volume (https://midas3.kitware.com/midas/item/34776) using VTK and Qt using a QVTKOpenGLWidget. I can display and interact with axes, sphere, cones... but the 3D volume does not show up in the scene.

I can display the volume correctly when using the example from VTK (https://www.vtk.org/Wiki/VTK/Examples/Cxx/VolumeRendering/SmartVolumeMapper).

Here is the minimal example (VTK 8.1.1, Qt 5.10.1, Win10):

CentralWidget::CentralWidget(QWidget *parent=0) : QWidget(parent)
{   
    mQVTKWidget = new QVTKOpenGLWidget(this);
    QVBoxLayout *layout = new QVBoxLayout(this);
    layout->addWidget(mQVTKWidget);
    mQVTKWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);

    vtkNew<vtkGenericOpenGLRenderWindow> renderWindow;
    mQVTKWidget->SetRenderWindow(renderWindow);

    mRenderer = vtkSmartPointer<vtkRenderer>::New();
    mQVTKWidget->GetRenderWindow()->AddRenderer(mRenderer);
    mRenderer->GradientBackgroundOn();
    mRenderer->SetBackground(.8, .8, 1);
    mRenderer->SetBackground2(.3, .3, 1);

    // Sphere
    vtkSmartPointer<vtkSphereSource> sphereSource = vtkSmartPointer<vtkSphereSource>::New();
    sphereSource->SetPhiResolution(30);
    sphereSource->SetThetaResolution(30);
    sphereSource->SetRadius(50.0);

    vtkSmartPointer<vtkPolyDataMapper> sphereMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
    sphereMapper->SetInputConnection(sphereSource->GetOutputPort());

    vtkSmartPointer<vtkActor> sphereActor = vtkSmartPointer<vtkActor>::New();
    sphereActor->SetMapper(sphereMapper);

    // 3D image: .vti file. 
    vtkSmartPointer<vtkXMLImageDataReader> reader = vtkSmartPointer<vtkXMLImageDataReader>::New();
    reader->SetFileName("NLM Visible Human Project.vti");
    reader->Update();
    vtkSmartPointer<vtkImageData> imageData = vtkSmartPointer<vtkImageData>::New();
    imageData->ShallowCopy(reader->GetOutput());

    vtkSmartPointer<vtkSmartVolumeMapper> volumeMapper = vtkSmartPointer<vtkSmartVolumeMapper>::New();
    volumeMapper->SetBlendModeToComposite(); // composite first
    volumeMapper->SetInputData(imageData);

    vtkSmartPointer<vtkVolumeProperty> volumeProperty = vtkSmartPointer<vtkVolumeProperty>::New();
    volumeProperty->ShadeOff();
    volumeProperty->SetInterpolationType(VTK_LINEAR_INTERPOLATION);

    vtkSmartPointer<vtkPiecewiseFunction> compositeOpacity = vtkSmartPointer<vtkPiecewiseFunction>::New();
    compositeOpacity->AddPoint(0.0, 0.0);
    compositeOpacity->AddPoint(700.0, 0.0);
    compositeOpacity->AddPoint(3600.0, 1.0);
    volumeProperty->SetScalarOpacity(compositeOpacity); // composite first.

    vtkSmartPointer<vtkColorTransferFunction> color = vtkSmartPointer<vtkColorTransferFunction>::New();
    color->AddRGBPoint(0.0, 1.0, 0.0, 0.0);
    color->AddRGBPoint(3600.0, 1.0, 0.0, 1.0);
    volumeProperty->SetColor(color);

    vtkSmartPointer<vtkVolume> volume = vtkSmartPointer<vtkVolume>::New();
    volume->SetMapper(volumeMapper);
    volume->SetProperty(volumeProperty);

    // Renderer
    mRenderer->AddActor(sphereActor);
    mRenderer->AddVolume(volume);
    mRenderer->ResetCamera();

    // axes
    vtkAxesActor *axes = vtkAxesActor::New();
    vtkOrientationMarkerWidget *widget = vtkOrientationMarkerWidget::New();
    widget->SetDefaultRenderer(mRenderer);  
    widget->SetOrientationMarker(axes);
    widget->SetInteractor(mQVTKWidget->GetRenderWindow()->GetInteractor());
    widget->EnabledOn();

    vtkSmartPointer<vtkCubeAxesActor> cubeAxesActor = 
    vtkSmartPointer<vtkCubeAxesActor>::New();
    cubeAxesActor->SetBounds(volume->GetBounds());
    cubeAxesActor->SetCamera(mRenderer->GetActiveCamera());
    mRenderer->AddActor(cubeAxesActor);
}

[edit] Here is a screen shot of the output: VTK does not show 2D volume

What am I doing wrong?

2
  • Well, you haven't added the volume to the renderer: mRenderer->AddVolume(volume);
    – shash
    Commented Oct 11, 2018 at 10:31
  • @shash you are right I forgot to copy-paste it in the example above. This does not solve the problem.
    – ordepedro
    Commented Oct 11, 2018 at 14:36

1 Answer 1

1

After comparing with the code example from here I found the solution. The error was not in the code snippets in the question but in the main() where:

QSurfaceFormat::setDefaultFormat(QVTKOpenGLWidget::defaultFormat());

should be replaced with:

auto defaultFormat = QVTKOpenGLWidget::defaultFormat();
defaultFormat.setSamples(0);
QSurfaceFormat::setDefaultFormat(defaultFormat);

This problem is also discussed in the issue #17095 of VTK bug tracker.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.