Geom Software - C++ Programming and Geometry Libraries
Fade2D Documentation pages v1.74
Point2.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 #pragma once
19 
20 #include "common.h"
21 #include "Vector2.h"
22 
23 #if GEOM_PSEUDO3D==GEOM_TRUE
24  namespace GEOM_FADE25D {
25 #elif GEOM_PSEUDO3D==GEOM_FALSE
26  namespace GEOM_FADE2D {
27 #else
28  #error GEOM_PSEUDO3D is not defined
29 #endif
30 
31 
32 class Triangle2; // FWD
33 extern bool benchCounter(const std::string& str);
39 class Point2
40 {
41 #ifdef GEOM_DBG
42  int getSeqNum()
43  {
44  static int seqNum(0);
45  //if(seqNum+1==156219)
46  //{
47  //int* nil(NULL);
48  //++*nil;
49  //}
50  return seqNum++;
51 
52  }
53 #endif
54 
55 public:
56 
57 #if GEOM_PSEUDO3D==GEOM_TRUE
58 
64  CLASS_DECLSPEC
65  Point2(const double x_,const double y_,const double z_):
66  coordX(x_),
67  coordY(y_),
68  coordZ(z_),
69  pAssociatedTriangle(NULL),
70  customIndex(-1)
71  {
72 #ifdef GEOM_DBG
73  customIndex=getSeqNum();
74 #endif
75  }
76 
77 
81  CLASS_DECLSPEC
82  Point2():coordX(-DBL_MAX),coordY(-DBL_MAX),coordZ(-DBL_MAX),pAssociatedTriangle(NULL),customIndex(-1)
83  {
84 #ifdef GEOM_DBG
85  customIndex=getSeqNum();
86 #endif
87  }
92  CLASS_DECLSPEC
93  Point2(const Point2& p_):
94  coordX(p_.x()),
95  coordY(p_.y()),
96  coordZ(p_.z()),
97  pAssociatedTriangle(NULL),
98  customIndex(p_.customIndex)
99  {
100  }
101 
102  CLASS_DECLSPEC
103  Point2 &operator=(const Point2& other)
104  {
105 //std::cout<<"Operator="<<std::endl;
106  coordX=other.x();
107  coordY=other.y();
108  coordZ=other.z();
109  pAssociatedTriangle=NULL;
110  customIndex=other.customIndex;
111  return *this;
112  }
113 
118 #if GEOM_PSEUDO3D==GEOM_TRUE
119  void print()
120  {
121  std::cout<<coordX<<" "<<coordY<<" "<<coordZ<<std::endl;
122  }
123 #else
124  void print()
125  {
126  std::cout<<coordX<<" "<<coordY<<std::endl;
127  }
128 #endif
129 
130 // Deprecated and undocumented, use setHeight() instead. Kept for backward compatibility
134  CLASS_DECLSPEC
135  void setZ(double z)
136  {
137  setHeight(z);
138  }
139 
140 #else
141 
147  CLASS_DECLSPEC
148  Point2(const double x_,const double y_):
149  coordX(x_),
150  coordY(y_),
151  pAssociatedTriangle(NULL),
152  customIndex(-1)
153  {
154  //BC("Point2 Constructor default");
155  }
160  CLASS_DECLSPEC
162  coordX(-DBL_MAX),
163  coordY(-DBL_MAX),
164  pAssociatedTriangle(NULL),
165  customIndex(-1)
166  {
167  }
172  CLASS_DECLSPEC
173  Point2(const Point2& p_):
174  coordX(p_.x()),
175  coordY(p_.y()),
176  pAssociatedTriangle(NULL),
177  customIndex(p_.customIndex)
178  {
179  }
180 #endif
181 
182 
183 
184 
185  CLASS_DECLSPEC
186  ~Point2()
187  {
188  }
189 
194  CLASS_DECLSPEC
195  double x() const
196  {
197  return coordX;
198  }
203  CLASS_DECLSPEC
204  double y() const
205  {
206  return coordY;
207  }
208 
209 #if GEOM_PSEUDO3D==GEOM_TRUE
210 
214  CLASS_DECLSPEC
215  double z() const
216  {
217  return coordZ;
218  }
219 #endif
220 
221 #if GEOM_PSEUDO3D==GEOM_TRUE
222 
229  CLASS_DECLSPEC
230  void xyz(double& x_,double& y_,double& z_) const
231  {
232  x_=coordX;
233  y_=coordY;
234  z_=coordZ;
235  }
236 #endif
237 
244  CLASS_DECLSPEC
245  void xy(double& x_,double& y_) const
246  {
247  x_=coordX;
248  y_=coordY;
249  }
250 
251 
252 #if GEOM_PSEUDO3D==GEOM_TRUE
253 
257  CLASS_DECLSPEC
258  void setHeight(double z)
259  {
260  coordZ=z;
261  }
262 #endif
263 
264 
265 
266 
270  CLASS_DECLSPEC
271  double getMaxAbs() const
272  {
273  double x(fabs(coordX));
274  double y(fabs(coordY));
275  bool b(x>=y);
276  return b*x+(!b)*y;
277  }
278 
279 
280 
281 
282 
283 
290  CLASS_DECLSPEC
291  bool operator<(const Point2& p) const
292  {
293  if(coordX<p.coordX) return true;
294  if(coordX>p.coordX) return false;
295  if(coordY<p.coordY) return true;
296  return false;
297  }
304  CLASS_DECLSPEC
305  bool operator>(const Point2& p) const
306  {
307  if(coordX>p.coordX) return true;
308  if(coordX<p.coordX) return false;
309  if(coordY>p.coordY) return true;
310  return false;
311  }
319  CLASS_DECLSPEC
320  bool operator==(const Point2& p) const
321  {
322  return (coordX==p.coordX && coordY==p.coordY);
323  }
324 #if GEOM_PSEUDO3D==GEOM_TRUE
325 
330  CLASS_DECLSPEC
331  bool samePoint(const Point2& p) const
332  {
333  return(coordX==p.coordX && coordY==p.coordY && coordZ==p.coordZ);
334  }
335 #endif
336 
342  CLASS_DECLSPEC
343  bool operator!=(const Point2& p) const
344  {
345  if(coordX!=p.coordX || coordY!=p.coordY) return true;
346  return false;
347  }
352  CLASS_DECLSPEC
354  {
355  return pAssociatedTriangle;
356  }
357 
358 
359 #if GEOM_PSEUDO3D==GEOM_TRUE
360 
371  void set(const double x_,const double y_,const double z_,int customIndex_)
372  {
373  coordX=x_;
374  coordY=y_;
375  coordZ=z_;
376  pAssociatedTriangle=NULL;
377  customIndex=customIndex_;
378 #ifdef GEOM_DBG
379  customIndex=getSeqNum();
380 #endif
381  }
382 #else
383 
391  void set(const double x_,const double y_,int customIndex_)
392  {
393  coordX=x_;
394  coordY=y_;
395  pAssociatedTriangle=NULL;
396  customIndex=customIndex_;
397 #ifdef GEOM_DBG
398  customIndex=getSeqNum();
399 #endif
400  }
401  void change(const double x_,const double y_)
402  {
403  coordX=x_;
404  coordY=y_;
405  }
406 #endif
407 
412 #if GEOM_PSEUDO3D==GEOM_TRUE
413  void set(const Point2& pnt)
414  {
415  coordX=pnt.x();
416  coordY=pnt.y();
417  coordZ=pnt.z();
418  pAssociatedTriangle=NULL;
419  this->customIndex=pnt.customIndex;
420  }
421 #else
422  void set(const Point2& pnt)
423  {
424  coordX=pnt.x();
425  coordY=pnt.y();
426  pAssociatedTriangle=NULL;
427  this->customIndex=pnt.customIndex;
428  }
429 #endif
430 
431 
432 
447  CLASS_DECLSPEC
448  void setCustomIndex(int customIndex_)
449  {
450  customIndex=customIndex_;
451  }
464  CLASS_DECLSPEC
465  int getCustomIndex() const
466  {
467  return customIndex;
468  }
469 
475  {
476  pAssociatedTriangle=pT;
477  }
478 
481 Vector2 operator-(const Point2& other) const
482 {
483 #if GEOM_PSEUDO3D==GEOM_TRUE
484  double xdiff(x()-other.x());
485  double ydiff(y()-other.y());
486  double zdiff(z()-other.z());
487  return Vector2(xdiff,ydiff,zdiff);
488 #else
489  double xdiff(x()-other.x());
490  double ydiff(y()-other.y());
491  return Vector2(xdiff,ydiff);
492 #endif
493 
494 
495 }
496 
499 Point2 operator+(const Vector2& vec) const
500 {
501 #if GEOM_PSEUDO3D==GEOM_TRUE
502  return Point2(x()+vec.x(),y()+vec.y(),z()+vec.z());
503 #else
504  return Point2(x()+vec.x(),y()+vec.y());
505 #endif
506 }
507 
510 Point2 operator-(const Vector2& vec) const
511 {
512 #if GEOM_PSEUDO3D==GEOM_TRUE
513  return Point2(x()-vec.x(),y()-vec.y(),z()-vec.z());
514 #else
515  return Point2(x()-vec.x(),y()-vec.y());
516 #endif
517 }
518 
519 
520  friend std::ostream &operator<<(std::ostream &stream, const Point2& pnt);
521  friend std::istream &operator>>(std::istream &stream, Point2& pnt);
522 
523 
524 protected:
525 friend class Dt2;
526  double coordX;
527  double coordY;
528 #if GEOM_PSEUDO3D==GEOM_TRUE
529  double coordZ;
530 #endif
531  Triangle2* pAssociatedTriangle;
532  int customIndex;
533 };
534 
535 
536 inline std::ostream &operator<<(std::ostream &stream, const Point2& pnt)
537 {
538 #if GEOM_PSEUDO3D==GEOM_TRUE
539  stream << "Point2 ("<<&pnt<<","<<pnt.customIndex<<"): "<<pnt.x()<<", "<<pnt.y()<<", "<<pnt.z();
540  //#ifndef NDEBUG
541  //stream<<", anyT="<<pnt.getIncidentTriangle();
542  //#endif
543 #else
544  stream << "Point2 ("<<&pnt<<","<<pnt.customIndex<<"): "<<pnt.x()<<", "<<pnt.y();
545 #endif
546 
547 
548  return stream;
549 }
550 
551 inline std::istream &operator>>(std::istream &stream, Point2& pnt)
552 {
553 #if GEOM_PSEUDO3D==GEOM_TRUE
554  stream >> pnt.coordX >> pnt.coordY >> pnt.coordZ;
555 #else
556  stream >> pnt.coordX >> pnt.coordY;
557 #endif
558  return stream;
559 }
560 
561 // Free functions
562 
567 inline
568 double sqDistance2D(const Point2& p0,const Point2& p1)
569 {
570  double deltaX=p1.x()-p0.x();
571  double deltaY=p1.y()-p0.y();
572  return (deltaX*deltaX+deltaY*deltaY);
573 }
574 
575 
580 inline
581 double sqDistance2D(const double x0,const double y0,const Point2& p1)
582 {
583  double deltaX=p1.x()-x0;
584  double deltaY=p1.y()-y0;
585  return (deltaX*deltaX+deltaY*deltaY);
586 }
587 
588 #if GEOM_PSEUDO3D==GEOM_TRUE
589 
594 inline
595 double sqDistance25D(const Point2& p0,const Point2& p1)
596 {
597  double deltaX=p1.x()-p0.x();
598  double deltaY=p1.y()-p0.y();
599  double deltaZ=p1.z()-p0.z();
600  double result(deltaX*deltaX+deltaY*deltaY+deltaZ*deltaZ);
601 
602  if(result!=result)
603  {
604  std::cerr<<"warning, sqDistance25D, nan value, no distance"<<std::endl;
605  std::cerr<<"p0="<<p0<<std::endl;
606  std::cerr<<"p1="<<p1<<std::endl;
607  }
608 
609  return result;
610 }
611 
612 
617 inline
618 double sqDistance25D(const double x0,const double y0,const double z0,const Point2& p1)
619 {
620  double deltaX=p1.x()-x0;
621  double deltaY=p1.y()-y0;
622  double deltaZ=p1.z()-z0;
623  double result(deltaX*deltaX+deltaY*deltaY+deltaZ*deltaZ);
624 
625  if(result!=result)
626  {
627  std::cerr<<"warning, sqDistance25D, nan value, no distance"<<std::endl;
628  std::cerr<<"x0="<<x0<<", y0="<<y0<<", z0="<<z0<<std::endl;
629  std::cerr<<"p1="<<p1<<std::endl;
630  }
631 
632  return result;
633 
634 }
635 
636 
637 #endif
638 
639 
640 
647 inline
648 Point2 center(const Point2& p0,const Point2& p1)
649 {
650 #if GEOM_PSEUDO3D==GEOM_TRUE
651  Point2 center((p0.x()+p1.x())/2.0,(p0.y()+p1.y())/2.0,(p0.z()+p1.z())/2.0);
652 
653 #else
654  Point2 center((p0.x()+p1.x())/2.0,(p0.y()+p1.y())/2.0);
655 #endif
656  return center;
657 }
658 
671 CLASS_DECLSPEC
672 Point2 centerWithShift(const Point2& p0,const Point2& p1);
673 
674 #if GEOM_PSEUDO3D==GEOM_TRUE
675 struct Func_ltPointXYZ
676 {
677  bool operator()(const Point2& p0,const Point2& p1) const
678  {
679  if(p0.x()<p1.x()) return true;
680  if(p0.x()>p1.x()) return false;
681  if(p0.y()<p1.y()) return true;
682  if(p0.y()>p1.y()) return false;
683  if(p0.z()<p1.z()) return true;
684  return false;
685  }
686 };
687 
688 #endif
689 
690 } // (namespace)
Point2(const Point2 &p_)
Copy constructor.
Definition: Point2.h:173
Point2(const double x_, const double y_)
Constructor.
Definition: Point2.h:148
double y() const
Get the y-coordinate.
Definition: Point2.h:204
Vector2 operator-(const Point2 &other) const
Returns a vector from other to *this.
Definition: Point2.h:481
Point2 operator+(const Vector2 &vec) const
Add vector and point.
Definition: Point2.h:499
Triangle2 * getIncidentTriangle() const
Get the associated triangle.
Definition: Point2.h:353
Point.
Definition: Point2.h:39
void xy(double &x_, double &y_) const
Get the x- and y-coordinate.
Definition: Point2.h:245
bool operator>(const Point2 &p) const
Greater than operator.
Definition: Point2.h:305
int getCustomIndex() const
Get the custom index.
Definition: Point2.h:465
bool operator!=(const Point2 &p) const
Inequality operator.
Definition: Point2.h:343
bool operator==(const Point2 &p) const
Equality operator.
Definition: Point2.h:320
double y() const
Get the y-value.
double x() const
Get the x-value.
double getMaxAbs() const
Get max(abs(x),abs(y))
Definition: Point2.h:271
Vector.
Definition: Vector2.h:34
void setCustomIndex(int customIndex_)
Set a custom index.
Definition: Point2.h:448
void setIncidentTriangle(Triangle2 *pT)
Associate a triangle with the point.
Definition: Point2.h:474
bool operator<(const Point2 &p) const
Less than operator.
Definition: Point2.h:291
Point2 operator-(const Vector2 &vec) const
Subtract vector from point.
Definition: Point2.h:510
Point2()
Default constructor.
Definition: Point2.h:161
double x() const
Get the x-coordinate.
Definition: Point2.h:195
Triangle.
Definition: Triangle2.h:48
Definition: Bbox2.h:25