Geom Software - C++ Programming and Geometry Libraries
Fade2D Documentation pages v1.63
TriangleAroundVertexIterator.h
1 // (c) 2010 Geom e.U. Bernhard Kornberger, Graz/Austria. All rights reserved.
2 //
3 // This file is part of the Fade2D library. You can use it for your personal
4 // non-commercial research. Licensees holding a commercial license may use this
5 // file in accordance with the Commercial License Agreement provided
6 // with the Software.
7 //
8 // This software is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING
9 // THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
10 //
11 // Please contact the author if any conditions of this licensing are not clear
12 // to you.
13 //
14 // Author: Bernhard Kornberger, bkorn (at) geom.at
15 // http://www.geom.at
16 
17 
18 #pragma once
19 #include "common.h"
20 #include "Point2.h"
21 #include "Triangle2.h"
22 
23 
24 #include "common.h"
25 #if GEOM_PSEUDO3D==GEOM_TRUE
26  namespace GEOM_FADE25D {
27 #elif GEOM_PSEUDO3D==GEOM_FALSE
28  namespace GEOM_FADE2D {
29 #else
30  #error GEOM_PSEUDO3D is not defined
31 #endif
32 
33 
34 inline
35 int inc1( int num)
36 {
37  ++num;
38  if(num>2) return 0;
39  return num;
40 }
41 
42 inline
43 int inc2( int num)
44 {
45  --num;
46  if(num<0) return 2;
47  return num;
48 }
58 class CLASS_DECLSPEC TriangleAroundVertexIterator
59 {
60 public:
67  explicit TriangleAroundVertexIterator(const Point2* pPnt_):pPnt(pPnt_),pTr(pPnt_->getIncidentTriangle()),pSavedTr(NULL)
68  {
69  if(pTr==NULL)
70  {
71  std::cerr<<"TriangleAroundVertexIterator::TriangleAroundVertexIterator(), created from an invalid point"<<std::endl;
72  FadeException fadeEx;
73  throw fadeEx;
74  }
75  assert(pTr!=NULL);
76 
77  }
83  TriangleAroundVertexIterator(Point2* pPnt_, Triangle2* pTr_):pPnt(pPnt_),pTr(pTr_),pSavedTr(NULL)
84  {
85  assert(pTr!=NULL);
86  }
90  TriangleAroundVertexIterator(const TriangleAroundVertexIterator& it) : pPnt(it.pPnt),pTr(it.pTr),pSavedTr(NULL)
91  {
92  assert(pTr!=NULL);
93  }
94 
96  {
97  pPnt=other.pPnt;
98  pTr=other.pTr;
99  pSavedTr=other.pSavedTr;
100  return *this;
101  }
111  {
112 //std::cout<<"tavi++"<<std::endl;
113  if(pTr==NULL)
114  {
115 //std::cout<<"pTr==NULL, calling loop"<<std::endl;
116  loop();
117  return *this;
118  }
119 
120  int ccwIdx=inc1(pTr->getIntraTriangleIndex(pPnt));
121 //std::cout<<"ccwIdx="<<ccwIdx<<"now swapping saved="<<pSavedTr<<", pTr="<<pTr<<std::endl;;
122  std::swap(pSavedTr,pTr);
123  pTr=pSavedTr->getOppositeTriangle(ccwIdx);
124 //std::cout<<"and pTr is now the opposite triangle of pSavedTr="<<*pSavedTr<<", namely "<<pTr<<std::endl;
125 
126  return *this;
127  }
128 
138  {
139  if(pTr==NULL)
140  {
141  loop();
142  return *this;
143  }
144  int cwIdx=inc2(pTr->getIntraTriangleIndex(pPnt));
145  std::swap(pSavedTr,pTr);
146  pTr=pSavedTr->getOppositeTriangle(cwIdx);
147  return *this;
148  }
153  {
154  return (pPnt==rhs.pPnt && pTr==rhs.pTr);
155  }
160  {
161  return (pPnt!=rhs.pPnt || pTr!=rhs.pTr);
162  }
169  {
170  return pTr;
171  }
179  {
180  TriangleAroundVertexIterator tmp(*this);
181  ++tmp;
182  return *tmp;
183  }
191  {
192  TriangleAroundVertexIterator tmp(*this);
193  --tmp;
194  return *tmp;
195  }
196 
197 
198 
199 protected:
200  const Point2* pPnt;
201  Triangle2 *pTr,*pSavedTr;
202 
203  void loop()
204  {
205  assert(pTr==NULL && pSavedTr!=NULL);
206  enum DIRECTION{DIRECTION_NONE,DIRECTION_BACK,DIRECTION_FWD};
207  DIRECTION direction(DIRECTION_NONE);
208 
209  int axisIndex=pSavedTr->getIntraTriangleIndex(pPnt);
210 
211  if(pSavedTr->getOppositeTriangle(inc2(axisIndex))!=NULL)
212  {
213  direction=DIRECTION_BACK;
214  }
215  if(pSavedTr->getOppositeTriangle(inc1(axisIndex))!=NULL)
216  {
217  assert(direction==DIRECTION_NONE);
218  direction=DIRECTION_FWD;
219  }
220  pTr=pSavedTr;
221  if(direction==DIRECTION_FWD) while(*operator++()!=NULL); // fast forward
222  if(direction==DIRECTION_BACK) while(*operator--()!=NULL); // rewind
223  pTr=pSavedTr;
224  }
225 };
226 
227 
228 } // (namespace)
TriangleAroundVertexIterator & operator++()
Proceed to the next triangle (the one in counterclockwise order)
Definition: TriangleAroundVertexIterator.h:110
Point.
Definition: Point2.h:39
Triangle2 * getOppositeTriangle(const int ith) const
Get the i-th neighbor triangle.
Definition: Triangle2.h:345
bool operator!=(const TriangleAroundVertexIterator &rhs)
Check if the center points or the current triangles of the iterators are different.
Definition: TriangleAroundVertexIterator.h:159
TriangleAroundVertexIterator(const Point2 *pPnt_)
Constructor.
Definition: TriangleAroundVertexIterator.h:67
Iterator for all triangles around a given vertex.
Definition: TriangleAroundVertexIterator.h:58
Triangle2 * previewPrevTriangle()
Preview previous triangle (CW direction)
Definition: TriangleAroundVertexIterator.h:190
Triangle2 * operator*()
Returns a pointer to the current triangle (or NULL)
Definition: TriangleAroundVertexIterator.h:168
TriangleAroundVertexIterator(const TriangleAroundVertexIterator &it)
Copy constructor.
Definition: TriangleAroundVertexIterator.h:90
Triangle2 * previewNextTriangle()
Preview next triangle (CCW direction)
Definition: TriangleAroundVertexIterator.h:178
TriangleAroundVertexIterator & operator--()
Proceed to the previous triangle (the one in clockwise order)
Definition: TriangleAroundVertexIterator.h:137
TriangleAroundVertexIterator(Point2 *pPnt_, Triangle2 *pTr_)
Constructor.
Definition: TriangleAroundVertexIterator.h:83
bool operator==(const TriangleAroundVertexIterator &rhs)
Check if the center points and the current triangles of the iterators are the same.
Definition: TriangleAroundVertexIterator.h:152
int getIntraTriangleIndex(const Point2 *p) const
Get the index of p in the triangle.
Definition: Triangle2.h:384
Triangle.
Definition: Triangle2.h:48
Definition: Bbox2.h:25