Skip to content

DeformPointSet

vtk-examples/Cxx/Meshes/DeformPointSet


Description

Deform a point set with a polyhedra. Supply a .vtp file and see it deformed.

Other languages

See (Python)

Question

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

Code

DeformPointSet.cxx

#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkCellArray.h>
#include <vtkDeformPointSet.h>
#include <vtkElevationFilter.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPoints.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSphereSource.h>
#include <vtkXMLPolyDataReader.h>

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

  vtkNew<vtkPolyData> input;
  double bounds[6];

  if (argc == 1)
  {
    // Create a sphere to warp
    vtkNew<vtkSphereSource> sphere;
    sphere->SetThetaResolution(51);
    sphere->SetPhiResolution(17);
    sphere->Update();
    sphere->GetOutput()->GetBounds(bounds);

    // Generate some scalars on the polydata
    vtkNew<vtkElevationFilter> ele;
    ele->SetInputConnection(sphere->GetOutputPort());
    ele->SetLowPoint(0, 0, -0.5);
    ele->SetHighPoint(0, 0, 0.5);
    ele->SetLowPoint((bounds[1] + bounds[0]) / 2.0,
                     (bounds[3] + bounds[2]) / 2.0, -bounds[5]);
    ele->SetHighPoint((bounds[1] + bounds[0]) / 2.0,
                      (bounds[3] + bounds[2]) / 2.0, bounds[5]);
    ele->Update();
    input->ShallowCopy(ele->GetOutput());
  }
  else
  {
    std::string inputFilename = argv[1];

    vtkNew<vtkXMLPolyDataReader> reader;
    reader->SetFileName(inputFilename.c_str());
    reader->Update();

    input->ShallowCopy(reader->GetOutput());
    input->GetBounds(bounds);
  }

  // Now create a control mesh, in this case a octagon that encloses
  // the point set
  vtkNew<vtkPoints> pts;
  pts->SetNumberOfPoints(6);
  pts->SetPoint(0, bounds[0] - .1 * (bounds[1] - bounds[0]),
                (bounds[3] + bounds[2]) / 2.0, (bounds[5] + bounds[4]) / 2.0);
  pts->SetPoint(1, bounds[1] + .1 * (bounds[1] - bounds[0]),
                (bounds[3] + bounds[2]) / 2.0, (bounds[5] + bounds[4]) / 2.0);
  pts->SetPoint(2, (bounds[1] + bounds[0]) / 2.0,
                bounds[2] - .1 * (bounds[3] - bounds[2]),
                (bounds[5] + bounds[4]) / 2.0);
  pts->SetPoint(3, (bounds[1] + bounds[0]) / 2.0,
                bounds[3] + .1 * (bounds[3] - bounds[2]),
                (bounds[5] + bounds[4]) / 2.0);
  pts->SetPoint(4, (bounds[1] + bounds[0]) / 2.0, (bounds[3] + bounds[2]) / 2.0,
                bounds[4] - .1 * (bounds[5] - bounds[4]));
  pts->SetPoint(5, (bounds[1] + bounds[0]) / 2.0, (bounds[3] + bounds[2]) / 2.0,
                bounds[5] + .1 * (bounds[5] - bounds[4]));

  vtkNew<vtkCellArray> tris;
  // clang-format off
  tris->InsertNextCell(3);
  tris->InsertCellPoint(2); tris->InsertCellPoint(0);  tris->InsertCellPoint(4);
  tris->InsertNextCell(3);
  tris->InsertCellPoint(1); tris->InsertCellPoint(2);  tris->InsertCellPoint(4);
  tris->InsertNextCell(3);
  tris->InsertCellPoint(3);  tris->InsertCellPoint(1);  tris->InsertCellPoint(4);
  tris->InsertNextCell(3);
  tris->InsertCellPoint(0);  tris->InsertCellPoint(3);  tris->InsertCellPoint(4);
  tris->InsertNextCell(3);
  tris->InsertCellPoint(0);  tris->InsertCellPoint(2);  tris->InsertCellPoint(5);
  tris->InsertNextCell(3);
  tris->InsertCellPoint(2);  tris->InsertCellPoint(1);  tris->InsertCellPoint(5);
  tris->InsertNextCell(3);
  tris->InsertCellPoint(1); tris->InsertCellPoint(3);  tris->InsertCellPoint(5);
  tris->InsertNextCell(3);
  tris->InsertCellPoint(3);  tris->InsertCellPoint(0);  tris->InsertCellPoint(5);
  // clang-format on

  vtkNew<vtkPolyData> pd;
  pd->SetPoints(pts);
  pd->SetPolys(tris);

  // Display the control mesh
  vtkNew<vtkPolyDataMapper> meshMapper;
  meshMapper->SetInputData(pd);
  vtkNew<vtkActor> meshActor;
  meshActor->SetMapper(meshMapper);
  meshActor->GetProperty()->SetRepresentationToWireframe();
  meshActor->GetProperty()->SetColor(colors->GetColor3d("Black").GetData());

  // Do the intitial weight generation
  vtkNew<vtkDeformPointSet> deform;
  deform->SetInputData(input);
  deform->SetControlMeshData(pd);
  deform->Update(); // this creates the initial weights

  // Now move one point and deform
  double controlPoint[3];
  pts->GetPoint(5, controlPoint);
  pts->SetPoint(5, controlPoint[0], controlPoint[1],
                bounds[5] + .8 * (bounds[5] - bounds[4]));
  pts->Modified();

  // Display the warped polydata
  vtkNew<vtkPolyDataMapper> polyMapper;
  polyMapper->SetInputConnection(deform->GetOutputPort());
  vtkNew<vtkActor> polyActor;
  polyActor->SetMapper(polyMapper);

  vtkNew<vtkRenderer> renderer;
  vtkNew<vtkRenderWindow> renWin;
  renWin->AddRenderer(renderer);
  vtkNew<vtkRenderWindowInteractor> iren;
  iren->SetRenderWindow(renWin);

  renderer->AddActor(polyActor);
  renderer->AddActor(meshActor);

  renderer->GetActiveCamera()->SetPosition(1, 1, 1);
  renderer->ResetCamera();
  renderer->SetBackground(colors->GetColor3d("DarkSlateGray").GetData());

  renWin->SetSize(300, 300);
  renWin->SetWindowName("DeformPointSet");
  renWin->Render();

  iren->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

project(DeformPointSet)

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

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

Download and Build DeformPointSet

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

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

./DeformPointSet

WINDOWS USERS

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