[vtkusers] Issues moving code to a function and returning a smart pointer
Manuel Corrales
manuelcorrales at gmail.com
Mon May 20 10:06:19 EDT 2013
Thanks David. Sorry about the example. I will attach a working example as
soon as I can. About the sample code you sent, my problem is when the code
is the other way around. I mean, reading the file and the connector in the
function and then rendering the viewer in the main method. It maybe a Qt
widget issue, but I am not sure.
Thanks for your response!
On Mon, May 20, 2013 at 10:55 AM, David Doria <daviddoria at gmail.com> wrote:
> On Fri, May 17, 2013 at 3:43 PM, Manuel Corrales
> <manuelcorrales at gmail.com> wrote:
> > Hello all. I have a simple code that reads an image using ITK, then it
> > convert it to VTK and after that I display the image in a QVTKWidget. The
> > code is this and works fine:
> >
> > typedef itk::Image< double, 2 > ImageType;
> > typedef itk::ImageToVTKImageFilter<ImageType> ConnectorType;
> > typedef itk::ImageFileReader<ImageType> ReaderType;
> >
> > ReaderType::Pointer reader = ReaderType::New();
> > reader->SetFileName(file.c_str());
> > reader->Update();
> >
> > ConnectorType::Pointer connector = ConnectorType::New();
> > connector->SetInput(reader->GetOutput());
> > connector->Update();
> > ImageReader *myReader = new ImageReader();
> > vtkSmartPointer<vtkImageViewer2> vtkImageViewer =
> > vtkSmartPointer<vtkImageViewer2>::New();
> > vtkImageViewer->SetInput(connector->GetOutput());
> >
> vtkImageViewer->SetRenderWindow(this->qvtkWidget->GetRenderWindow());
> > vtkImageViewer->Render();
> >
> >
> > Now if I change this code and create a separate function to perform the
> > connection from ITK to VTK, I get a segfault:
> >
> > ImageReader *myReader = new ImageReader();
> > vtkSmartPointer<vtkImageViewer2> vtkImageViewer =
> > myReader->renderITKReaderInVTK(file);
> >
> vtkImageViewer->SetRenderWindow(this->qvtkWidget->GetRenderWindow());
> > vtkImageViewer->Render();
> >
> > and the called function:
> >
> > vtkSmartPointer<vtkImageViewer2>
> > ImageReader::renderITKReaderInVTK(std::string fileName) {
> > typedef itk::Image< double, 2 > ImageType;
> > typedef itk::ImageToVTKImageFilter<ImageType> ConnectorType;
> > typedef itk::ImageFileReader<ImageType> ReaderType;
> >
> > ReaderType::Pointer reader = ReaderType::New();
> > qDebug() << "Setting reader file name";
> > reader->SetFileName(fileName.c_str());
> > qDebug() << "Updating reader";
> > reader->Update();
> >
> > ConnectorType::Pointer connector = ConnectorType::New();
> > qDebug() << "Setting input to connector";
> > connector->SetInput(reader->GetOutput());
> > qDebug() << "Updating connector";
> > connector->Update();
> >
> > vtkSmartPointer<vtkImageViewer2> vtkImageViewer =
> > vtkSmartPointer<vtkImageViewer2>::New();
> > qDebug() << "Setting connector outupt -> imageViewer input";
> > vtkImageViewer->SetInput(connector->GetOutput());
> > qDebug() << "Image viewer set up ready";
> > return vtkImageViewer;
> > }
> >
> > Any ideas why the same code (I think) works if it is all together but
> fail
> > when it's in a different function?
> >
> > Thanks in advance!
> > Regards,
> > Manuel.
>
>
> It is much easier for us if you provide a compilable example of the
> error. I tried the following, but it seems to work fine. If you can
> provide something like this that segfaults, I'm sure someone can help
> you out.
>
> #include <vtkVersion.h>
> #include <vtkSmartPointer.h>
> #include <vtkRenderWindow.h>
> #include <vtkRenderWindowInteractor.h>
> #include <vtkRenderer.h>
> #include <vtkImageViewer2.h>
>
> #include "itkImage.h"
> #include "itkImageFileReader.h"
> #include "itkImageToVTKImageFilter.h"
>
> vtkSmartPointer<vtkImageViewer2> CreateImageViewer();
>
> int main(int, char *[])
> {
>
> typedef itk::Image< double, 2 > ImageType;
> typedef itk::ImageToVTKImageFilter<ImageType> ConnectorType;
> typedef itk::ImageFileReader<ImageType> ReaderType;
>
> ReaderType::Pointer reader = ReaderType::New();
> reader->SetFileName("test.png");
> reader->Update();
>
> ConnectorType::Pointer connector = ConnectorType::New();
> connector->SetInput(reader->GetOutput());
> connector->Update();
>
> vtkSmartPointer<vtkImageViewer2> imageViewer = CreateImageViewer();
> imageViewer->SetInputData(connector->GetOutput());
> imageViewer->Render();
>
> return EXIT_SUCCESS;
> }
>
> vtkSmartPointer<vtkImageViewer2> CreateImageViewer()
> {
> vtkSmartPointer<vtkImageViewer2> imageViewer =
> vtkSmartPointer<vtkImageViewer2>::New();
> return imageViewer;
> }
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20130520/cfc95dc7/attachment.htm>
More information about the vtkusers
mailing list