Merged changes in the trunk up to revision 54171.
[blender.git] / source / blender / freestyle / intern / view_map / ViewMapAdvancedIterators.h
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2010 Blender Foundation.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 #ifndef __FREESTYLE_VIEW_MAP_ADVANCED_ITERATORS_H__
29 #define __FREESTYLE_VIEW_MAP_ADVANCED_ITERATORS_H__
30
31 /** \file blender/freestyle/intern/view_map/ViewMapAdvancedIterators.h
32  *  \ingroup freestyle
33  *  \brief Iterators used to iterate over the various elements of the ViewMap.
34  *         These iterators can't be exported to python.
35  *  \author Stephane Grabli
36  *  \date 01/07/2003
37  */
38
39 #include "ViewMap.h"
40
41 #include "../system/Iterator.h" //soc
42
43 /**********************************/
44 /*                                */
45 /*                                */
46 /*             ViewMap            */
47 /*                                */
48 /*                                */
49 /**********************************/
50
51 /**********************************/
52 /*                                */
53 /*                                */
54 /*             ViewVertex         */
55 /*                                */
56 /*                                */
57 /**********************************/
58
59 namespace ViewVertexInternal {
60
61 class edge_const_traits : public Const_traits< ::ViewVertex::directedViewEdge>
62 {
63 public:
64         typedef vector< ::ViewVertex::directedViewEdge> edges_container;
65         typedef edges_container::const_iterator edges_container_iterator;
66         typedef vector< ::ViewVertex::directedViewEdge*> edge_pointers_container;
67         typedef edge_pointers_container::const_iterator edge_pointers_container_iterator;
68 };
69
70 class edge_nonconst_traits : public Nonconst_traits< ::ViewVertex::directedViewEdge>
71 {
72 public:
73         typedef vector< ::ViewVertex::directedViewEdge> edges_container;
74         typedef edges_container::iterator edges_container_iterator;
75         typedef vector< ::ViewVertex::directedViewEdge*> edge_pointers_container;
76         typedef edge_pointers_container::iterator edge_pointers_container_iterator;
77 };
78
79 template<class Traits>
80 class edge_iterator_base : public IteratorBase<Traits,InputIteratorTag_Traits>
81 {
82 public:
83         typedef typename Traits::value_type value_type;
84         typedef typename Traits::difference_type difference_type;
85         typedef typename Traits::pointer pointer;
86         typedef typename Traits::reference reference;
87         typedef edge_iterator_base<Traits> Self;
88         typedef typename Traits::edges_container_iterator edges_container_iterator;
89         typedef typename Traits::edge_pointers_container_iterator edge_pointers_container_iterator;
90         typedef edge_iterator_base<edge_nonconst_traits> iterator;
91         typedef edge_iterator_base<edge_const_traits> const_iterator;
92
93 public:
94         friend class ViewVertex;
95         friend class TVertex;
96         friend class NonTVertex;
97         friend class ViewEdge;
98         friend class edge_iterator;
99
100 protected:
101         Nature::VertexNature _Nature; // the nature of the underlying vertex
102         // T vertex attributes
103         edge_pointers_container_iterator _tbegin;
104         edge_pointers_container_iterator _tend;
105         edge_pointers_container_iterator _tvertex_iter;
106
107 #if 0
108         mutable value_type _tvertex_iter;
109         value_type _feA;
110         value_type _feB;
111         value_type _beA;
112         value_type _beB;
113 #endif
114
115         // Non TVertex attributes
116         edges_container_iterator _begin;
117         edges_container_iterator _end;
118         edges_container_iterator _nontvertex_iter;
119
120         typedef IteratorBase<Traits,InputIteratorTag_Traits> parent_class;
121
122 public:
123         inline edge_iterator_base() : parent_class() {}
124
125         inline edge_iterator_base(Nature::VertexNature iNature) : parent_class()
126         {
127                 _Nature = iNature;
128         }
129
130         edge_iterator_base(const edge_iterator_base<edge_nonconst_traits>& iBrother) : parent_class(iBrother)
131         {
132                 _Nature = iBrother._Nature;
133                 if (_Nature & Nature::T_VERTEX) {
134 #if 0
135                         _feA = iBrother._feA;
136                         _feB = iBrother._feB;
137                         _beA = iBrother._beA;
138                         _beB = iBrother._beB;
139                         _tvertex_iter = iBrother._tvertex_iter;
140 #endif
141                         _tbegin = iBrother._tbegin;
142                         _tend = iBrother._tend;
143                         _tvertex_iter = iBrother._tvertex_iter;
144                 }
145                 else {
146                         _begin = iBrother._begin;
147                         _end = iBrother._end;
148                         _nontvertex_iter = iBrother._nontvertex_iter;
149                 }
150         }
151
152         edge_iterator_base(const edge_iterator_base<edge_const_traits>& iBrother) : parent_class(iBrother)
153         {
154                 _Nature = iBrother._Nature;
155                 if (_Nature & Nature::T_VERTEX) {
156 #if 0
157                         _feA = iBrother._feA;
158                         _feB = iBrother._feB;
159                         _beA = iBrother._beA;
160                         _beB = iBrother._beB;
161                         _tvertex_iter = iBrother._tvertex_iter;
162 #endif
163                         _tbegin = iBrother._tbegin;
164                         _tend = iBrother._tend;
165                         _tvertex_iter = iBrother._tvertex_iter;
166                 }
167                 else {
168                         _begin = iBrother._begin;
169                         _end = iBrother._end;
170                         _nontvertex_iter = iBrother._nontvertex_iter;
171                 }
172         }
173
174         virtual ~edge_iterator_base() {}
175
176 //protected://FIXME
177 public:
178 #if 0
179         inline edge_iterator_base(value_type ifeA, value_type ifeB, value_type ibeA, value_type ibeB, value_type iter)
180         : parent_class()
181         {
182                 _Nature = Nature::T_VERTEX;
183                 _feA = ifeA;
184                 _feB = ifeB;
185                 _beA = ibeA;
186                 _beB = ibeB;
187                 _tvertex_iter = iter;
188         }
189 #endif
190
191         inline edge_iterator_base(edge_pointers_container_iterator begin, edge_pointers_container_iterator end,
192                                   edge_pointers_container_iterator iter)
193         : parent_class()
194         {
195                 _Nature = Nature::T_VERTEX;
196                 _tbegin = begin;
197                 _tend = end;
198                 _tvertex_iter = iter;
199         }
200
201         inline edge_iterator_base(edges_container_iterator begin, edges_container_iterator end,
202                                   edges_container_iterator iter)
203         : parent_class()
204         {
205                 _Nature = Nature::NON_T_VERTEX;
206                 _begin = begin;
207                 _end = end;
208                 _nontvertex_iter = iter;
209         }
210
211 public:
212         virtual bool begin() const
213         {
214                 if (_Nature & Nature::T_VERTEX)
215                         return (_tvertex_iter == _tbegin);
216                         //return (_tvertex_iter == _feA);
217                 else
218                         return (_nontvertex_iter == _begin);
219         }
220
221         virtual bool end() const
222         {
223                 if (_Nature & Nature::T_VERTEX)
224                         //return (_tvertex_iter.first == 0);
225                         return (_tvertex_iter == _tend);
226                 else
227                         return (_nontvertex_iter == _end);
228         }
229
230         // operators
231         // operator corresponding to ++i
232         virtual Self& operator++()
233         {
234                 increment();
235                 return *this;
236         }
237
238         // operator corresponding to i++, i.e. which returns the value *and then* increments it.
239         // That's why we store the value in a temp.
240         virtual Self operator++(int)  // op√©rateur correspondant √† i++ 
241         {
242                 Self tmp = *this;
243                 increment();
244                 return tmp;
245         }
246
247         // comparibility
248         virtual bool operator!=(const Self& b) const
249         {
250                 if (_Nature & Nature::T_VERTEX)
251                         return (_tvertex_iter != b._tvertex_iter);
252                 else
253                         return (_nontvertex_iter != b._nontvertex_iter);
254         }
255
256         virtual bool operator==(const Self& b) const
257         {
258                 return !(*this != b);
259         }
260
261         // dereferencing
262         virtual reference operator*() const
263         {
264                 if (_Nature & Nature::T_VERTEX)
265                         //return _tvertex_iter;
266                         return **_tvertex_iter;
267                 else
268                         return (*_nontvertex_iter);
269         }
270
271         virtual pointer operator->() const
272         {
273                 return &(operator*());
274         }
275
276 protected:
277         inline void increment()
278         {
279                 if (_Nature & Nature::T_VERTEX) {
280                         value_type tmp = (**_tvertex_iter);
281                         ++_tvertex_iter;
282                         value_type tmp2 = (**_tvertex_iter);
283                         if (tmp2.first == tmp.first)
284                                 ++_tvertex_iter;
285 #if 0
286                         // Hack to deal with cusp. the result of a cusp is a TVertex having two identical viewedges.
287                         // In order to iterate properly, we chose to to skip these last ones.
288                         if (_feB.first == _beA.first) {
289                                 if (_feA.first == _beB.first) {
290                                         _tvertex_iter.first = 0;
291                                         return;
292                                 }
293
294                                 if (_tvertex_iter.first == _feA.first)
295                                         _tvertex_iter.first = _beB.first;
296                                 else if (_tvertex_iter.first == _beB.first)
297                                         _tvertex_iter.first = 0;
298                                 else
299                                         _tvertex_iter.first = _feA.first;
300                                 return;
301                         }
302                         if (_feA.first == _beB.first) {
303                                 if (_feB.first == _beA.first) {
304                                         _tvertex_iter.first = 0;
305                                         return;
306                                 }
307
308                                 if (_tvertex_iter.first == _feB.first)
309                                         _tvertex_iter.first = _beA.first;
310                                 else if (_tvertex_iter.first == _beA.first)
311                                         _tvertex_iter.first = 0;
312                                 else
313                                         _tvertex_iter.first = _feB.first;
314                                 return;
315                         }
316                         // End of hack
317
318                         if (_tvertex_iter.first == _feA.first) {
319                                 // we return bea or beb
320                                 // choose one of them
321                                 _tvertex_iter.first = _feB.first;
322                                 return;
323                         }
324                         if (_tvertex_iter.first == _feB.first) {
325                                 _tvertex_iter.first = _beA.first;
326                                 return;
327                         }
328                         if (_tvertex_iter.first == _beA.first) {
329                                 _tvertex_iter.first = _beB.first;
330                                 return;
331                         }
332                         if (_tvertex_iter.first == _beB.first) {
333                                 _tvertex_iter.first = 0;
334                                 return;
335                         }
336 #endif
337                 }
338                 else {
339                         ++_nontvertex_iter;
340                 }
341         }
342 };
343
344 }  // ViewVertexInternal namespace
345
346 /**********************************/
347 /*                                */
348 /*                                */
349 /*             ViewEdge           */
350 /*                                */
351 /*                                */
352 /**********************************/
353
354 namespace ViewEdgeInternal {
355
356 /*!----------------------*/
357 /*! Iterators definition */
358 /*!----------------------*/
359 template<class Traits>
360 class edge_iterator_base : public IteratorBase<Traits,BidirectionalIteratorTag_Traits>
361 {
362 public:
363         typedef typename Traits::value_type value_type;
364         typedef typename Traits::difference_type difference_type;
365         typedef typename Traits::pointer pointer;
366         typedef typename Traits::reference reference;
367         typedef edge_iterator_base<Traits> Self;
368
369 public:
370         mutable value_type _ViewEdge;
371         //friend class edge_iterator_base<Nonconst_traits<ViewEdge*> >;
372         //friend class edge_iterator_base<Const_traits<ViewEdge*> >;
373         value_type _first;
374         bool _orientation;
375         typedef IteratorBase<Traits,BidirectionalIteratorTag_Traits> parent_class;
376
377 public:
378         friend class ViewEdge;
379         inline edge_iterator_base() : parent_class()
380         {
381                 _orientation = true;
382                 _first = 0;
383         }
384
385         inline edge_iterator_base(const edge_iterator_base<Nonconst_traits< ::ViewEdge*> >& iBrother) : parent_class()
386         {
387                 _ViewEdge = iBrother._ViewEdge;
388                 _first = iBrother._first;
389                 _orientation = iBrother._orientation;
390         }
391
392         inline edge_iterator_base(const edge_iterator_base<Const_traits< ::ViewEdge*> >& iBrother) : parent_class()
393         {
394                 _ViewEdge = iBrother._ViewEdge;
395                 _first = iBrother._first;
396                 _orientation = iBrother._orientation;
397         }
398
399 //protected://FIXME
400 public:
401         inline edge_iterator_base(value_type iEdge, bool orientation = true) : parent_class()
402         {
403                 _ViewEdge = iEdge;
404                 _first = iEdge;
405                 _orientation = orientation;
406         }
407
408 public:
409         virtual Self *clone() const
410         {
411                 return new edge_iterator_base(*this);
412         }
413
414         virtual ~edge_iterator_base() {}
415
416 public:
417         virtual bool orientation()
418         {
419                 return _orientation;
420         }
421
422         virtual void set_edge(value_type iVE)
423         {
424                 _ViewEdge = iVE;
425         }
426
427         virtual void set_orientation(bool iOrientation)
428         {
429                 _orientation = iOrientation;
430         }
431
432         virtual void change_orientation()
433         {
434                 _orientation = !_orientation;
435         }
436
437         // operators
438         // operator corresponding to ++i
439         inline Self& operator++()
440         {
441                 //++_ViewEdge->getTimeStamp();
442                 increment();
443                 return *this;
444         }
445
446         // operator corresponding to i++, i.e. which returns the value *and then* increments it.
447         // That's why we store the value in a temp.
448         inline Self operator++(int)
449         {
450                 //++_ViewEdge->getTimeStamp();
451                 Self tmp = *this;
452                 increment();
453                 return tmp;
454         }
455
456         // operator corresponding to --i
457         inline Self& operator--()
458         {
459                 //++_ViewEdge->getTimeStamp();
460                 decrement();
461                 return *this;
462         }
463
464         // operator corresponding to i--, i.e. which returns the value *and then* increments it.
465         // That's why we store the value in a temp.
466         inline Self operator--(int)
467         {
468                 //++_ViewEdge->getTimeStamp();
469                 Self tmp = *this;
470                 decrement();
471                 return tmp;
472         }
473
474         // comparibility
475         virtual bool operator!=(const Self& b) const
476         {
477                 return (_ViewEdge != b._ViewEdge);
478         }
479
480         virtual bool operator==(const Self& b) const
481         {
482                 return !(*this != b);
483         }
484
485         // dereferencing
486         virtual reference operator*() const
487         {
488                 return (_ViewEdge);
489         }
490
491         virtual pointer operator->() const
492         {
493                 return &(operator*());
494         }
495
496 public:
497         virtual bool begin() const
498         {
499                 return (_ViewEdge == _first) ? true : false;
500         }
501
502         virtual bool end() const
503         {
504                 return (_ViewEdge == 0) ? true : false;
505         }
506
507 protected:
508         virtual void increment() {}
509         virtual void decrement() {}
510 };
511
512 template<class Traits>
513 class fedge_iterator_base : public IteratorBase<Traits,BidirectionalIteratorTag_Traits>
514 {
515 public:
516         typedef typename Traits::value_type value_type;
517         typedef typename Traits::difference_type difference_type;
518         typedef typename Traits::pointer pointer;
519         typedef typename Traits::reference reference;
520         typedef fedge_iterator_base<Traits> Self;
521
522 public:
523         typedef IteratorBase<Traits,BidirectionalIteratorTag_Traits> parent_class;
524         mutable value_type _FEdge;
525         value_type _first;
526         value_type _FEdgeB; // last fedge of the view edge
527
528 public:
529         friend class ::ViewEdge;
530         friend class fedge_iterator;
531
532         inline fedge_iterator_base() : parent_class() {}
533
534         inline fedge_iterator_base(const fedge_iterator_base<Nonconst_traits<FEdge*> >& iBrother) : parent_class()
535         {
536                 _FEdge = iBrother._FEdge;
537                 _first = iBrother._first;
538                 _FEdgeB = iBrother._FEdgeB;
539         }
540
541         inline fedge_iterator_base(const fedge_iterator_base<Const_traits<FEdge*> >& iBrother) : parent_class()
542         {
543                 _FEdge = iBrother._FEdge;
544                 _first = iBrother._first;
545                 _FEdgeB = iBrother._FEdgeB;
546         }
547
548 //protected://FIXME
549 public:
550         inline fedge_iterator_base(value_type iEdge, value_type iFEdgeB) : parent_class()
551         {
552                 _FEdge = iEdge;
553                 _first = iEdge;
554                 _FEdgeB = iFEdgeB;
555         }
556
557 public:
558         virtual ~fedge_iterator_base() {}
559
560         // operators
561         // operator corresponding to ++i.
562         inline Self& operator++()
563         { 
564         increment();
565         return *this;
566         }
567
568         // operator corresponding to i++, i.e. which returns the value *and then* increments it.
569         // That's why we store the value in a temp.
570         inline Self operator++(int)
571         {
572                 Self tmp = *this;
573                 increment();
574                 return tmp;
575         }
576
577         // operator corresponding to --i
578         inline Self& operator--()
579         {
580                 decrement();
581                 return *this;
582         }
583
584         // operator corresponding to i--, i.e. which returns the value *and then* increments it.
585         // That's why we store the value in a temp.
586         inline Self operator--(int)
587         {
588                 Self tmp = *this;
589                 decrement();
590                 return tmp;
591         }
592
593         // comparibility
594         virtual bool operator!=(const Self& b) const
595         {
596                 return (_FEdge != b._FEdge);
597         }
598
599         virtual bool operator==(const Self& b) const
600         {
601                 return !(*this != b);
602         }
603
604         // dereferencing
605         virtual reference operator*() const
606         {
607                 return (_FEdge);
608         }
609
610         virtual pointer operator->() const
611         {
612                 return &(operator*());
613         }
614
615 public:
616         virtual bool begin() const
617         {
618                 return (_FEdge == _first) ? true : false;
619         }
620
621         virtual bool end() const
622         {
623                 return (_FEdge == 0) ? true : false;
624         }
625
626 protected:
627         virtual void increment()
628         {
629                 _FEdge = _FEdge->nextEdge(); // we don't change or
630         }
631
632         virtual void decrement()
633         {
634                 if (0 == _FEdge) {
635                         _FEdge = _FEdgeB;
636                         return;
637                 }
638                 _FEdge = _FEdge->previousEdge(); // we don't change or
639         }
640 };
641
642 template<class Traits>
643 class vertex_iterator_base : public IteratorBase<Traits,BidirectionalIteratorTag_Traits>
644 {
645 public:
646         typedef typename Traits::value_type value_type;
647         typedef typename Traits::difference_type difference_type;
648         typedef typename Traits::pointer pointer;
649         typedef typename Traits::reference reference;
650         typedef vertex_iterator_base<Traits> Self;
651
652 protected:
653         typedef IteratorBase<Traits,BidirectionalIteratorTag_Traits> parent_class;
654
655 public:
656         mutable value_type _SVertex;
657         FEdge *_NextFEdge;
658         FEdge *_PreviousFEdge;
659
660 public:
661         friend class ViewEdge;
662         friend class vertex_iterator;
663
664         inline vertex_iterator_base() : parent_class() {}
665
666         inline vertex_iterator_base(const vertex_iterator_base<Const_traits<SVertex*> >& iBrother) : parent_class()
667         {
668                 _SVertex = iBrother._SVertex;
669                 _NextFEdge = iBrother._NextFEdge;
670                 _PreviousFEdge = iBrother._PreviousFEdge;
671         }
672
673         inline vertex_iterator_base(const vertex_iterator_base<Nonconst_traits<SVertex*> >& iBrother) : parent_class()
674         {
675                 _SVertex = iBrother._SVertex;
676                 _NextFEdge = iBrother._NextFEdge;
677                 _PreviousFEdge = iBrother._PreviousFEdge;
678         }
679
680 //protected://FIXME
681 public:
682         inline vertex_iterator_base(value_type iVertex, FEdge *iPreviousFEdge, FEdge *iNextFEdge) : parent_class()
683         {
684                 _SVertex = iVertex;
685                 _NextFEdge = iNextFEdge;
686                 _PreviousFEdge = iPreviousFEdge;
687         }
688
689 public:
690         virtual ~vertex_iterator_base() {}
691
692         virtual bool begin() const
693         {
694                 return (_PreviousFEdge == 0) ? true : false;
695         }
696
697         virtual bool end() const
698         {
699                 return (_SVertex == 0) ? true : false;
700         }
701
702         // operators
703         // operator corresponding to ++i
704         inline Self& operator++()
705         {
706                 increment();
707                 return *this;
708         }
709
710         // operator corresponding to i++, i.e. which returns the value *and then* increments it.
711         // That's why we store the value in a temp.
712         inline Self operator++(int)
713         {
714                 Self tmp = *this;
715                 increment();
716                 return tmp;
717         }
718
719         // operator corresponding to --i
720         inline Self& operator--()
721         {
722                 decrement();
723                 return *this;
724         }
725
726         // operator corresponding to --i, i.e. which returns the value *and then* increments it.
727         // That's why we store the value in a temp.
728         inline Self operator--(int)
729         {
730                 Self tmp = *this;
731                 decrement();
732                 return tmp;
733         }
734
735         // comparibility
736         virtual bool operator!=(const Self& b) const
737         {
738                 return (_SVertex != b._SVertex);
739         }
740
741         virtual bool operator==(const Self& b) const
742         {
743                 return !(*this != b);
744         }
745
746         // dereferencing
747         virtual reference operator*() const
748         {
749                 return (_SVertex);
750         }
751
752         virtual pointer operator->() const
753         {
754                 return &(operator*());
755         }
756
757 protected:
758         virtual void increment()
759         {
760                 if (!_NextFEdge) {
761                         _SVertex = NULL;
762                         return;
763                 }
764                 _SVertex = _NextFEdge->vertexB();
765                 _PreviousFEdge = _NextFEdge;
766                 _NextFEdge = _NextFEdge->nextEdge();
767         }
768
769         virtual void decrement()
770         {
771 #if 0
772                 if (!_SVertex) {
773                         _SVertex = _PreviousFEdge->vertexB();
774                         return;
775                 }
776 #endif
777                 if (!_PreviousFEdge) {
778                         _SVertex = NULL;
779                         return;
780                 }
781                 _SVertex = _PreviousFEdge->vertexA();
782                 _NextFEdge = _PreviousFEdge;
783                 _PreviousFEdge = _PreviousFEdge->previousEdge();
784         }
785 };
786
787 } // end of namespace ViewEdgeInternal
788
789 #endif // __FREESTYLE_VIEW_MAP_ADVANCED_ITERATORS_H__