VTK window thread from main thread, C ++

I am just learning VTK (and C ++ GUI programming) and hopefully a simple question.

The main application launches the rendered window at some point in the application. I would like the main thread to continue while the VTK window is displayed. Is there a special way to launch a VTK window as a stream?

My environment is Linux, with boost and pthreads at my disposal. Thanks.

VTK is a visualization toolkit, see vtk.org

+4
source share
2 answers

You can call the vtkRenderWindowInteractor-> Start () method. (Get interaction with your renderer if you haven't created one).

VTK has many examples; you should take a look at them! If you don’t have them, make sure to enable VTK_BUILD_EXAMPLES when building VTK with cmake.

Edit:

You should take a look at GUI examples as this is similar to what you are trying to create.

+2
source

Here is my solution. Hope this helps!

#include <Windows.h> #include <iostream> #include <vtkVersion.h> #include <vtkSmartPointer.h> #include <vtkPolyData.h> #include <vtkPolyDataMapper.h> #include <vtkActor.h> #include <vtkRenderWindow.h> #include <vtkRenderer.h> #include <vtkRenderWindowInteractor.h> #include <vtkInteractorStyleTrackballCamera.h> #include <vtkSphereSource.h> #include <vtkElevationFilter.h> #include <vtkVectorText.h> #include <vtkCommand.h> #include <vtkAutoInit.h> VTK_MODULE_INIT(vtkInteractionStyle); VTK_MODULE_INIT(vtkRenderingOpenGL); #define w 400 //---------------------------------------------o0o---------------------------------------------// #define VTK_CREATE(type, name) \ vtkSmartPointer<type> name = vtkSmartPointer<type>::New() //---------------------------------------------o0o---------------------------------------------// using namespace std; void* mutex; void* handler_thread = 0; VTK_CREATE(vtkRenderer, renDisplay3D); VTK_CREATE(vtkRenderWindow, renderwindowDisplay3D); VTK_CREATE(vtkRenderWindowInteractor, irenDisplay3D); vtkInteractorStyleTrackballCamera *styleDisplay3D = vtkInteractorStyleTrackballCamera::New(); class CommandSubclass2 : public vtkCommand { public: vtkTypeMacro(CommandSubclass2, vtkCommand); static CommandSubclass2 *New() { return new CommandSubclass2; } void Execute(vtkObject *caller, unsigned long vtkNotUsed(eventId), void *vtkNotUsed(callData)) { vtkRenderWindowInteractor *iren = static_cast<vtkRenderWindowInteractor*>(caller); iren->Render(); } }; unsigned long __stdcall displayVTKThread(void* param) { //WaitForSingleObject(mutex, INFINITE); renderwindowDisplay3D->SetSize(600, 400); renderwindowDisplay3D->AddRenderer(renDisplay3D); renderwindowDisplay3D->Render(); irenDisplay3D->SetRenderWindow(renderwindowDisplay3D); irenDisplay3D->SetInteractorStyle(styleDisplay3D); // Initialize must be called prior to creating timer events. irenDisplay3D->Initialize(); vtkSmartPointer<CommandSubclass2> timerCallback = vtkSmartPointer<CommandSubclass2>::New(); irenDisplay3D->AddObserver ( vtkCommand::TimerEvent, timerCallback ); irenDisplay3D->CreateRepeatingTimer(100); irenDisplay3D->Start(); //ReleaseMutex(mutex); return 0; } int main (int argv, char* argc[]) { mutex = CreateMutex(0, false, 0); unsigned long id_thread; VTK_CREATE(vtkVectorText, text); text->SetText("Display 3D Point Clouds!"); VTK_CREATE(vtkElevationFilter, elevation); elevation->SetInputConnection(text->GetOutputPort()); elevation->SetLowPoint(0,0,0); elevation->SetHighPoint(10,0,0); // Mapper VTK_CREATE(vtkPolyDataMapper, mapper); mapper->SetInputConnection(elevation->GetOutputPort()); mapper->Update(); // Actor in scene VTK_CREATE(vtkActor, actor); actor->SetMapper(mapper); // Add Actor to renderer renDisplay3D->AddActor(actor); renDisplay3D->SetBackground(0.0, 0.0, 0.0); handler_thread = CreateThread(0, 0, displayVTKThread, 0, 0, &id_thread); if(!handler_thread) { printf("Cannot create thread. Error code = %d\n", GetLastError()); getchar(); return -1; } char myChar = ' '; while(myChar != 'q') { myChar = getchar(); if (myChar == 'v') { //WaitForSingleObject(mutex, INFINITE); // Create a sphere VTK_CREATE(vtkSphereSource, sphereSource); sphereSource->SetCenter(0.0, 0.0, 0.0); sphereSource->SetRadius(5.0); mapper->SetInputConnection(sphereSource->GetOutputPort()); actor->SetMapper(mapper); renDisplay3D->ResetCamera(); renDisplay3D->AddActor(actor); //ReleaseMutex(mutex); } } CloseHandle(handler_thread); printf("\n\nExit program\n"); Sleep(1000); CloseHandle(mutex); return 0; } 
0
source

Source: https://habr.com/ru/post/1299382/


All Articles