Geom Software - C++ Programming and Geometry Libraries
Fade2.5D Documentation pages v1.63
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  }
61
65  CLASS_DECLSPEC
66  Point2():coordX(-DBL_MAX),coordY(-DBL_MAX),coordZ(-DBL_MAX),pAssociatedTriangle(NULL),customIndex(-1)
67  {
68
69  }
74  CLASS_DECLSPEC
75  Point2(const Point2& p_):
76  coordX(p_.x()),
77  coordY(p_.y()),
78  coordZ(p_.z()),
79  pAssociatedTriangle(NULL),
80  customIndex(p_.customIndex)
81  {
82  }
83
84  CLASS_DECLSPEC
85  Point2 &operator=(const Point2& other)
86  {
87  coordX=other.x();
88  coordY=other.y();
89  coordZ=other.z();
90  pAssociatedTriangle=NULL;
91  customIndex=other.customIndex;
92  return *this;
93  }
94
99 #if GEOM_PSEUDO3D==GEOM_TRUE
100  void print()
101  {
102  std::cout<<coordX<<" "<<coordY<<" "<<coordZ<<std::endl;
103  }
104 #else
105  void print()
106  {
107  std::cout<<coordX<<" "<<coordY<<std::endl;
108  }
109 #endif
110
111
115  CLASS_DECLSPEC
116  void setZ(double z)
117  {
118  setHeight(z);
119  }
120
121 #else
122
128  CLASS_DECLSPEC
129  Point2(const double x_,const double y_):
130  coordX(x_),
131  coordY(y_),
132  pAssociatedTriangle(NULL),
133  customIndex(-1)
134  {
135  //BC("Point2 Constructor default");
136  }
141  CLASS_DECLSPEC
142  Point2():
143  coordX(-DBL_MAX),
144  coordY(-DBL_MAX),
145  pAssociatedTriangle(NULL),
146  customIndex(-1)
147  {
148  }
153  CLASS_DECLSPEC
154  Point2(const Point2& p_):
155  coordX(p_.x()),
156  coordY(p_.y()),
157  pAssociatedTriangle(NULL),
158  customIndex(p_.customIndex)
159  {
160  }
161 #endif
162
163
164
165
166  CLASS_DECLSPEC
167  ~Point2()
168  {
169  }
170
175  CLASS_DECLSPEC
176  double x() const
177  {
178  return coordX;
179  }
184  CLASS_DECLSPEC
185  double y() const
186  {
187  return coordY;
188  }
189
190 #if GEOM_PSEUDO3D==GEOM_TRUE
191
195  CLASS_DECLSPEC
196  double z() const
197  {
198  return coordZ;
199  }
200 #endif
201
202 #if GEOM_PSEUDO3D==GEOM_TRUE
203
210  CLASS_DECLSPEC
211  void xyz(double& x_,double& y_,double& z_) const
212  {
213  x_=coordX;
214  y_=coordY;
215  z_=coordZ;
216  }
217 #endif
218
225  CLASS_DECLSPEC
226  void xy(double& x_,double& y_) const
227  {
228  x_=coordX;
229  y_=coordY;
230  }
231
232
233 #if GEOM_PSEUDO3D==GEOM_TRUE
234
238  CLASS_DECLSPEC
239  void setHeight(double z)
240  {
241  coordZ=z;
242  }
243 #endif
244
245
246
247
251  CLASS_DECLSPEC
252  double getMaxAbs() const
253  {
254  double x(fabs(coordX));
255  double y(fabs(coordY));
256  bool b(x>=y);
257  return b*x+(!b)*y;
258  }
259
260
261
262
263
264
271  CLASS_DECLSPEC
272  bool operator<(const Point2& p) const
273  {
274  if(coordX<p.coordX) return true;
275  if(coordX>p.coordX) return false;
276  if(coordY<p.coordY) return true;
277  return false;
278  }
285  CLASS_DECLSPEC
286  bool operator>(const Point2& p) const
287  {
288  if(coordX>p.coordX) return true;
289  if(coordX<p.coordX) return false;
290  if(coordY>p.coordY) return true;
291  return false;
292  }
299  CLASS_DECLSPEC
300  bool operator==(const Point2& p) const
301  {
302  if(coordX==p.coordX && coordY==p.coordY) return true;
303  return false;
304  }
311  CLASS_DECLSPEC
312  bool operator!=(const Point2& p) const
313  {
314  if(coordX!=p.coordX || coordY!=p.coordY) return true;
315  return false;
316  }
321  CLASS_DECLSPEC
323  {
324  return pAssociatedTriangle;
325  }
326
327
328 #if GEOM_PSEUDO3D==GEOM_TRUE
329
340  void set(const double x_,const double y_,const double z_,int customIndex_)
341  {
342  coordX=x_;
343  coordY=y_;
344  coordZ=z_;
345  pAssociatedTriangle=NULL;
346  customIndex=customIndex_;
347  }
348 #else
349
357  void set(const double x_,const double y_,int customIndex_)
358  {
359  coordX=x_;
360  coordY=y_;
361  pAssociatedTriangle=NULL;
362  customIndex=customIndex_;
363  }
364  void change(const double x_,const double y_)
365  {
366  coordX=x_;
367  coordY=y_;
368  }
369 #endif
370
375 #if GEOM_PSEUDO3D==GEOM_TRUE
376  void set(const Point2& pnt)
377  {
378  coordX=pnt.x();
379  coordY=pnt.y();
380  coordZ=pnt.z();
381  pAssociatedTriangle=NULL;
382  this->customIndex=pnt.customIndex;
383  }
384 #else
385  void set(const Point2& pnt)
386  {
387  coordX=pnt.x();
388  coordY=pnt.y();
389  pAssociatedTriangle=NULL;
390  this->customIndex=pnt.customIndex;
391  }
392 #endif
393
394
395
410  CLASS_DECLSPEC
411  void setCustomIndex(int customIndex_)
412  {
413  customIndex=customIndex_;
414  }
427  CLASS_DECLSPEC
428  int getCustomIndex() const
429  {
430  return customIndex;
431  }
432
438  {
439  pAssociatedTriangle=pT;
440  }
441
444 Vector2 operator-(const Point2& other) const
445 {
446 #if GEOM_PSEUDO3D==GEOM_TRUE
447  double xdiff(x()-other.x());
448  double ydiff(y()-other.y());
449  double zdiff(z()-other.z());
450  return Vector2(xdiff,ydiff,zdiff);
451 #else
452  double xdiff(x()-other.x());
453  double ydiff(y()-other.y());
454  return Vector2(xdiff,ydiff);
455 #endif
456
457 }
458
461 Point2 operator+(const Vector2& vec) const
462 {
463 #if GEOM_PSEUDO3D==GEOM_TRUE
464  return Point2(x()+vec.x(),y()+vec.y(),z()+vec.z());
465 #else
466  return Point2(x()+vec.x(),y()+vec.y());
467 #endif
468 }
469
472 Point2 operator-(const Vector2& vec) const
473 {
474 #if GEOM_PSEUDO3D==GEOM_TRUE
475  return Point2(x()-vec.x(),y()-vec.y(),z()-vec.z());
476 #else
477  return Point2(x()-vec.x(),y()-vec.y());
478 #endif
479 }
480
481
482  friend std::ostream &operator<<(std::ostream &stream, const Point2& pnt);
483  friend std::istream &operator>>(std::istream &stream, Point2& pnt);
484
485
486 protected:
487 friend class Dt2;
488  double coordX;
489  double coordY;
490 #if GEOM_PSEUDO3D==GEOM_TRUE
491  double coordZ;
492 #endif
493  Triangle2* pAssociatedTriangle;
494  int customIndex;
495 };
496
497
498 inline std::ostream &operator<<(std::ostream &stream, const Point2& pnt)
499 {
500 #if GEOM_PSEUDO3D==GEOM_TRUE
501  stream << "Point2 ("<<&pnt<<","<<pnt.customIndex<<"): "<<pnt.x()<<", "<<pnt.y()<<", "<<pnt.z();
502  //#ifndef NDEBUG
503  //stream<<", anyT="<<pnt.getIncidentTriangle();
504  //#endif
505 #else
506  stream << "Point2 ("<<&pnt<<","<<pnt.customIndex<<"): "<<pnt.x()<<", "<<pnt.y();
507 #endif
508
509
510  return stream;
511 }
512
513 inline std::istream &operator>>(std::istream &stream, Point2& pnt)
514 {
515 #if GEOM_PSEUDO3D==GEOM_TRUE
516  stream >> pnt.coordX >> pnt.coordY >> pnt.coordZ;
517 #else
518  stream >> pnt.coordX >> pnt.coordY;
519 #endif
520  return stream;
521 }
522
523 // Free functions
524
529 inline
530 double sqDistance2D(const Point2& p0,const Point2& p1)
531 {
532  double deltaX=p1.x()-p0.x();
533  double deltaY=p1.y()-p0.y();
534  return (deltaX*deltaX+deltaY*deltaY);
535 }
536
537
542 inline
543 double sqDistance2D(const double x0,const double y0,const Point2& p1)
544 {
545  double deltaX=p1.x()-x0;
546  double deltaY=p1.y()-y0;
547  return (deltaX*deltaX+deltaY*deltaY);
548 }
549
550 #if GEOM_PSEUDO3D==GEOM_TRUE
551
556 inline
557 double sqDistance25D(const Point2& p0,const Point2& p1)
558 {
559  double deltaX=p1.x()-p0.x();
560  double deltaY=p1.y()-p0.y();
561  double deltaZ=p1.z()-p0.z();
562  double result(deltaX*deltaX+deltaY*deltaY+deltaZ*deltaZ);
563
564  if(result!=result)
565  {
566  std::cerr<<"warning, sqDistance25D, nan value, no distance"<<std::endl;
567  std::cerr<<"p0="<<p0<<std::endl;
568  std::cerr<<"p1="<<p1<<std::endl;
569  }
570
571  return result;
572 }
573
574
579 inline
580 double sqDistance25D(const double x0,const double y0,const double z0,const Point2& p1)
581 {
582  double deltaX=p1.x()-x0;
583  double deltaY=p1.y()-y0;
584  double deltaZ=p1.z()-z0;
585  double result(deltaX*deltaX+deltaY*deltaY+deltaZ*deltaZ);
586
587  if(result!=result)
588  {
589  std::cerr<<"warning, sqDistance25D, nan value, no distance"<<std::endl;
590  std::cerr<<"x0="<<x0<<", y0="<<y0<<", z0="<<z0<<std::endl;
591  std::cerr<<"p1="<<p1<<std::endl;
592  }
593
594  return result;
595
596 }
597
598
599 #endif
600
601
602
609 inline
610 Point2 center(const Point2& p0,const Point2& p1)
611 {
612 #if GEOM_PSEUDO3D==GEOM_TRUE
613  Point2 center((p0.x()+p1.x())/2.0,(p0.y()+p1.y())/2.0,(p0.z()+p1.z())/2.0);
614
615 #else
616  Point2 center((p0.x()+p1.x())/2.0,(p0.y()+p1.y())/2.0);
617 #endif
618  return center;
619 }
620
633 CLASS_DECLSPEC
634 Point2 centerWithShift(const Point2& p0,const Point2& p1);
635
636
637 } // (namespace)
double y() const
Get the y-coordinate.
Definition: Point2.h:185
double z() const
Get the z-coordinate.
Definition: Point2.h:196
int getCustomIndex() const
Get the custom index.
Definition: Point2.h:428
void print()
Print.
Definition: Point2.h:100
bool operator==(const Point2 &p) const
Equality operator.
Definition: Point2.h:300
double getMaxAbs() const
Get max(abs(x),abs(y))
Definition: Point2.h:252
Point2(const double x_, const double y_, const double z_)
Constructor.
Definition: Point2.h:51
bool operator>(const Point2 &p) const
Greater than operator.
Definition: Point2.h:286
Point2 operator+(const Vector2 &vec) const
Add vector and point.
Definition: Point2.h:461
Point2 operator-(const Vector2 &vec) const
Subtract vector from point.
Definition: Point2.h:472
void xyz(double &x_, double &y_, double &z_) const
Get the x-, y- and z-coordinate.
Definition: Point2.h:211
void setIncidentTriangle(Triangle2 *pT)
Associate a triangle with the point.
Definition: Point2.h:437
void setHeight(double z)
Set the z-coordinate.
Definition: Point2.h:239
double y() const
Get the y-value.
double z() const
Get the z-value.
Vector.
Definition: Vector2.h:37
bool operator<(const Point2 &p) const
Less than operator.
Definition: Point2.h:272
double x() const
Get the x-coordinate.
Definition: Point2.h:176
Triangle.
Definition: Triangle2.h:48
Triangle2 * getIncidentTriangle() const
Get the associated triangle.
Definition: Point2.h:322
Point2(const Point2 &p_)
Copy constructor.
Definition: Point2.h:75
void setCustomIndex(int customIndex_)
Set a custom index.
Definition: Point2.h:411
bool operator!=(const Point2 &p) const
Inequality operator.
Definition: Point2.h:312
Definition: Bbox2.h:23
Point2()
Default constructor.
Definition: Point2.h:66
Vector2 operator-(const Point2 &other) const
Returns a vector from other to *this.
Definition: Point2.h:444
Point.
Definition: Point2.h:39
double x() const
Get the x-value.
void setZ(double z)
Deprecated, use setHeight() instead. Kept for backward compatibility.
Definition: Point2.h:116
void xy(double &x_, double &y_) const
Get the x- and y-coordinate.
Definition: Point2.h:226