ITK/Examples/Meshes/Decimation: Difference between revisions

From KitwarePublic
< ITK‎ | Examples
Jump to navigationJump to search
(Update example.)
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
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">
<source lang="text">
./MeshDecimation sphere.vtk sphere_decimated.vtk N
./MeshDecimation inputMesh.obj outputMesh.obj [N]
</source>
</source>
where N is the number of target triangles.
where N is the number of target triangles in the output mesh. The default target is 100, if none is provided.
 
The sample sphere file is [http://www.itk.org/Wiki/images/8/89/Sphere.vtk.zip here].


<source lang="cpp">
<source lang="cpp">
#include <iostream>
#include <iostream>
#include <string>
#include "itkRegularSphereMeshSource.h"
#include <sstream>
 
#include "/home/doriad/src/Insight/Code/Review/itkVTKPolyDataReader.h"
#include "/home/doriad/src/Insight/Code/Review/itkVTKPolyDataWriter.h"
#include "itkQuadEdgeMesh.h"
#include "itkQuadEdgeMesh.h"
#include "itkQuadEdgeMeshDecimationCriteria.h"
#include "itkQuadEdgeMeshDecimationCriteria.h"
#include "itkQuadEdgeMeshSquaredEdgeLengthDecimation.h"
#include "itkSquaredEdgeLengthDecimationQuadEdgeMeshFilter.h"
#include "itkMeshFileWriter.h"


const unsigned int Dimension = 3;
const unsigned int Dimension = 3;
typedef itk::QuadEdgeMesh< float, Dimension >   MeshType;
 
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 )
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();
  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;


typedef ReaderType::PointType  PointType;
  std::cout << "Number of cells in decimated mesh: "
typedef ReaderType::VectorType  VectorType;
            << decimate->GetOutput()->GetNumberOfCells() << std::endl;


polyDataReader->SetFileName(InputFilename.c_str());
  MeshWriterType::Pointer writer = MeshWriterType::New();
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();
  writer->SetFileName( argv[1] );
  writer->SetInput( source->GetOutput() );
  writer->Update();


typedef itk::NumberOfFacesCriterion<MeshType> CriterionType;
  writer->SetFileName( argv[2] );
CriterionType::Pointer criterion = CriterionType::New();
  writer->SetInput( decimate->GetOutput() );
criterion->SetTopologicalChange(false);
  writer->Update();
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();
  return EXIT_SUCCESS;


polyDataWriter->SetFileName(OutputFilename.c_str());
polyDataWriter->SetInput(OutputMesh);
polyDataWriter->Update();
polyDataWriter->Write();
}
return 0;
}
}
</source>
</source>

Revision as of 16:07, 1 February 2016

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>