An interactionator style is when you set up an interactionator and it has various default or customized interactions, like you can set the scroll wheel to zoom in and out of the image. Or you can set the mouse click and move to change the camera’s perspective to see different sides of the object.

The VTK program needs to first capture your events and then execute the corresponding functions based on the events you trigger, combined with the style of the interactionator.

When I was studying, I saw many examples and wrote a lot of custom styles by imitating them, but IT was difficult to understand the mechanism inside. Later I read “VTK graphics and image development advanced” Zhang Xiaodong, Luo Huoling edited the book, I think this book for the implementation of this interactive content is relatively shallow, but to understand the interaction style is enough, after all, our main goal is to use, rather than fully understand the mechanism inside.

But I’m not going to cover too much of the mechanism here, because it’s not really that useful, so I’ll cover a little bit in this section, and then I’ll start writing my own interactive styles in the next section.

First we introduce vtkRenderWindowInteractor, namely the render window interaction, used to provide interactive mechanism of events such as the mouse button, and then to send vtkInteractorObserver news of the event or its subclasses.

Let’s start with a program:

`#include<vtkAutoInit.h> VTK_MODULE_INIT(vtkRenderingOpenGL2) VTK_MODULE_INIT(vtkInteractionStyle); VTK_MODULE_INIT(vtkRenderingFreeType); VTK_MODULE_INIT(vtkRenderingVolumeOpenGL2); #include <iostream> using std::cout; using std::endl; #include <vtkSmartPointer.h> #include <vtkPNGReader.h> #include <vtkImageViewer2.h> #include <vtkRenderWindowInteractor.h> #include <vtkCallbackCommand.h> #include <vtkRenderWindow.h> #include <vtkRenderer.h> #include <vtkInteractorStyleImage.h> void MywCallbackFunc(vtkObject*, unsigned long eid, void* clientdata, void *calldata) { cout << "aaaaaaa "<<endl; } int main(int argc, char* argv[]) { vtkSmartPointer<vtkPNGReader> reader = vtkSmartPointer<vtkPNGReader>::New(); reader->SetFileName("timg.png"); vtkSmartPointer<vtkImageViewer2> viewer = vtkSmartPointer<vtkImageViewer2>::New(); viewer->SetInputConnection(reader->GetOutputPort()); vtkSmartPointer<vtkRenderWindowInteractor> interactor = vtkSmartPointer<vtkRenderWindowInteractor>::New(); viewer->SetupInteractor(interactor); viewer->Render(); The viewer - > GetRenderer () - > SetBackground (1.0, 1.0, 1.0); viewer->SetSize(640, 480); viewer->GetRenderWindow()->SetWindowName("ai jiao sha jiao sha"); vtkSmartPointer<vtkCallbackCommand> mouseCallback = vtkSmartPointer<vtkCallbackCommand>::New(); mouseCallback->SetCallback(MywCallbackFunc); interactor->SetRenderWindow(viewer->GetRenderWindow()); interactor->AddObserver(vtkCommand::LeftButtonPressEvent, mouseCallback); vtkSmartPointer<vtkInteractorStyleImage> style = vtkSmartPointer<vtkInteractorStyleImage>::New(); interactor->SetInteractorStyle(style); interactor->Initialize(); interactor->Start(); return EXIT_SUCCESS; } `Copy the code

VtkRenderWindowInteractor creates a subclass object associated with the Windows in the system (such as subclasses associated with Windows on Windows, Linux subclasses associated with Linux), and then use the Start function constantly get messages from the message queue, The obtained message is distributed to the callback function, which invokes different response message functions for different messages. In each response function, vtkObject::InvokeEvent translates platform-related messages into VTK events, such as the one we wrote above. Will be translated into events like onMousePressEvent.

vtkInteractorStyle:

VtkObject: : InvokeEvent () will be distributed to each VTK events different observers, observer callback function called vtkInteractorStyle: : ProcessEvents () to handle different VTK events, These events are then distributed to the message response function of vtkInteractorStyle, and the entire message is passed.