AnimateActors
Repository source: AnimateActors
Question
If you have a question about this example, please use the VTK Discourse Forum
Code¶
AnimateActors.cxx
#include "AnimateActors.h"
#include <vtkAnimationCue.h>
#include <vtkAnimationScene.h>
#include <vtkCamera.h>
#include <vtkCommand.h>
#include <vtkConeSource.h>
#include <vtkLogger.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSphereSource.h>
int main(int argc, char* argv[])
{
vtkLogger::Init(argc, argv);
// Colors
vtkNew<vtkNamedColors> colors;
vtkColor3d coneColor = colors->GetColor3d("Tomato");
vtkColor3d sphereColor = colors->GetColor3d("Banana");
vtkColor3d backgroundColor = colors->GetColor3d("Peacock");
// Create the graphics structure. The renderer renders into the
// render window.
vtkNew<vtkRenderWindowInteractor> iren;
vtkNew<vtkRenderer> ren1;
ren1->SetBackground(backgroundColor.GetData());
vtkNew<vtkRenderWindow> renWin;
iren->SetRenderWindow(renWin);
renWin->AddRenderer(ren1);
// Generate a sphere
vtkNew<vtkSphereSource> sphereSource;
sphereSource->SetPhiResolution(31);
sphereSource->SetThetaResolution(31);
vtkNew<vtkPolyDataMapper> sphereMapper;
sphereMapper->SetInputConnection(sphereSource->GetOutputPort());
vtkNew<vtkActor> sphere;
sphere->SetMapper(sphereMapper);
sphere->GetProperty()->SetDiffuseColor(sphereColor.GetData());
sphere->GetProperty()->SetDiffuse(.7);
sphere->GetProperty()->SetSpecular(.3);
sphere->GetProperty()->SetSpecularPower(30.0);
ren1->AddActor(sphere);
// Generate a cone
vtkNew<vtkConeSource> coneSource;
coneSource->SetResolution(31);
vtkNew<vtkPolyDataMapper> coneMapper;
coneMapper->SetInputConnection(coneSource->GetOutputPort());
// auto cone = vtkSmartPointer<vtkActor>::New();
vtkNew<vtkActor> cone;
cone->SetMapper(coneMapper);
cone->GetProperty()->SetDiffuseColor(coneColor.GetData());
ren1->AddActor(cone);
// Create an Animation Scene
vtkNew<vtkAnimationScene> scene;
if (argc >= 2 && strcmp(argv[1], "-real") == 0)
{
vtkLogF(INFO, "real-time mode");
scene->SetModeToRealTime();
}
else
{
vtkLogF(INFO, "sequence mode");
scene->SetModeToSequence();
}
scene->SetLoop(0);
scene->SetFrameRate(5);
scene->SetStartTime(0);
scene->SetEndTime(20);
scene->AddObserver(vtkCommand::AnimationCueTickEvent, renWin.GetPointer(),
&vtkWindow::Render);
// Create an Animation Cue for each actor
vtkNew<vtkAnimationCue> cue1;
cue1->SetStartTime(5);
cue1->SetEndTime(23);
scene->AddCue(cue1);
vtkNew<vtkAnimationCue> cue2;
cue2->SetStartTime(1);
cue2->SetEndTime(10);
scene->AddCue(cue2);
// Create an ActorAnimator for each actor;
ActorAnimator animateSphere;
animateSphere.SetActor(sphere);
animateSphere.AddObserversToCue(cue1);
ActorAnimator animateCone;
animateCone.SetEndPosition(vtkVector3d(-1, -1, -1));
animateCone.SetActor(cone);
animateCone.AddObserversToCue(cue2);
renWin->SetWindowName("AnimateActors");
renWin->Render();
ren1->ResetCamera();
ren1->GetActiveCamera()->Dolly(.5);
ren1->ResetCameraClippingRange();
// Create Cue observer.
scene->Play();
scene->Stop();
iren->Start();
return EXIT_SUCCESS;
}
AnimateActors.h
#ifndef __AnimateActors_h
#define __AnimateActors_h
#include <vtkActor.h>
#include <vtkAnimationCue.h>
#include <vtkCommand.h>
#include <vtkVectorOperators.h>
class ActorAnimator
{
public:
ActorAnimator(vtkActor* Actor, const vtkVector3d& StartPosition,
const vtkVector3d& EndPosition)
: Actor(Actor), StartPosition(StartPosition), EndPosition(EndPosition)
{
}
ActorAnimator()
: Actor(nullptr), StartPosition(0, 0, 0), EndPosition(0.5, 0.5, 0.5)
{
}
~ActorAnimator() = default;
void SetActor(vtkActor* actor)
{
this->Actor = actor;
}
void SetStartPosition(const vtkVector3d& position)
{
this->StartPosition = position;
}
void SetEndPosition(const vtkVector3d& position)
{
this->EndPosition = position;
}
void AddObserversToCue(vtkAnimationCue* cue)
{
cue->AddObserver(vtkCommand::StartAnimationCueEvent, this,
&ActorAnimator::Start);
cue->AddObserver(vtkCommand::EndAnimationCueEvent, this,
&ActorAnimator::End);
cue->AddObserver(vtkCommand::AnimationCueTickEvent, this,
&ActorAnimator::Tick);
}
private:
//@{
/**
* These are callbacks that called when corresponding events are fired by the
* cue (see AddObserversToCue)
*/
void Start()
{
this->Actor->SetPosition(this->StartPosition.GetData());
}
void Tick(vtkObject* vtkNotUsed(caller), unsigned long vtkNotUsed(event),
void* calldata)
{
vtkAnimationCue::AnimationCueInfo* info =
reinterpret_cast<vtkAnimationCue::AnimationCueInfo*>(calldata);
const double t = (info->AnimationTime - info->StartTime) /
(info->EndTime - info->StartTime);
vtkVector3d position =
this->StartPosition + (this->EndPosition - this->StartPosition) * t;
this->Actor->SetPosition(position.GetData());
}
void End()
{
this->Actor->SetPosition(this->EndPosition.GetData());
}
//@}
vtkActor* Actor;
vtkVector3d StartPosition;
vtkVector3d EndPosition;
};
#endif // __AnimateActors_h
CMakeLists.txt¶
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
project(AnimateActors)
find_package(VTK COMPONENTS
CommonColor
CommonCore
CommonDataModel
FiltersSources
InteractionStyle
RenderingContextOpenGL2
RenderingCore
RenderingFreeType
RenderingGL2PSOpenGL2
RenderingOpenGL2
)
if (NOT VTK_FOUND)
message(FATAL_ERROR "AnimateActors: 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(AnimateActors MACOSX_BUNDLE AnimateActors.cxx )
target_link_libraries(AnimateActors PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
TARGETS AnimateActors
MODULES ${VTK_LIBRARIES}
)
Download and Build AnimateActors¶
Click here to download AnimateActors and its CMakeLists.txt file. Once the tarball AnimateActors.tar has been downloaded and extracted,
cd AnimateActors/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:
./AnimateActors
WINDOWS USERS
Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.