We can create an timer in vtkRenderWindowInteractor object, then register timerEvent for it to trigger task.

Here is simple example about rotating a cone automatically.

#include <iostream>
#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>
#include <vtkActor.h>
#include <vtkConeSource.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkPolyDataMapper.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkTimerLog.h>
#include <chrono>
#include <iostream>

#define vtkSPtr vtkSmartPointer
#define vtkSPtrNew(Var, Type) vtkSPtr<Type> Var = vtkSPtr<Type>::New();

using namespace std;

long long getMs()
{
    auto now = std::chrono::system_clock::now();

    // convert to milliseconds
    auto now_ms = std::chrono::time_point_cast<std::chrono::milliseconds>(now);

    auto value = now_ms.time_since_epoch();

    long long milliseconds = value.count();
    return milliseconds;
}

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))
    {
        auto milliseconds = getMs();
        std::cout << "time: " << milliseconds << std::endl;
        actor->RotateZ(10);
        renWin->Render();
    }

    vtkSPtr<vtkActor> actor;
    vtkSPtr<vtkRenderWindow> renWin;
};

int main()
{
    vtkSPtrNew( cone, vtkConeSource );
    vtkSPtrNew( mapper, vtkPolyDataMapper );
    mapper->SetInputConnection( cone->GetOutputPort() );

    vtkSPtrNew( actor, vtkActor );
    actor->SetMapper( mapper );

    vtkSPtrNew( renderer, vtkRenderer );
    renderer->AddActor(actor);
    renderer->SetBackground( 0, 0, 0 );

    vtkSPtrNew( renderWindow, vtkRenderWindow );
    renderWindow->AddRenderer( renderer );

    vtkSPtrNew( renderWindowInteractor, vtkRenderWindowInteractor );
    renderWindowInteractor->SetRenderWindow( renderWindow );
    // Initialize must be called prior to creating timer events.
    renderWindowInteractor->Initialize();
    int timerId = renderWindowInteractor->CreateRepeatingTimer(200);
    std::cout << "timerId: " << timerId << std::endl;

    vtkSPtrNew( rotateCallback, RotateCommand );
    rotateCallback->actor = actor;
    rotateCallback->renWin = renderWindow;
    renderWindowInteractor->AddObserver(vtkCommand::TimerEvent, rotateCallback );

    //renderWindowInteractor->RemoveObserver( rotateCallback );
    //renderWindowInteractor->DestroyTimer( timerId);

    renderer->ResetCamera();
    renderWindow->Render();
    renderWindowInteractor->Start();
    return 0;
}

Result:

Categories: VTK

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments

Content Summary
: Input your strings, the tool can get a brief summary of the content for you.

X
0
Would love your thoughts, please comment.x
()
x