Fade2D Documentation pages v2.16.8
Delaunay Features
TriangleAroundVertexIterator.h
Go to the documentation of this file.
1 // Copyright (C) Geom Software e.U, Bernhard Kornberger, Graz/Austria
2 //
3 // This file is part of the Fade2D library. The student license is free
4 // of charge and covers personal, non-commercial research. Licensees
5 // holding a commercial license may use this file in accordance with
6 // the Commercial License Agreement.
7 //
8 // This software is provided AS IS with NO WARRANTY OF ANY KIND,
9 // INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS
10 // FOR A PARTICULAR PURPOSE.
11 //
12 // Please contact the author if any conditions of this licensing are
13 // unclear to you.
14 //
15 // Support: https://www.geom.at/contact/
16 // Project: https://www.geom.at/fade2d/html/
18 
19 #pragma once
20 #include "common.h"
21 #include "Point2.h"
22 #include "Triangle2.h"
23 //#include "tools.h"
24 
25 #include "common.h"
26 #if GEOM_PSEUDO3D==GEOM_TRUE
27  namespace GEOM_FADE25D {
28 #elif GEOM_PSEUDO3D==GEOM_FALSE
29  namespace GEOM_FADE2D {
30 #else
31  #error GEOM_PSEUDO3D is not defined
32 #endif
33 
35 inline
36 int incBy1( int num)
37 {
38  ++num;
39  if(num>2) return 0;
40  return num;
41 }
42 
44 inline
45 int incBy2( int num)
46 {
47  --num;
48  if(num<0) return 2;
49  return num;
50 }
51 
82 class CLASS_DECLSPEC TriangleAroundVertexIterator
83 {
84 public:
93  explicit TriangleAroundVertexIterator(const Point2* pPnt_):pPnt(pPnt_),pTr(pPnt_->getIncidentTriangle()),pSavedTr(NULL)
94  {
95  if(pTr==NULL)
96  {
97  //flog("TriangleAroundVertexIterator::TriangleAroundVertexIterator(), created from an invalid point");
98  FadeException fadeEx;
99  throw fadeEx;
100  }
101  assert(pTr!=NULL);
102 
103  }
109  TriangleAroundVertexIterator(Point2* pPnt_, Triangle2* pTr_):pPnt(pPnt_),pTr(pTr_),pSavedTr(NULL)
110  {
111  assert(pTr!=NULL);
112  }
119  TriangleAroundVertexIterator(const TriangleAroundVertexIterator& it) : pPnt(it.pPnt),pTr(it.pTr),pSavedTr(NULL)
120  {
121  assert(pTr!=NULL);
122  }
131  {
132  pPnt=other.pPnt;
133  pTr=other.pTr;
134  pSavedTr=other.pSavedTr;
135  return *this;
136  }
137 
147  {
148 //GCOUT<<"tavi++"<<std::endl;
149  if(pTr==NULL)
150  {
151 //GCOUT<<"pTr==NULL, calling loop"<<std::endl;
152  loop();
153  return *this;
154  }
155 
156  int ccwIdx=incBy1(pTr->getIntraTriangleIndex(pPnt));
157 //GCOUT<<"ccwIdx="<<ccwIdx<<"now swapping saved="<<pSavedTr<<", pTr="<<pTr<<std::endl;;
158  std::swap(pSavedTr,pTr);
159  pTr=pSavedTr->getOppositeTriangle(ccwIdx);
160 //GCOUT<<"and pTr is now the opposite triangle of pSavedTr="<<*pSavedTr<<", namely "<<pTr<<std::endl;
161 
162  return *this;
163  }
164 
174  {
175  if(pTr==NULL)
176  {
177  loop();
178  return *this;
179  }
180  int cwIdx=incBy2(pTr->getIntraTriangleIndex(pPnt));
181  std::swap(pSavedTr,pTr);
182  pTr=pSavedTr->getOppositeTriangle(cwIdx);
183  return *this;
184  }
192  {
193  return (pPnt==rhs.pPnt && pTr==rhs.pTr);
194  }
202  {
203  return (pPnt!=rhs.pPnt || pTr!=rhs.pTr);
204  }
212  {
213  return pTr;
214  }
225  {
226  TriangleAroundVertexIterator tmp(*this);
227  ++tmp;
228  return *tmp;
229  }
238  {
239  TriangleAroundVertexIterator tmp(*this);
240  --tmp;
241  return *tmp;
242  }
243 
244 
245 
246 protected:
247  const Point2* pPnt;
248  Triangle2 *pTr,*pSavedTr;
250  void loop()
251  {
252  assert(pTr==NULL && pSavedTr!=NULL);
253 
254  enum DIRECTION{DIRECTION_NONE,DIRECTION_BACK,DIRECTION_FWD};
255  DIRECTION direction(DIRECTION_NONE);
256 
257  int axisIndex=pSavedTr->getIntraTriangleIndex(pPnt);
258 
259  if(pSavedTr->getOppositeTriangle(incBy2(axisIndex))!=NULL)
260  {
261  direction=DIRECTION_BACK;
262  }
263  if(pSavedTr->getOppositeTriangle(incBy1(axisIndex))!=NULL)
264  {
265  assert(direction==DIRECTION_NONE);
266  direction=DIRECTION_FWD;
267  }
268  pTr=pSavedTr;
269  if(direction==DIRECTION_FWD) while(*operator++()!=NULL); // fast forward
270  if(direction==DIRECTION_BACK) while(*operator--()!=NULL); // rewind
271  pTr=pSavedTr;
272  }
273 };
274 
275 
276 } // (namespace)
Represents a 2D point.
Definition: Point2.h:76
Represents a triangle in a triangulation.
Definition: Triangle2.h:59
Triangle2 * getOppositeTriangle(const int idx) const
Get the neighbor triangle at the specified index.
Definition: Triangle2.h:420
int getIntraTriangleIndex(const Point2 *pCorner) const
Get the index of the specified corner.
Definition: Triangle2.h:451
Iterator for all triangles around a given vertex.
Definition: TriangleAroundVertexIterator.h:83
bool operator!=(const TriangleAroundVertexIterator &rhs)
Compare the current iterator with another iterator.
Definition: TriangleAroundVertexIterator.h:201
bool operator==(const TriangleAroundVertexIterator &rhs)
Compare the current iterator with another iterator.
Definition: TriangleAroundVertexIterator.h:191
Triangle2 * previewNextTriangle()
Preview the next triangle (CCW direction).
Definition: TriangleAroundVertexIterator.h:224
Triangle2 * operator*()
Returns the current triangle (or NULL)
Definition: TriangleAroundVertexIterator.h:211
Triangle2 * previewPrevTriangle()
Preview the previous triangle (CW direction).
Definition: TriangleAroundVertexIterator.h:237
TriangleAroundVertexIterator & operator=(const TriangleAroundVertexIterator &other)
Assignment operator.
Definition: TriangleAroundVertexIterator.h:130
TriangleAroundVertexIterator & operator++()
Move the iterator to the next triangle (counterclockwise order)
Definition: TriangleAroundVertexIterator.h:146
TriangleAroundVertexIterator(const TriangleAroundVertexIterator &it)
Copy constructor.
Definition: TriangleAroundVertexIterator.h:119
TriangleAroundVertexIterator(Point2 *pPnt_, Triangle2 *pTr_)
Constructor.
Definition: TriangleAroundVertexIterator.h:109
TriangleAroundVertexIterator & operator--()
Move the iterator to the previous triangle (clockwise order)
Definition: TriangleAroundVertexIterator.h:173
TriangleAroundVertexIterator(const Point2 *pPnt_)
Constructor.
Definition: TriangleAroundVertexIterator.h:93