[vtkusers] Rotate actor programmatically while vtkRenderWindowInteractor is active

Pablo Rubi pabloarubi at gmail.com
Tue Jun 30 13:44:07 EDT 2015


Solved in:
http://stackoverflow.com/questions/31075569/vtk-rotate-actor-programmatically-while-vtkrenderwindowinteractor-is-active

-------------------------

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



2015-06-26 11:25 GMT-03:00 Pablo Rubi <pabloarubi at gmail.com>:

> (This message was also posted in StackOverflow, those who want to start
> using it as a platform for help, can answer in this link
> <http://stackoverflow.com/questions/31075569/vtk-rotate-actor-programmatically-while-vtkrenderwindowinteractor-is-active>
> )
>
> Hi all,
>
> I'm trying to rotate a vtkActor using vtkActor::RotateZ and then calling
> vtkRenderWindow::Render. It works fine (it rotates the actor) but I can't
> move, resize, or even focus the window.
>
> I suspected this was caused due to something not catching operating system
> events, so I added a vtkRenderWindowInteractor to the mix. Now I can
> move, resize and focus the window, but the actor is not rotating anymore.
>
> I've isolated the code in the snippet below, comment line 43 to see both
> effects:
>
> renderWindowInteractor->Start();
>
> I'm compiling VTK 6.2 with mingw-w64 (GCC 4.9.1), running in Windows 8.1.
> I've uploaded the code in this repo
> <https://github.com/Blito/VTK-SFML/tree/stackoverflow-question> with a
> small CMake setup so you can test it easily.
>
> Thanks for your help!
>
> -------------------------------
>
> 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();
>     }}
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20150630/d6967a7f/attachment.html>


More information about the vtkusers mailing list