[vtkusers] QVTKWidget and vtkImagePlaneWidget

DVigneault davis.vigneault at gmail.com
Tue Oct 13 10:13:58 EDT 2015


All--

I'd like to use a set of ImagePlaneWidgets within a QVTKWidget, but am
having some trouble.  I tried creating a renderer/window/interactor and
adding the window to the QVTKWidget, but that produced a blank white window. 
Calling Render() on the widget's window produces a second window in the
background, which has the planes, but which you can't interact with.  When
closing the white window, both windows close, and an error is printed to the
console--sometimes a Segfault, sometimes an [xcb] Abort error.  I've also
tried not creating the window, but using the QVTKWidget's default window,
but this produces similar results.  I've pasted an example below which
produces the errors for me--you can change the DEBUG_ATTEMPT to 1, 2, 3, or
4 in order to cycle through.  I did find a similar question on the list [1],
but with no response.

I'm on Ubuntu 15 using the most recent git clone of VTK.  Hoping someone can
point out my error, or towards an example which might help.

Best, and thanks in advance,

--Davis

[1]
http://vtk.1045678.n5.nabble.com/vtkImplicitPlaneWidget2-and-Qt-td5169621.html

// Qt
#include <QApplication>
#include <QVTKWidget.h>

// ITK
#include <itkImageFileReader.h>
#include <itkImageToVTKImageFilter.h>

// VTK
#include <vtkSmartPointer.h>
#include <vtkImagePlaneWidget.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkProperty.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkImageData.h>
#include <vtkCommand.h>
#include <vtkProperty2D.h>
#include <vtkTextProperty.h>
#include <vtkRendererCollection.h>

#define DEBUG_ATTEMPT 4

const unsigned int Dimension = 3;
typedef short      TPixel;

typedef itk::Image< TPixel, Dimension >      TImage;
typedef itk::ImageFileReader< TImage >       TImageReader;
typedef itk::ImageToVTKImageFilter< TImage > TITK2VTK;

int main(int argc, char ** argv)
{

  if (2 != argc)
    {
    std::cerr << "Usage: " << argv[0] << " <InputImage>" << std::endl;
    return EXIT_FAILURE;
    }

  const std::string inputFile = argv[1];

  QApplication app(argc, argv);
  QVTKWidget widget;
  widget.resize(600,600);

  //////////////////////////////////////
  // Renderer/Window/Interactor/Style //
  //////////////////////////////////////

#if DEBUG_ATTEMPT == 1 || DEBUG_ATTEMPT == 2

  vtkSmartPointer<vtkRenderer> renderer =
    vtkSmartPointer<vtkRenderer>::New();

  vtkSmartPointer<vtkRenderWindow> window =
    vtkSmartPointer<vtkRenderWindow>::New();

  window->AddRenderer( renderer );

  vtkSmartPointer<vtkRenderWindowInteractor> interactor =
    vtkSmartPointer<vtkRenderWindowInteractor>::New();

  vtkSmartPointer<vtkInteractorStyleTrackballCamera> style =
    vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New();

  interactor->SetInteractorStyle( style );
  interactor->SetRenderWindow( window );

#elif DEBUG_ATTEMPT == 3 || DEBUG_ATTEMPT == 4

  vtkSmartPointer<vtkRenderer> renderer =
    vtkSmartPointer<vtkRenderer>::New();

  widget.GetRenderWindow()->AddRenderer( renderer );

  vtkSmartPointer<vtkRenderWindowInteractor> interactor =
    vtkSmartPointer<vtkRenderWindowInteractor>::New();

  vtkSmartPointer<vtkInteractorStyleTrackballCamera> style =
    vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New();

  interactor->SetInteractorStyle( style );
  interactor->SetRenderWindow( widget.GetRenderWindow() );

#endif

  /////////////////////
  // Read the images //
  /////////////////////

  TImageReader::Pointer imageReader = TImageReader::New();
  imageReader->SetFileName( inputFile );

  TITK2VTK::Pointer itk2vtk = TITK2VTK::New();
  itk2vtk->SetInput( imageReader->GetOutput() );
  itk2vtk->Update();

  vtkSmartPointer<vtkImageData> imageData = itk2vtk->GetOutput();

  int dims[Dimension];
  imageData->GetDimensions(dims);
  
  ///////////////////////
  // Setup image plane //
  ///////////////////////
  
  vtkSmartPointer<vtkProperty> property =
    vtkSmartPointer<vtkProperty>::New();

  // Set up the plane widgets
  vtkSmartPointer<vtkImagePlaneWidget> planeWidget[Dimension];
  
  for (unsigned int i = 0; i < Dimension; ++i)
    {

    planeWidget[i] = vtkSmartPointer< vtkImagePlaneWidget >::New();
    planeWidget[i]->SetInteractor( interactor );
    planeWidget[i]->RestrictPlaneToVolumeOn();
    double color[3] = {0,0,0};
    color[i] = 1;
    planeWidget[i]->GetPlaneProperty()->SetColor(color);
    planeWidget[i]->SetTexturePlaneProperty( property );
    planeWidget[i]->TextureInterpolateOff();
    planeWidget[i]->SetResliceInterpolateToLinear();
    planeWidget[i]->SetInputData( imageData );
    planeWidget[i]->SetPlaneOrientation(i);
    planeWidget[i]->SetSliceIndex(dims[i]/2);
    planeWidget[i]->DisplayTextOn();
    planeWidget[i]->SetDefaultRenderer( renderer );
    planeWidget[i]->SetWindowLevel(1358, -27);
    planeWidget[i]->On();
    planeWidget[i]->InteractionOn();
  
    }

  //////////////////
  // Finish Setup //
  //////////////////

#if DEBUG_ATTEMPT == 1

  // White Window
  widget.SetRenderWindow( window );
//  widget.GetInteractor()->Initialize();
//  widget.GetRenderWindow()->Render();
//  widget.GetInteractor()->Start();

#elif DEBUG_ATTEMPT == 2

  // In addition to the white window, you get a second window with the
planes, but which
  // you can't interact with.  Also, you get this error on exiting:
  // [xcb] Unknown sequence number while appending request
  // [xcb] Most likely this is a multi-threaded client and XInitThreads has
not been called
  // [xcb] Aborting, sorry about that.
  // dv-threshold: ../../src/xcb_io.c:161: append_pending_request: Assertion
`!xcb_xlib_unknown_seq_number' failed.
  // Aborted (core dumped)
  // Sometimes, segmentation fault instead of this message
  widget.SetRenderWindow( window );
  widget.GetRenderWindow()->Render();

#elif DEBUG_ATTEMPT == 3

  widget.GetRenderWindow()->AddRenderer( renderer );

#elif DEBUG_ATTEMPT == 4

  widget.GetRenderWindow()->AddRenderer( renderer );
  widget.GetRenderWindow()->Render();

#endif

  widget.show();
  app.exec();

  return EXIT_SUCCESS;

}




--
View this message in context: http://vtk.1045678.n5.nabble.com/QVTKWidget-and-vtkImagePlaneWidget-tp5734389.html
Sent from the VTK - Users mailing list archive at Nabble.com.


More information about the vtkusers mailing list