Geom Software - C++ Programming and Geometry Libraries
Fade2.5D Documentation pages v1.74
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 
136 
137 
138 #endif
139 
152 double getInteriorAngle2D(int ith) const;
153 
154 #if GEOM_PSEUDO3D==GEOM_TRUE
155 
159 double getInteriorAngle25D(int ith) const;
160 #endif
161 
162 
163 
176  double getArea2D() const;
177 
178 
179 
180 
181 #if GEOM_PSEUDO3D==GEOM_TRUE
182 
189  double getArea25D() const;
190 #endif
191 
192 
206  Triangle2* getOppositeTriangle(const int ith) const;
207 
208 
220  int getIntraTriangleIndex(const Point2* p) const;
233  int getIntraTriangleIndex(const Triangle2* pTriangle) const;
234 
240  int getIntraTriangleIndex(const Point2* p0,const Point2* p1) const;
241 
242 
244  //*
245  //*
246  //* Internal use
247 //*/
248  //bool getState() const;
249 
257  double getSquaredEdgeLength2D(int ith) const;
258 
261  double getSquaredEdgeLength(int ith) const; // Deprecated, use getSquaredEdgeLength2D()
262 
263 #if GEOM_PSEUDO3D==GEOM_TRUE
264 
268  double getSquaredEdgeLength25D(int ith) const;
269 #endif
270 //** \brief Method for internal use
271  //*
272  //* Internal use
273 //*/
274  //void setState(bool bState_);
284  void setOppTriangle(const int ith, Triangle2* pTriangle);
287  void setProperties( Point2* pI, Point2* pJ, Point2* pK);
288 
291  void clearProperties();
292 
295  void setPropertiesAndOppT(Point2* pI, Point2* pJ, Point2* pK,Triangle2* pNeig0,Triangle2* pNeig1,Triangle2* pNeig2);
296 
299  void setVertexPointer(const int ith, Point2* pp);
300 
305  bool hasVertex(Point2* pVtx) const;
306 
311  bool hasVertex(const Point2& vtx) const;
312 
317  bool hasOnEdge(int i,const Point2& q) const;
318 
321  int getMaxIndex() const;
322 
325  int getMinIndex() const;
326 
329  double getMaxSqEdgeLen2D() const;
330 
331 
332  // DEBUG
333  CLASS_DECLSPEC
334  friend std::ostream &operator<<(std::ostream &stream, const Triangle2& c);
335  friend inline void registerTriangles(Triangle2* fromTriangle,int ith,Triangle2* toTriangle,int jth);
336 protected:
337  double computeArea(double l0,double l1,double l2) const;
338  bool isAccurateCC(int maxIdx,const Point2& cc) const;
339  bool getCC_strategy1(double avgOffX,double avgOffY,Point2& cc) const;
340  void getCC_strategy2(int maxIdx,double avgOffX,double avgOffY,Point2& cc) const;
341  void getCommonOffset(double& x,double& y) const;
342  Point2* aVertexPointer[3];
343  Triangle2* aOppTriangles[3];
344  //bool bState;
345 };
346 
347 namespace{
348 inline bool checkRange(int ith)
349 {
350  return (ith==0 || ith==1 || ith==2); // true if ith={0,1,2}
351 }
352 
353 }
354 inline Triangle2* Triangle2::getOppositeTriangle(const int ith) const
355 {
356  assert(checkRange(ith));
357  return aOppTriangles[ith];
358 }
359 
360 inline void Triangle2::setOppTriangle(const int ith, Triangle2* pNeig)
361 {
362  //if(!checkRange(ith))
363  //{
364  //std::cout<<"ith="<<ith<<std::endl;
365  //int* a(NULL);
366  //*a+=1;
367  //}
368 
369  assert(checkRange(ith));
370  aOppTriangles[ith]=pNeig;
371 }
372 
373 
374 inline int Triangle2::getIntraTriangleIndex(const Point2* p0,const Point2* p1) const
375 {
376  for(int i=0;i<3;++i)
377  {
378  int ici1((i+1)%3);
379  int ici2((i+2)%3);
380 
381  if( aVertexPointer[ici1]==p0 && aVertexPointer[ici2]==p1) return i;
382  if( aVertexPointer[ici1]==p1 && aVertexPointer[ici2]==p0) return i;
383  }
384 
385  std::cout<<"BUG: Triangle2::getIntraTriangleIndex failed for"<<std::endl;// COUTOK
386  std::cout<<*p0<<std::endl;// COUTOK
387  std::cout<<*p1<<std::endl;// COUTOK
388  std::cout<<*this<<std::endl;// COUTOK
389  assert(false);
390  return -1;
391 }
392 
393 inline int Triangle2::getIntraTriangleIndex(const Point2* pVtx) const
394 {
395 #ifndef NDEBUG
396  // Just debug code
397  for(int i=0;i<3;++i)
398  {
399  if(aVertexPointer[i]==pVtx)
400  {
401  return i;
402  }
403  }
404  assert(false);
405 #endif
406  return ( (aVertexPointer[1]==pVtx) + 2*(aVertexPointer[2]==pVtx));
407 }
408 
409 
410 inline int Triangle2::getIntraTriangleIndex(const Triangle2* pTriangle) const
411 {
412 #ifndef NDEBUG
413  // Just debug code
414  for(int i=0;i<3;++i)
415  {
416  if(aOppTriangles[i]==pTriangle)
417  {
418  return i;
419  }
420  }
421  assert(false);
422 #endif
423  return ( (aOppTriangles[1]==pTriangle) + 2*(aOppTriangles[2]==pTriangle));
424 }
425 
426 //inline int Triangle2::getIntraTriangleIndex(const Triangle2* pTriangle) const
427 //{
428 
429  //if(getOppositeTriangle(0)==pTriangle) return 0;
430  //if(getOppositeTriangle(1)==pTriangle) return 1;
431 
432 //#ifndef NDEBUG
433  //if(getOppositeTriangle(2)!=pTriangle)
434  //{
435  //std::cout<<"Triangle2::getIntraTriangleIndex, pTriangle is not a neighbor of the current triangle"<<std::endl;// COUTOK
436  //std::cout<<"Current triangle: "<<*this<<std::endl;// COUTOK
437  //std::cout<<"pTriangle: "<<*pTriangle<<std::endl;// COUTOK
438 
439 
440  //assert(false);
441  //}
442 //#endif
443  //return 2;
444 //}
445 
446 inline Point2* Triangle2::getCorner(const int ith) const
447 {
448  assert(checkRange(ith));
449  return aVertexPointer[ith];
450 }
451 
452 inline void Triangle2::setVertexPointer(const int ith, Point2* pp)
453 {
454  aVertexPointer[ith]=pp;
455 }
456 
457 inline void Triangle2::setProperties( Point2* pI, Point2* pJ, Point2* pK)
458 {
459  assert((pI!=NULL && pJ!=NULL && pK!=NULL));
460  aVertexPointer[0]=pI;
461  aVertexPointer[1]=pJ;
462  aVertexPointer[2]=pK;
463  pI->setIncidentTriangle(this);
464  pJ->setIncidentTriangle(this);
465  pK->setIncidentTriangle(this);
466  aOppTriangles[0]=NULL;
467  aOppTriangles[1]=NULL;
468  aOppTriangles[2]=NULL;
469 }
470 
472 {
473  for(int i=0;i<3;++i)
474  {
475  aVertexPointer[i]=NULL;
476  aOppTriangles[i]=NULL;
477  }
478 }
479 
481  Point2* pI, Point2* pJ, Point2* pK,
482  Triangle2* pNeig0,Triangle2* pNeig1,Triangle2* pNeig2
483  )
484 {
485  assert((pI!=NULL && pJ!=NULL && pK!=NULL));
486  aVertexPointer[0]=pI;
487  aVertexPointer[1]=pJ;
488  aVertexPointer[2]=pK;
489  pI->setIncidentTriangle(this);
490  pJ->setIncidentTriangle(this);
491  pK->setIncidentTriangle(this);
492  aOppTriangles[0]=pNeig0;
493  aOppTriangles[1]=pNeig1;
494  aOppTriangles[2]=pNeig2;
495 }
496 
497 inline void registerTriangles(Triangle2* pFromT,int ith,Triangle2* pToT,int jth)
498 {
499  assert(checkRange(ith));
500  assert(checkRange(jth));
501 
502  pFromT->aOppTriangles[ith]=pToT;
503  pToT->aOppTriangles[jth]=pFromT;
504 
505 }
506 
507 
508 
509 } // (namespace)
void clearProperties()
Clear all corners and neighbor pointers.
Definition: Triangle2.h:471
void setPropertiesAndOppT(Point2 *pI, Point2 *pJ, Point2 *pK, Triangle2 *pNeig0, Triangle2 *pNeig1, Triangle2 *pNeig2)
Set all corners and neighbor triangles.
Definition: Triangle2.h:480
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:474
void setVertexPointer(const int ith, Point2 *pp)
Set the i-th corner.
Definition: Triangle2.h:452
void setOppTriangle(const int ith, Triangle2 *pTriangle)
Set the i-th neighbor triangle.
Definition: Triangle2.h:360
Vector.
Definition: Vector2.h:34
Triangle2()
Constructor.
Definition: Triangle2.h:54
int getIntraTriangleIndex(const Point2 *p) const
Get the index of p in the triangle.
Definition: Triangle2.h:393
Triangle.
Definition: Triangle2.h:48
Definition: Bbox2.h:23
void setProperties(Point2 *pI, Point2 *pJ, Point2 *pK)
Set all corners.
Definition: Triangle2.h:457
Triangle2 * getOppositeTriangle(const int ith) const
Get the i-th neighbor triangle.
Definition: Triangle2.h:354
Point2 * getCorner(const int ith) const
Get the i-th corner of the triangle.
Definition: Triangle2.h:446
Point.
Definition: Point2.h:39