ITK/Examples/Meshes/QuadEdgeMeshNormalFilter

From KitwarePublic
< ITK‎ | Examples
Revision as of 17:44, 25 December 2012 by Lorensen (talk | contribs)
Jump to navigationJump to search

This example reads a mesh from a vtk file. It then compute normals for the mesh.

The sample sphere file is here.

Please Note: In ITKv4, itkQuadEdgeMeshNormalFilter was renamed to itkNormalQuadEdgeMeshFilter.

QuadEdgeMeshNormalFilter.cxx

<source lang="cpp">

  1. include "itkVector.h"
  2. include "itkQuadEdgeMesh.h"
  3. include "itkVTKPolyDataReader.h"
  1. include "itkQuadEdgeMeshExtendedTraits.h"
  2. if ITK_VERSION_MAJOR >= 4
  3. include "itkNormalQuadEdgeMeshFilter.h"
  4. else
  5. include "itkQuadEdgeMeshNormalFilter.h"
  6. endif
  7. include <stdlib.h>

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

 if( argc < 2 )
   {
   std::cout <<"This program requires at least 1 argument" <<std::endl;
   std::cout <<"1- Input filename" <<std::endl;
   std::cout <<"2- Weight type" <<std::endl;
   std::cout <<"   * 0:  GOURAUD" <<std::endl;
   std::cout <<"   * 1:  THURMER" <<std::endl;
   std::cout <<"   * 2:  AREA" <<std::endl;
   return EXIT_FAILURE;
   }
 const unsigned int    Dimension = 3;
 typedef double        CoordType;
 typedef itk::QuadEdgeMesh< CoordType, Dimension > InputMeshType;
 typedef itk::Vector< CoordType, Dimension > VectorType;
 typedef itk::QuadEdgeMeshExtendedTraits <
   VectorType,
   Dimension,
   2,
   CoordType,
   CoordType,
   VectorType,
   bool,
   bool > Traits;
 typedef itk::QuadEdgeMesh < VectorType, Dimension, Traits > OutputMeshType;
 typedef itk::VTKPolyDataReader< InputMeshType > ReaderType;
  1. if ITK_VERSION_MAJOR >= 4
 typedef itk::NormalQuadEdgeMeshFilter< InputMeshType, OutputMeshType > NormalFilterType;
  1. else
 typedef itk::QuadEdgeMeshNormalFilter< InputMeshType, OutputMeshType > NormalFilterType;
  1. endif
 NormalFilterType::WeightType weight_type;
 int param = atoi( argv[2] );
 if( ( param < 0 ) || ( param > 2 ) )
   {
   std::cout <<"Weight type must be either: " <<std::endl;
   std::cout <<"   * 0:  GOURAUD" <<std::endl;
   std::cout <<"   * 1:  THURMER" <<std::endl;
   std::cout <<"   * 2:  AREA" <<std::endl;
   return EXIT_FAILURE;
   }
 else
   {
   switch( param )
     {
     default:
     case 0:
       weight_type = NormalFilterType::GOURAUD;
       break;
     case 1:
       weight_type = NormalFilterType::THURMER;
       break;
     case 2:
       weight_type = NormalFilterType::AREA;
       break;
     }
   }
 ReaderType::Pointer reader = ReaderType::New( );
 reader->SetFileName( argv[1] );
 try
   {
   reader->Update( );
   }
 catch( itk::ExceptionObject & excp )
   {
   std::cerr << "Exception thrown while reading the input file " << std::endl;
   std::cerr << excp << std::endl;
   return EXIT_FAILURE;
   }
 InputMeshType::Pointer mesh = reader->GetOutput( );
 NormalFilterType::Pointer normals = NormalFilterType::New( );
 normals->SetInput( mesh );
 normals->SetWeight( weight_type );
 try
   {
   normals->Update( );
   }
 catch( itk::ExceptionObject & excp )
   {
   std::cerr << excp << std::endl;
   return EXIT_FAILURE;
   }
 OutputMeshType::Pointer output = normals->GetOutput( );
 std::cout << normals << std::endl;
 return EXIT_SUCCESS;

} </source>

CMakeLists.txt

<syntaxhighlight lang="cmake"> cmake_minimum_required(VERSION 3.9.5)

project(QuadEdgeMeshNormalFilter)

find_package(ITK REQUIRED) include(${ITK_USE_FILE}) if (ITKVtkGlue_LOADED)

 find_package(VTK REQUIRED)
 include(${VTK_USE_FILE})

endif()

add_executable(QuadEdgeMeshNormalFilter MACOSX_BUNDLE QuadEdgeMeshNormalFilter.cxx)

if( "${ITK_VERSION_MAJOR}" LESS 4 )

 target_link_libraries(QuadEdgeMeshNormalFilter ITKReview ${ITK_LIBRARIES})

else( "${ITK_VERSION_MAJOR}" LESS 4 )

 target_link_libraries(QuadEdgeMeshNormalFilter ${ITK_LIBRARIES})

endif( "${ITK_VERSION_MAJOR}" LESS 4 )

</syntaxhighlight>

Download and Build QuadEdgeMeshNormalFilter

Click here to download QuadEdgeMeshNormalFilter and its CMakeLists.txt file. Once the tarball QuadEdgeMeshNormalFilter.tar has been downloaded and extracted,

cd QuadEdgeMeshNormalFilter/build
  • If ITK is installed:
cmake ..
  • If ITK is not installed but compiled on your system, you will need to specify the path to your ITK build:
cmake -DITK_DIR:PATH=/home/me/itk_build ..

Build the project:

make

and run it:

./QuadEdgeMeshNormalFilter

WINDOWS USERS PLEASE NOTE: Be sure to add the ITK bin directory to your path. This will resolve the ITK dll's at run time.

Building All of the Examples

Many of the examples in the ITK Wiki Examples Collection require VTK. You can build all of the the examples by following these instructions. If you are a new VTK user, you may want to try the Superbuild which will build a proper ITK and VTK.

ItkVtkGlue

ITK >= 4

For examples that use QuickView (which depends on VTK), you must have built ITK with Module_ITKVtkGlue=ON.

ITK < 4

Some of the ITK Examples require VTK to display the images. If you download the entire ITK Wiki Examples Collection, the ItkVtkGlue directory will be included and configured. If you wish to just build a few examples, then you will need to download ItkVtkGlue and build it. When you run cmake it will ask you to specify the location of the ItkVtkGlue binary directory.