Timer
vtk-examples/Cxx/Utilities/Timer
Description¶
This example demonstrates how to use a timer with an interactor. It outputs a count every specified interval.
Question
If you have a question about this example, please use the VTK Discourse Forum
Code¶
Timer.cxx
#include <vtkCallbackCommand.h>
#include <vtkCommand.h>
#include <vtkNew.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <iostream>
#include <string>
class vtkTimerCallback : public vtkCallbackCommand
{
public:
vtkTimerCallback() = default;
static vtkTimerCallback* New()
{
vtkTimerCallback* cb = new vtkTimerCallback;
cb->TimerCount = 0;
return cb;
}
virtual void Execute(vtkObject* caller, unsigned long eventId,
void* vtkNotUsed(callData))
{
if (vtkCommand::TimerEvent == eventId)
{
++this->TimerCount;
}
std::cout << this->TimerCount << std::endl;
if (this->TimerCount >= this->maxCount)
{
auto iren = dynamic_cast<vtkRenderWindowInteractor*>(caller);
if (this->timerId > -1)
{
iren->DestroyTimer(this->timerId);
}
}
}
private:
int TimerCount = 0;
public:
int maxCount = 0;
int timerId = -1;
};
int main(int, char*[])
{
// Setup renderer, render window, and interactor.
vtkNew<vtkRenderer> renderer;
vtkNew<vtkRenderWindow> renderWindow;
renderWindow->AddRenderer(renderer);
renderWindow->SetWindowName("Timer");
vtkNew<vtkRenderWindowInteractor> interactor;
interactor->SetRenderWindow(renderWindow);
// Initialize must be called prior to creating timer events.
renderWindow->Render();
interactor->Initialize();
// Sign up to receive TimerEvent.
vtkNew<vtkTimerCallback> cb;
interactor->AddObserver(vtkCommand::TimerEvent, cb);
int timerId = interactor->CreateRepeatingTimer(100);
std::cout << "timerId: " << timerId << std::endl;
// Destroy the timer when maxCount is reached.
cb->maxCount = 10;
cb->timerId = timerId;
// Note: nothing is displayed in the render window.
// Start the interaction and timer.
interactor->Start();
return EXIT_SUCCESS;
}
CMakeLists.txt¶
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
project(Timer)
find_package(VTK COMPONENTS
)
if (NOT VTK_FOUND)
message(FATAL_ERROR "Timer: Unable to find the VTK build folder.")
endif()
# Prevent a "command line is too long" failure in Windows.
set(CMAKE_NINJA_FORCE_RESPONSE_FILE "ON" CACHE BOOL "Force Ninja to use response files.")
add_executable(Timer MACOSX_BUNDLE Timer.cxx )
target_link_libraries(Timer PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
TARGETS Timer
MODULES ${VTK_LIBRARIES}
)
Download and Build Timer¶
Click here to download Timer and its CMakeLists.txt file. Once the tarball Timer.tar has been downloaded and extracted,
cd Timer/build
If VTK is installed:
cmake ..
If VTK is not installed but compiled on your system, you will need to specify the path to your VTK build:
cmake -DVTK_DIR:PATH=/home/me/vtk_build ..
Build the project:
make
and run it:
./Timer
WINDOWS USERS
Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.