Referring to This example: https://lorensen.github.io/VTKExamples/site/Cxx/Utilities/Animation/
I made a small change in the callback function, to stop it after certain number of timer count.
#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkCommand.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <iostream>
using namespace std;
class vtkTimerCallback2 : public vtkCommand
{
public:
static vtkTimerCallback2 *New()
{
vtkTimerCallback2 *cb = new vtkTimerCallback2;
cb->TimerCount = 0;
return cb;
}
virtual void Execute(vtkObject *caller, unsigned long eventId,
void * vtkNotUsed(callData))
{
if (vtkCommand::TimerEvent == eventId)
{
++this->TimerCount;
}
vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::SafeDownCast(caller);
std::cout << this->TimerCount << std::endl;
if(TimerCount<20)
{
actor->SetPosition(this->TimerCount, this->TimerCount,0);
iren->GetRenderWindow()->Render();
}
else
{
//iren->DestroyTimer();
//The following will print 1 if timer is destroyed
//And 0, if it is not destroyed
cout << "Timer Destroyed: " <<iren->DestroyTimer() << endl;;
}
}
private:
int TimerCount;
public:
vtkActor* actor;
};
int main(int, char* [])
{
// Create a sphere
vtkSmartPointer<vtkSphereSource> sphereSource =
vtkSmartPointer<vtkSphereSource>::New();
sphereSource->SetCenter(0.0, 0.0, 0.0);
sphereSource->SetRadius(5.0);
sphereSource->Update();
// Create a mapper and actor
vtkSmartPointer<vtkPolyDataMapper> mapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputConnection(sphereSource->GetOutputPort());
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
// Create a renderer, render window, and interactor
vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer);
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
// Add the actor to the scene
renderer->AddActor(actor);
renderer->SetBackground(1,1,1); // Background color white
// Render and interact
renderWindow->Render();
// Initialize must be called prior to creating timer events.
renderWindowInteractor->Initialize();
// Sign up to receive TimerEvent
vtkSmartPointer<vtkTimerCallback2> cb =
vtkSmartPointer<vtkTimerCallback2>::New();
cb->actor = actor;
renderWindowInteractor->AddObserver(vtkCommand::TimerEvent, cb);
int timerId = renderWindowInteractor->CreateRepeatingTimer(100);
std::cout << "timerId: " << timerId << std::endl;
// Start the interaction and timer
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
The issue is that, if I interact with the renderWindowInteractor using mouse during the animation, then later after releasing the mouse, callback function runs forever and it can be confirmed by the console log (Basically cout << "Timer Destroyed: " <<iren->DestroyTimer() << endl;
returns 0, meaning timer destruction failed).
However, otherwise it works as intended and stops, since nothing is printed "forever" in the console. Am I doing something wrong? Is this behavior expected?
I solved the problem myself. Basically DestroyTimer() without arguments is kept for backward compatibility. DestroyTimer(int timerId) is what needs to be called. Hence, the following works perfectly now:
#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkCommand.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <iostream>
using namespace std;
class vtkTimerCallback2 : public vtkCommand
{
public:
int timerId;
static vtkTimerCallback2 *New()
{
vtkTimerCallback2 *cb = new vtkTimerCallback2;
cb->TimerCount = 0;
return cb;
}
virtual void Execute(vtkObject *caller, unsigned long eventId,
void * vtkNotUsed(callData))
{
if (vtkCommand::TimerEvent == eventId)
{
++this->TimerCount;
}
vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::SafeDownCast(caller);
std::cout << this->TimerCount << std::endl;
if(TimerCount<20)
{
actor->SetPosition(this->TimerCount, this->TimerCount,0);
iren->GetRenderWindow()->Render();
}
else
{
//iren->DestroyTimer();
//The following will print 1 if timer is destroyed
//And 0, if it is not destroyed
cout << "Timer Destroyed: " <<iren->DestroyTimer(this->timerId) << endl;
}
}
private:
int TimerCount;
public:
vtkActor* actor;
};
int main(int, char* [])
{
// Create a sphere
vtkSmartPointer<vtkSphereSource> sphereSource =
vtkSmartPointer<vtkSphereSource>::New();
sphereSource->SetCenter(0.0, 0.0, 0.0);
sphereSource->SetRadius(5.0);
sphereSource->Update();
// Create a mapper and actor
vtkSmartPointer<vtkPolyDataMapper> mapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputConnection(sphereSource->GetOutputPort());
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
// Create a renderer, render window, and interactor
vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer);
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
// Add the actor to the scene
renderer->AddActor(actor);
renderer->SetBackground(1,1,1); // Background color white
// Render and interact
renderWindow->Render();
// Initialize must be called prior to creating timer events.
renderWindowInteractor->Initialize();
// Sign up to receive TimerEvent
vtkSmartPointer<vtkTimerCallback2> cb =
vtkSmartPointer<vtkTimerCallback2>::New();
cb->actor = actor;
renderWindowInteractor->AddObserver(vtkCommand::TimerEvent, cb);
int timerId = renderWindowInteractor->CreateRepeatingTimer(100);
cb->timerId = timerId;
std::cout << "timerId: " << timerId << std::endl;
// Start the interaction and timer
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
My mistake, I did not read the documentation carefully. I hope it may be useful.