vtf-logo

FaceIterator.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00008 #if !defined(__geom_mesh_simplicial_FaceIterator_h__)
00009 #define __geom_mesh_simplicial_FaceIterator_h__
00010 
00011 #if defined(DEBUG_geom) && !defined(DEBUG_FaceIterator)
00012 #define DEBUG_FaceIterator
00013 #endif
00014 
00015 
00016 BEGIN_NAMESPACE_GEOM
00017 
00019 template<int M, typename _Face, typename _CellHandle>
00020 class 
00021 FaceIterator  :
00022   public std::iterator<std::bidirectional_iterator_tag, // Iterator tag.
00023                        _Face, // Value type.
00024                        std::ptrdiff_t, // Pointer difference type.
00025                        const _Face*, // Pointer type.
00026                        const _Face&> { // Reference type. 
00027   //
00028   // Private types.
00029   //
00030 
00031 private:
00032 
00034   typedef std::iterator<std::bidirectional_iterator_tag,
00035                         _Face,
00036                         std::ptrdiff_t,
00037                         const _Face*,
00038                         const _Face&> Base;
00039   
00040   //
00041   // Public types.
00042   //
00043 
00044 public:
00045 
00047   typedef typename Base::iterator_category iterator_category;
00049   typedef typename Base::value_type value_type;
00051   typedef typename Base::difference_type difference_type;
00053   typedef typename Base::pointer pointer;
00055   typedef typename Base::reference reference;
00056 
00058   typedef _Face Face;
00060   typedef _CellHandle CellHandle;
00061 
00062   //
00063   // Member data.
00064   //
00065 
00066 private:
00067   
00069   Face _face;
00071   CellHandle _cellsEnd;
00072 
00073   //
00074   // Not implemented.
00075   //
00076 
00077 private:
00078 
00080   FaceIterator();
00081 
00082 public:
00083 
00084   //--------------------------------------------------------------------------
00086 
00087 
00089   FaceIterator(const CellHandle c, const CellHandle cellsEnd) :
00090     Base(),
00091     _face(c, 0),
00092     _cellsEnd(cellsEnd) {
00093     if (c != cellsEnd && ! isValid()) {
00094       increment();
00095     }
00096   }
00097 
00099 
00100 public:
00101 
00102   //--------------------------------------------------------------------------
00104 
00105 
00107   FaceIterator(const FaceIterator& other) :
00108     Base(),
00109     _face(other._face),
00110     _cellsEnd(other._cellsEnd)
00111   {}    
00112 
00114   FaceIterator&
00115   operator=(const FaceIterator& other) {
00116     if (&other != this) {
00117       _face = other._face;
00118       _cellsEnd = other._cellsEnd;
00119     }
00120     return *this;
00121   }
00122 
00124   ~FaceIterator()
00125   {}
00126 
00128 
00131   const Face&
00132   getFace() const {
00133     return _face;
00134   }
00135 
00137 
00140   const CellHandle&
00141   getCellsEnd() const {
00142     return _cellsEnd;
00143   }
00144 
00146   template<typename AnyFace, typename AnyCellHandle>
00147   FaceIterator(const FaceIterator<M, AnyFace, AnyCellHandle>& other) :
00148     Base(),
00149     _face(other.getFace()),
00150     _cellsEnd(other.getCellsEnd())
00151   {}    
00152 
00154   //--------------------------------------------------------------------------
00156 
00157 
00159   reference
00160   operator*() const {
00161     // Return a constant reference to the face.
00162     return _face;
00163   }
00164 
00166   pointer
00167   operator->() const {
00168     // Return a constant pointer to the face.
00169     return &_face;
00170   }
00171   
00173   FaceIterator&
00174   operator++() { 
00175     increment();
00176     return *this; 
00177   }
00178       
00180 
00184   FaceIterator
00185   operator++(int) {
00186     FaceIterator x(*this); 
00187     ++*this;
00188     return x;
00189   }
00190       
00192   //--------------------------------------------------------------------------
00194 
00195 
00197   FaceIterator&
00198   operator--() { 
00199     decrement();
00200     return *this; 
00201   }
00202       
00204 
00208   FaceIterator
00209   operator--(int) {
00210     FaceIterator x(*this); 
00211     --*this;
00212     return x;
00213   }
00214 
00216   //--------------------------------------------------------------------------
00218 
00219       
00220   //
00221   // Forward iterator requirements
00222   //
00223 
00225   bool
00226   operator==(const FaceIterator& x) {
00227 #ifdef DEBUG_FaceIterator
00228     // These must be iterators over the same mesh.
00229     assert(_cellsEnd == x._cellsEnd);
00230 #endif
00231     return _face == x._face;
00232   }
00233 
00235   bool
00236   operator!=(const FaceIterator& x) {
00237     return ! operator==(x);
00238   }
00239 
00241 
00242 
00243 private:
00244 
00245   // If there is no adjacent cell through the face or
00246   // if the identifier of this cell is less than the identifier of the 
00247   // adjacent cell through the face.
00248   bool
00249   isValid() {
00250     return (_face.first->getNeighbor(_face.second) == 0 || 
00251              _face.first->getIdentifier() < 
00252              _face.first->getNeighbor(_face.second)->getIdentifier());
00253   }
00254 
00255   // Increment the iterator.
00256   void
00257   increment() {
00258     // While we have not gone through all of the cells.
00259     while (_face.first != _cellsEnd) {
00260 
00261       // Advance to the next face.
00262 
00263       // If we have not gone through all the faces of this cell.
00264       if (_face.second != M) {
00265         // Advance to the next face within the cell.
00266         ++_face.second;
00267       }
00268       else {
00269         // Advance to the next cell.
00270         ++_face.first;
00271         _face.second = 0;
00272       }
00273 
00274       // First check that we have not gone through all of the cells.
00275       // If there is no adjacent cell through the face or
00276       // if the identifier of this cell is less than the identifier of the 
00277       // adjacent cell through the face.
00278       if (_face.first != _cellsEnd && isValid()) {
00279         // Then we have a face.  Break out of the loop and return.
00280         break;
00281       }
00282     }
00283   }
00284 
00285   // Decrement the iterator.
00286   void
00287   decrement() {
00288     // While we have not gone through all of the cells.
00289     while (_face.first != _cellsEnd) {
00290 
00291       // Move to the previous face.
00292 
00293       // If we have not gone through all the faces of this cell.
00294       if (_face.second != 0) {
00295         // Go to the previous face within the cell.
00296         --_face.second;
00297       }
00298       else {
00299         // Go to the previous cell.
00300         --_face.first;
00301         _face.second = M;
00302       }
00303 
00304       // First check that we have not gone through all of the cells.
00305       // If there is no adjacent cell through the face or
00306       // if the identifier of this cell is less than the identifier of the 
00307       // adjacent cell through the face.
00308       if (_face.first != _cellsEnd && isValid()) {
00309         // Then we have a face.  Break out of the loop and return.
00310         break;
00311       }
00312     }
00313   }
00314 
00315 };
00316 
00317 END_NAMESPACE_GEOM
00318 
00319 #endif

Generated on Fri Aug 24 12:55:53 2007 for Computational Geometry Package by  doxygen 1.4.7