h o m e d o c u m e n t a t i o n c l a s s h i e r a r c h y

ViewMapIterators.h

00001 //
00002 //  Filename         : ViewMapIterators.h
00003 //  Author(s)        : Stephane Grabli
00004 //  Purpose          : Iterators used to iterate over the various elements
00005 //                     of the ViewMap
00006 //  Date of creation : 01/07/2003
00007 //
00009 
00010 
00011 //
00012 //  Copyright (C) : Please refer to the COPYRIGHT file distributed 
00013 //   with this source distribution. 
00014 //
00015 //  This program is free software; you can redistribute it and/or
00016 //  modify it under the terms of the GNU General Public License
00017 //  as published by the Free Software Foundation; either version 2
00018 //  of the License, or (at your option) any later version.
00019 //
00020 //  This program is distributed in the hope that it will be useful,
00021 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00022 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023 //  GNU General Public License for more details.
00024 //
00025 //  You should have received a copy of the GNU General Public License
00026 //  along with this program; if not, write to the Free Software
00027 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00028 //
00030 
00031 #ifndef  VIEWMAPITERATORS_H
00032 # define VIEWMAPITERATORS_H
00033 
00034 #include "ViewMap.h"
00035 
00036                   /**********************************/
00037                   /*                                */
00038                   /*                                */
00039                   /*             ViewMap            */
00040                   /*                                */
00041                   /*                                */
00042                   /**********************************/
00043 
00044                   /**********************************/
00045                   /*                                */
00046                   /*                                */
00047                   /*             ViewVertex         */
00048                   /*                                */
00049                   /*                                */
00050                   /**********************************/
00051 
00052 namespace ViewVertexInternal{
00053 
00060   class orientedViewEdgeIterator 
00061   {
00062   public:
00063     friend class ViewVertex;
00064     friend class TVertex;
00065     friend class NonTVertex;
00066     friend class ViewEdge;
00067 
00068     // FIXME
00069     typedef ::TVertex::edge_pointers_container edge_pointers_container;
00070     typedef ::NonTVertex::edges_container edges_container;
00071   protected:
00072 
00073     Nature::VertexNature _Nature; // the nature of the underlying vertex
00074     // T vertex attributes
00075     edge_pointers_container::iterator _tbegin;
00076     edge_pointers_container::iterator _tend;
00077     edge_pointers_container::iterator _tvertex_iter;
00078 
00079     // Non TVertex attributes
00080     edges_container::iterator _begin;
00081     edges_container::iterator _end;
00082     edges_container::iterator _nontvertex_iter;
00083 
00084   public:
00086     inline orientedViewEdgeIterator() {}
00087     inline orientedViewEdgeIterator(Nature::VertexNature iNature)
00088     {_Nature = iNature;}
00090     orientedViewEdgeIterator(const orientedViewEdgeIterator& iBrother)
00091     {
00092       _Nature = iBrother._Nature;
00093       if(_Nature & Nature::T_VERTEX)
00094       {
00095         _tbegin = iBrother._tbegin;
00096         _tend = iBrother._tend;
00097         _tvertex_iter = iBrother._tvertex_iter;
00098       }
00099       else
00100       {
00101         _begin = iBrother._begin;
00102         _end = iBrother._end;
00103         _nontvertex_iter = iBrother._nontvertex_iter;
00104       }
00105     }
00106     virtual ~orientedViewEdgeIterator() {}
00107  
00108   public:
00109     inline orientedViewEdgeIterator(edge_pointers_container::iterator begin, 
00110       edge_pointers_container::iterator end,
00111       edge_pointers_container::iterator iter)
00112     {
00113       _Nature = Nature::T_VERTEX;
00114       _tbegin = begin;
00115       _tend = end;
00116       _tvertex_iter = iter;
00117     } 
00118     inline orientedViewEdgeIterator(edges_container::iterator begin, 
00119       edges_container::iterator end,
00120       edges_container::iterator iter)
00121     {
00122       _Nature = Nature::NON_T_VERTEX;
00123       _begin = begin;
00124       _end = end;
00125       _nontvertex_iter = iter;
00126     } 
00127 
00128   public:
00129 
00130 
00135     virtual bool isBegin() const 
00136     {
00137       if(_Nature & Nature::T_VERTEX)
00138         return (_tvertex_iter == _tbegin);
00139       else
00140         return (_nontvertex_iter == _begin);
00141     }
00146     virtual bool isEnd() const 
00147     {
00148       if(_Nature & Nature::T_VERTEX)
00149         return (_tvertex_iter == _tend);
00150       else
00151         return (_nontvertex_iter == _end);
00152     }
00153 
00154     // operators
00158     virtual orientedViewEdgeIterator& operator++()  // operator corresponding to ++i
00159     { 
00160       increment();
00161       return *this;
00162     }
00166     virtual orientedViewEdgeIterator operator++(int)  // opérateur correspondant à i++ 
00167     {                                  // c.a.d qui renvoie la valeur *puis* incrémente.
00168       orientedViewEdgeIterator tmp = *this;        // C'est pour cela qu'on stocke la valeur
00169       increment();                    // dans un temporaire. 
00170       return tmp;
00171     } 
00172   
00173     // comparibility
00175     virtual bool operator!=(const orientedViewEdgeIterator& b) const
00176     {
00177       if(_Nature & Nature::T_VERTEX)
00178         return (_tvertex_iter != b._tvertex_iter);
00179       else
00180         return (_nontvertex_iter != b._nontvertex_iter);
00181     }
00182 
00184     virtual bool operator==(const orientedViewEdgeIterator& b) const
00185     {return !(*this != b);}
00186     
00187     // dereferencing
00192     virtual ::ViewVertex::directedViewEdge& operator*() const
00193     {
00194       if(_Nature & Nature::T_VERTEX)
00195         //return _tvertex_iter;
00196         return **_tvertex_iter;
00197       else
00198         return (*_nontvertex_iter);
00199     }
00203     virtual ::ViewVertex::directedViewEdge* operator->() const { return &(operator*());}
00204 
00205   public:
00207     inline void increment()
00208     {
00209       if(_Nature & Nature::T_VERTEX)
00210       { 
00211         ::ViewVertex::directedViewEdge tmp = (**_tvertex_iter);
00212         ++_tvertex_iter;
00213         if(_tvertex_iter != _tend){
00214           // FIXME : pquoi deja ?
00215           ::ViewVertex::directedViewEdge tmp2 = (**_tvertex_iter);
00216           if(tmp2.first == tmp.first)
00217             ++_tvertex_iter;
00218         }
00219       }
00220       else
00221         ++_nontvertex_iter;
00222     }
00223   };
00224 
00225   }
00226                   /**********************************/
00227                   /*                                */
00228                   /*                                */
00229                   /*             ViewEdge           */
00230                   /*                                */
00231                   /*                                */
00232                   /**********************************/
00233 
00234 namespace ViewEdgeInternal {
00235 //
00236 // SVertexIterator
00237 //
00239 
00240   class SVertexIterator : public Interface0DIteratorNested
00241   {
00242   public:
00243 
00244     SVertexIterator() {
00245       _vertex = NULL;
00246       _begin = NULL;
00247       _previous_edge = NULL;
00248       _next_edge = NULL;
00249       _t = 0;
00250     }
00251 
00252     SVertexIterator(const SVertexIterator& vi) {
00253       _vertex = vi._vertex;
00254       _begin = vi._begin;
00255       _previous_edge = vi._previous_edge;
00256       _next_edge = vi._next_edge;
00257       _t = vi._t;
00258     }
00259 
00260     SVertexIterator(SVertex* v, SVertex* begin, FEdge* prev, FEdge* next, float t) {
00261       _vertex = v;
00262       _begin = begin;
00263       _previous_edge = prev;
00264       _next_edge = next;
00265       _t = t;
00266     }
00267 
00268     SVertexIterator& operator=(const SVertexIterator& vi) {
00269       _vertex = vi._vertex;
00270       _begin = vi._begin;
00271       _previous_edge = vi._previous_edge;
00272       _next_edge = vi._next_edge;
00273       _t = vi._t;
00274       return *this;
00275     }
00276 
00277     virtual ~SVertexIterator() {}
00278 
00279     virtual string getExactTypeName() const {
00280       return "SVertexIterator";
00281     }
00282 
00283     virtual SVertex& operator*() {
00284       return *_vertex;
00285     }
00286 
00287     virtual SVertex* operator->() {
00288       return &(operator*());
00289     }
00290 
00291     virtual SVertexIterator& operator++() {
00292       increment();
00293       return *this;
00294     }
00295 
00296     virtual SVertexIterator operator++(int) {
00297       SVertexIterator ret(*this);
00298       increment();
00299       return ret;
00300     }
00301 
00302     virtual SVertexIterator& operator--() {
00303       decrement();
00304       return *this;
00305     }
00306 
00307     virtual SVertexIterator operator--(int) {
00308       SVertexIterator ret(*this);
00309       decrement();
00310       return ret;
00311     }
00312 
00313     virtual void increment(){
00314       if (!_next_edge) {
00315               _vertex = 0;
00316             return;
00317       }
00318       _t += (float)_next_edge->getLength2D();
00319       _vertex = _next_edge->vertexB();
00320       _previous_edge = _next_edge;
00321       _next_edge = _next_edge->nextEdge();
00322       
00323     }
00324     virtual void decrement(){
00325       if (!_previous_edge) {
00326               _vertex = 0;
00327             return;
00328       }
00329       if((!_next_edge) && (!_vertex)){
00330         _vertex = _previous_edge->vertexB();
00331         return;
00332       }
00333       _t -= (float)_previous_edge->getLength2D();
00334       _vertex = _previous_edge->vertexA();
00335       _next_edge = _previous_edge;
00336       _previous_edge = _previous_edge->previousEdge();
00337   } 
00338 
00339     bool isBegin() const {
00340       return _vertex == _begin;
00341     }
00342 
00343     bool isEnd() const {
00344       return (!_vertex) || (_vertex == _begin && _previous_edge);
00345     }
00346 
00347     virtual float t() const {
00348       return _t;
00349     }
00350     virtual float u() const {
00351       return _t/(float)_next_edge->viewedge()->getLength2D();
00352     }
00353 
00354     virtual bool operator==(const Interface0DIteratorNested& it) const {
00355       const SVertexIterator* it_exact = dynamic_cast<const SVertexIterator*>(&it);
00356       if (!it_exact)
00357         return false;
00358       return (_vertex == it_exact->_vertex);
00359     }
00360 
00361     virtual SVertexIterator* copy() const {
00362       return new SVertexIterator(*this);
00363     }
00364 
00365   private:
00366 
00367     SVertex*    _vertex;
00368     SVertex*    _begin;
00369     FEdge*      _previous_edge;
00370     FEdge*      _next_edge;
00371     float _t; // curvilinear abscissa
00372   };
00373 
00374 
00375 
00376 //
00377 // ViewEdgeIterator (base class)
00378 //
00380 
00388 class ViewEdgeIterator
00389 {
00390 public:
00391 
00401   ViewEdgeIterator(ViewEdge* begin = 0, bool orientation = true) {
00402     _orientation = orientation;
00403     _edge = begin;
00404     _begin = begin;
00405   }
00406 
00408   ViewEdgeIterator(const ViewEdgeIterator& it) {
00409     _orientation = it._orientation;
00410     _edge = it._edge;
00411     _begin = it._begin;
00412   }
00413 
00414   virtual ~ViewEdgeIterator() {}
00415 
00417   virtual string getExactTypeName() const {
00418     return "ViewEdgeIterator";
00419   }
00420 
00422   ViewEdge* getCurrentEdge() {
00423     return _edge;
00424   }
00425 
00427   void setCurrentEdge(ViewEdge* edge) {
00428     _edge = edge;
00429   }
00430 
00432   ViewEdge* getBegin() {
00433     return _begin;
00434   }
00435 
00437   void setBegin(ViewEdge* begin) {
00438     _begin = begin;
00439   }
00440 
00442   bool getOrientation() const {
00443     return _orientation;
00444   }
00445 
00447   void setOrientation(bool orientation) {
00448     _orientation = orientation;
00449   }
00450 
00452   void changeOrientation() {
00453     _orientation = !_orientation;
00454   }
00455 
00457   virtual ViewEdge* operator*() {
00458     return _edge;
00459   }
00460 
00461   virtual ViewEdge* operator->() {
00462     return operator*();
00463   }
00464 
00468   virtual ViewEdgeIterator& operator++() {
00469     increment();
00470     return *this;
00471   }
00472 
00476   virtual ViewEdgeIterator operator++(int) {
00477     ViewEdgeIterator tmp(*this);
00478     increment();
00479     return tmp;
00480   }
00481 
00483   virtual void increment() {
00484     cerr << "Warning: method increment() not implemented" << endl;
00485   }
00486 
00490   virtual ViewEdgeIterator& operator--() {
00491     decrement();
00492     return *this;
00493   }
00494 
00498   virtual ViewEdgeIterator operator--(int) {
00499     ViewEdgeIterator tmp(*this);
00500     decrement();
00501     return tmp;
00502   }
00503 
00505   virtual void decrement(){
00506     cerr << "Warning: method decrement() not implemented" << endl;
00507   }
00508 
00512   virtual bool isBegin() const {
00513     return _edge == _begin;
00514   }
00515 
00518   virtual bool isEnd() const {
00519     return !_edge;
00520   }
00521 
00523   virtual bool operator==(ViewEdgeIterator& it) const {
00524     return _edge == it._edge;
00525   }
00526 
00528   virtual bool operator!=(ViewEdgeIterator& it) const {
00529     return !(*this == it);
00530   }
00531 
00532 protected:
00533 
00534   bool          _orientation;
00535   ViewEdge*     _edge;
00536   ViewEdge*     _begin;
00537 };
00538 
00539 } // end of namespace ViewEdgeInternal
00540 
00541 #endif // VIEWMAPITERATORS_H
00542