# Decimation

Repository source: Decimation

### Description¶

This example decimates a mesh using progressive decimation. The SetTargetReduction function specifies how many triangles should reduced by specifying the percentage (0,1) of triangles to be removed. For example, if the mesh contains 100 triangles and SetTargetReduction(.90) is called, after the decimation there will be approximately 10 triangles - a 90% reduction.

The implementation of vtkDecimatePro is similar to the algorithm originally described in "Decimation of Triangle Meshes", Proc Siggraph 92, with three major differences. First, this algorithm does not necessarily preserve the topology of the mesh. Second, it is guaranteed to give the a mesh reduction factor specified by the user (as long as certain constraints are not set - see Caveats). Third, it is set up generate progressive meshes, that is a stream of operations that can be easily transmitted and incrementally updated (see Hugues Hoppe's Siggraph '96 paper on progressive meshes).

Seealso

Other languages

See (Python)

Question

### Code¶

Decimation.cxx

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

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

float reduction = 0.9; // 90% reduction
if (argc > 2)
{
reduction = atof(argv[2]);
}
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<vtkDecimatePro> decimate;
decimate->SetInputData(inputPolyData);
decimate->SetTargetReduction(reduction);
decimate->PreserveTopologyOn();
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("Decimation");

// 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;
leftRenderer->SetViewport(leftViewport);
leftRenderer->SetBackground(colors->GetColor3d("Peru").GetData());

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

// Add the sphere to the left and the cube to the right

// Shared camera
// 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.12 FATAL_ERROR)

project(Decimation)

find_package(VTK COMPONENTS
CommonColor
CommonCore
CommonDataModel
FiltersCore
IOXML
InteractionStyle
RenderingContextOpenGL2
RenderingCore
RenderingFreeType
RenderingGL2PSOpenGL2
RenderingOpenGL2
)

if (NOT VTK_FOUND)
message(FATAL_ERROR "Decimation: Unable to find the VTK build folder.")
endif()

# 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.")
target_link_libraries(Decimation PRIVATE ${VTK_LIBRARIES} ) # vtk_module_autoinit is needed vtk_module_autoinit( TARGETS Decimation MODULES${VTK_LIBRARIES}
)


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

./Decimation
`

WINDOWS USERS

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