VTK window thread from main thread, C++

好久不见. 提交于 2020-01-01 06:18:20

问题


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

The main application launches rendered window at some point in application. Would like to be able for the main thread to continue, while VTK window is displayed. Is there a particular method to launch VTK window as a thread?

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

VTK is visualization toolkit, see vtk.org


回答1:


You can call vtkRenderWindowInteractor->Start() method. (Get the interactor from your renderer if you didn't create one).

There are tons of examples included with VTK; you should take a look at those! If you don't have them, be sure that when building VTK with cmake, turn on VTK_BUILD_EXAMPLES.

Edit:

You should take a look at the GUI examples since that seems what you are trying to build.




回答2:


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;
}


来源:https://stackoverflow.com/questions/2136711/vtk-window-thread-from-main-thread-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!