Geom Software - C++ Programming and Geometry Libraries
Fade2D Documentation pages v1.52
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 
87  CLASS_DECLSPEC
88  void setZ(double z)
89  {
90  setHeight(z);
91  }
92 
93 #else
94 
100  CLASS_DECLSPEC
101  Point2(const double x_,const double y_):
102  coordX(x_),
103  coordY(y_),
104  pAssociatedTriangle(NULL),
105  customIndex(-1)
106  {
107  //BC("Point2 Constructor default");
108  }
113  CLASS_DECLSPEC
115  coordX(-DBL_MAX),
116  coordY(-DBL_MAX),
117  pAssociatedTriangle(NULL),
118  customIndex(-1)
119  {
120  }
125  CLASS_DECLSPEC
126  Point2(const Point2& p_):
127  coordX(p_.x()),
128  coordY(p_.y()),
129  pAssociatedTriangle(NULL),
130  customIndex(p_.customIndex)
131  {
132  }
133 #endif
134 
135  CLASS_DECLSPEC
136  ~Point2()
137  {
138  }
139 
144  CLASS_DECLSPEC
145  double x() const
146  {
147  return coordX;
148  }
153  CLASS_DECLSPEC
154  double y() const
155  {
156  return coordY;
157  }
158 
159 #if GEOM_PSEUDO3D==GEOM_TRUE
160 
164  CLASS_DECLSPEC
165  double z() const
166  {
167  return coordZ;
168  }
169 #endif
170 
171 #if GEOM_PSEUDO3D==GEOM_TRUE
172 
179  CLASS_DECLSPEC
180  void xyz(double& x_,double& y_,double& z_) const
181  {
182  x_=coordX;
183  y_=coordY;
184  z_=coordZ;
185  }
186 #endif
187 
194  CLASS_DECLSPEC
195  void xy(double& x_,double& y_) const
196  {
197  x_=coordX;
198  y_=coordY;
199  }
200 
201 
202 #if GEOM_PSEUDO3D==GEOM_TRUE
203 
207  CLASS_DECLSPEC
208  void setHeight(double z)
209  {
210  coordZ=z;
211  }
212 #endif
213 
214 
215 
216 
220  CLASS_DECLSPEC
221  double getMaxAbs() const
222  {
223  double x(fabs(coordX));
224  double y(fabs(coordY));
225  bool b(x>=y);
226  return b*x+(!b)*y;
227  }
228 
229 
230 
231 
232 
233 
240  CLASS_DECLSPEC
241  bool operator<(const Point2& p) const
242  {
243  if(coordX<p.coordX) return true;
244  if(coordX>p.coordX) return false;
245  if(coordY<p.coordY) return true;
246  return false;
247  }
254  CLASS_DECLSPEC
255  bool operator>(const Point2& p) const
256  {
257  if(coordX>p.coordX) return true;
258  if(coordX<p.coordX) return false;
259  if(coordY>p.coordY) return true;
260  return false;
261  }
268  CLASS_DECLSPEC
269  bool operator==(const Point2& p) const
270  {
271  if(coordX==p.coordX && coordY==p.coordY) return true;
272  return false;
273  }
280  CLASS_DECLSPEC
281  bool operator!=(const Point2& p) const
282  {
283  if(coordX!=p.coordX || coordY!=p.coordY) return true;
284  return false;
285  }
290  CLASS_DECLSPEC
292  {
293  return pAssociatedTriangle;
294  }
295 
296 
297 #if GEOM_PSEUDO3D==GEOM_TRUE
298 
309  void set(const double x_,const double y_,const double z_,int customIndex_)
310  {
311  coordX=x_;
312  coordY=y_;
313  coordZ=z_;
314  pAssociatedTriangle=NULL;
315  customIndex=customIndex_;
316  }
317 #else
318 
326  void set(const double x_,const double y_,int customIndex_)
327  {
328  coordX=x_;
329  coordY=y_;
330  pAssociatedTriangle=NULL;
331  customIndex=customIndex_;
332  }
333  void change(const double x_,const double y_)
334  {
335  coordX=x_;
336  coordY=y_;
337  }
338 #endif
339 
344 #if GEOM_PSEUDO3D==GEOM_TRUE
345  void set(const Point2& pnt)
346  {
347  coordX=pnt.x();
348  coordY=pnt.y();
349  coordZ=pnt.z();
350  pAssociatedTriangle=NULL;
351  this->customIndex=pnt.customIndex;
352  }
353 #else
354  void set(const Point2& pnt)
355  {
356  coordX=pnt.x();
357  coordY=pnt.y();
358  pAssociatedTriangle=NULL;
359  this->customIndex=pnt.customIndex;
360  }
361 #endif
362 
370  CLASS_DECLSPEC
371  void setCustomIndex(int customIndex_)
372  {
373  customIndex=customIndex_;
374  }
385  CLASS_DECLSPEC
386  int getCustomIndex() const
387  {
388  return customIndex;
389  }
390 
396  {
397  pAssociatedTriangle=pT;
398  }
399 
402 Vector2 operator-(const Point2& other) const
403 {
404 #if GEOM_PSEUDO3D==GEOM_TRUE
405  double xdiff(x()-other.x());
406  double ydiff(y()-other.y());
407  double zdiff(z()-other.z());
408  return Vector2(xdiff,ydiff,zdiff);
409 #else
410  double xdiff(x()-other.x());
411  double ydiff(y()-other.y());
412  return Vector2(xdiff,ydiff);
413 #endif
414 
415 }
416 
419 Point2 operator+(const Vector2& vec) const
420 {
421 #if GEOM_PSEUDO3D==GEOM_TRUE
422  return Point2(x()+vec.x(),y()+vec.y(),z()+vec.z());
423 #else
424  return Point2(x()+vec.x(),y()+vec.y());
425 #endif
426 }
427 
430 Point2 operator-(const Vector2& vec) const
431 {
432 #if GEOM_PSEUDO3D==GEOM_TRUE
433  return Point2(x()-vec.x(),y()-vec.y(),z()-vec.z());
434 #else
435  return Point2(x()-vec.x(),y()-vec.y());
436 #endif
437 }
438 
439 
440  friend std::ostream &operator<<(std::ostream &stream, const Point2& pnt);
441  friend std::istream &operator>>(std::istream &stream, Point2& pnt);
442 
443 
444 protected:
445 friend class Dt2;
446  double coordX;
447  double coordY;
448 #if GEOM_PSEUDO3D==GEOM_TRUE
449  double coordZ;
450 #endif
451  Triangle2* pAssociatedTriangle;
452  int customIndex;
453 };
454 
455 
456 inline std::ostream &operator<<(std::ostream &stream, const Point2& pnt)
457 {
458 #if GEOM_PSEUDO3D==GEOM_TRUE
459  stream << "Point2 ("<<&pnt<<","<<pnt.customIndex<<"): "<<pnt.x()<<", "<<pnt.y()<<", "<<pnt.z();
460  //#ifndef NDEBUG
461  //stream<<", anyT="<<pnt.getIncidentTriangle();
462  //#endif
463 #else
464  stream << "Point2 ("<<&pnt<<","<<pnt.customIndex<<"): "<<pnt.x()<<", "<<pnt.y();
465 #endif
466 
467 
468  return stream;
469 }
470 
471 inline std::istream &operator>>(std::istream &stream, Point2& pnt)
472 {
473 #if GEOM_PSEUDO3D==GEOM_TRUE
474  stream >> pnt.coordX >> pnt.coordY >> pnt.coordZ;
475 #else
476  stream >> pnt.coordX >> pnt.coordY;
477 #endif
478  return stream;
479 }
480 
481 // Free functions
482 
487 inline
488 double sqDistance2D(const Point2& p0,const Point2& p1)
489 {
490  double deltaX=p1.x()-p0.x();
491  double deltaY=p1.y()-p0.y();
492  double result(deltaX*deltaX+deltaY*deltaY);
493 
494  if(result!=result)
495  {
496  std::cerr<<"warning, sqDistance25D, nan value, no distance"<<std::endl;
497  std::cerr<<"p0="<<p0<<std::endl;
498  std::cerr<<"p1="<<p1<<std::endl;
499  }
500 
501  return result;
502 
503 }
504 
505 
510 inline
511 double sqDistance2D(const double x0,const double y0,const Point2& p1)
512 {
513  double deltaX=p1.x()-x0;
514  double deltaY=p1.y()-y0;
515  double result(deltaX*deltaX+deltaY*deltaY);
516 
517  if(result!=result)
518  {
519  std::cerr<<"warning, sqDistance25D, nan value, no distance"<<std::endl;
520  std::cerr<<"x0="<<x0<<", y0="<<y0<<std::endl;
521  std::cerr<<"p1="<<p1<<std::endl;
522  }
523 
524  return result;
525 
526 }
527 
528 #if GEOM_PSEUDO3D==GEOM_TRUE
529 
534 inline
535 double sqDistance25D(const Point2& p0,const Point2& p1)
536 {
537  double deltaX=p1.x()-p0.x();
538  double deltaY=p1.y()-p0.y();
539  double deltaZ=p1.z()-p0.z();
540  double result(deltaX*deltaX+deltaY*deltaY+deltaZ*deltaZ);
541 
542  if(result!=result)
543  {
544  std::cerr<<"warning, sqDistance25D, nan value, no distance"<<std::endl;
545  std::cerr<<"p0="<<p0<<std::endl;
546  std::cerr<<"p1="<<p1<<std::endl;
547  }
548 
549  return result;
550 }
551 
552 
557 inline
558 double sqDistance25D(const double x0,const double y0,const double z0,const Point2& p1)
559 {
560  double deltaX=p1.x()-x0;
561  double deltaY=p1.y()-y0;
562  double deltaZ=p1.z()-z0;
563  double result(deltaX*deltaX+deltaY*deltaY+deltaZ*deltaZ);
564 
565  if(result!=result)
566  {
567  std::cerr<<"warning, sqDistance25D, nan value, no distance"<<std::endl;
568  std::cerr<<"x0="<<x0<<", y0="<<y0<<", z0="<<z0<<std::endl;
569  std::cerr<<"p1="<<p1<<std::endl;
570  }
571 
572  return result;
573 
574 }
575 
576 
577 #endif
578 
579 
580 
587 inline
588 Point2 center(const Point2& p0,const Point2& p1)
589 {
590 #if GEOM_PSEUDO3D==GEOM_TRUE
591  Point2 center((p0.x()+p1.x())/2.0,(p0.y()+p1.y())/2.0,(p0.z()+p1.z())/2.0);
592 
593 #else
594  Point2 center((p0.x()+p1.x())/2.0,(p0.y()+p1.y())/2.0);
595 #endif
596  return center;
597 }
598 
611 CLASS_DECLSPEC
612 Point2 centerWithShift(const Point2& p0,const Point2& p1);
613 
614 
615 } // (namespace)
Point2(const Point2 &p_)
Copy constructor.
Definition: Point2.h:126
Point2(const double x_, const double y_)
Constructor.
Definition: Point2.h:101
Triangle2 * getIncidentTriangle() const
Get the associated triangle.
Definition: Point2.h:291
bool operator<(const Point2 &p) const
Less than operator.
Definition: Point2.h:241
Point2 operator+(const Vector2 &vec) const
Add vector and point.
Definition: Point2.h:419
Point.
Definition: Point2.h:39
bool operator==(const Point2 &p) const
Equality operator.
Definition: Point2.h:269
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:221
bool operator>(const Point2 &p) const
Greater than operator.
Definition: Point2.h:255
bool operator!=(const Point2 &p) const
Inequality operator.
Definition: Point2.h:281
int getCustomIndex() const
Get the vertex index.
Definition: Point2.h:386
Vector.
Definition: Vector2.h:37
void setCustomIndex(int customIndex_)
Set a custom index.
Definition: Point2.h:371
double x() const
Get the x-coordinate.
Definition: Point2.h:145
void setIncidentTriangle(Triangle2 *pT)
Associate a triangle with the point.
Definition: Point2.h:395
Point2 operator-(const Vector2 &vec) const
Subtract vector from point.
Definition: Point2.h:430
double y() const
Get the y-coordinate.
Definition: Point2.h:154
void xy(double &x_, double &y_) const
Get the x- and y-coordinate.
Definition: Point2.h:195
Vector2 operator-(const Point2 &other) const
Returns a vector from other to *this.
Definition: Point2.h:402
Point2()
Default constructor.
Definition: Point2.h:114
Triangle.
Definition: Triangle2.h:48
Definition: Bbox2.h:25