Decimation
VTKExamples/Cxx/Meshes/Decimation
Description¶
This example decimates a mesh. 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.
Code¶
Decimation.cxx
#include <vtkVersion.h> #include <vtkPolyData.h> #include <vtkSphereSource.h> #include <vtkDecimatePro.h> #include <vtkSmartPointer.h> #include <vtkPolyDataMapper.h> #include <vtkProperty.h> #include <vtkRenderWindow.h> #include <vtkRenderWindowInteractor.h> #include <vtkRenderer.h> int main(int, char *[]) { vtkSmartPointer<vtkSphereSource> sphereSource = vtkSmartPointer<vtkSphereSource>::New(); sphereSource->Update(); vtkSmartPointer<vtkPolyData> input = vtkSmartPointer<vtkPolyData>::New(); input->ShallowCopy(sphereSource->GetOutput()); std::cout << "Before decimation" << std::endl << "------------" << std::endl; std::cout << "There are " << input->GetNumberOfPoints() << " points." << std::endl; std::cout << "There are " << input->GetNumberOfPolys() << " polygons." << std::endl; vtkSmartPointer<vtkDecimatePro> decimate = vtkSmartPointer<vtkDecimatePro>::New(); #if VTK_MAJOR_VERSION <= 5 decimate->SetInputConnection(input->GetProducerPort()); #else decimate->SetInputData(input); #endif //decimate->SetTargetReduction(.99); //99% reduction (if there was 100 triangles, now there will be 1) decimate->SetTargetReduction(.10); //10% reduction (if there was 100 triangles, now there will be 90) decimate->Update(); vtkSmartPointer<vtkPolyData> decimated = vtkSmartPointer<vtkPolyData>::New(); 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; vtkSmartPointer<vtkPolyDataMapper> inputMapper = vtkSmartPointer<vtkPolyDataMapper>::New(); #if VTK_MAJOR_VERSION <= 5 inputMapper->SetInputConnection(input->GetProducerPort()); #else inputMapper->SetInputData(input); #endif vtkSmartPointer<vtkActor> inputActor = vtkSmartPointer<vtkActor>::New(); inputActor->SetMapper(inputMapper); vtkSmartPointer<vtkPolyDataMapper> decimatedMapper = vtkSmartPointer<vtkPolyDataMapper>::New(); #if VTK_MAJOR_VERSION <= 5 decimatedMapper->SetInputConnection(decimated->GetProducerPort()); #else decimatedMapper->SetInputData(decimated); #endif vtkSmartPointer<vtkActor> decimatedActor = vtkSmartPointer<vtkActor>::New(); decimatedActor->SetMapper(decimatedMapper); // There will be one render window vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New(); renderWindow->SetSize(600, 300); // And one interactor vtkSmartPointer<vtkRenderWindowInteractor> interactor = vtkSmartPointer<vtkRenderWindowInteractor>::New(); 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 vtkSmartPointer<vtkRenderer> leftRenderer = vtkSmartPointer<vtkRenderer>::New(); renderWindow->AddRenderer(leftRenderer); leftRenderer->SetViewport(leftViewport); leftRenderer->SetBackground(.6, .5, .4); vtkSmartPointer<vtkRenderer> rightRenderer = vtkSmartPointer<vtkRenderer>::New(); renderWindow->AddRenderer(rightRenderer); rightRenderer->SetViewport(rightViewport); rightRenderer->SetBackground(.4, .5, .6); // Add the sphere to the left and the cube to the right leftRenderer->AddActor(inputActor); rightRenderer->AddActor(decimatedActor); leftRenderer->ResetCamera(); rightRenderer->ResetCamera(); renderWindow->Render(); interactor->Start(); return EXIT_SUCCESS; }
CMakeLists.txt¶
cmake_minimum_required(VERSION 2.8) PROJECT(Decimation) find_package(VTK REQUIRED) include(${VTK_USE_FILE}) add_executable(Decimation MACOSX_BUNDLE Decimation.cxx) target_link_libraries(Decimation ${VTK_LIBRARIES})
Download and Build Decimation¶
Danger
The generation of tar files has not been ported to the new VTKExamples. Some tarballs may be missing or out-of-date.
Click here to download Decimation and its CMakeLists.txt file. Once the tarball Decimation.tar has been downloaded and extracted,
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 PLEASE NOTE: Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.