来自主线程的 VTK 窗口线程,C++

发布于 2024-08-18 18:42:28 字数 198 浏览 6 评论 0原文

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

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

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

枕头说它不想醒 2024-08-25 18:42:28

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

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

编辑:

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

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.

甜是你 2024-08-25 18:42:28

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

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

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文