ITK  4.1.0
Insight Segmentation and Registration Toolkit
itkAutomaticTopologyMeshSource.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 __itkAutomaticTopologyMeshSource_h
00019 #define __itkAutomaticTopologyMeshSource_h
00020 
00021 #include "itkArray.h"
00022 #include "itkDefaultStaticMeshTraits.h"
00023 #include "itksys/hash_map.hxx"
00024 #include "itkHexahedronCell.h"
00025 #include "itkIntTypes.h"
00026 #include "itkMesh.h"
00027 #include "itkMeshSource.h"
00028 #include "itkStructHashFunction.h"
00029 #include "itkTetrahedronCell.h"
00030 #include "itkVertexCell.h"
00031 
00032 namespace itk
00033 {
00110 template< class TOutputMesh >
00111 class ITK_EXPORT AutomaticTopologyMeshSource:public MeshSource< TOutputMesh >
00112 {
00113 public:
00115   typedef AutomaticTopologyMeshSource Self;
00116   typedef MeshSource< TOutputMesh >   Superclass;
00117   typedef SmartPointer< Self >        Pointer;
00118   typedef SmartPointer< const Self >  ConstPointer;
00119 
00121   typedef          TOutputMesh               MeshType;
00122   typedef typename MeshType::PointHashType   PointHashType;
00123   typedef typename MeshType::PointType       PointType;
00124   typedef typename MeshType::CellType        CellType;
00125   typedef typename MeshType::Pointer         MeshPointer;
00126   typedef typename PointType::CoordRepType   CoordinateType;
00127   typedef typename CellType::CellAutoPointer CellAutoPointer;
00128 
00130   typedef::itk::VertexCell< CellType >        VertexCell;
00131   typedef::itk::LineCell< CellType >          LineCell;
00132   typedef::itk::TriangleCell< CellType >      TriangleCell;
00133   typedef::itk::QuadrilateralCell< CellType > QuadrilateralCell;
00134   typedef::itk::TetrahedronCell< CellType >   TetrahedronCell;
00135   typedef::itk::HexahedronCell< CellType >    HexahedronCell;
00136 
00139   typedef ::itk::IdentifierType IdentifierType;
00140 
00142   typedef Array< IdentifierType > IdentifierArrayType;
00143 
00146   typedef itksys::hash_map<
00147     PointType,
00148     IdentifierType,
00149     StructHashFunction< PointHashType > >          PointHashMap;
00150 
00152   itkStaticConstMacro(PointDimension, unsigned int,
00153                       MeshType::PointDimension);
00154   itkStaticConstMacro(MaxTopologicalDimension, unsigned int,
00155                       MeshType::MaxTopologicalDimension);
00157 
00159   itkNewMacro(Self);
00160 
00162   itkTypeMacro(AutomaticTopologyMeshSource, MeshSource);
00163 
00165   IdentifierType AddPoint(const PointType & p0);
00166 
00167   IdentifierType AddPoint(const CoordinateType *p0);
00168 
00174   IdentifierType AddPoint(CoordinateType x0 = 0, CoordinateType x1 = 0,
00175                           CoordinateType x2 = 0, CoordinateType x3 = 0,
00176                           CoordinateType x4 = 0, CoordinateType x5 = 0);
00177 
00179   IdentifierType AddVertex(const IdentifierArrayType & pointIds);
00180 
00181   IdentifierType AddVertex(IdentifierType pointId0);
00182 
00183   IdentifierType AddVertex(const PointType & p0);
00184 
00185   IdentifierType AddVertex(const CoordinateType *p0);
00186 
00191   IdentifierType AddLine(const IdentifierArrayType & pointIds);
00192 
00193   IdentifierType AddLine(
00194     IdentifierType pointId0, IdentifierType pointId1);
00195 
00196   IdentifierType AddLine(const PointType & p0, const PointType & p1);
00197 
00198   IdentifierType AddLine(const CoordinateType *p0,
00199                          const CoordinateType *p1);
00200 
00211   IdentifierType AddTriangle(const IdentifierArrayType & pointIds);
00212 
00213   IdentifierType AddTriangle(
00214     IdentifierType pointId0, IdentifierType pointId1,
00215     IdentifierType pointId2);
00216 
00217   IdentifierType AddTriangle(const PointType & p0, const PointType & p1,
00218                              const PointType & p2);
00219 
00220   IdentifierType AddTriangle(const CoordinateType *p0,
00221                              const CoordinateType *p1,
00222                              const CoordinateType *p2);
00223 
00244   IdentifierType AddQuadrilateral(const IdentifierArrayType & pointIds);
00245 
00246   IdentifierType AddQuadrilateral(
00247     IdentifierType pointId0, IdentifierType pointId1,
00248     IdentifierType pointId2, IdentifierType pointId3);
00249 
00250   IdentifierType AddQuadrilateral(const PointType & p0, const PointType & p1,
00251                                   const PointType & p2, const PointType & p3);
00252 
00253   IdentifierType AddQuadrilateral(const CoordinateType *p0,
00254                                   const CoordinateType *p1,
00255                                   const CoordinateType *p2,
00256                                   const CoordinateType *p3);
00257 
00270   IdentifierType AddTetrahedron(const IdentifierArrayType & pointIds);
00271 
00272   IdentifierType AddTetrahedron(
00273     IdentifierType pointId0, IdentifierType pointId1,
00274     IdentifierType pointId2, IdentifierType pointId3);
00275 
00276   IdentifierType AddTetrahedron(const PointType & p0, const PointType & p1,
00277                                 const PointType & p2, const PointType & p3);
00278 
00279   IdentifierType AddTetrahedron(const CoordinateType *p0,
00280                                 const CoordinateType *p1,
00281                                 const CoordinateType *p2,
00282                                 const CoordinateType *p3);
00283 
00312   IdentifierType AddHexahedron(const IdentifierArrayType & pointIds);
00313 
00314   IdentifierType AddHexahedron(
00315     IdentifierType pointId0, IdentifierType pointId1,
00316     IdentifierType pointId2, IdentifierType pointId3,
00317     IdentifierType pointId4, IdentifierType pointId5,
00318     IdentifierType pointId6, IdentifierType pointId7);
00319 
00320   IdentifierType AddHexahedron(
00321     const PointType & p0, const PointType & p1, const PointType & p2,
00322     const PointType & p3, const PointType & p4, const PointType & p5,
00323     const PointType & p6, const PointType & p7
00324     );
00325 
00326   IdentifierType AddHexahedron(const CoordinateType *p0,
00327                                const CoordinateType *p1,
00328                                const CoordinateType *p2,
00329                                const CoordinateType *p3,
00330                                const CoordinateType *p4,
00331                                const CoordinateType *p5,
00332                                const CoordinateType *p6,
00333                                const CoordinateType *p7);
00334 
00335   class IdentifierArrayHashFunction
00336   {
00337 public:
00338     IdentifierType operator()(Array< IdentifierType > identifierArray) const
00339     {
00340       typedef IdentifierType IdType;
00341 
00342       IdType size = identifierArray.Size();
00343 
00344       std::sort( identifierArray.begin(), identifierArray.end() );
00345 
00346       IdType  hash = 0;
00347       IdType *id = &identifierArray[0];
00348 
00349       while ( size-- )
00350         {
00351         hash += *id++;
00352         hash = ( hash << 7 ) | ( hash >> 25 ); // Rotate left by 7.
00353         }
00354 
00355       return hash;
00356     }
00357   };
00358 
00359   class IdentifierArrayEqualsFunction
00360   {
00361 public:
00362     bool operator()(
00363       Array< IdentifierType > identifierArray1,
00364       Array< IdentifierType > identifierArray2
00365       ) const
00366     {
00367       typedef IdentifierType IdType;
00368 
00369       IdType size1 = identifierArray1.Size();
00370       IdType size2 = identifierArray2.Size();
00371 
00372       if ( size1 != size2 )
00373         {
00374         return false;
00375         }
00376 
00377       std::sort( identifierArray1.begin(), identifierArray1.end() );
00378       std::sort( identifierArray2.begin(), identifierArray2.end() );
00379 
00380       return ( identifierArray1 == identifierArray2 );
00381     }
00382   };
00383 protected:
00384   AutomaticTopologyMeshSource();
00385   ~AutomaticTopologyMeshSource();
00386 
00387   void GenerateData() {}  // GenerateData is a no-op, since the entries
00388                           // are controlled manually
00389 private:
00390   AutomaticTopologyMeshSource(const Self &);  //purposely not implemented
00391   void operator=(const Self &);               //purposely not implemented
00392 
00393   typedef itksys::hash_map<
00394     Array< IdentifierType >,
00395     IdentifierType,
00396     IdentifierArrayHashFunction,
00397     IdentifierArrayEqualsFunction >          CellHashMap;
00398 
00399   PointHashMap m_PointsHashTable;
00400   CellHashMap  m_CellsHashTable;
00401   MeshPointer  m_OutputMesh;   // Retained for convenience.
00402 };
00403 } // end namespace itk
00404 
00405 #ifndef ITK_MANUAL_INSTANTIATION
00406 #include "itkAutomaticTopologyMeshSource.hxx"
00407 #endif
00408 
00409 #endif // __itkAutomaticTopologyMeshSource_h
00410