ITK  5.0.0
Insight Segmentation and Registration Toolkit
SphinxExamples/src/Core/QuadEdgeMesh/CutMesh/Code.cxx
/*=========================================================================
*
* Copyright Insight Software Consortium
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
int main( int argc, char* argv[] )
{
if( argc != 3 )
{
std::cerr << "Usage: "<< std::endl;
std::cerr << argv[0];
std::cerr << " <InputFileName> <OutputFileName>";
std::cerr << std::endl;
return EXIT_FAILURE;
}
const char * inputFileName = argv[1];
const char * outputFileName = argv[2];
constexpr unsigned int Dimension = 3;
using CoordType = double;
using MeshPointer = MeshType::Pointer;
using MeshPointsContainerPointer = MeshType::PointsContainerPointer;
using MeshPointsContainerIterator = MeshType::PointsContainerIterator;
using MeshPointType = MeshType::PointType;
using MeshPointIdentifier = MeshType::PointIdentifier;
using MeshCellIdentifier = MeshType::CellIdentifier;
using ReaderType = itk::MeshFileReader< MeshType >;
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName( inputFileName );
MeshPointer mesh = reader->GetOutput();
MeshPointer output = MeshType::New();
std::map< MeshPointIdentifier, MeshPointIdentifier > verticesMap;
std::set< MeshCellIdentifier > facesSet;
MeshPointsContainerPointer points = mesh->GetPoints();
MeshPointsContainerIterator pIt = points->Begin();
MeshPointsContainerIterator pEnd = points->End();
while( pIt != pEnd )
{
MeshPointType p = pIt->Value();
if( ( p[2] < 0. ) )
{
// here you do not want to use operator = to copy the coordinates from the
// input mesh to the output one. Indeed the operator = does not only copy
// the coordinates, but also the pointer to first QuadEdge of the 0-ring,
// and it would be wrong.
MeshPointType q;
q.CastFrom( p );
verticesMap[ pIt->Index() ] = output->AddPoint( q );
// iterate on the 0-ring (vertex neighbors)
MeshType::QEType* qe = p.GetEdge();
MeshType::QEType* temp = qe;
do
{
// insert the corresponding faces into std::set
facesSet.insert( temp->GetLeft() );
temp = temp->GetOnext();
} while( qe != temp );
}
++pIt;
}
MeshType::CellsContainerPointer cells = mesh->GetCells();
// iterate on the faces to be added into resulting mesh
for( auto fIt : facesSet )
{
auto* face = dynamic_cast< PolygonType* >( cells->ElementAt( fIt ) );
MeshType::PointIdentifier id[3];
if( face )
{
MeshType::QEType *qe = face->GetEdgeRingEntry();
MeshType::QEType *temp = qe;
unsigned int k = 0;
// iterate on the vertices of a given face
do
{
// add the corresponding vertex into the output mesh if it has not been added yet
if( verticesMap.find( temp->GetOrigin() ) == verticesMap.end() )
{
MeshType::PointType p = mesh->GetPoint( temp->GetOrigin() );
q.CastFrom( p );
verticesMap[ temp->GetOrigin() ] = output->AddPoint( q );
}
id[ k++ ] = verticesMap[ temp->GetOrigin() ];
temp = temp->GetLnext();
} while( qe != temp );
// add the corresponding face into the output mesh
output->AddFaceTriangle( id[0], id[1], id[2] );
}
}
// save the corresponding mesh
using MeshWriterType = itk::MeshFileWriter< MeshType >;
MeshWriterType::Pointer writer = MeshWriterType::New();
writer->SetInput( output );
writer->SetFileName( outputFileName );
try
{
writer->Update();
}
{
std::cerr << "Error: " << e << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}