Geom Software - C++ Programming and Geometry Libraries
Fade2D Documentation pages v1.55
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 public:
42 
43 #if GEOM_PSEUDO3D==GEOM_TRUE
44 
50  CLASS_DECLSPEC
51  Point2(const double x_,const double y_,const double z_):
52  coordX(x_),
53  coordY(y_),
54  coordZ(z_),
55  pAssociatedTriangle(NULL),
56  customIndex(-1)
57  {
58  BC("Point2 Constructor default");
59  customIndex=-1;
60  }
64  CLASS_DECLSPEC
65  Point2():coordX(-DBL_MAX),coordY(-DBL_MAX),coordZ(-DBL_MAX),pAssociatedTriangle(NULL),customIndex(-1)
66  {
67 
68  }
73  CLASS_DECLSPEC
74  Point2(const Point2& p_):
75  coordX(p_.x()),
76  coordY(p_.y()),
77  coordZ(p_.z()),
78  pAssociatedTriangle(NULL),
79  customIndex(p_.customIndex)
80  {
81 
82  }
83 
84 
89 #if GEOM_PSEUDO3D==GEOM_TRUE
90  void print()
91  {
92  std::cout<<coordX<<" "<<coordY<<" "<<coordZ<<std::endl;
93  }
94 #else
95  void print()
96  {
97  std::cout<<coordX<<" "<<coordY<<std::endl;
98  }
99 #endif
100 
101 
105  CLASS_DECLSPEC
106  void setZ(double z)
107  {
108  setHeight(z);
109  }
110 
111 #else
112 
118  CLASS_DECLSPEC
119  Point2(const double x_,const double y_):
120  coordX(x_),
121  coordY(y_),
122  pAssociatedTriangle(NULL),
123  customIndex(-1)
124  {
125  //BC("Point2 Constructor default");
126  }
131  CLASS_DECLSPEC
133  coordX(-DBL_MAX),
134  coordY(-DBL_MAX),
135  pAssociatedTriangle(NULL),
136  customIndex(-1)
137  {
138  }
143  CLASS_DECLSPEC
144  Point2(const Point2& p_):
145  coordX(p_.x()),
146  coordY(p_.y()),
147  pAssociatedTriangle(NULL),
148  customIndex(p_.customIndex)
149  {
150  }
151 #endif
152 
153  CLASS_DECLSPEC
154  ~Point2()
155  {
156  }
157 
162  CLASS_DECLSPEC
163  double x() const
164  {
165  return coordX;
166  }
171  CLASS_DECLSPEC
172  double y() const
173  {
174  return coordY;
175  }
176 
177 #if GEOM_PSEUDO3D==GEOM_TRUE
178 
182  CLASS_DECLSPEC
183  double z() const
184  {
185  return coordZ;
186  }
187 #endif
188 
189 #if GEOM_PSEUDO3D==GEOM_TRUE
190 
197  CLASS_DECLSPEC
198  void xyz(double& x_,double& y_,double& z_) const
199  {
200  x_=coordX;
201  y_=coordY;
202  z_=coordZ;
203  }
204 #endif
205 
212  CLASS_DECLSPEC
213  void xy(double& x_,double& y_) const
214  {
215  x_=coordX;
216  y_=coordY;
217  }
218 
219 
220 #if GEOM_PSEUDO3D==GEOM_TRUE
221 
225  CLASS_DECLSPEC
226  void setHeight(double z)
227  {
228  coordZ=z;
229  }
230 #endif
231 
232 
233 
234 
238  CLASS_DECLSPEC
239  double getMaxAbs() const
240  {
241  double x(fabs(coordX));
242  double y(fabs(coordY));
243  bool b(x>=y);
244  return b*x+(!b)*y;
245  }
246 
247 
248 
249 
250 
251 
258  CLASS_DECLSPEC
259  bool operator<(const Point2& p) const
260  {
261  if(coordX<p.coordX) return true;
262  if(coordX>p.coordX) return false;
263  if(coordY<p.coordY) return true;
264  return false;
265  }
272  CLASS_DECLSPEC
273  bool operator>(const Point2& p) const
274  {
275  if(coordX>p.coordX) return true;
276  if(coordX<p.coordX) return false;
277  if(coordY>p.coordY) return true;
278  return false;
279  }
286  CLASS_DECLSPEC
287  bool operator==(const Point2& p) const
288  {
289  if(coordX==p.coordX && coordY==p.coordY) return true;
290  return false;
291  }
298  CLASS_DECLSPEC
299  bool operator!=(const Point2& p) const
300  {
301  if(coordX!=p.coordX || coordY!=p.coordY) return true;
302  return false;
303  }
308  CLASS_DECLSPEC
310  {
311  return pAssociatedTriangle;
312  }
313 
314 
315 #if GEOM_PSEUDO3D==GEOM_TRUE
316 
327  void set(const double x_,const double y_,const double z_,int customIndex_)
328  {
329  coordX=x_;
330  coordY=y_;
331  coordZ=z_;
332  pAssociatedTriangle=NULL;
333  customIndex=customIndex_;
334  }
335 #else
336 
344  void set(const double x_,const double y_,int customIndex_)
345  {
346  coordX=x_;
347  coordY=y_;
348  pAssociatedTriangle=NULL;
349  customIndex=customIndex_;
350  }
351  void change(const double x_,const double y_)
352  {
353  coordX=x_;
354  coordY=y_;
355  }
356 #endif
357 
362 #if GEOM_PSEUDO3D==GEOM_TRUE
363  void set(const Point2& pnt)
364  {
365  coordX=pnt.x();
366  coordY=pnt.y();
367  coordZ=pnt.z();
368  pAssociatedTriangle=NULL;
369  this->customIndex=pnt.customIndex;
370  }
371 #else
372  void set(const Point2& pnt)
373  {
374  coordX=pnt.x();
375  coordY=pnt.y();
376  pAssociatedTriangle=NULL;
377  this->customIndex=pnt.customIndex;
378  }
379 #endif
380 
381 
382 
391  CLASS_DECLSPEC
392  void setCustomIndex(int customIndex_)
393  {
394  customIndex=customIndex_;
395  }
406  CLASS_DECLSPEC
407  int getCustomIndex() const
408  {
409  return customIndex;
410  }
411 
417  {
418  pAssociatedTriangle=pT;
419  }
420 
423 Vector2 operator-(const Point2& other) const
424 {
425 #if GEOM_PSEUDO3D==GEOM_TRUE
426  double xdiff(x()-other.x());
427  double ydiff(y()-other.y());
428  double zdiff(z()-other.z());
429  return Vector2(xdiff,ydiff,zdiff);
430 #else
431  double xdiff(x()-other.x());
432  double ydiff(y()-other.y());
433  return Vector2(xdiff,ydiff);
434 #endif
435 
436 }
437 
440 Point2 operator+(const Vector2& vec) const
441 {
442 #if GEOM_PSEUDO3D==GEOM_TRUE
443  return Point2(x()+vec.x(),y()+vec.y(),z()+vec.z());
444 #else
445  return Point2(x()+vec.x(),y()+vec.y());
446 #endif
447 }
448 
451 Point2 operator-(const Vector2& vec) const
452 {
453 #if GEOM_PSEUDO3D==GEOM_TRUE
454  return Point2(x()-vec.x(),y()-vec.y(),z()-vec.z());
455 #else
456  return Point2(x()-vec.x(),y()-vec.y());
457 #endif
458 }
459 
460 
461  friend std::ostream &operator<<(std::ostream &stream, const Point2& pnt);
462  friend std::istream &operator>>(std::istream &stream, Point2& pnt);
463 
464 
465 protected:
466 friend class Dt2;
467  double coordX;
468  double coordY;
469 #if GEOM_PSEUDO3D==GEOM_TRUE
470  double coordZ;
471 #endif
472  Triangle2* pAssociatedTriangle;
473  int customIndex;
474 };
475 
476 
477 inline std::ostream &operator<<(std::ostream &stream, const Point2& pnt)
478 {
479 #if GEOM_PSEUDO3D==GEOM_TRUE
480  stream << "Point2 ("<<&pnt<<","<<pnt.customIndex<<"): "<<pnt.x()<<", "<<pnt.y()<<", "<<pnt.z();
481  //#ifndef NDEBUG
482  //stream<<", anyT="<<pnt.getIncidentTriangle();
483  //#endif
484 #else
485  stream << "Point2 ("<<&pnt<<","<<pnt.customIndex<<"): "<<pnt.x()<<", "<<pnt.y();
486 #endif
487 
488 
489  return stream;
490 }
491 
492 inline std::istream &operator>>(std::istream &stream, Point2& pnt)
493 {
494 #if GEOM_PSEUDO3D==GEOM_TRUE
495  stream >> pnt.coordX >> pnt.coordY >> pnt.coordZ;
496 #else
497  stream >> pnt.coordX >> pnt.coordY;
498 #endif
499  return stream;
500 }
501 
502 // Free functions
503 
508 inline
509 double sqDistance2D(const Point2& p0,const Point2& p1)
510 {
511  double deltaX=p1.x()-p0.x();
512  double deltaY=p1.y()-p0.y();
513  double result(deltaX*deltaX+deltaY*deltaY);
514 
515  if(result!=result)
516  {
517  std::cerr<<"warning, sqDistance25D, nan value, no distance"<<std::endl;
518  std::cerr<<"p0="<<p0<<std::endl;
519  std::cerr<<"p1="<<p1<<std::endl;
520  }
521 
522  return result;
523 
524 }
525 
526 
531 inline
532 double sqDistance2D(const double x0,const double y0,const Point2& p1)
533 {
534  double deltaX=p1.x()-x0;
535  double deltaY=p1.y()-y0;
536  double result(deltaX*deltaX+deltaY*deltaY);
537 
538  if(result!=result)
539  {
540  std::cerr<<"warning, sqDistance25D, nan value, no distance"<<std::endl;
541  std::cerr<<"x0="<<x0<<", y0="<<y0<<std::endl;
542  std::cerr<<"p1="<<p1<<std::endl;
543  }
544 
545  return result;
546 
547 }
548 
549 #if GEOM_PSEUDO3D==GEOM_TRUE
550 
555 inline
556 double sqDistance25D(const Point2& p0,const Point2& p1)
557 {
558  double deltaX=p1.x()-p0.x();
559  double deltaY=p1.y()-p0.y();
560  double deltaZ=p1.z()-p0.z();
561  double result(deltaX*deltaX+deltaY*deltaY+deltaZ*deltaZ);
562 
563  if(result!=result)
564  {
565  std::cerr<<"warning, sqDistance25D, nan value, no distance"<<std::endl;
566  std::cerr<<"p0="<<p0<<std::endl;
567  std::cerr<<"p1="<<p1<<std::endl;
568  }
569 
570  return result;
571 }
572 
573 
578 inline
579 double sqDistance25D(const double x0,const double y0,const double z0,const Point2& p1)
580 {
581  double deltaX=p1.x()-x0;
582  double deltaY=p1.y()-y0;
583  double deltaZ=p1.z()-z0;
584  double result(deltaX*deltaX+deltaY*deltaY+deltaZ*deltaZ);
585 
586  if(result!=result)
587  {
588  std::cerr<<"warning, sqDistance25D, nan value, no distance"<<std::endl;
589  std::cerr<<"x0="<<x0<<", y0="<<y0<<", z0="<<z0<<std::endl;
590  std::cerr<<"p1="<<p1<<std::endl;
591  }
592 
593  return result;
594 
595 }
596 
597 
598 #endif
599 
600 
601 
608 inline
609 Point2 center(const Point2& p0,const Point2& p1)
610 {
611 #if GEOM_PSEUDO3D==GEOM_TRUE
612  Point2 center((p0.x()+p1.x())/2.0,(p0.y()+p1.y())/2.0,(p0.z()+p1.z())/2.0);
613 
614 #else
615  Point2 center((p0.x()+p1.x())/2.0,(p0.y()+p1.y())/2.0);
616 #endif
617  return center;
618 }
619 
632 CLASS_DECLSPEC
633 Point2 centerWithShift(const Point2& p0,const Point2& p1);
634 
635 
636 } // (namespace)
Point2(const Point2 &p_)
Copy constructor.
Definition: Point2.h:144
Point2(const double x_, const double y_)
Constructor.
Definition: Point2.h:119
double y() const
Get the y-coordinate.
Definition: Point2.h:172
Vector2 operator-(const Point2 &other) const
Returns a vector from other to *this.
Definition: Point2.h:423
Point2 operator+(const Vector2 &vec) const
Add vector and point.
Definition: Point2.h:440
Triangle2 * getIncidentTriangle() const
Get the associated triangle.
Definition: Point2.h:309
Point.
Definition: Point2.h:39
void xy(double &x_, double &y_) const
Get the x- and y-coordinate.
Definition: Point2.h:213
bool operator>(const Point2 &p) const
Greater than operator.
Definition: Point2.h:273
int getCustomIndex() const
Get the vertex index.
Definition: Point2.h:407
bool operator!=(const Point2 &p) const
Inequality operator.
Definition: Point2.h:299
bool operator==(const Point2 &p) const
Equality operator.
Definition: Point2.h:287
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:239
Vector.
Definition: Vector2.h:37
void setCustomIndex(int customIndex_)
Set a custom index.
Definition: Point2.h:392
void setIncidentTriangle(Triangle2 *pT)
Associate a triangle with the point.
Definition: Point2.h:416
bool operator<(const Point2 &p) const
Less than operator.
Definition: Point2.h:259
Point2 operator-(const Vector2 &vec) const
Subtract vector from point.
Definition: Point2.h:451
Point2()
Default constructor.
Definition: Point2.h:132
double x() const
Get the x-coordinate.
Definition: Point2.h:163
Triangle.
Definition: Triangle2.h:48
Definition: Bbox2.h:25