Skip to content

WarpVector

vtk-examples/Cxx/PolyData/WarpVector


Other languages

See (Python), (CSharp)

Question

If you have a question about this example, please use the VTK Discourse Forum

Code

WarpVector.cxx

#include <vtkActor.h>
#include <vtkCellArray.h>
#include <vtkDoubleArray.h>
#include <vtkLine.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPointData.h>
#include <vtkPoints.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkWarpVector.h>

int main(int, char*[])
{

  vtkNew<vtkNamedColors> colors;

  vtkNew<vtkPoints> points;
  points->InsertNextPoint(0.0, 0.0, 0.0);
  points->InsertNextPoint(1.0, 0.0, 0.0);
  points->InsertNextPoint(2.0, 0.0, 0.0);
  points->InsertNextPoint(3.0, 0.0, 0.0);
  points->InsertNextPoint(4.0, 0.0, 0.0);

  vtkNew<vtkCellArray> lines;
  vtkNew<vtkLine> line;
  line->GetPointIds()->SetId(0, 0);
  line->GetPointIds()->SetId(1, 1);
  lines->InsertNextCell(line);
  line->GetPointIds()->SetId(0, 1);
  line->GetPointIds()->SetId(1, 2);
  lines->InsertNextCell(line);
  line->GetPointIds()->SetId(0, 2);
  line->GetPointIds()->SetId(1, 3);
  lines->InsertNextCell(line);
  line->GetPointIds()->SetId(0, 3);
  line->GetPointIds()->SetId(1, 4);
  lines->InsertNextCell(line);

  vtkNew<vtkDoubleArray> warpData;
  warpData->SetNumberOfComponents(3);
  warpData->SetName("warpData");
  double warp[3] = {0.0, 0.0, 0.0};
  warp[1] = 0.0;
  warpData->InsertNextTuple(warp);
  warp[1] = 0.1;
  warpData->InsertNextTuple(warp);
  warp[1] = 0.3;
  warpData->InsertNextTuple(warp);
  warp[1] = 0.0;
  warpData->InsertNextTuple(warp);
  warp[1] = 0.1;
  warpData->InsertNextTuple(warp);

  vtkNew<vtkPolyData> polydata;
  polydata->SetPoints(points);
  polydata->SetLines(lines);
  polydata->GetPointData()->AddArray(warpData);
  polydata->GetPointData()->SetActiveVectors(warpData->GetName());

  // WarpVector will use the array marked as active vector in polydata
  // it has to be a 3 component array with the same number of
  // tuples as points in polydata.
  vtkNew<vtkWarpVector> warpVector;
  warpVector->SetInputData(polydata);
  warpVector->Update();

  vtkNew<vtkPolyDataMapper> mapper;
  mapper->SetInputData(warpVector->GetPolyDataOutput());

  vtkNew<vtkActor> actor;
  actor->SetMapper(mapper);

  vtkNew<vtkRenderer> renderer;
  renderer->AddActor(actor);
  renderer->SetBackground(colors->GetColor3d("cobalt_green").GetData());

  vtkNew<vtkRenderWindow> renderWindow;
  renderWindow->AddRenderer(renderer);
  renderWindow->SetWindowName("WarpVector");

  vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
  renderWindowInteractor->SetRenderWindow(renderWindow);
  renderWindow->Render();
  renderWindowInteractor->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(WarpVector)

find_package(VTK COMPONENTS 
  CommonColor
  CommonCore
  CommonDataModel
  FiltersGeneral
  InteractionStyle
  RenderingContextOpenGL2
  RenderingCore
  RenderingFreeType
  RenderingGL2PSOpenGL2
  RenderingOpenGL2
)

if (NOT VTK_FOUND)
  message(FATAL_ERROR "WarpVector: 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(WarpVector MACOSX_BUNDLE WarpVector.cxx )
  target_link_libraries(WarpVector PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
  TARGETS WarpVector
  MODULES ${VTK_LIBRARIES}
)

Download and Build WarpVector

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

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

./WarpVector

WINDOWS USERS

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