ITK/Examples/Meshes/Decimation: Difference between revisions

From KitwarePublic
< ITK‎ | Examples
Jump to navigationJump to search
(New page: 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 s...)
 
(Deprecated content that is moved to sphinx)
 
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
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
{{warning|1=The media wiki content on this page is no longer maintained. The examples presented on the https://itk.org/Wiki/*  pages likely require ITK version 4.13 or earlier releasesIn many cases, the examples on this page no longer conform to the best practices for modern ITK versions.}}
<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">
#include <iostream>
#include <string>
#include <sstream>
 
#include "/home/doriad/src/Insight/Code/Review/itkVTKPolyDataReader.h"
#include "/home/doriad/src/Insight/Code/Review/itkVTKPolyDataWriter.h"
#include "itkQuadEdgeMesh.h"
 
#include "itkQuadEdgeMeshDecimationCriteria.h"
#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>

Latest revision as of 16:40, 7 June 2019

Warning: The media wiki content on this page is no longer maintained. The examples presented on the https://itk.org/Wiki/* pages likely require ITK version 4.13 or earlier releases. In many cases, the examples on this page no longer conform to the best practices for modern ITK versions.