AnimateVectors

VTKExamples/Cxx/Texture/AnimateVectors


Description

Texture maps can be animated as a function of time. By choosing a texture map whose intensity varies monotonically from dark to light, and then “moving” the texture along an object,the object appears to crawl in the direction of the texture map motion. We can use this technique to add apparent motion to things like hedgehogs to show vector magnitude. This example uses texture map animation to simulate vector field motion.

Cite

See B. Yamrom and K. M. Martin. Vector Field Animation with Texture Maps. IEEE Computer Graphics and Applications. 15(2):22–24, 1995 for background.

Info

See Figure 7-3 in Chapter 7 the VTK Textbook.

Other Languages

See (Python)

Question

If you have a simple question about this example contact us at VTKExamplesProject If your question is more complex and may require extended discussion, please use the VTK Discourse Forum

Code

AnimateVectors.cxx

#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkGlyph3D.h>
#include <vtkLineSource.h>
#include <vtkNamedColors.h>
#include <vtkOutlineFilter.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkStructuredPointsReader.h>
#include <vtkTexture.h>
#include <vtkThresholdPoints.h>

#include <vector>

int main (int argc, char *argv[])
{
  if (argc < 3)
  {
    std::cout << "Usage: " << argv[0] << " carotid.vtk vecAnim1.vtk ..." << std::endl;
    return EXIT_FAILURE;
  }

  vtkSmartPointer<vtkNamedColors> colors =
    vtkSmartPointer<vtkNamedColors>::New();

  // Setup render window, renderer, and interactor
  vtkSmartPointer<vtkRenderer> renderer = 
    vtkSmartPointer<vtkRenderer>::New();
  vtkSmartPointer<vtkRenderWindow> renderWindow = 
    vtkSmartPointer<vtkRenderWindow>::New();
  renderWindow->AddRenderer(renderer);
  vtkSmartPointer<vtkRenderWindowInteractor> interactor = 
    vtkSmartPointer<vtkRenderWindowInteractor>::New();
  interactor->SetRenderWindow(renderWindow);


// read data
//
// create pipeline
//
  vtkSmartPointer<vtkStructuredPointsReader> reader =
    vtkSmartPointer<vtkStructuredPointsReader>::New();
  reader->SetFileName(argv[1]);

  vtkSmartPointer<vtkThresholdPoints> threshold =
    vtkSmartPointer<vtkThresholdPoints>::New();
  threshold->SetInputConnection(reader->GetOutputPort());
  threshold->ThresholdByUpper(200);

  vtkSmartPointer<vtkLineSource> line =
    vtkSmartPointer<vtkLineSource>::New();
  line->SetResolution(1);

  vtkSmartPointer<vtkGlyph3D> lines =
    vtkSmartPointer<vtkGlyph3D>::New();
  lines->SetInputConnection(threshold->GetOutputPort());
  lines->SetSourceConnection(line->GetOutputPort());
  lines->SetScaleFactor(0.005);
  lines->SetScaleModeToScaleByScalar();
  lines->Update();

  vtkSmartPointer<vtkPolyDataMapper> vectorMapper =
    vtkSmartPointer<vtkPolyDataMapper>::New();
  vectorMapper->SetInputConnection(lines->GetOutputPort());
  vectorMapper->SetScalarRange(lines->GetOutput()->GetScalarRange());

  vtkSmartPointer<vtkActor> vectorActor =
    vtkSmartPointer<vtkActor>::New();
  vectorActor->SetMapper(vectorMapper);
  vectorActor->GetProperty()->SetOpacity(0.99);
  vectorActor->GetProperty()->SetLineWidth(1.5);

// outline
  vtkSmartPointer<vtkOutlineFilter> outline =
    vtkSmartPointer<vtkOutlineFilter>::New();
  outline->SetInputConnection(reader->GetOutputPort());

  vtkSmartPointer<vtkPolyDataMapper> outlineMapper =
    vtkSmartPointer<vtkPolyDataMapper>::New();
  outlineMapper->SetInputConnection(outline->GetOutputPort());

  vtkSmartPointer<vtkActor> outlineActor =
    vtkSmartPointer<vtkActor>::New();
  outlineActor->SetMapper(outlineMapper);
  outlineActor->GetProperty()->SetColor(colors->GetColor3d("Black").GetData());

//  texture maps
  std::vector<vtkSmartPointer<vtkTexture> > textureMaps;
  for (int i =  2; i < argc; ++i)
  {
    vtkSmartPointer<vtkStructuredPointsReader> tmap =
      vtkSmartPointer<vtkStructuredPointsReader>::New();
    tmap->SetFileName(argv[i]);

    vtkSmartPointer<vtkTexture> texture =
      vtkSmartPointer<vtkTexture>::New();
    texture->SetInputConnection(tmap->GetOutputPort());
    texture->InterpolateOff();
    texture->RepeatOff();
    textureMaps.push_back(texture);
  }
  vectorActor->SetTexture(textureMaps[0]);

// Add the actors to the renderer, set the background and size
//
  renderer->AddActor(vectorActor);
  renderer->AddActor(outlineActor);

  vtkSmartPointer<vtkCamera> cam1 =
    vtkSmartPointer<vtkCamera>::New();
  cam1->SetClippingRange(17.4043, 870.216);
  cam1->SetFocalPoint(136.71, 104.025, 23);
  cam1->SetPosition(204.747, 258.939, 63.7925);
  cam1->SetViewUp(-0.102647, -0.210897, 0.972104);
  cam1->Zoom(1.5);
  renderer->SetActiveCamera(cam1);

  renderer->SetBackground(colors->GetColor3d("Wheat").GetData());
  renderWindow->SetSize(640, 480);

// go into loop
  for (int j = 0; j < 100; ++j)
  {
    for (size_t i = 0; i < textureMaps.size(); ++i)
    {
      vectorActor->SetTexture(textureMaps[i]);
      renderWindow->Render();
    }
  }
  interactor->Start();
  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

project(AnimateVectors)

find_package(VTK COMPONENTS 
  vtkCommonColor
  vtkFiltersCore
  vtkFiltersModeling
  vtkFiltersSources
  vtkIOLegacy
  vtkInteractionStyle
  vtkRenderingCore
  vtkRenderingFreeType
  vtkRenderingOpenGL2 QUIET)
if (NOT VTK_FOUND)
  message("Skipping AnimateVectors: ${VTK_NOT_FOUND_MESSAGE}")
  return ()
endif()
message (STATUS "VTK_VERSION: ${VTK_VERSION}")
if (VTK_VERSION VERSION_LESS "8.90.0")
  # old system
  include(${VTK_USE_FILE})
  add_executable(AnimateVectors MACOSX_BUNDLE AnimateVectors.cxx )
  target_link_libraries(AnimateVectors PRIVATE ${VTK_LIBRARIES})
else ()
  # include all components
  add_executable(AnimateVectors MACOSX_BUNDLE AnimateVectors.cxx )
  target_link_libraries(AnimateVectors PRIVATE ${VTK_LIBRARIES})
  # vtk_module_autoinit is needed
  vtk_module_autoinit(
    TARGETS AnimateVectors
    MODULES ${VTK_LIBRARIES}
    )
endif () 

Download and Build AnimateVectors

Click here to download AnimateVectors and its CMakeLists.txt file. Once the tarball AnimateVectors.tar has been downloaded and extracted,

cd AnimateVectors/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:

./AnimateVectors

WINDOWS USERS

Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.