[vtkusers] Issues moving code to a function and returning a smart pointer

Manuel Corrales manuelcorrales at gmail.com
Mon May 20 11:50:41 EDT 2013


Ok, so changing the code David provided a little bit like this (also
attached) I got a segfault:

#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 *[])
{
     vtkSmartPointer<vtkImageViewer2> imageViewer = CreateImageViewer();

     imageViewer->Render();

  return EXIT_SUCCESS;
}

vtkSmartPointer<vtkImageViewer2> CreateImageViewer()
{
    vtkSmartPointer<vtkImageViewer2> imageViewer =
vtkSmartPointer<vtkImageViewer2>::New();
    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();
     imageViewer->SetInput(connector->GetOutput());
    return imageViewer;
}

Any hints?
Thanks in advance!


On Mon, May 20, 2013 at 12:46 PM, Bill Lorensen <bill.lorensen at gmail.com>wrote:

> Yo0 probably using vtk 5.x. Change SetInputData to SetInput
>
>
>
> On Mon, May 20, 2013 at 11:45 AM, Manuel Corrales <
> manuelcorrales at gmail.com> wrote:
>
>> David, I am getting this error on your code:
>>
>> error: ‘class vtkImageViewer2’ has no member named ‘SetInputData’
>>       imageViewer->SetInputData(connector->GetOutput());
>>
>>
>> Am I using and old or bad compiled version of vtk?
>>
>> Regards,
>> Manuel.
>>
>>
>>
>> On Mon, May 20, 2013 at 11:06 AM, Manuel Corrales <
>> manuelcorrales at gmail.com> wrote:
>>
>>> 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;
>>>> }
>>>>
>>>
>>>
>>
>> _______________________________________________
>> Powered by www.kitware.com
>>
>> Visit other Kitware open-source projects at
>> http://www.kitware.com/opensource/opensource.html
>>
>> Please keep messages on-topic and check the VTK FAQ at:
>> http://www.vtk.org/Wiki/VTK_FAQ
>>
>> Follow this link to subscribe/unsubscribe:
>> http://www.vtk.org/mailman/listinfo/vtkusers
>>
>>
>
>
> --
> Unpaid intern in BillsBasement at noware dot com
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20130520/e379d6ca/attachment.htm>
-------------- next part --------------

cmake_minimum_required(VERSION 2.8)
 
PROJECT(CluDifDef)
 
find_package(ITK REQUIRED)
include(${ITK_USE_FILE})
if (ITKVtkGlue_LOADED)
  find_package(VTK REQUIRED)
  include(${VTK_USE_FILE})
else()
  find_package(ItkVtkGlue REQUIRED)
  include(${ItkVtkGlue_USE_FILE})
  set(Glue ItkVtkGlue)
endif()

find_package(VTK REQUIRED)
include(${VTK_USE_FILE})

find_package(Qt4 REQUIRED)
include(${QT_USE_FILE})

include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR}) 

file(GLOB UI_FILES *.ui)
file(GLOB QT_WRAP *.h)
file(GLOB CXX_FILES *.cxx *.cpp)

QT4_WRAP_UI(UISrcs ${UI_FILES})
QT4_WRAP_CPP(MOCSrcs ${QT_WRAP})
add_executable(CluDifDef MACOSX_BUNDLE ${CXX_FILES} ${UISrcs} ${MOCSrcs})
 
SET(CMAKE_INSTALL_RPATH "$ORIGIN/../lib:$ORIGIN/")

target_link_libraries(CluDifDef ${VTK_LIBRARIES} ${QT_LIBRARIES} QVTK ${ITK_LIBRARIES} ${Glue})
-------------- next part --------------
A non-text attachment was scrubbed...
Name: main.cxx
Type: application/octet-stream
Size: 1318 bytes
Desc: not available
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20130520/e379d6ca/attachment.obj>


More information about the vtkusers mailing list