Skip to content

DecimateHawaii

vtk-examples/Cxx/VisualizationAlgorithms/DecimateHawaii


Description

This examples shows reduction on the order of 90 percent for a 10:1 compression ratio. the wireframe image are shown to accentuate the density of the polygonal meshes. The left-hand image is the original data; the right-hand image is the decimated mesh.

Other languages

See (Python)

Question

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

Code

DecimateHawaii.cxx

#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkDecimatePro.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPNGReader.h>
#include <vtkPolyDataMapper.h>
#include <vtkPolyDataNormals.h>
#include <vtkPolyDataReader.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>

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

  // This example shows how to use decimation to reduce a polygonal mesh. We
  // also use mesh smoothing and generate surface normals to give a pleasing
  // result.
  //

  vtkNew<vtkPolyDataReader> hawaii;
  hawaii->SetFileName(argv[1]);

  // We want to preserve topology (not let any cracks form). This may limit
  // the total reduction possible, which we have specified at 90%.
  //
  vtkNew<vtkDecimatePro> deci;
  deci->SetInputConnection(hawaii->GetOutputPort());
  deci->SetTargetReduction(0.9);
  deci->PreserveTopologyOn();

  vtkNew<vtkPolyDataNormals> decimatedNormals;
  decimatedNormals->SetInputConnection(deci->GetOutputPort());
  decimatedNormals->FlipNormalsOn();
  decimatedNormals->SetFeatureAngle(60);

  vtkNew<vtkNamedColors> colors;

  vtkNew<vtkPolyDataMapper> decimatedMapper;
  decimatedMapper->SetInputConnection(decimatedNormals->GetOutputPort());

  vtkNew<vtkActor> decimatedActor;
  decimatedActor->SetMapper(decimatedMapper);
  decimatedActor->GetProperty()->SetColor(colors->GetColor3d("Sienna").GetData());
  decimatedActor->GetProperty()->SetRepresentationToWireframe();

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

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

  // Create the RenderWindow, Renderer and both Actors
  //
  vtkNew<vtkRenderer> renderer1;
  renderer1->SetViewport(0.0, 0.0, 0.5, 1.0);

  vtkNew<vtkRenderer> renderer2;
  renderer2->SetViewport(0.5, 0.0, 1.0, 1.0);

  vtkNew<vtkRenderWindow> renderWindow;
  renderWindow->AddRenderer(renderer1);
  renderWindow->AddRenderer(renderer2);
  renderWindow->SetWindowName("DecimateHawaii");

  vtkNew<vtkRenderWindowInteractor> interactor;
  interactor->SetRenderWindow(renderWindow);

  // Add the actors to the renderer, set the background and size
  //
  renderer1->AddActor(originalActor);
  renderer2->AddActor(decimatedActor);
  renderer1->SetBackground(colors->GetColor3d("Wheat").GetData());
  renderer2->SetBackground(colors->GetColor3d("Papaya_Whip").GetData());
  renderWindow->SetSize(800, 400);

  // render the image
  //
  vtkNew<vtkCamera> cam1;
  renderer1->SetActiveCamera(cam1);
  renderer2->SetActiveCamera(cam1);
  renderer1->ResetCamera();
  cam1->Elevation(-30);
  cam1->Dolly(1.2);
  renderer1->ResetCameraClippingRange();
  renderWindow->Render();
  interactor->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

project(DecimateHawaii)

find_package(VTK COMPONENTS 
  vtkCommonColor
  vtkCommonCore
  vtkFiltersCore
  vtkIOImage
  vtkIOLegacy
  vtkInteractionStyle
  vtkRenderingContextOpenGL2
  vtkRenderingCore
  vtkRenderingFreeType
  vtkRenderingGL2PSOpenGL2
  vtkRenderingOpenGL2
  QUIET
)

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

Download and Build DecimateHawaii

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

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

./DecimateHawaii

WINDOWS USERS

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