Custom event loop for generic render window interactor

Hello, I want to implement my own event loop using a vtkGenericRenderWindowInteractor instance. I couldn’t find any examples where the event loop is implemented.

The code should have the following structure:

#include <vtkGenericRenderWindowInteractor.h>

vtkNew<vtkGenericRenderWindowInteractor> interactor;

// custom event
int renderEvent = vtkCommand::UserEvent + 1;

function processData()
{
    // do some data processing

    // when finished, update the visualization
    interactor->InvokeEvent(renderEvent);  // invoke custom event to render the scene
}

int main()
{
    // initialize the visualization
    // ...

    // Start the interactor without blocking the execution
    interactor->Start();

    // Event loop
    while (true)
    {
        processData(); // do some data processing and update the scene when finished

        // invoke mouse events for interactions with the scene
        // ...
    }
}

However, I have several questions.

First, if the execution of processData() takes 1 second, would the interactor be unresponsive during that time? If yes, can this be solved by calling processData() in a separate thread with a flag that checks if the previous call was finished before processData() is called again?

Second, how can I enable the same events of a vtkRenderWindowInteractor instance in a vtkGenericRenderWindowInteractor instance, i.e., the mouse events such as rotating, etc.?

This may be done with the following code:

interactor->Initialize();
while (running)
 {
    interactor->ProcessEvents();
}
renderWindow->Finalize();
renderWindow->GetInteractor()->TerminateApp();

Useful if you need to nest VTK in your own event system.

The vtkOpenVRInteractor initialization is slightly different.

Also note ProcessEvents is broken for Linux on VTK 9.0. It is fixed in VTK 9.1 though.

Thank you for your response. Unfortunately, I forgot to mention that I’m still using VTK 8. It seems like ProcessEvents() is not available in VTK 8. Is there a workaround or an alternative method?

Ah yeah. Anyways, it should still use an event loop whether it exposes it or not. So what you can do is depending on the system you’re targeting, or both. Copy vtkWin32RenderWindowInteractor or vtkXRenderWindowInteractor. Rename them to vtkWin32RenderWindowInteractor2 or something & use it instead.

If we pop open vtkWin32RenderWindowInteractor we can see,

https://gitlab.kitware.com/vtk/vtk/-/blob/v8.2.0/Rendering/OpenGL2/vtkWin32RenderWindowInteractor.cxx#L146

Which implements said event loop. Handling win32 system messages.

Similar linux will be done here: https://gitlab.kitware.com/vtk/vtk/-/blob/v8.2.0/Rendering/OpenGL2/vtkXRenderWindowInteractor.cxx#L249

It’s also worth mentioning mentioning whatever data you are wanting to process. You could setup a timer event which would be invoked by VTK every Nms (outside of rendering, ie: done in sequence).

https://kitware.github.io/vtk-examples/site/Cxx/Utilities/Timer/