从主线程,C ++的VTK窗口线程

时间:2010-01-26 00:12:57

标签: c++ multithreading vtk

我正在学习VTK(和C ++ GUI编程),并且希望有一个简单的问题。

主应用程序在应用程序的某个时刻启动渲染窗口。希望能够继续主线程,同时显示VTK窗口。是否有一种特殊的方法可以将VTK窗口作为线程启动?

我的环境是Linux,我可以使用boost和pthreads。 感谢。

VTK是可视化工具包,请参阅vtk.org

2 个答案:

答案 0 :(得分:3)

您可以调用vtkRenderWindowInteractor-> Start()方法。 (如果您没有创建一个,则从渲染器获取交互器。)

VTK包含大量示例;你应该看看那些!如果您没有它们,请确保在使用cmake构建VTK时,请启用VTK_BUILD_EXAMPLES。

编辑:

您应该看一下GUI示例,因为这似乎是您要构建的内容。

答案 1 :(得分:0)

这是我的解决方案。希望这有帮助!

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