VTK:在 vtkRenderWindowInteractor 处于活动状态时以编程方式旋转演员

VTK: Rotate actor programmatically while vtkRenderWindowInteractor is active

我正在尝试使用 vtkActor::RotateZ 旋转 vtkActor,然后调用 vtkRenderWindow::Render。它工作正常(旋转演员)但我无法移动、调整大小,甚至无法聚焦 window。

我怀疑这是由于未捕获操作系统事件引起的,因此我添加了一个 vtkRenderWindowInteractor。现在我可以移动、调整大小和聚焦 window,但演员不再旋转。

我在下面的代码片段中分离了代码,评论第 43 行以查看两种效果:

renderWindowInteractor->Start();

我正在使用 mingw-w64 (GCC 4.9.1) 编译 VTK 6.2,运行 在 Windows 8.1 中。我已经使用小型 CMake 设置在 this repo 中上传了代码,因此您可以轻松地对其进行测试。

感谢您的帮助!


constexpr float planeWidth = 200.0f;
constexpr float planeHeight = 100.0f;

int main()
{
    auto renderer = vtkRenderer::New();

    // Create render window
    auto renWin = vtkRenderWindow::New();
    renWin->AddRenderer(renderer);
    renWin->SetSize(600,600);

    // Create a plane
    auto texturedPlane = vtkActor::New();
    auto plane = vtkPlaneSource::New();
    plane->SetOrigin(0, planeHeight, 0);
    plane->SetPoint1(planeWidth, planeHeight, 0);
    plane->SetPoint2(0, 0, 0);

    auto planeMapper = vtkPolyDataMapper::New();
    planeMapper->SetInputConnection(plane->GetOutputPort());
    texturedPlane->SetMapper(planeMapper);
    texturedPlane->SetOrigin(planeWidth / 2, planeHeight, 0);

    renderer->AddActor(texturedPlane);
    renderer->ResetCamera();

    // Create a RenderWindowInteractor
    auto renderWindowInteractor = vtkRenderWindowInteractor::New();
    renderWindowInteractor->SetRenderWindow(renWin);
    renderWindowInteractor->Start(); // <-- Comment this line!

    // Render
    float rot = 0.0f;
    while(true)
    {
        texturedPlane->SetOrientation(0,0,0);
        texturedPlane->RotateZ(rot++);

        renWin->Render();
    }
}

使用asdfasdf的方案解决: (代码在this repo

constexpr float planeWidth = 200.0f;
constexpr float planeHeight = 100.0f;
vtkActor * texturedPlane;
vtkRenderWindowInteractor * renderWindowInteractor;
vtkRenderWindow * renWin;
float rot = 0.0f;

class RotateCommand : public vtkCommand
{
public:
    vtkTypeMacro(RotateCommand, vtkCommand);

    static RotateCommand * New()
    {
        return new RotateCommand;
    }

    void Execute(vtkObject * vtkNotUsed(caller),
                 unsigned long vtkNotUsed(eventId), 
                 void * vtkNotUsed(callData))
    {
        texturedPlane->SetOrientation(0,0,0);
        texturedPlane->RotateZ(rot++);

        renWin->Render();
    }
};

int main()
{
    auto renderer = vtkRenderer::New();

    // Create render window
    renWin = vtkRenderWindow::New();
    renWin->AddRenderer(renderer);
    renWin->SetSize(600,600);

    // Create a plane
    texturedPlane = vtkActor::New();
    auto plane = vtkPlaneSource::New();
    plane->SetOrigin(0, planeHeight, 0);
    plane->SetPoint1(planeWidth, planeHeight, 0);
    plane->SetPoint2(0, 0, 0);

    auto planeMapper = vtkPolyDataMapper::New();
    planeMapper->SetInputConnection(plane->GetOutputPort());
    texturedPlane->SetMapper(planeMapper);
    texturedPlane->SetOrigin(planeWidth / 2, planeHeight, 0);

    renderer->AddActor(texturedPlane);

    renderer->ResetCamera();

    // Create a RenderWindowInteractor
    renderWindowInteractor = vtkRenderWindowInteractor::New();
    renderWindowInteractor->SetRenderWindow(renWin);
    renderWindowInteractor->Initialize();
    renderWindowInteractor->CreateRepeatingTimer(1);
    RotateCommand * rotateCallback =  RotateCommand::New();
    renderWindowInteractor->AddObserver(vtkCommand::TimerEvent, rotateCallback );

    renderWindowInteractor->Start();
}

黑客解决部分问题。

vtkRenderWindowInteractor * renderWindowInteractor;
constexpr float planeWidth = 200.0f;
constexpr float planeHeight = 100.0f;

class CommandSubclass2 : public vtkCommand
{
  public:
    vtkTypeMacro(CommandSubclass2, vtkCommand);

    static CommandSubclass2 *New()
    {
        return new CommandSubclass2;
    }

    void Execute(vtkObject *vtkNotUsed(caller), unsigned long vtkNotUsed(eventId), 
                    void *vtkNotUsed(callData))
    {
        std::cout << "timer callback" << std::endl;
        renderWindowInteractor->ExitCallback();
    }
};

int main()
{
    auto renderer = vtkRenderer::New();

    // Create render window
    auto renWin = vtkRenderWindow::New();
    renWin->AddRenderer(renderer);
    renWin->SetSize(600,600);

    // Create a plane
    auto texturedPlane = vtkActor::New();
    auto plane = vtkPlaneSource::New();
    plane->SetOrigin(0, planeHeight, 0);
    plane->SetPoint1(planeWidth, planeHeight, 0);
    plane->SetPoint2(0, 0, 0);

    auto planeMapper = vtkPolyDataMapper::New();
    planeMapper->SetInputConnection(plane->GetOutputPort());
    texturedPlane->SetMapper(planeMapper);
    texturedPlane->SetOrigin(planeWidth / 2, planeHeight, 0);

    renderer->AddActor(texturedPlane);
    renderer->ResetCamera();

    // Create a RenderWindowInteractor
    renderWindowInteractor = vtkRenderWindowInteractor::New();
    renderWindowInteractor->SetRenderWindow(renWin);

    renderWindowInteractor->Initialize();
    renderWindowInteractor->CreateRepeatingTimer(1);

    vtkSmartPointer<CommandSubclass2> timerCallback =  vtkSmartPointer<CommandSubclass2>::New();
    renderWindowInteractor->AddObserver ( vtkCommand::TimerEvent, timerCallback );


    // Render
    float rot = 0.0f;
    while(true)
    {
        renderWindowInteractor->Start(); // <-- Comment this line!
        texturedPlane->SetOrientation(0,0,0);
        texturedPlane->RotateZ(rot++);           
        renWin->Render();
    }
}

很抱歉没有给你任何其他的回复,但是由于这个解决方案很糟糕,我一直在等待其他人找到更好的解决方案,但似乎没有人回答你,所以这是我会做的。

你几乎可以猜到它的作用。开始交互后每 1 ms 调用一个回调来停止交互,并进行轮换。