The code for the writer looks fine. If you run your program in a debugger, it might be able to tell you why the window disappears. It isn’t a question that I can answer for you.
The callback and key press parts are divided as follows:
class vtkIPWCallback : public vtkCommand
{
public:
static vtkIPWCallback *New()
{ return new vtkIPWCallback; }
virtual void Execute(vtkObject *caller, unsigned long, void*)
{
vtkImplicitPlaneWidget2 *planeWidget = reinterpret_cast<vtkImplicitPlaneWidget2*>(caller);
vtkImplicitPlaneRepresentation *rep =reinterpret_cast<vtkImplicitPlaneRepresentation*>(planeWidget->GetRepresentation());
rep->GetPlane(this->Plane);
(Compute of the 4x4 matrix)
vtkImageReslice *reslice = this->Reslice;
reslice->SetResliceAxes(resliceMatrix);
reslice->Update();
}
vtkIPWCallback():Plane(0),Reslice(0){};
vtkPlane *Plane;
vtkImageReslice *Reslice;
};
class KeyPressInteractorStyle : public vtkInteractorStyleTrackballCamera
{
public:
static KeyPressInteractorStyle* New();
vtkTypeMacro(KeyPressInteractorStyle, vtkInteractorStyleTrackballCamera);
virtual void OnKeyPress() override
{
// Get the keypress
vtkRenderWindowInteractor *rwi = this->Interactor;
std::string key = rwi->GetKeySym();
vtkImageReslice *reslice = this->ImageReslice;
// Output the key that was pressed
std::cout << "Pressed " << key << std::endl;
// Handle a "normal" key
if(key == "c")
{
std::cout << "The a key was pressed." << std::endl;
// adapt path !
std::string filePathRaw = "slice_512x512.raw";
vtkSmartPointer<vtkImageCast> castFilter =
vtkSmartPointer<vtkImageCast>::New();
castFilter->SetOutputScalarTypeToUnsignedChar();
castFilter->SetInputConnection(reslice->GetOutputPort());
castFilter->Update();
vtkSmartPointer<vtkImageWriter> writer =
vtkSmartPointer<vtkImageWriter>::New();
writer->SetInputConnection(castFilter->GetOutputPort());
writer->SetFileName(filePathRaw.c_str());
writer->Write();
}
// Forward events
vtkInteractorStyleTrackballCamera::OnKeyPress();
}
// Pointer to vtkImageReslice
vtkImageReslice *ImageReslice;
};
vtkStandardNewMacro(KeyPressInteractorStyle);
Callback and key press are separated.
Using the same object, I tried to adapt the “reslice” updated in the callback with the “writer” that presses the key.
Should the key press part be included in the callback?
I would be grateful if you could give me advice.
There are two problems that I can see with the interator style.
First is that the “Reslice” of the interactor style is never set. You need code like this:
style->Reslice = reslice;
In order for the “style” and the “callback” to have the same reslice object, you must do myCallback->Reslice = reslice and style->Reslice = reslice.
Second, only one interactor style object is allowed, but you have two (the KeyPressInteractorStyle and the vtkInteractorStyleImage). To fix this problem, you should make your KeyPressInteractorStyle a subclass of vtkInteractorStyleImage.