ITK/Examples/Meshes/Decimation

From KitwarePublic
< ITK‎ | Examples
Revision as of 16:07, 1 February 2016 by DVigneault (talk | contribs) (Update example.)
Jump to navigationJump to search

This example demonstrates decimation of an itk::QuadEdgeMesh. The input mesh is created by the itk::RegularSphereMeshSource filter and written out by the example.

Usage is as follows:

<source lang="text"> ./MeshDecimation inputMesh.obj outputMesh.obj [N] </source> where N is the number of target triangles in the output mesh. The default target is 100, if none is provided.

<source lang="cpp">

  1. include <iostream>
  2. include "itkRegularSphereMeshSource.h"
  3. include "itkQuadEdgeMesh.h"
  4. include "itkQuadEdgeMeshDecimationCriteria.h"
  5. include "itkSquaredEdgeLengthDecimationQuadEdgeMeshFilter.h"
  6. include "itkMeshFileWriter.h"

const unsigned int Dimension = 3;

typedef itk::QuadEdgeMesh< float, Dimension > MeshType; typedef itk::RegularSphereMeshSource< MeshType > MeshSourceType; typedef itk::NumberOfFacesCriterion<MeshType> CriterionType; typedef itk::SquaredEdgeLengthDecimationQuadEdgeMeshFilter<MeshType,

                                                          MeshType,
                                                          CriterionType> DecimationType;

typedef itk::MeshFileWriter< MeshType > MeshWriterType;

int main( int argc, char ** argv ) {

 if (argc < 3)
   {
   std::cerr << "Usage: " << argv[0] << " <inputMesh> <outputMesh> [<count>]" << std::endl;
   return EXIT_FAILURE;
   }
 const unsigned int TriangleCount = (argc >= 4) ? atoi(argv[3]) : 100;
 MeshSourceType::Pointer source = MeshSourceType::New();
 CriterionType::Pointer criterion = CriterionType::New();
 DecimationType::Pointer decimate = DecimationType::New();
 criterion->SetTopologicalChange(false);
 criterion->SetNumberOfElements(TriangleCount);
 decimate->SetInput(source->GetOutput());
 decimate->SetCriterion(criterion);
 decimate->Update();
 std::cout << "Number of cells in source mesh: "
           << source->GetOutput()->GetNumberOfCells() << std::endl;
 std::cout << "Number of cells in decimated mesh: "
           << decimate->GetOutput()->GetNumberOfCells() << std::endl;
 MeshWriterType::Pointer writer = MeshWriterType::New();
 writer->SetFileName( argv[1] );
 writer->SetInput( source->GetOutput() );
 writer->Update();
 writer->SetFileName( argv[2] );
 writer->SetInput( decimate->GetOutput() );
 writer->Update();
 return EXIT_SUCCESS;

} </source>