Skip to content

Morph3D

vtk-examples/Cxx/Visualization/Morph3D


Description

This example uses vtkInterpolateDataSetAttributes to interpolate datasets created using vtkImplicitModeller. Then vtkContourFilter extracts the resulting isosurface. An animation loop of 100 steps shows each interpolated isosurface with a 100 millisecond delay between each frame.

Question

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

Code

Morph3D.cxx

#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkContourFilter.h>
#include <vtkImplicitModeller.h>
#include <vtkInterpolateDataSetAttributes.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkVectorText.h>

#include <vtksys/SystemTools.hxx>

int main(int argc, char* argv[])
{
  double t = 2.0;
  if (argc > 1)
  {
    t = atof(argv[1]);
  }

  // make the letter v
  vtkNew<vtkVectorText> letterV;
  letterV->SetText("v");

  // read the geometry file containing the letter t
  vtkNew<vtkVectorText> letterT;
  letterT->SetText("t");

  // read the geometry file containing the letter k
  vtkNew<vtkVectorText> letterK;
  letterK->SetText("k");

  // create implicit models of each letter
  vtkNew<vtkImplicitModeller> blobbyV;
  blobbyV->SetInputConnection(letterV->GetOutputPort());
  blobbyV->SetMaximumDistance(.2);
  blobbyV->SetSampleDimensions(50, 50, 12);
  blobbyV->SetModelBounds(-0.5, 1.5, -0.5, 1.5, -0.5, 0.5);

  vtkNew<vtkImplicitModeller> blobbyT;
  blobbyT->SetInputConnection(letterT->GetOutputPort());
  blobbyT->SetMaximumDistance(.2);
  blobbyT->SetSampleDimensions(50, 50, 12);
  blobbyT->SetModelBounds(-0.5, 1.5, -0.5, 1.5, -0.5, 0.5);

  vtkNew<vtkImplicitModeller> blobbyK;
  blobbyK->SetInputConnection(letterK->GetOutputPort());
  blobbyK->SetMaximumDistance(.2);
  blobbyK->SetSampleDimensions(50, 50, 12);
  blobbyK->SetModelBounds(-0.5, 1.5, -0.5, 1.5, -0.5, 0.5);

  // Interpolate the data
  vtkNew<vtkInterpolateDataSetAttributes> interpolate;
  interpolate->AddInputConnection(blobbyV->GetOutputPort());
  interpolate->AddInputConnection(blobbyT->GetOutputPort());
  interpolate->AddInputConnection(blobbyK->GetOutputPort());
  interpolate->SetT(0.0);

  // extract an iso surface
  vtkNew<vtkContourFilter> blobbyIso;
  blobbyIso->SetInputConnection(interpolate->GetOutputPort());
  blobbyIso->SetValue(0, 0.1);

  // map to rendering primitives
  vtkNew<vtkPolyDataMapper> blobbyMapper;
  blobbyMapper->SetInputConnection(blobbyIso->GetOutputPort());
  blobbyMapper->ScalarVisibilityOff();

  vtkNew<vtkNamedColors> colors;

  // now an actor
  vtkNew<vtkActor> blobby;
  blobby->SetMapper(blobbyMapper);
  blobby->GetProperty()->SetDiffuseColor(
      colors->GetColor3d("Banana").GetData());
  blobby->GetProperty()->EdgeVisibilityOn();

  // Create the RenderWindow, Renderer and both Actors
  //
  vtkNew<vtkRenderer> renderer;
  vtkNew<vtkRenderWindow> renderWindow;
  renderWindow->AddRenderer(renderer);
  vtkNew<vtkRenderWindowInteractor> interactor;
  interactor->SetRenderWindow(renderWindow);

  vtkNew<vtkCamera> camera;
  camera->SetClippingRange(0.265, 13.2);
  camera->SetFocalPoint(0.539, 0.47464, 0);
  camera->SetPosition(0.539, 0.474674, 2.644);
  camera->SetViewUp(0, 1, 0);
  camera->Azimuth(30.);
  camera->Elevation(30.);
  renderer->SetActiveCamera(camera);

  //  now  make a renderer and tell it about lights and actors
  renderWindow->SetSize(640, 480);
  renderWindow->SetWindowName("Morph3D");

  renderer->AddActor(blobby);
  renderer->SetBackground(colors->GetColor3d("SlateGray").GetData());
  renderWindow->Render();
  for (unsigned i = 0; i < 100; ++i)
  {
    interpolate->SetT(i / 99.0 * 2.0);
    interpolate->Modified();
    renderWindow->Render();
    vtksys::SystemTools::Delay(100);
  }
  interpolate->SetT(t);
  renderWindow->Render();

  // interact
  interactor->Start();
  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

project(Morph3D)

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

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

Download and Build Morph3D

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

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

./Morph3D

WINDOWS USERS

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