How write a own template class which inherits from an existing vtk class

Hi everyone,

I want to write a template class in vtk ,which inherits from an existing vtk class.

I use this ReadDICOMSeries as example.

Assuming I’m not sure the data type of _ImageViewer,maybe vtkResliceImageViewer or vtkImageViewer2.

So I should use a template class in this case .
the class should look like this :

template
class myVtkInteractorStyleImage : public vtkInteractorStyleImage
{
public:
static myVtkInteractorStyleImage* New();
vtkTypeMacro(myVtkInteractorStyleImage, vtkInteractorStyleImage);

protected:
T1* _ImageViewer;



};
vtkStandardNewMacro(?)

int main(){

 vtkSmartPointer<myVtkInteractorStyleImage<vtkImageViewer2>> myInteractorStyle =
      vtkSmartPointer<myVtkInteractorStyleImage<vtkImageViewer2>>::New();

   myInteractorStyle->SetImageViewer(imageViewer);
...
...
}

I have tried but i am strucked by how to define vtkStandardNewMacro()

Can someone help me?
Thanks!

5days :sob:

My understanding is that templates are not permitted in public API of VTK classes, only in internal implementation (see VTK coding standards).

This is not a limitation, but a really nice feature. Although templated classes can be easier to write and maintain for VTK developers, they are generally less convenient for VTK users (templated implementations are more rigid, as you need to commit to specific types at compile time).

For example, look at how much simpler to use your vtkMyInteractorStyleImage class without templates in the public API:

vtkNew<vtkMyInteractorStyleImage> myInteractorStyle;
myInteractorStyle->SetViewer(someImageViewer);

Also see how flexible this class is - you can dynamically change between viewer classes at runtime, regardless of their type:

myInteractorStyle->SetViewer(someResliceImageViewer);
1 Like

Thanks! I didn’t know I couldn’t use template classes. From your reply, My understanding is that I can write 2 SetViewer() as overloading function, am I right?
But if so ,how to initialize the member of class vtkMyInteractorStyleImage?
Assume I wrote my own InteractorStyle class.
The function of this style is when I move forward the mouse wheel then change the slice.
The vtkMyInteractorStyleImage Class like this:

class myVtkInteractorStyleImage : public vtkInteractorStyleImage
{
public:
static myVtkInteractorStyleImage* New();
vtkTypeMacro(myVtkInteractorStyleImage, vtkInteractorStyleImage);
protected:
int _Slice;
int _MinSlice;
int _MaxSlice;
vtkimageviewer2 * _ImageViewer;
public:
void Setviewer(vtkimageviewer2 * Viewer){
_ImageViewer = Viewer;
_MinSlice = imageViewer->GetSliceMin();
_MaxSlice = imageViewer->GetSliceMax();
_Slice = _MinSlice;
}

void MoveSliceForward() {
      if(_Slice < _MaxSlice) {
         _Slice += 1;
         cout << "MoveSliceForward::Slice = " << _Slice << std::endl;
         _ImageViewer->SetSlice(_Slice);
         _ImageViewer->Render();
      }
   }
};
vtkStandardNewMacro(myVtkInteractorStyleImage)

For this class I would like to use for vtkResliceImageViewer as well.

How can I change the code to adapt to vtkResliceImageViewer class.?

It’ll look something like this:

void vtkMyInteractorStyleImage::MoveSliceForward()
{
  vtkResliceImageViewer* resliceImageViewer = vtkResliceImageViewer::SafeDownCast(this->ImageViewer);
  vtkImageViewer2* imageViewer = vtkImageViewer2::SafeDownCast(this->ImageViewer);
  if (resliceImageViewer)
  {
    resliceImageViewer->SetSlice(...);
    ...
  }
  else if (imageViewer)
  {
    imageViewer->SetSliceSomeOtherWay(...);
    ...
  }
  else
  {
    vtkErrorMacro("vtkMyInteractorStyleImage::MoveSliceForward failed: Invalid ImageViewer")
  }
};

SafeDownCast(this->ImageViewer) is the “key” .

I should have thought of that, thank you very much!

1 Like