参考这个例子:https ://lorensen.github.io/VTKExamples/site/Cxx/Utilities/Animation/
我对回调函数做了一个小改动,在一定数量的计时器计数后停止它。
#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;
}
问题是,如果我在动画过程中使用鼠标与 renderWindowInteractor 交互,然后在释放鼠标后,回调函数将永远运行,并且可以通过控制台日志确认(基本上cout << "Timer Destroyed: " <<iren->DestroyTimer() << endl;
返回 0,意味着计时器销毁失败)。但是,否则它会按预期工作并停止,因为控制台中不会“永远”打印任何内容。难道我做错了什么?这种行为是预期的吗?