VTK: Rotate actor programmatically while vtkRenderWindowInteractor is active

喜夏-厌秋 提交于 2019-12-04 13:38:32

Hack to get around part of the problem.

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

I'm sorry if I didn't give you any other reply, but since this solution is quite bad, I was waiting for someone else to find a better solution, but it seems that no one answered you, so here is what I would do.

You can pretty much guess what it does. Each 1 ms after starting the interaction, a callback is called that will stop the interaction, and do a rotation.

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