Python Wrapping error: cannot define or redeclare 'X' here because namespace 'wrappers' does not enclose namespace 'itk'

I am trying to wrap a single function itk::ViewImage, but hitting errors that I am not sure how to solve. Any hint would be appreciated!

ITK/build-src/Wrapping/itkViewImage.cxx:26:18: error: cannot define or redeclare 'ViewImage' here because namespace 'wrappers' does not enclose namespace 'itk'
    typedef itk::ViewImage< itk::Image< unsigned char,2 > > itkViewImageIUC2;
            ~~~~~^
ITK/build-src/Wrapping/itkViewImage.cxx:26:18: error: C++ requires a type specifier for all declarations
    typedef itk::ViewImage< itk::Image< unsigned char,2 > > itkViewImageIUC2;
    ~~~~~~~      ^
ITK/build-src/Wrapping/itkViewImage.cxx:26:18: error: typedef declarator cannot be qualified
    typedef itk::ViewImage< itk::Image< unsigned char,2 > > itkViewImageIUC2;
            ~~~~~^
ITK/build-src/Wrapping/itkViewImage.cxx:26:18: error: typedef name must be an identifier
    typedef itk::ViewImage< itk::Image< unsigned char,2 > > itkViewImageIUC2;
                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ITK/build-src/Wrapping/itkViewImage.cxx:26:60: error: expected ';' after top level declarator
    typedef itk::ViewImage< itk::Image< unsigned char,2 > > itkViewImageIUC2;
                                                           ^
                                                           ;

The function signature in itkViewImage.h

#ifndef itkViewImage_h
#define itkViewImage_h
#include <cstddef>
#include <string>
namespace itk
{
template<typename T >
void
ViewImage(const T* img,
          const std::string& win_title = "itkView",
          size_t win_x = 600,
          size_t win_y = 600);
}// namespace itk

#ifndef ITK_MANUAL_INSTANTIATION
#include "itkViewImage.hxx"
#endif
#endif

itkViewImage.hxx

#ifndef itkViewImage_hxx
#define itkViewImage_hxx
#include <vtkSmartPointer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleRubberBand3D.h>
#include <vtkRenderer.h>
#include <vtkCamera.h>
#include <vtkImageMapper.h>
#include <vtkImagePlaneWidget.h>
#include "itkImage.h"
#include "itkImageToVTKImageFilter.h"
#include "itkStatisticsImageFilter.h"
#include <itkFixedArray.h>
namespace itk
{
template< typename T >
void
ViewImage( const T* img,
           const std::string& win_title,
           size_t win_x,
           size_t win_y )
{
  using ConnectorType = itk::ImageToVTKImageFilter< T >;
  auto connector = ConnectorType::New();
  connector->SetInput(img);
  connector->Update();
  connector->UpdateLargestPossibleRegion();

  // Setup renderers
  vtkSmartPointer< vtkRenderer > renderer = vtkSmartPointer< vtkRenderer >::New();

  // Setup render window
  vtkSmartPointer< vtkRenderWindow > renderWindow = vtkSmartPointer< vtkRenderWindow >::New();
  renderWindow->SetWindowName(win_title.c_str());
  renderWindow->SetSize(win_x, win_y);
  renderWindow->AddRenderer(renderer);

  // Setup render window interactor
  vtkSmartPointer< vtkRenderWindowInteractor > renderWindowInteractor =
    vtkSmartPointer< vtkRenderWindowInteractor >::New();
  vtkSmartPointer< vtkInteractorStyleRubberBand3D > style =
    vtkSmartPointer< vtkInteractorStyleRubberBand3D >::New();
  renderWindowInteractor->SetInteractorStyle(style);

  // Render and start interaction
  renderWindowInteractor->SetRenderWindow(renderWindow);

  // Prepare for slices.
  using FilterType = itk::StatisticsImageFilter< T >;
  auto filter = FilterType::New();
  filter->SetInput(img);
  filter->Update();
  filter->UpdateLargestPossibleRegion();
  double min_intensity = filter->GetMinimum();
  double max_intensity = filter->GetMaximum();
  double window = max_intensity - min_intensity;
  double level  = min_intensity + window / 2;
  /** SLICES */
  FixedArray< vtkSmartPointer< vtkImagePlaneWidget >, 3 > slice_planes;
  for ( unsigned i = 0; i < 3; ++i )
    {
    slice_planes[i] = vtkSmartPointer< vtkImagePlaneWidget >::New();
    slice_planes[i]->SetResliceInterpolateToCubic();
    slice_planes[i]->DisplayTextOn();
    slice_planes[i]->SetInteractor(renderWindowInteractor);
    slice_planes[i]->PlaceWidget();
    slice_planes[i]->SetSliceIndex(0);
    slice_planes[i]->SetMarginSizeX(0);
    slice_planes[i]->SetMarginSizeY(0);
    slice_planes[i]->SetRightButtonAction(
      vtkImagePlaneWidget::VTK_SLICE_MOTION_ACTION);
    slice_planes[i]->SetMiddleButtonAction(
      vtkImagePlaneWidget::VTK_WINDOW_LEVEL_ACTION);
    slice_planes[i]->TextureInterpolateOff();

    slice_planes[i]->SetInputData(connector->GetOutput());
    slice_planes[i]->SetPlaneOrientation(i);
    slice_planes[i]->UpdatePlacement();
    slice_planes[i]->SetWindowLevel(window, level);
    slice_planes[i]->On();
    }
  // Flip camera because VTK-ITK different corner for origin.
  double pos[3];
  double vup[3];
  vtkCamera *cam = renderer->GetActiveCamera();
  cam->GetPosition(pos);
  cam->GetViewUp(vup);
  for ( unsigned int i = 0; i < 3; ++i )
    {
    pos[i] = -pos[i];
    vup[i] = -vup[i];
    }
  cam->SetPosition(pos);
  cam->SetViewUp(vup);

  renderer->ResetCamera();
  renderWindowInteractor->Initialize();
  renderWindowInteractor->Start();
}
}// namespace itk
#endif

And the .wrap file:

itk_wrap_class("itk::ViewImage")
  UNIQUE(types "${WRAP_ITK_SCALAR}")
  UNIQUE(dims "2;3")
  foreach(d ${dims})
    foreach(t ${types})
      itk_wrap_template("${ITKM_I${t}${d}}" "${ITKT_I${t}${d}}")
    endforeach()
  endforeach()
itk_end_wrap_class()

Hi Pablo,

To use the existing wrapping macros, you may need to put the function in a class.

HTH,
Matt

1 Like

Thanks Matt, is there any example of wrapping a free function, even if existing macros don’t cover it?

Nothing comes to mind.

It could be a static method of a class.

That sounds good, I am going to give it a try.

This is not possible either right?

#ifndef itkViewImage_h
#define itkViewImage_h
#include <cstddef>
#include <string>
namespace itk
{
class ViewImage {
  template<typename TImageType >
  static void View(const TImageType* img,
      const std::string& win_title = "itkView",
      size_t win_x = 600,
      size_t win_y = 600);
};
}// namespace itk

#ifndef ITK_MANUAL_INSTANTIATION
#include "itkViewImage.hxx"
#endif
#endif

Instead of having a template class, template the static function and wrap it.

Maybe I can just create a single constructor with that function and template the class.

1 Like

Or just template the class. But would python know the type when itkViewImage.View(inputImage) based on the input image without the need of explicitly write the type? Or would we need: itkViewImage[ImageType].View(inputImage)?

1 Like

This currently requires the class to have a

  • SetInput or SetInputImage or SetInput1 method
  • UpdateLargestPossibleRegion method
  • GetOutput method

per:

Thanks Matt, I think I would have to set up the template from python. I prefer this simple viewer to be stateless.

1 Like

Sounds good, Pablo.