Geom Software - C++ Programming and Geometry Libraries
Fade2D Documentation pages v1.56
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(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 
104  {
105 //std::cout<<"tavi++"<<std::endl;
106  if(pTr==NULL)
107  {
108 //std::cout<<"pTr==NULL, calling loop"<<std::endl;
109  loop();
110  return *this;
111  }
112 
113  int ccwIdx=inc1(pTr->getIntraTriangleIndex(pPnt));
114 //std::cout<<"ccwIdx="<<ccwIdx<<"now swapping saved="<<pSavedTr<<", pTr="<<pTr<<std::endl;;
115  std::swap(pSavedTr,pTr);
116  pTr=pSavedTr->getOppositeTriangle(ccwIdx);
117 //std::cout<<"and pTr is now the opposite triangle of pSavedTr="<<*pSavedTr<<", namely "<<pTr<<std::endl;
118 
119  return *this;
120  }
121 
131  {
132  if(pTr==NULL)
133  {
134  loop();
135  return *this;
136  }
137  int cwIdx=inc2(pTr->getIntraTriangleIndex(pPnt));
138  std::swap(pSavedTr,pTr);
139  pTr=pSavedTr->getOppositeTriangle(cwIdx);
140  return *this;
141  }
146  {
147  return (pPnt==rhs.pPnt && pTr==rhs.pTr);
148  }
153  {
154  return (pPnt!=rhs.pPnt || pTr!=rhs.pTr);
155  }
162  {
163  return pTr;
164  }
172  {
173  TriangleAroundVertexIterator tmp(*this);
174  ++tmp;
175  return *tmp;
176  }
184  {
185  TriangleAroundVertexIterator tmp(*this);
186  --tmp;
187  return *tmp;
188  }
189 
190 
191 
192 protected:
193  Point2* pPnt;
194  Triangle2 *pTr,*pSavedTr;
195 
196  void loop()
197  {
198  assert(pTr==NULL && pSavedTr!=NULL);
199  enum DIRECTION{DIRECTION_NONE,DIRECTION_BACK,DIRECTION_FWD};
200  DIRECTION direction(DIRECTION_NONE);
201 
202  int axisIndex=pSavedTr->getIntraTriangleIndex(pPnt);
203 
204  if(pSavedTr->getOppositeTriangle(inc2(axisIndex))!=NULL)
205  {
206  direction=DIRECTION_BACK;
207  }
208  if(pSavedTr->getOppositeTriangle(inc1(axisIndex))!=NULL)
209  {
210  assert(direction==DIRECTION_NONE);
211  direction=DIRECTION_FWD;
212  }
213  pTr=pSavedTr;
214  if(direction==DIRECTION_FWD) while(*operator++()!=NULL); // fast forward
215  if(direction==DIRECTION_BACK) while(*operator--()!=NULL); // rewind
216  pTr=pSavedTr;
217  }
218 };
219 
220 
221 } // (namespace)
TriangleAroundVertexIterator & operator++()
Proceed to the next triangle (the one in counterclockwise order)
Definition: TriangleAroundVertexIterator.h:103
Point.
Definition: Point2.h:39
Triangle2 * getOppositeTriangle(const int ith) const
Get the i-th neighbor triangle.
Definition: Triangle2.h:322
bool operator!=(const TriangleAroundVertexIterator &rhs)
Check if the center points or the current triangles of the iterators are different.
Definition: TriangleAroundVertexIterator.h:152
TriangleAroundVertexIterator(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:183
Triangle2 * operator*()
Returns a pointer to the current triangle (or NULL)
Definition: TriangleAroundVertexIterator.h:161
TriangleAroundVertexIterator(const TriangleAroundVertexIterator &it)
Copy constructor.
Definition: TriangleAroundVertexIterator.h:90
Triangle2 * previewNextTriangle()
Preview next triangle (CCW direction)
Definition: TriangleAroundVertexIterator.h:171
TriangleAroundVertexIterator & operator--()
Proceed to the previous triangle (the one in clockwise order)
Definition: TriangleAroundVertexIterator.h:130
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:145
int getIntraTriangleIndex(const Point2 *p) const
Get the index of p in the triangle.
Definition: Triangle2.h:354
Triangle.
Definition: Triangle2.h:48
Definition: Bbox2.h:25