⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 meshcellvisitor2.cxx

📁 InsightToolkit-1.4.0(有大量的优化算法程序)
💻 CXX
📖 第 1 页 / 共 2 页
字号:
/*=========================================================================

  Program:   Insight Segmentation & Registration Toolkit
  Module:    $RCSfile: MeshCellVisitor2.cxx,v $
  Language:  C++
  Date:      $Date: 2003/09/10 14:29:51 $
  Version:   $Revision: 1.9 $

  Copyright (c) Insight Software Consortium. All rights reserved.
  See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even 
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/

//  Software Guide : BeginLatex
//
//  The following section illustrates a realistic example of the use of Cell
//  visitors on the \doxygen{Mesh}. A set of different visitors is defined
//  here, each visitor associated with a particular type of cell. All the
//  visitors are registered with a MultiVisitor class which is passed to the
//  mesh.
// 
//  The first step is to include the \code{CellInterfaceVisitor} header file.
//
//  \index{itk::Mesh!CellVisitor}
//  \index{itk::Mesh!CellInterfaceVisitor}
//  \index{CellVisitor}
//  \index{CellInterfaceVisitor}
//
//  Software Guide : EndLatex 


#include "itkMesh.h"
#include "itkVertexCell.h"
#include "itkLineCell.h"
#include "itkTriangleCell.h"
#include "itkTetrahedronCell.h"


// Software Guide : BeginCodeSnippet
#include "itkCellInterfaceVisitor.h"
// Software Guide : EndCodeSnippet


  //  Software Guide : BeginLatex
  //
  //  The typical mesh types are now declared 
  //
  //  Software Guide : EndLatex 

  // Software Guide : BeginCodeSnippet
  typedef float                             PixelType;
  typedef itk::Mesh< PixelType, 3 >         MeshType;

  typedef MeshType::CellType                CellType;

  typedef itk::VertexCell< CellType >       VertexType;
  typedef itk::LineCell< CellType >         LineType;
  typedef itk::TriangleCell< CellType >     TriangleType;
  typedef itk::TetrahedronCell< CellType >  TetrahedronType;
  // Software Guide : EndCodeSnippet


  //  Software Guide : BeginLatex
  //
  //  Then, custom CellVisitor classes should be declared. The only requirement
  //  on the declaration of each visitor class is to provide a method named
  //  \code{Visit()}. This method expects as arguments a cell identifier and a
  //  pointer to the \emph{specific} cell type for which this visitor is
  //  intended. 
  //
  //  \index{itk::Mesh!CellInterfaceVisitor}
  //  \index{CellInterfaceVisitor!requirements}
  //  \index{CellInterfaceVisitor!Visit()}
  //
  //  Software Guide : EndLatex 


  //  Software Guide : BeginLatex
  //
  //  The following Vertex visitor simply prints out the identifier of the
  //  point with which the cell is associated. Note that the cell uses the
  //  method \code{GetPointId()} without any arguments. This method is only
  //  defined on the VertexCell.
  //
  //  \index{itk::CellInterface!GetPointId()}
  //  \index{GetPointId()}
  //
  //  Software Guide : EndLatex 

  // Software Guide : BeginCodeSnippet
  class CustomVertexVisitor
    {
    public:
      void Visit(unsigned long cellId, VertexType * t )
        {
        std::cout << "cell " << cellId << " is a Vertex " << std::endl;
        std::cout << "    associated with point id = ";
        std::cout << t->GetPointId() << std::endl;
        }
    };
  // Software Guide : EndCodeSnippet


  //  Software Guide : BeginLatex
  //
  //  The following Line visitor computes the length of the line.  Note
  //  that this visitor is slightly more complicated since it needs to get
  //  access to the actual mesh in order to get point coordinates from the
  //  point identifiers returned by the line cell. This is done by holding a
  //  pointer to the mesh and querying the mesh each time point coordinates are
  //  required. The mesh pointer is set up in this case with the
  //  \code{SetMesh()} method.
  //
  //  Software Guide : EndLatex 

  // Software Guide : BeginCodeSnippet
  class CustomLineVisitor
    {
    public:
      CustomLineVisitor():m_Mesh( 0 ) {}

      void SetMesh( MeshType * mesh ) { m_Mesh = mesh; }

      void Visit(unsigned long cellId, LineType * t )
        {
        std::cout << "cell " << cellId << " is a Line " << std::endl;
        LineType::PointIdIterator pit = t->PointIdsBegin();
        MeshType::PointType p0;
        MeshType::PointType p1;
        m_Mesh->GetPoint( *pit++, &p0 );
        m_Mesh->GetPoint( *pit++, &p1 );
        const double length = p0.EuclideanDistanceTo( p1 );
        std::cout << " length = " << length << std::endl;
        }

    private:
      MeshType::Pointer m_Mesh;
    };
  // Software Guide : EndCodeSnippet


  //  Software Guide : BeginLatex
  //
  //  The Triangle visitor below prints out the identifiers of its points.
  //  Note the use of the \code{PointIdIterator} and the \code{PointIdsBegin()}
  //  and \code{PointIdsEnd()} methods.
  //
  //  \index{CellInterface!PointIdsBegin()}
  //  \index{CellInterface!PointIdsEnd()}
  //  \index{CellInterface!iterating points}
  //  \index{PointIdsBegin()}
  //  \index{PointIdsEnd()}
  //
  //  Software Guide : EndLatex 


  // Software Guide : BeginCodeSnippet
  class CustomTriangleVisitor
    {
    public:
      void Visit(unsigned long cellId, TriangleType * t )
        {
        std::cout << "cell " << cellId << " is a Triangle " << std::endl;
        LineType::PointIdIterator pit = t->PointIdsBegin();
        LineType::PointIdIterator end = t->PointIdsEnd();
        while( pit != end ) 
          {
          std::cout << "  point id = " << *pit << std::endl;
          ++pit;
          }
        }
    };
  // Software Guide : EndCodeSnippet


  //  Software Guide : BeginLatex
  //
  //  The TetrahedronVisitor below simply returns the number of faces on this
  //  figure. Note that \code{GetNumberOfFaces()} is a method exclusive of 3D
  //  cells.
  //
  //  \index{GetNumberOfFaces()!TetrahedronCell}
  //  \index{TetrahedronCell!GetNumberOfFaces()}
  //
  //  Software Guide : EndLatex 

  // Software Guide : BeginCodeSnippet
  class CustomTetrahedronVisitor
    {
    public:
      void Visit(unsigned long cellId, TetrahedronType * t )
        {
        std::cout << "cell " << cellId << " is a Tetrahedron " << std::endl;
        std::cout << "  number of faces = ";
        std::cout << t->GetNumberOfFaces() << std::endl;
        }
    };
  // Software Guide : EndCodeSnippet


int main()
{
  MeshType::Pointer  mesh = MeshType::New();

  // Creating the points and inserting them in the mesh
  //
  MeshType::PointType   point0;
  MeshType::PointType   point1;
  MeshType::PointType   point2;
  MeshType::PointType   point3;

  point0[0] = -1; point0[1] = -1; point0[2] = -1; 
  point1[0] =  1; point1[1] =  1; point1[2] = -1; 
  point2[0] =  1; point2[1] = -1; point2[2] =  1; 
  point3[0] = -1; point3[1] =  1; point3[2] =  1; 

  mesh->SetPoint( 0, point0 );
  mesh->SetPoint( 1, point1 );
  mesh->SetPoint( 2, point2 );
  mesh->SetPoint( 3, point3 );


  // Creating and associating the Tetrahedron
  //
  CellType::CellAutoPointer cellpointer;

  cellpointer.TakeOwnership( new TetrahedronType );
  cellpointer->SetPointId( 0, 0 );
  cellpointer->SetPointId( 1, 1 );
  cellpointer->SetPointId( 2, 2 );
  cellpointer->SetPointId( 3, 3 );
  mesh->SetCell( 0, cellpointer );

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -