Geom Software - C++ Programming and Geometry Libraries
Fade2D Documentation pages v1.63
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 getSquaredEdgeLength2D(int ith) const;
255 
258  double getSquaredEdgeLength(int ith) const; // Deprecated, use getSquaredEdgeLength2D()
259 
260 #if GEOM_PSEUDO3D==GEOM_TRUE
261 
265  double getSquaredEdgeLength25D(int ith) const;
266 #endif
267 //** \brief Method for internal use
268  //*
269  //* Internal use
270 //*/
271  //void setState(bool bState_);
281  void setOppTriangle(const int ith, Triangle2* pTriangle);
284  void setProperties( Point2* pI, Point2* pJ, Point2* pK);
285 
288  void clearProperties();
289 
292  void setPropertiesAndOppT(Point2* pI, Point2* pJ, Point2* pK,Triangle2* pNeig0,Triangle2* pNeig1,Triangle2* pNeig2);
293 
296  void setVertexPointer(const int ith, Point2* pp);
297 
302  bool hasVertex(Point2* pVtx) const;
303 
308  bool hasOnEdge(int i,const Point2& q) const;
309 
312  int getMaxIndex() const;
313 
316  int getMinIndex() const;
317 
320  double getMaxSqEdgeLen2D() const;
321 
322 
323  // DEBUG
324  CLASS_DECLSPEC
325  friend std::ostream &operator<<(std::ostream &stream, const Triangle2& c);
326  friend inline void registerTriangles(Triangle2* fromTriangle,int ith,Triangle2* toTriangle,int jth);
327 protected:
328  double computeArea(double l0,double l1,double l2) const;
329  bool isAccurateCC(int maxIdx,const Point2& cc) const;
330  bool getCC_strategy1(double avgOffX,double avgOffY,Point2& cc) const;
331  void getCC_strategy2(int maxIdx,double avgOffX,double avgOffY,Point2& cc) const;
332  void getCommonOffset(double& x,double& y) const;
333  Point2* aVertexPointer[3];
334  Triangle2* aOppTriangles[3];
335  //bool bState;
336 };
337 
338 namespace{
339 inline bool checkRange(int ith)
340 {
341  return (ith==0 || ith==1 || ith==2); // true if ith={0,1,2}
342 }
343 
344 }
345 inline Triangle2* Triangle2::getOppositeTriangle(const int ith) const
346 {
347  assert(checkRange(ith));
348  return aOppTriangles[ith];
349 }
350 
351 inline void Triangle2::setOppTriangle(const int ith, Triangle2* pNeig)
352 {
353  //if(!checkRange(ith))
354  //{
355  //std::cout<<"ith="<<ith<<std::endl;
356  //int* a(NULL);
357  //*a+=1;
358  //}
359 
360  assert(checkRange(ith));
361  aOppTriangles[ith]=pNeig;
362 }
363 
364 
365 inline int Triangle2::getIntraTriangleIndex(const Point2* p0,const Point2* p1) const
366 {
367  for(int i=0;i<3;++i)
368  {
369  int ici1((i+1)%3);
370  int ici2((i+2)%3);
371 
372  if( aVertexPointer[ici1]==p0 && aVertexPointer[ici2]==p1) return i;
373  if( aVertexPointer[ici1]==p1 && aVertexPointer[ici2]==p0) return i;
374  }
375 
376  std::cout<<"BUG: Triangle2::getIntraTriangleIndex failed for"<<std::endl;// COUTOK
377  std::cout<<*p0<<std::endl;// COUTOK
378  std::cout<<*p1<<std::endl;// COUTOK
379  std::cout<<*this<<std::endl;// COUTOK
380  assert(false);
381  return -1;
382 }
383 
384 inline int Triangle2::getIntraTriangleIndex(const Point2* pVtx) const
385 {
386 #ifndef NDEBUG
387  // Just debug code
388  for(int i=0;i<3;++i)
389  {
390  if(aVertexPointer[i]==pVtx)
391  {
392  return i;
393  }
394  }
395  assert(false);
396 #endif
397  return ( (aVertexPointer[1]==pVtx) + 2*(aVertexPointer[2]==pVtx));
398 }
399 
400 
401 inline int Triangle2::getIntraTriangleIndex(const Triangle2* pTriangle) const
402 {
403 #ifndef NDEBUG
404  // Just debug code
405  for(int i=0;i<3;++i)
406  {
407  if(aOppTriangles[i]==pTriangle)
408  {
409  return i;
410  }
411  }
412  assert(false);
413 #endif
414  return ( (aOppTriangles[1]==pTriangle) + 2*(aOppTriangles[2]==pTriangle));
415 }
416 
417 //inline int Triangle2::getIntraTriangleIndex(const Triangle2* pTriangle) const
418 //{
419 
420  //if(getOppositeTriangle(0)==pTriangle) return 0;
421  //if(getOppositeTriangle(1)==pTriangle) return 1;
422 
423 //#ifndef NDEBUG
424  //if(getOppositeTriangle(2)!=pTriangle)
425  //{
426  //std::cout<<"Triangle2::getIntraTriangleIndex, pTriangle is not a neighbor of the current triangle"<<std::endl;// COUTOK
427  //std::cout<<"Current triangle: "<<*this<<std::endl;// COUTOK
428  //std::cout<<"pTriangle: "<<*pTriangle<<std::endl;// COUTOK
429 
430 
431  //assert(false);
432  //}
433 //#endif
434  //return 2;
435 //}
436 
437 inline Point2* Triangle2::getCorner(const int ith) const
438 {
439  assert(checkRange(ith));
440  return aVertexPointer[ith];
441 }
442 
443 inline void Triangle2::setVertexPointer(const int ith, Point2* pp)
444 {
445  aVertexPointer[ith]=pp;
446 }
447 
448 inline void Triangle2::setProperties( Point2* pI, Point2* pJ, Point2* pK)
449 {
450  assert((pI!=NULL && pJ!=NULL && pK!=NULL));
451  aVertexPointer[0]=pI;
452  aVertexPointer[1]=pJ;
453  aVertexPointer[2]=pK;
454  pI->setIncidentTriangle(this);
455  pJ->setIncidentTriangle(this);
456  pK->setIncidentTriangle(this);
457  aOppTriangles[0]=NULL;
458  aOppTriangles[1]=NULL;
459  aOppTriangles[2]=NULL;
460 }
461 
462 inline void Triangle2::clearProperties()
463 {
464  for(int i=0;i<3;++i)
465  {
466  aVertexPointer[i]=NULL;
467  aOppTriangles[i]=NULL;
468  }
469 }
470 
471 inline void Triangle2::setPropertiesAndOppT(
472  Point2* pI, Point2* pJ, Point2* pK,
473  Triangle2* pNeig0,Triangle2* pNeig1,Triangle2* pNeig2
474  )
475 {
476  assert((pI!=NULL && pJ!=NULL && pK!=NULL));
477  aVertexPointer[0]=pI;
478  aVertexPointer[1]=pJ;
479  aVertexPointer[2]=pK;
480  pI->setIncidentTriangle(this);
481  pJ->setIncidentTriangle(this);
482  pK->setIncidentTriangle(this);
483  aOppTriangles[0]=pNeig0;
484  aOppTriangles[1]=pNeig1;
485  aOppTriangles[2]=pNeig2;
486 }
487 
488 inline void registerTriangles(Triangle2* pFromT,int ith,Triangle2* pToT,int jth)
489 {
490  assert(checkRange(ith));
491  assert(checkRange(jth));
492 
493  pFromT->aOppTriangles[ith]=pToT;
494  pToT->aOppTriangles[jth]=pFromT;
495 
496 }
497 
498 
499 
500 } // (namespace)
Point.
Definition: Point2.h:39
Triangle2()
Constructor.
Definition: Triangle2.h:54
Vector.
Definition: Vector2.h:37
void setIncidentTriangle(Triangle2 *pT)
Associate a triangle with the point.
Definition: Point2.h:437
Triangle.
Definition: Triangle2.h:48
Definition: Bbox2.h:25