ITK  4.0.0
Insight Segmentation and Registration Toolkit
itkSimplexMeshToTriangleMeshFilter.h
Go to the documentation of this file.
00001 /*=========================================================================
00002  *
00003  *  Copyright Insight Software Consortium
00004  *
00005  *  Licensed under the Apache License, Version 2.0 (the "License");
00006  *  you may not use this file except in compliance with the License.
00007  *  You may obtain a copy of the License at
00008  *
00009  *         http://www.apache.org/licenses/LICENSE-2.0.txt
00010  *
00011  *  Unless required by applicable law or agreed to in writing, software
00012  *  distributed under the License is distributed on an "AS IS" BASIS,
00013  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  *  See the License for the specific language governing permissions and
00015  *  limitations under the License.
00016  *
00017  *=========================================================================*/
00018 #ifndef __itkSimplexMeshToTriangleMeshFilter_h
00019 #define __itkSimplexMeshToTriangleMeshFilter_h
00020 
00021 #include "itkPolygonCell.h"
00022 
00023 #include "itkSimplexMesh.h"
00024 #include "itkMeshToMeshFilter.h"
00025 #include "itkVectorContainer.h"
00026 #include "itkAutomaticTopologyMeshSource.h"
00027 
00028 namespace itk
00029 {
00042 template< class TInputMesh, class TOutputMesh >
00043 class ITK_EXPORT SimplexMeshToTriangleMeshFilter:public MeshToMeshFilter< TInputMesh, TOutputMesh >
00044 {
00045 public:
00047   typedef SimplexMeshToTriangleMeshFilter Self;
00048 
00050   typedef MeshToMeshFilter< TInputMesh, TOutputMesh > Superclass;
00051 
00053   typedef SmartPointer< Self >       Pointer;
00054   typedef SmartPointer< const Self > ConstPointer;
00055 
00057   itkNewMacro(Self);
00058 
00060   itkTypeMacro(SimplexMeshToTriangleMeshFilter, MeshToMeshFilter);
00061 
00062   typedef TInputMesh                                     InputMeshType;
00063   typedef typename InputMeshType::ConstPointer           InputMeshConstPointer;
00064   typedef typename InputMeshType::PointType              InputPointType;
00065   typedef typename InputMeshType::PixelType              InputPixelType;
00066   typedef typename InputMeshType::MeshTraits::CellTraits InputCellTraitsType;
00067   typedef typename InputMeshType::PointIdentifier        PointIdentifier;
00068   typedef typename InputMeshType::CellIdentifier         CellIdentifier;
00069 
00070   typedef typename InputMeshType::PointsContainer InputPointsContainer;
00071   typedef typename InputPointsContainer::Pointer  InputPointsContainerPointer;
00072   typedef typename InputPointsContainer::Iterator InputPointsContainerIterator;
00073 
00074   typedef typename InputMeshType::NeighborListType           InputNeighbors;
00075   typedef typename InputMeshType::NeighborListType::iterator InputNeighborsIterator;
00076 
00077   typedef          itk::AutomaticTopologyMeshSource< TOutputMesh > AutoMeshSourceType;
00078 
00079   typedef typename InputMeshType::CellType             SimplexCellType;
00080   typedef          itk::PolygonCell< SimplexCellType > SimplexPolygonType;
00081 
00082   // stores the center for each simplex mesh cell, key is the point id
00083   typedef          itk::MapContainer< PointIdentifier, InputPointType > PointMapType;
00084   typedef typename PointMapType::Pointer                                PointMapPointer;
00085 
00092   class SimplexCellVisitor
00093   {
00094 public:
00095 
00099     SimplexCellVisitor()
00100     {
00101       m_CenterMap = PointMapType::New();
00102     }
00103 
00107     virtual ~SimplexCellVisitor() {}
00108 
00112     void Visit(CellIdentifier cellId, SimplexPolygonType *poly)
00113     {
00114       typedef typename SimplexPolygonType::PointIdIterator PointIdIterator;
00115       PointIdIterator it =  poly->PointIdsBegin();
00116       InputPointType  center;
00117       center.Fill(0);
00119 
00120       InputPointType p;
00121       p.Fill(0);
00122 
00123       while ( it != poly->PointIdsEnd() )
00124         {
00125         this->m_Mesh->GetPoint(*it, &p);
00126         center += p.GetVectorFromOrigin();
00127         it++;
00128         }
00129 
00130       center[0] /= poly->GetNumberOfPoints();
00131       center[1] /= poly->GetNumberOfPoints();
00132       center[2] /= poly->GetNumberOfPoints();
00133 
00134       m_CenterMap->InsertElement(cellId, center);
00135     }
00136 
00137     PointMapPointer GetCenterMap()
00138     {
00139       return m_CenterMap;
00140     }
00141 
00142     void SetMesh(const InputMeshType *mesh)
00143     {
00144       this->m_Mesh = mesh;
00145     }
00146 
00147 protected:
00148     InputMeshConstPointer m_Mesh;
00149     PointMapPointer       m_CenterMap;
00150   };
00151 
00152   typedef itk::CellInterfaceVisitorImplementation< InputPixelType,
00153                                                    InputCellTraitsType,
00154                                                    SimplexPolygonType,
00155                                                    SimplexCellVisitor >
00156   SimplexVisitorInterfaceType;
00157 
00158   typedef typename SimplexVisitorInterfaceType::Pointer SimplexVisitorInterfacePointer;
00159   typedef typename SimplexCellType::MultiVisitor        CellMultiVisitorType;
00160   typedef typename CellMultiVisitorType::Pointer        CellMultiVisitorPointer;
00161 protected:
00162 
00163   SimplexMeshToTriangleMeshFilter();
00164   virtual ~SimplexMeshToTriangleMeshFilter();
00165   SimplexMeshToTriangleMeshFilter(const Self &) {}
00166   void operator=(const Self &) {}
00167 
00168   void PrintSelf(std::ostream & os, Indent indent) const;
00169 
00170   virtual void GenerateData();
00171 
00172   void Initialize();
00173 
00175   void CreateTriangles();
00176 
00178   CellIdentifier FindCellId(CellIdentifier id1, CellIdentifier id2, CellIdentifier id3);
00179 
00181   PointMapPointer m_Centers;
00182 };
00183 } //end of namespace
00184 
00185 #ifndef ITK_MANUAL_INSTANTIATION
00186 #include "itkSimplexMeshToTriangleMeshFilter.hxx"
00187 #endif
00188 
00189 #endif //__SimplexMeshToTriangleMeshFilter_h
00190