Geom Software - C++ Programming and Geometry Libraries
Fade2.5D Documentation pages v1.60
Triangle2.h
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 // not clear to you.
14 //
15 // Author: Bernhard Kornberger, bkorn (at) geom.at
16 // http://www.geom.at
17 
18 
19 #pragma once
20 #include "Point2.h"
21 
22 
23 #include "common.h"
24 #if GEOM_PSEUDO3D==GEOM_TRUE
25  namespace GEOM_FADE25D {
26 #elif GEOM_PSEUDO3D==GEOM_FALSE
27  namespace GEOM_FADE2D {
28 #else
29  #error GEOM_PSEUDO3D is not defined
30 #endif
31 
48 class CLASS_DECLSPEC Triangle2
49 {
50 public:
55  {
56  aVertexPointer[0]=NULL;
57  aVertexPointer[1]=NULL;
58  aVertexPointer[2]=NULL;
59  aOppTriangles[0]=NULL;
60  aOppTriangles[1]=NULL;
61  aOppTriangles[2]=NULL;
62 
63  } // Never used!
64 
65 
66 
67 
77  Point2* getCorner(const int ith) const;
78 
79 #if GEOM_PSEUDO3D==GEOM_TRUE
80 
100 std::pair<Point2,bool> getDual() const;
101 #else
102 
119 std::pair<Point2,bool> getDual() const;
120 #endif
121 
126  Point2 getBarycenter() const;
127 
128 
129 #if GEOM_PSEUDO3D==GEOM_TRUE
130 
134  Vector2 getNormalVector() const;
135 #endif
136 
149 double getInteriorAngle2D(int ith) const;
150 
151 #if GEOM_PSEUDO3D==GEOM_TRUE
152 
156 double getInteriorAngle25D(int ith) const;
157 #endif
158 
159 
160 
173  double getArea2D() const;
174 
175 
176 
177 
178 #if GEOM_PSEUDO3D==GEOM_TRUE
179 
186  double getArea25D() const;
187 #endif
188 
189 
203  Triangle2* getOppositeTriangle(const int ith) const;
204 
205 
217  int getIntraTriangleIndex(const Point2* p) const;
230  int getIntraTriangleIndex(const Triangle2* pTriangle) const;
231 
237  int getIntraTriangleIndex(const Point2* p0,const Point2* p1) const;
238 
239 
241  //*
242  //*
243  //* Internal use
244 //*/
245  //bool getState() const;
246 
254  double getSquaredEdgeLength(int ith) const;
255 
256 //** \brief Method for internal use
257  //*
258  //* Internal use
259 //*/
260  //void setState(bool bState_);
270  void setOppTriangle(const int ith, Triangle2* pTriangle);
273  void setProperties( Point2* pI, Point2* pJ, Point2* pK);
274 
277  void clearProperties();
278 
281  void setPropertiesAndOppT(Point2* pI, Point2* pJ, Point2* pK,Triangle2* pNeig0,Triangle2* pNeig1,Triangle2* pNeig2);
282 
285  void setVertexPointer(const int ith, Point2* pp);
286 
291  bool hasVertex(Point2* pVtx) const;
292 
297  bool hasOnEdge(int i,const Point2& q) const;
298 
301  int getMaxIndex() const;
302 
305  int getMinIndex() const;
306 
307 
308  // DEBUG
309  CLASS_DECLSPEC
310  friend std::ostream &operator<<(std::ostream &stream, const Triangle2& c);
311  friend inline void registerTriangles(Triangle2* fromTriangle,int ith,Triangle2* toTriangle,int jth);
312 protected:
313  double computeArea(double l0,double l1,double l2) const;
314  bool isAccurateCC(int maxIdx,const Point2& cc) const;
315  bool getCC_strategy1(double avgOffX,double avgOffY,Point2& cc) const;
316  void getCC_strategy2(int maxIdx,double avgOffX,double avgOffY,Point2& cc) const;
317  void getCommonOffset(double& x,double& y) const;
318  Point2* aVertexPointer[3];
319  Triangle2* aOppTriangles[3];
320  //bool bState;
321 };
322 
323 namespace{
324 inline bool checkRange(int ith)
325 {
326  return (ith==0 || ith==1 || ith==2); // true if ith={0,1,2}
327 }
328 
329 }
330 inline Triangle2* Triangle2::getOppositeTriangle(const int ith) const
331 {
332  assert(checkRange(ith));
333  return aOppTriangles[ith];
334 }
335 
336 inline void Triangle2::setOppTriangle(const int ith, Triangle2* pNeig)
337 {
338  //if(!checkRange(ith))
339  //{
340  //std::cout<<"ith="<<ith<<std::endl;
341  //int* a(NULL);
342  //*a+=1;
343  //}
344 
345  assert(checkRange(ith));
346  aOppTriangles[ith]=pNeig;
347 }
348 
349 
350 inline int Triangle2::getIntraTriangleIndex(const Point2* p0,const Point2* p1) const
351 {
352  for(int i=0;i<3;++i)
353  {
354  int ici1((i+1)%3);
355  int ici2((i+2)%3);
356 
357  if( aVertexPointer[ici1]==p0 && aVertexPointer[ici2]==p1) return i;
358  if( aVertexPointer[ici1]==p1 && aVertexPointer[ici2]==p0) return i;
359  }
360 
361  std::cout<<"BUG: Triangle2::getIntraTriangleIndex failed for"<<std::endl;// COUTOK
362  std::cout<<*p0<<std::endl;// COUTOK
363  std::cout<<*p1<<std::endl;// COUTOK
364  std::cout<<*this<<std::endl;// COUTOK
365  assert(false);
366  return -1;
367 }
368 
369 inline int Triangle2::getIntraTriangleIndex(const Point2* pVtx) const
370 {
371 #ifndef NDEBUG
372  // Just debug code
373  for(int i=0;i<3;++i)
374  {
375  if(aVertexPointer[i]==pVtx)
376  {
377  return i;
378  }
379  }
380  assert(false);
381 #endif
382  return ( (aVertexPointer[1]==pVtx) + 2*(aVertexPointer[2]==pVtx));
383 }
384 
385 
386 inline int Triangle2::getIntraTriangleIndex(const Triangle2* pTriangle) const
387 {
388 #ifndef NDEBUG
389  // Just debug code
390  for(int i=0;i<3;++i)
391  {
392  if(aOppTriangles[i]==pTriangle)
393  {
394  return i;
395  }
396  }
397  assert(false);
398 #endif
399  return ( (aOppTriangles[1]==pTriangle) + 2*(aOppTriangles[2]==pTriangle));
400 }
401 
402 //inline int Triangle2::getIntraTriangleIndex(const Triangle2* pTriangle) const
403 //{
404 
405  //if(getOppositeTriangle(0)==pTriangle) return 0;
406  //if(getOppositeTriangle(1)==pTriangle) return 1;
407 
408 //#ifndef NDEBUG
409  //if(getOppositeTriangle(2)!=pTriangle)
410  //{
411  //std::cout<<"Triangle2::getIntraTriangleIndex, pTriangle is not a neighbor of the current triangle"<<std::endl;// COUTOK
412  //std::cout<<"Current triangle: "<<*this<<std::endl;// COUTOK
413  //std::cout<<"pTriangle: "<<*pTriangle<<std::endl;// COUTOK
414 
415 
416  //assert(false);
417  //}
418 //#endif
419  //return 2;
420 //}
421 
422 inline Point2* Triangle2::getCorner(const int ith) const
423 {
424  assert(checkRange(ith));
425  return aVertexPointer[ith];
426 }
427 
428 inline void Triangle2::setVertexPointer(const int ith, Point2* pp)
429 {
430  aVertexPointer[ith]=pp;
431 }
432 
433 inline void Triangle2::setProperties( Point2* pI, Point2* pJ, Point2* pK)
434 {
435  assert((pI!=NULL && pJ!=NULL && pK!=NULL));
436  aVertexPointer[0]=pI;
437  aVertexPointer[1]=pJ;
438  aVertexPointer[2]=pK;
439  pI->setIncidentTriangle(this);
440  pJ->setIncidentTriangle(this);
441  pK->setIncidentTriangle(this);
442  aOppTriangles[0]=NULL;
443  aOppTriangles[1]=NULL;
444  aOppTriangles[2]=NULL;
445 }
446 
448 {
449  for(int i=0;i<3;++i)
450  {
451  aVertexPointer[i]=NULL;
452  aOppTriangles[i]=NULL;
453  }
454 }
455 
457  Point2* pI, Point2* pJ, Point2* pK,
458  Triangle2* pNeig0,Triangle2* pNeig1,Triangle2* pNeig2
459  )
460 {
461  assert((pI!=NULL && pJ!=NULL && pK!=NULL));
462  aVertexPointer[0]=pI;
463  aVertexPointer[1]=pJ;
464  aVertexPointer[2]=pK;
465  pI->setIncidentTriangle(this);
466  pJ->setIncidentTriangle(this);
467  pK->setIncidentTriangle(this);
468  aOppTriangles[0]=pNeig0;
469  aOppTriangles[1]=pNeig1;
470  aOppTriangles[2]=pNeig2;
471 }
472 
473 inline void registerTriangles(Triangle2* pFromT,int ith,Triangle2* pToT,int jth)
474 {
475  assert(checkRange(ith));
476  assert(checkRange(jth));
477 
478  pFromT->aOppTriangles[ith]=pToT;
479  pToT->aOppTriangles[jth]=pFromT;
480 
481 }
482 
483 
484 
485 } // (namespace)
void clearProperties()
Clear all corners and neighbor pointers.
Definition: Triangle2.h:447
void setPropertiesAndOppT(Point2 *pI, Point2 *pJ, Point2 *pK, Triangle2 *pNeig0, Triangle2 *pNeig1, Triangle2 *pNeig2)
Set all corners and neighbor triangles.
Definition: Triangle2.h:456
Vector2 getNormalVector(const Point2 &p0, const Point2 &p1, const Point2 &p2, bool &bOK)
Get normal vector.
void setIncidentTriangle(Triangle2 *pT)
Associate a triangle with the point.
Definition: Point2.h:437
void setVertexPointer(const int ith, Point2 *pp)
Set the i-th corner.
Definition: Triangle2.h:428
void setOppTriangle(const int ith, Triangle2 *pTriangle)
Set the i-th neighbor triangle.
Definition: Triangle2.h:336
Vector.
Definition: Vector2.h:37
Triangle2()
Constructor.
Definition: Triangle2.h:54
int getIntraTriangleIndex(const Point2 *p) const
Get the index of p in the triangle.
Definition: Triangle2.h:369
Triangle.
Definition: Triangle2.h:48
Definition: Bbox2.h:23
void setProperties(Point2 *pI, Point2 *pJ, Point2 *pK)
Set all corners.
Definition: Triangle2.h:433
Triangle2 * getOppositeTriangle(const int ith) const
Get the i-th neighbor triangle.
Definition: Triangle2.h:330
Point2 * getCorner(const int ith) const
Get the i-th corner of the triangle.
Definition: Triangle2.h:422
Point.
Definition: Point2.h:39