Fade2D Documentation pages v2.12
Point2.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 // not clear to you.
14 //
15 // Author: Bernhard Kornberger, bkorn (at) geom.at
16 // http://www.geom.at
17
19
20 #pragma once
21
22 #include "common.h"
23 #include "Vector2.h"
24
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 class Triangle2; // FWD
35
36
37
38 struct VtxColor
39 {
40  VtxColor(unsigned char r_,unsigned char g_,unsigned char b_,unsigned char a_):r(r_),g(g_),b(b_),a(a_)
41  {}
42  float r,g,b,a;
43 };
44
45
51 class CLASS_DECLSPEC Point2
52 {
53 #ifdef GEOM_DBG
54  int getSeqNum()
55  {
56  static int seqNum(0);
57  return seqNum++;
58  }
59 #endif
60
61 public:
62
63 #if GEOM_PSEUDO3D==GEOM_TRUE
70  Point2(const double x_,const double y_,const double z_):
71  coordX(x_),
72  coordY(y_),
73  coordZ(z_),
74  pAssociatedTriangle(NULL),
75  customIndex(-1)
76  {
77 #ifdef GEOM_DBG
78  customIndex=getSeqNum();
79 #endif
80  }
81
82
86  Point2():coordX(-DBL_MAX),coordY(-DBL_MAX),coordZ(-DBL_MAX),pAssociatedTriangle(NULL),customIndex(-1)
87  {
88 #ifdef GEOM_DBG
89  customIndex=getSeqNum();
90 #endif
91  }
96  Point2(const Point2& p_):
97  coordX(p_.x()),
98  coordY(p_.y()),
99  coordZ(p_.z()),
100  pAssociatedTriangle(NULL),
101  customIndex(p_.customIndex)
102  {
103  }
104
105  Point2 &operator=(const Point2& other)
106  {
107  coordX=other.x();
108  coordY=other.y();
109  coordZ=other.z();
110  pAssociatedTriangle=NULL;
111  customIndex=other.customIndex;
112  return *this;
113  }
114
115
116 // Deprecated, use setHeight() instead. Kept for backward compatibility
119  void setZ(double z)
120  {
121  setHeight(z);
122  }
123
124
127  void print()
128  {
129  std::cout<<coordX<<" "<<coordY<<" "<<coordZ<<std::endl;
130  }
131
132
133
134 #else
135
136
142  Point2(const double x_,const double y_):
143  coordX(x_),
144  coordY(y_),
145  pAssociatedTriangle(NULL),
146  customIndex(-1)
147  {
148  //BC("Point2 Constructor default");
149  }
155  coordX(-DBL_MAX),
156  coordY(-DBL_MAX),
157  pAssociatedTriangle(NULL),
158  customIndex(-1)
159  {
160  }
165  Point2(const Point2& p_):
166  coordX(p_.x()),
167  coordY(p_.y()),
168  pAssociatedTriangle(NULL),
169  customIndex(p_.customIndex)
170  {
171  }
172
173  Point2 &operator=(const Point2& other)
174  {
175  coordX=other.x();
176  coordY=other.y();
177  pAssociatedTriangle=NULL;
178  customIndex=other.customIndex;
179  return *this;
180  }
181
184  void print()
185  {
186  std::cout<<coordX<<" "<<coordY<<std::endl;
187  }
188
189 #endif
190
191
192
193
194
195  ~Point2()
196  {
197  }
198
204  double x() const
205  {
206  return coordX;
207  }
213  double y() const
214  {
215  return coordY;
216  }
217
218 #if GEOM_PSEUDO3D==GEOM_TRUE
224  double z() const
225  {
226  return coordZ;
227  }
228 #endif
229
230 #if GEOM_PSEUDO3D==GEOM_TRUE
239  void xyz(double& x_,double& y_,double& z_) const
240  {
241  x_=coordX;
242  y_=coordY;
243  z_=coordZ;
244  }
245 #endif
246
254  void xy(double& x_,double& y_) const
255  {
256  x_=coordX;
257  y_=coordY;
258  }
259
260
261 #if GEOM_PSEUDO3D==GEOM_TRUE
267  void setHeight(double z)
268  {
269  coordZ=z;
270  }
271 #endif
272
273
274
275
280  double getMaxAbs() const
281  {
282  double a(fabs(coordX));
283  double b(fabs(coordY));
284  return (std::max)(a,b);
285  }
286
287
288
289
290
291
299  bool operator<(const Point2& p) const
300  {
301  if(coordX<p.coordX) return true;
302  if(coordX>p.coordX) return false;
303  if(coordY<p.coordY) return true;
304  return false;
305  }
313  bool operator>(const Point2& p) const
314  {
315  if(coordX>p.coordX) return true;
316  if(coordX<p.coordX) return false;
317  if(coordY>p.coordY) return true;
318  return false;
319  }
327  bool operator==(const Point2& p) const
328  {
329  return (coordX==p.coordX && coordY==p.coordY);
330  }
331 #if GEOM_PSEUDO3D==GEOM_TRUE
337  bool samePoint(const Point2& p) const
338  {
339  return(coordX==p.coordX && coordY==p.coordY && coordZ==p.coordZ);
340  }
341 #endif
348  bool operator!=(const Point2& p) const
349  {
350  if(coordX!=p.coordX || coordY!=p.coordY) return true;
351  return false;
352  }
358  {
359  return pAssociatedTriangle;
360  }
361
362
363 #if GEOM_PSEUDO3D==GEOM_TRUE
374  void setCoords(const double x_,const double y_,const double z_)
375  {
376  coordX=x_;
377  coordY=y_;
378  coordZ=z_;
379  }
390  void set(const double x_,const double y_,const double z_,int customIndex_)
391  {
392  coordX=x_;
393  coordY=y_;
394  coordZ=z_;
395  pAssociatedTriangle=NULL;
396  customIndex=customIndex_;
397 #ifdef GEOM_DBG
398  customIndex=getSeqNum();
399 #endif
400  }
401 #else
410  void set(const double x_,const double y_,int customIndex_)
411  {
412  coordX=x_;
413  coordY=y_;
414  pAssociatedTriangle=NULL;
415  customIndex=customIndex_;
416 #ifdef GEOM_DBG
417  customIndex=getSeqNum();
418 #endif
419  }
420  void change(const double x_,const double y_)
421  {
422  coordX=x_;
423  coordY=y_;
424  }
425 #endif
426
431 #if GEOM_PSEUDO3D==GEOM_TRUE
432  void set(const Point2& pnt)
433  {
434  coordX=pnt.x();
435  coordY=pnt.y();
436  coordZ=pnt.z();
437  pAssociatedTriangle=NULL;
438  this->customIndex=pnt.customIndex;
439  }
440 #else
441  void set(const Point2& pnt)
442  {
443  coordX=pnt.x();
444  coordY=pnt.y();
445  pAssociatedTriangle=NULL;
446  this->customIndex=pnt.customIndex;
447  }
448 #endif
449
450
451
467  void setCustomIndex(int customIndex_)
468  {
469  customIndex=customIndex_;
470  }
484  int getCustomIndex() const
485  {
486  return customIndex;
487  }
488
494  {
495  pAssociatedTriangle=pT;
496  }
497
500 Vector2 operator-(const Point2& other) const
501 {
502 #if GEOM_PSEUDO3D==GEOM_TRUE
503  double xdiff(x()-other.x());
504  double ydiff(y()-other.y());
505  double zdiff(z()-other.z());
506  return Vector2(xdiff,ydiff,zdiff);
507 #else
508  double xdiff(x()-other.x());
509  double ydiff(y()-other.y());
510  return Vector2(xdiff,ydiff);
511 #endif
512
513
514 }
515
518 Point2 operator+(const Vector2& vec) const
519 {
520 #if GEOM_PSEUDO3D==GEOM_TRUE
521  return Point2(x()+vec.x(),y()+vec.y(),z()+vec.z());
522 #else
523  return Point2(x()+vec.x(),y()+vec.y());
524 #endif
525 }
526
529 Point2 operator-(const Vector2& vec) const
530 {
531 #if GEOM_PSEUDO3D==GEOM_TRUE
532  return Point2(x()-vec.x(),y()-vec.y(),z()-vec.z());
533 #else
534  return Point2(x()-vec.x(),y()-vec.y());
535 #endif
536 }
537
538
539  friend std::ostream &operator<<(std::ostream &stream, const Point2& pnt);
540  friend std::istream &operator>>(std::istream &stream, Point2& pnt);
541
542
543 protected:
544 friend class Dt2;
545  double coordX;
546  double coordY;
547 #if GEOM_PSEUDO3D==GEOM_TRUE
548  double coordZ;
549 #endif
550  Triangle2* pAssociatedTriangle;
551  int customIndex;
552 }; // End of class
553
555 inline std::ostream &operator<<(std::ostream &stream, const Point2& pnt)
556 {
557 #if GEOM_PSEUDO3D==GEOM_TRUE
558  stream << "Point2 ("<<&pnt<<",ci="<<pnt.customIndex<<"): "<<pnt.x()<<", "<<pnt.y()<<", "<<pnt.z();
559  //#ifndef NDEBUG
560  //stream<<", anyT="<<pnt.getIncidentTriangle();
561  //#endif
562 #else
563  stream << "Point2 ("<<&pnt<<","<<pnt.customIndex<<"): "<<pnt.x()<<", "<<pnt.y();
564 #endif
565
566
567  return stream;
568 }
569
571 inline std::istream &operator>>(std::istream &stream, Point2& pnt)
572 {
573 #if GEOM_PSEUDO3D==GEOM_TRUE
574  stream >> pnt.coordX >> pnt.coordY >> pnt.coordZ;
575 #else
576  stream >> pnt.coordX >> pnt.coordY;
577 #endif
578  return stream;
579 }
580
581 // Free functions
582
589 inline
590 double sqDistance2D(const Point2& p0,const Point2& p1)
591 {
592  double deltaX=p1.x()-p0.x();
593  double deltaY=p1.y()-p0.y();
594  return (deltaX*deltaX+deltaY*deltaY);
595 }
596
597
604 inline
605 double sqDistance2D(const double x0,const double y0,const Point2& p1)
606 {
607  double deltaX=p1.x()-x0;
608  double deltaY=p1.y()-y0;
609  return (deltaX*deltaX+deltaY*deltaY);
610 }
611
612 #if GEOM_PSEUDO3D==GEOM_TRUE
613
618 inline
619 double sqDistance25D(const Point2& p0,const Point2& p1)
620 {
621  double deltaX=p1.x()-p0.x();
622  double deltaY=p1.y()-p0.y();
623  double deltaZ=p1.z()-p0.z();
624  double result(deltaX*deltaX+deltaY*deltaY+deltaZ*deltaZ);
625
626  if(result!=result)
627  {
628  std::cerr<<"warning, sqDistance25D, nan value, no distance"<<std::endl;
629  std::cerr<<"p0="<<p0<<std::endl;
630  std::cerr<<"p1="<<p1<<std::endl;
631  }
632
633  return result;
634 }
635
636
641 inline
642 double sqDistance25D(const double x0,const double y0,const double z0,const Point2& p1)
643 {
644  double deltaX=p1.x()-x0;
645  double deltaY=p1.y()-y0;
646  double deltaZ=p1.z()-z0;
647  double result(deltaX*deltaX+deltaY*deltaY+deltaZ*deltaZ);
648
649  if(result!=result)
650  {
651  std::cerr<<"warning, sqDistance25D, nan value, no distance"<<std::endl;
652  std::cerr<<"x0="<<x0<<", y0="<<y0<<", z0="<<z0<<std::endl;
653  std::cerr<<"p1="<<p1<<std::endl;
654  }
655
656  return result;
657
658 }
659
660
661 #endif
662
663
664
671 inline
672 Point2 center(const Point2& p0,const Point2& p1)
673 {
674 #if GEOM_PSEUDO3D==GEOM_TRUE
675  Point2 center((p0.x()+p1.x())/2.0,(p0.y()+p1.y())/2.0,(p0.z()+p1.z())/2.0);
676
677 #else
678  Point2 center((p0.x()+p1.x())/2.0,(p0.y()+p1.y())/2.0);
679 #endif
680  return center;
681 }
682
694 CLASS_DECLSPEC
695 Point2 centerWithShift(const Point2& p0,const Point2& p1);
696
697 #if GEOM_PSEUDO3D==GEOM_TRUE
699 struct CLASS_DECLSPEC Func_ltPointXYZ
700 {
701  bool operator()(const Point2& p0,const Point2& p1) const
702  {
703  if(p0.x()<p1.x()) return true;
704  if(p0.x()>p1.x()) return false;
705  if(p0.y()<p1.y()) return true;
706  if(p0.y()>p1.y()) return false;
707  if(p0.z()<p1.z()) return true;
708  return false;
709  }
710 };
711
712 #endif
713
714 } // (namespace)
std::ostream & operator<<(std::ostream &stream, const Bbox2 &pC)
Print the box.
Definition: Bbox2.h:492
std::istream & operator>>(std::istream &stream, Point2 &pnt)
Stream-to-Point.
Definition: Point2.h:571
double sqDistance2D(const Point2 &p0, const Point2 &p1)
Get the squared distance between two points in 2D.
Definition: Point2.h:590
Point2 centerWithShift(const Point2 &p0, const Point2 &p1)
Compute the midpoint of p0 and p1 and adapt it.
Point2 center(const Point2 &p0, const Point2 &p1)
Compute the midpoint of p0 and p1.
Definition: Point2.h:672
Point.
Definition: Point2.h:52
void set(const Point2 &pnt)
Set the coordiantes.
Definition: Point2.h:441
Point2(const Point2 &p_)
Copy constructor.
Definition: Point2.h:165
Point2()
Default constructor.
Definition: Point2.h:154
void xy(double &x_, double &y_) const
Get the x- and y-coordinate.
Definition: Point2.h:254
void setCustomIndex(int customIndex_)
Set a custom index.
Definition: Point2.h:467
void set(const double x_, const double y_, int customIndex_)
Set the coordinates and customIndex.
Definition: Point2.h:410
double getMaxAbs() const
Get max(abs(x),abs(y))
Definition: Point2.h:280
int getCustomIndex() const
Get the custom index.
Definition: Point2.h:484
bool operator==(const Point2 &p) const
Equality operator.
Definition: Point2.h:327
bool operator>(const Point2 &p) const
Greater than operator.
Definition: Point2.h:313
Vector2 operator-(const Point2 &other) const
Returns a vector from other to *this.
Definition: Point2.h:500
void setIncidentTriangle(Triangle2 *pT)
Associate a triangle with the point.
Definition: Point2.h:493
bool operator<(const Point2 &p) const
Less than operator.
Definition: Point2.h:299
bool operator!=(const Point2 &p) const
Inequality operator.
Definition: Point2.h:348
Point2 operator-(const Vector2 &vec) const
Subtract vector from point.
Definition: Point2.h:529
Triangle2 * getIncidentTriangle() const
Get the associated triangle.
Definition: Point2.h:357
double x() const
Get the x-coordinate.
Definition: Point2.h:204
double y() const
Get the y-coordinate.
Definition: Point2.h:213
Point2 operator+(const Vector2 &vec) const
Add vector and point.
Definition: Point2.h:518
Point2(const double x_, const double y_)
Constructor.
Definition: Point2.h:142
Triangle.
Definition: Triangle2.h:60
Vector.
Definition: Vector2.h:42
double y() const
Get the y-value.
double x() const
Get the x-value.
Definition: Point2.h:39