VTK/Examples/Cxx/Utilities/DataAnimation

From KitwarePublic

Jump to: navigation, search
VTK Examples Baseline Utilities TestDataAnimation.png

This causes one point of the sphere to be moved outwards at each iteration.

DataAnimation.cxx

#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkProgrammableFilter.h>
#include <vtkCallbackCommand.h>
 
void TimerCallbackFunction ( vtkObject* caller, long unsigned int eventId, void* clientData, void* callData );
 
// Globals
unsigned int counter2 = 0;
 
void AdjustPoints2(void* arguments)
{
  std::cout << "AdjustPoints2" << std::endl;
  vtkProgrammableFilter* programmableFilter = 
      static_cast<vtkProgrammableFilter*>(arguments);
 
  vtkPoints* inPts = programmableFilter->GetPolyDataInput()->GetPoints();
  vtkIdType numPts = inPts->GetNumberOfPoints();
  vtkSmartPointer<vtkPoints> newPts =
      vtkSmartPointer<vtkPoints>::New();
  newPts->SetNumberOfPoints(numPts);
 
  for(vtkIdType i = 0; i < numPts; i++)
    {
    double p[3];
    inPts->GetPoint(i, p);
    newPts->SetPoint(i, p);
    }
 
  double p0[3];
  inPts->GetPoint(0, p0);
  p0[2] = p0[2] + counter2 * 0.1;
  newPts->SetPoint(0, p0);
 
  programmableFilter->GetPolyDataOutput()->CopyStructure(programmableFilter->GetPolyDataInput());
  programmableFilter->GetPolyDataOutput()->SetPoints(newPts);
}
 
int main(int, char *[])
{
  // Create a sphere
  vtkSmartPointer<vtkSphereSource> sphereSource = 
    vtkSmartPointer<vtkSphereSource>::New();
  sphereSource->Update();
 
  vtkSmartPointer<vtkProgrammableFilter> programmableFilter = 
    vtkSmartPointer<vtkProgrammableFilter>::New();
  programmableFilter->SetInputConnection(sphereSource->GetOutputPort());
 
  programmableFilter->SetExecuteMethod(AdjustPoints2, programmableFilter);
 
  // Create a mapper and actor
  vtkSmartPointer<vtkPolyDataMapper> mapper = 
    vtkSmartPointer<vtkPolyDataMapper>::New();
  mapper->SetInputConnection(programmableFilter->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);
 
  // Initialize must be called prior to creating timer events.
  renderWindowInteractor->Initialize();
  renderWindowInteractor->CreateRepeatingTimer(500);
 
  vtkSmartPointer<vtkCallbackCommand> timerCallback = 
    vtkSmartPointer<vtkCallbackCommand>::New();
  timerCallback->SetCallback ( TimerCallbackFunction );
  timerCallback->SetClientData(programmableFilter);
 
  renderWindowInteractor->AddObserver ( vtkCommand::TimerEvent, timerCallback );
 
  // Add the actor to the scene
  renderer->AddActor(actor);
  renderer->SetBackground(1,1,1); // Background color white
 
  // Render and interact
  renderWindow->Render();
  renderWindowInteractor->Start();
 
  return EXIT_SUCCESS;
}
 
 
void TimerCallbackFunction ( vtkObject* caller, long unsigned int vtkNotUsed(eventId), void* clientData, void* vtkNotUsed(callData) )
{
  cout << "timer callback" << endl;
 
  vtkSmartPointer<vtkProgrammableFilter> programmableFilter = 
      static_cast<vtkProgrammableFilter*>(clientData);
 
  vtkRenderWindowInteractor *iren = 
    static_cast<vtkRenderWindowInteractor*>(caller);
 
  programmableFilter->Modified();
 
  iren->Render();
 
  counter2++;
}

CMakeLists.txt

cmake_minimum_required(VERSION 2.6)
PROJECT(DataAnimation)
 
FIND_PACKAGE(VTK REQUIRED)
INCLUDE(${VTK_USE_FILE})
 
ADD_EXECUTABLE(DataAnimation DataAnimation.cxx)
TARGET_LINK_LIBRARIES(DataAnimation vtkHybrid)
Personal tools