Skip to content

ResamplePolyLine

vtk-examples/Cxx/PolyData/ResamplePolyLine


Description

This example resamples a polyline with a vtkCardinalSpline. The resampled line will have 10 times the number of points contained in the original polyline. If no XML file is provided, a random polyline is generated.

Question

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

Code

ResamplePolyLine.cxx

#include <vtkActor.h>
#include <vtkCardinalSpline.h>
#include <vtkCellArray.h>
#include <vtkGlyph3D.h>
#include <vtkMath.h>
#include <vtkMinimalStandardRandomSequence.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPoints.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>
#include <vtkSplineFilter.h>
#include <vtkXMLPolyDataReader.h>

int main(int argc, char* argv[])
{
  vtkNew<vtkNamedColors> colors;

  auto polyData = vtkSmartPointer<vtkPolyData>::New();
  if (argc > 1)
  {
    vtkNew<vtkXMLPolyDataReader> reader;
    reader->SetFileName(argv[1]);
    reader->Update();
    polyData = reader->GetOutput();
  }
  else
  {
    unsigned int numberOfPoints = 10;
    vtkNew<vtkPoints> points;
    vtkNew<vtkMinimalStandardRandomSequence> randomSequence;
    randomSequence->SetSeed(8775070);
    for (unsigned int i = 0; i < numberOfPoints; ++i)
    {
      double x, y, z;
      // random position and radius
      x = randomSequence->GetRangeValue(-1.0, 1.0);
      randomSequence->Next();
      y = randomSequence->GetRangeValue(-1.0, 1.0);
      randomSequence->Next();
      z = randomSequence->GetRangeValue(-1.0, 1.0);
      randomSequence->Next();
      points->InsertNextPoint(x, y, z);
    }
    vtkNew<vtkCellArray> lines;
    lines->InsertNextCell(numberOfPoints);
    for (unsigned int i = 0; i < numberOfPoints; ++i)
    {
      lines->InsertCellPoint(i);
    }
    polyData->SetPoints(points);
    polyData->SetLines(lines);
  }

  vtkNew<vtkCardinalSpline> spline;
  spline->SetLeftConstraint(2);
  spline->SetLeftValue(0.0);
  spline->SetRightConstraint(2);
  spline->SetRightValue(0.0);

  vtkNew<vtkSplineFilter> splineFilter;
  splineFilter->SetInputData(polyData);
  splineFilter->SetNumberOfSubdivisions(polyData->GetNumberOfPoints() * 10);
  splineFilter->SetSpline(spline);

  vtkNew<vtkPolyDataMapper> splineMapper;
  splineMapper->SetInputConnection(splineFilter->GetOutputPort());

  vtkNew<vtkActor> splineActor;
  splineActor->SetMapper(splineMapper);

  vtkNew<vtkSphereSource> originalNodes;
  originalNodes->SetRadius(.04);
  originalNodes->SetPhiResolution(10);
  originalNodes->SetThetaResolution(10);

  vtkNew<vtkGlyph3D> glyphOriginal;
  glyphOriginal->SetInputData(polyData);
  glyphOriginal->SetSourceConnection(originalNodes->GetOutputPort());

  vtkNew<vtkSphereSource> newNodes;
  newNodes->SetRadius(.02);
  newNodes->SetPhiResolution(10);
  newNodes->SetThetaResolution(10);

  vtkNew<vtkGlyph3D> glyphNew;
  glyphNew->SetInputConnection(splineFilter->GetOutputPort());
  glyphNew->SetSourceConnection(newNodes->GetOutputPort());

  vtkNew<vtkPolyDataMapper> originalMapper;
  originalMapper->SetInputConnection(glyphOriginal->GetOutputPort());

  vtkNew<vtkActor> originalActor;
  originalActor->SetMapper(originalMapper);
  originalActor->GetProperty()->SetColor(
      colors->GetColor3d("Banana").GetData());
  originalActor->GetProperty()->SetOpacity(.6);

  vtkNew<vtkPolyDataMapper> newMapper;
  newMapper->SetInputConnection(glyphNew->GetOutputPort());

  vtkNew<vtkActor> newActor;
  newActor->SetMapper(newMapper);
  newActor->GetProperty()->SetColor(colors->GetColor3d("Tomato").GetData());

  // A renderer and render window
  vtkNew<vtkRenderer> renderer;
  renderer->SetBackground(colors->GetColor3d("SteelBlue").GetData());

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

  // An interactor
  vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
  renderWindowInteractor->SetRenderWindow(renderWindow);

  // Add the actors to the scene
  renderer->AddActor(originalActor);
  renderer->AddActor(newActor);
  renderer->AddActor(splineActor);

  renderWindow->Render();

  renderWindowInteractor->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

project(ResamplePolyLine)

find_package(VTK COMPONENTS 
  vtkCommonColor
  vtkCommonComputationalGeometry
  vtkCommonCore
  vtkCommonDataModel
  vtkFiltersCore
  vtkFiltersGeneral
  vtkFiltersSources
  vtkIOXML
  vtkInteractionStyle
  vtkRenderingContextOpenGL2
  vtkRenderingCore
  vtkRenderingFreeType
  vtkRenderingGL2PSOpenGL2
  vtkRenderingOpenGL2
  QUIET
)

if (NOT VTK_FOUND)
  message("Skipping ResamplePolyLine: ${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(ResamplePolyLine MACOSX_BUNDLE ResamplePolyLine.cxx )
  target_link_libraries(ResamplePolyLine PRIVATE ${VTK_LIBRARIES})
else ()
  # include all components
  add_executable(ResamplePolyLine MACOSX_BUNDLE ResamplePolyLine.cxx )
  target_link_libraries(ResamplePolyLine PRIVATE ${VTK_LIBRARIES})
  # vtk_module_autoinit is needed
  vtk_module_autoinit(
    TARGETS ResamplePolyLine
    MODULES ${VTK_LIBRARIES}
    )
endif ()

Download and Build ResamplePolyLine

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

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

./ResamplePolyLine

WINDOWS USERS

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