Skip to content

QuadricDecimation

vtk-examples/Cxx/Meshes/QuadricDecimation


Description

This example uses Quadric Clustering, based on the work of Garland and Heckbert who first presented the quadric error measure at Siggraph '97 "Surface Simplification Using Quadric Error Metrics". For details of the algorithm Michael Garland's Ph.D. thesis is also recommended. Hughues Hoppe's Vis '99 paper, "New Quadric Metric for Simplifying Meshes with Appearance Attributes" is also a good take on the subject especially as it pertains to the error metric applied to attributes.

Other languages

See (CSharp)

Question

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

Code

QuadricDecimation.cxx

#include <vtkCamera.h>
#include <vtkNamedColors.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkQuadricDecimation.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>
#include <vtkTriangleFilter.h>
#include <vtkXMLPolyDataReader.h>

int main(int argc, char* argv[])
{
  vtkSmartPointer<vtkPolyData> inputPolyData;
  if (argc > 1)
  {
    vtkNew<vtkXMLPolyDataReader> reader;
    reader->SetFileName(argv[1]);
    vtkNew<vtkTriangleFilter> triangles;
    triangles->SetInputConnection(reader->GetOutputPort());
    triangles->Update();
    inputPolyData = triangles->GetOutput();
  }
  else
  {
    vtkNew<vtkSphereSource> sphereSource;
    sphereSource->SetThetaResolution(30);
    sphereSource->SetPhiResolution(15);
    sphereSource->Update();
    inputPolyData = sphereSource->GetOutput();
  }

  vtkNew<vtkNamedColors> colors;
  std::cout << "Before decimation" << std::endl << "------------" << std::endl;
  std::cout << "There are " << inputPolyData->GetNumberOfPoints() << " points."
            << std::endl;
  std::cout << "There are " << inputPolyData->GetNumberOfPolys() << " polygons."
            << std::endl;

  vtkNew<vtkQuadricDecimation> decimate;
  decimate->SetInputData(inputPolyData);
  decimate->AttributeErrorMetricOn();
  decimate->SetTargetReduction(.9);
  decimate->VolumePreservationOn();

  decimate->Update();

  vtkNew<vtkPolyData> decimated;
  decimated->ShallowCopy(decimate->GetOutput());

  std::cout << "After decimation" << std::endl << "------------" << std::endl;
  std::cout << "There are " << decimated->GetNumberOfPoints() << " points."
            << std::endl;
  std::cout << "There are " << decimated->GetNumberOfPolys() << " polygons."
            << std::endl;
  std::cout << "Reduction: "
            << static_cast<double>((inputPolyData->GetNumberOfPolys() -
                                    decimated->GetNumberOfPolys())) /
          static_cast<double>(inputPolyData->GetNumberOfPolys())
            << std::endl;

  vtkNew<vtkPolyDataMapper> inputMapper;
  inputMapper->SetInputData(inputPolyData);

  vtkNew<vtkProperty> backFace;
  backFace->SetColor(colors->GetColor3d("Gold").GetData());

  vtkNew<vtkActor> inputActor;
  inputActor->SetMapper(inputMapper);
  inputActor->GetProperty()->SetInterpolationToFlat();
  inputActor->GetProperty()->SetColor(
      colors->GetColor3d("NavajoWhite").GetData());
  inputActor->SetBackfaceProperty(backFace);

  vtkNew<vtkPolyDataMapper> decimatedMapper;
  decimatedMapper->SetInputData(decimated);

  vtkNew<vtkActor> decimatedActor;
  decimatedActor->SetMapper(decimatedMapper);
  decimatedActor->GetProperty()->SetColor(
      colors->GetColor3d("NavajoWhite").GetData());
  decimatedActor->GetProperty()->SetInterpolationToFlat();
  decimatedActor->SetBackfaceProperty(backFace);

  // There will be one render window
  vtkNew<vtkRenderWindow> renderWindow;
  renderWindow->SetSize(600, 300);
  renderWindow->SetWindowName("QuadricDecimation");

  // And one interactor
  vtkNew<vtkRenderWindowInteractor> interactor;
  interactor->SetRenderWindow(renderWindow);

  // Define viewport ranges
  // (xmin, ymin, xmax, ymax)
  double leftViewport[4] = {0.0, 0.0, 0.5, 1.0};
  double rightViewport[4] = {0.5, 0.0, 1.0, 1.0};

  // Setup both renderers
  vtkNew<vtkRenderer> leftRenderer;
  renderWindow->AddRenderer(leftRenderer);
  leftRenderer->SetViewport(leftViewport);
  leftRenderer->SetBackground(colors->GetColor3d("Peru").GetData());

  vtkNew<vtkRenderer> rightRenderer;
  renderWindow->AddRenderer(rightRenderer);
  rightRenderer->SetViewport(rightViewport);
  rightRenderer->SetBackground(colors->GetColor3d("CornflowerBlue").GetData());

  // Add the sphere to the left and the cube to the right
  leftRenderer->AddActor(inputActor);
  rightRenderer->AddActor(decimatedActor);

  // Shared camera looking down the -y axis
  vtkNew<vtkCamera> camera;
  camera->SetPosition(0, -1, 0);
  camera->SetFocalPoint(0, 0, 0);
  camera->SetViewUp(0, 0, 1);
  camera->Elevation(30);
  camera->Azimuth(30);

  leftRenderer->SetActiveCamera(camera);
  rightRenderer->SetActiveCamera(camera);

  leftRenderer->ResetCamera();
  leftRenderer->ResetCameraClippingRange();

  renderWindow->Render();
  interactor->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

project(QuadricDecimation)

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

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

Download and Build QuadricDecimation

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

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

./QuadricDecimation

WINDOWS USERS

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