ITK/Examples/Meshes/Decimation

From KitwarePublic
< ITK‎ | Examples
Revision as of 01:15, 16 September 2009 by Daviddoria (talk | contribs)
Jump to navigationJump to search

This example reads a sphere from a vtk file. It then decimates the mesh, attempting to get to a target number of triangles, N. It should be run with <source lang="text"> ./MeshDecimation sphere.vtk sphere_decimated.vtk N </source> where N is the number of target triangles.

The sample sphere file is here.

<source lang="cpp">

  1. include <iostream>
  2. include <string>
  3. include <sstream>
  1. include "/home/doriad/src/Insight/Code/Review/itkVTKPolyDataReader.h"
  2. include "/home/doriad/src/Insight/Code/Review/itkVTKPolyDataWriter.h"
  3. include "itkQuadEdgeMesh.h"
  1. include "itkQuadEdgeMeshDecimationCriteria.h"
  2. include "itkQuadEdgeMeshSquaredEdgeLengthDecimation.h"

const unsigned int Dimension = 3; typedef itk::QuadEdgeMesh< float, Dimension > MeshType;

int main( int argc, char ** argv ) { if(argc != 4) { std:: cout << "Required arguments: InputFilename OutputFilename" << std::endl; exit(-1); }

std::string InputFilename = argv[1]; std::cout << "Input file: " << InputFilename << std::endl;

std::string OutputFilename = argv[2]; std::cout << "Output file: " << OutputFilename << std::endl;

std::string strTriangleCount = argv[3]; std::stringstream ssTriangleCount; ssTriangleCount << strTriangleCount; unsigned int TriangleCount; ssTriangleCount >> TriangleCount;

typedef itk::VTKPolyDataReader< MeshType > ReaderType;

ReaderType::Pointer polyDataReader = ReaderType::New();

typedef ReaderType::PointType PointType; typedef ReaderType::VectorType VectorType;

polyDataReader->SetFileName(InputFilename.c_str());

try { std::cerr << "Trying to read..." << std::endl; polyDataReader->Update(); } catch( itk::ExceptionObject & excp ) { std::cerr << "Error during Update() " << std::endl; std::cerr << excp << std::endl; return EXIT_FAILURE; }

std::cout << "polyDataReader:" << std::endl << polyDataReader << std::endl;

MeshType::Pointer mesh = polyDataReader->GetOutput();

typedef itk::NumberOfFacesCriterion<MeshType> CriterionType; CriterionType::Pointer criterion = CriterionType::New(); criterion->SetTopologicalChange(false); criterion->SetNumberOfElements(TriangleCount);

typedef itk::QuadEdgeMeshSquaredEdgeLengthDecimation<MeshType,MeshType,CriterionType> DecimationType;

DecimationType::Pointer decimate = DecimationType::New(); decimate->SetInput(mesh); decimate->SetCriterion(criterion); decimate->SetRelocate(true); decimate->Update();

MeshType::Pointer OutputMesh = decimate->GetOutput();

{ typedef itk::VTKPolyDataWriter< MeshType > WriterType;

WriterType::Pointer polyDataWriter = WriterType::New();

polyDataWriter->SetFileName(OutputFilename.c_str()); polyDataWriter->SetInput(OutputMesh); polyDataWriter->Update(); polyDataWriter->Write(); }

return 0; } </source>