Fade2D Documentation pages v2.16.8
Delaunay Features
Bbox2.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 // unclear to you.
14 //
15 // Support: https://www.geom.at/contact/
16 // Project: https://www.geom.at/fade2d/html/
18 
19 #pragma once
20 #include "Segment2.h"
21 #include "common.h"
22 #if GEOM_PSEUDO3D==GEOM_TRUE
23  namespace GEOM_FADE25D {
24 #elif GEOM_PSEUDO3D==GEOM_FALSE
25  namespace GEOM_FADE2D {
26 #else
27  #error GEOM_PSEUDO3D is not defined
28 #endif
29 
30 class GeomTest; // FWD
31 
38 class CLASS_DECLSPEC Bbox2
39 {
40 public:
47  explicit Bbox2(GeomTest* pGeomTest_=NULL):
48  minX(DBL_MAX),minY(DBL_MAX),
49  maxX(-DBL_MAX),maxY(-DBL_MAX),
50  maxAbsCoord(0),
51  bValid(false),pGeomTest(pGeomTest_)
52  {
53  }
54  ~Bbox2();
55 
56 
62  void reset()
63  {
64  minX=DBL_MAX;
65  minY=DBL_MAX;
66  maxX=-DBL_MAX;
67  maxY=-DBL_MAX;
68  bValid=false;
69  }
70 
77  bool isValid() const
78  {
79  return minX<DBL_MAX;
80  }
81 
89  void getCorners(std::vector<Point2>& vBoxCorners) const;
90 
98  void getBoundary(std::vector<Segment2>& vBoundary) const;
99 
108  void getOffsetCorners(double offset,std::vector<Point2>& vBoxCorners) const;
109 
110 
119  bool doIntersect(const Bbox2& other) const;
120 
121 
130  bool add(std::vector<Point2*>::const_iterator start_it,std::vector<Point2*>::const_iterator end_it)
131  {
132  if(start_it==end_it) return false;
133  if(bValid)
134  {
135  double oldMinX(minX),oldMinY(minY),oldMaxX(maxX),oldMaxY(maxY);
136  for(;start_it!=end_it;++start_it) treatPointForValidBox(**start_it);
137  updateMaxAbs();
138  if(oldMinX!=minX || oldMinY!=minY || oldMaxX!=maxX || oldMaxY!=maxY ) return true;
139  return false;
140  }
141  else
142  {
143  treatPointForInvalidBox(**start_it);
144  ++start_it;
145  for(;start_it!=end_it;++start_it) treatPointForValidBox(**start_it);
146  updateMaxAbs();
147  return true;
148  }
149 
150  }
151 
161  bool add(std::vector<Point2>::const_iterator start_it,std::vector<Point2>::const_iterator end_it)
162  {
163  if(start_it==end_it) return false;
164  if(bValid)
165  {
166  double oldMinX(minX),oldMinY(minY),oldMaxX(maxX),oldMaxY(maxY);
167  for(;start_it!=end_it;++start_it) treatPointForValidBox(*start_it);
168  updateMaxAbs();
169  if(oldMinX!=minX || oldMinY!=minY || oldMaxX!=maxX || oldMaxY!=maxY ) return true;
170  return false;
171  }
172  else
173  {
174  treatPointForInvalidBox(*start_it);
175  ++start_it;
176  for(;start_it!=end_it;++start_it) treatPointForValidBox(*start_it);
177  updateMaxAbs();
178  return true;
179  }
180  }
181 
191  bool add(size_t numPoints,double * coordinates)
192  {
193 #if GEOM_PSEUDO3D==GEOM_TRUE
194  const int NUMCOMPONENTS(3);
195 #else
196  const int NUMCOMPONENTS(2);
197 #endif
198 
199  if(numPoints==0) return false;
200  double oldMinX(minX),oldMinY(minY),oldMaxX(maxX),oldMaxY(maxY);
201  double firstX(coordinates[0]);
202  double firstY(coordinates[1]);
203  if(firstX<minX) minX=firstX;
204  if(firstX>maxX) maxX=firstX;
205  if(firstY<minY) minY=firstY;
206  if(firstY>maxY) maxY=firstY;
207 
208  for(size_t i=0;i<numPoints;++i)
209  {
210  double x(coordinates[NUMCOMPONENTS*i]);
211  double y(coordinates[NUMCOMPONENTS*i+1]);
212  if(x<minX) minX=x;
213  else if(x>maxX) maxX=x;
214  if(y<minY) minY=y;
215  else if(y>maxY) maxY=y;
216  }
217  bValid=true;
218  updateMaxAbs();
219  if(oldMinX!=minX || oldMinY!=minY || oldMaxX!=maxX || oldMaxY!=maxY ) return true;
220  else return false;
221  }
222 
231  bool add(const Point2& p)
232  {
233  //GCOUT<<"Add point: "<<p<<std::endl;
234  if(bValid)
235  {
236  double oldMinX(minX),oldMinY(minY),oldMaxX(maxX),oldMaxY(maxY);
237  treatPointForValidBox(p);
238  updateMaxAbs();
239  if(oldMinX!=minX || oldMinY!=minY || oldMaxX!=maxX || oldMaxY!=maxY ) return true;
240  else return false;
241  }
242  else
243  {
244  treatPointForInvalidBox(p);
245  updateMaxAbs();
246  return true;
247  }
248  }
249  bool add(const Bbox2& other)
258  {
259  bool bRet(false);
260  if(other.minX<minX) {minX=other.minX;bRet=true;}
261  if(other.minY<minY) {minY=other.minY;bRet=true;}
262  if(other.maxX>maxX) {maxX=other.maxX;bRet=true;}
263  if(other.maxY>maxY) {maxY=other.maxY;bRet=true;}
264  updateMaxAbs();
265  return bRet;
266  }
267 
276  bool isInBox(const Point2& p) const;
277 
278 
287 
296  Bbox2 operator+(const Bbox2& b);
297 
298 
307  {
308 #if GEOM_PSEUDO3D==GEOM_TRUE
309  return Point2(minX,minY,0);
310 #else
311  return Point2(minX,minY);
312 #endif
313  }
314 
315 
323  {
324 #if GEOM_PSEUDO3D==GEOM_TRUE
325  return Point2(maxX,maxY,0);
326 #else
327  return Point2(maxX,maxY);
328 #endif
329  }
330 
337  double getMinCoord() const
338  {
339  return (std::min)(minX,minY);
340  }
341 
347  double getMaxCoord() const
348  {
349  return (std::max)(maxX,maxY);
350  }
351 
352 
358  double getRangeX() const
359  {
360  return maxX-minX;
361  }
362 
368  double getRangeY() const
369  {
370  return maxY-minY;
371  }
372 
378  double getMaxRange() const
379  {
380  double range0=getRangeX();
381  double range1=getRangeY();
382  if(range0>range1) return range0;
383  return range1;
384  }
385 
391  double get_minX() const {return minX;}
397  double get_minY() const {return minY;}
398 
404  double get_maxX() const {return maxX;}
405 
411  double get_maxY() const {return maxY;}
412 
423  void getBounds(double& minX_,double& maxX_,double& minY_,double& maxY_) const;
424 
425 
426 
433  void doubleTheBox();
434 
440  void setMinX(double val)
441  {
442  minX=val;
443  updateMaxAbs();
444  if(minX<=maxX && minY<=maxY) bValid=true;
445  }
446 
452  void setMaxX(double val)
453  {
454  maxX=val;
455  updateMaxAbs();
456  if(minX<=maxX && minY<=maxY) bValid=true;
457  }
458 
464  void setMinY(double val)
465  {
466  minY=val;
467  updateMaxAbs();
468  if(minX<=maxX && minY<=maxY) bValid=true;
469  }
470 
476  void setMaxY(double val)
477  {
478  maxY=val;
479  updateMaxAbs();
480  if(minX<=maxX && minY<=maxY) bValid=true;
481  }
482 
483 
495  void enlargeRanges(double factor,bool bUseMaxRange,double minRange);
496 
506  void inflateIfDegenerate(double val)
507  {
508  if(bValid)
509  {
510  if(minX==maxX) maxX+=val;
511  if(minY==maxY) maxY+=val;
512  }
513  updateMaxAbs();
514  }
515 protected:
516  inline void treatPointForValidBox(const Point2& p)
517  {
518  double x,y;
519  p.xy(x,y);
520  if(x<minX) minX=x;
521  else if(x>maxX) maxX=x;
522  if(y<minY) minY=y;
523  else if(y>maxY) maxY=y;
524  updateMaxAbs();
525  }
526  inline void treatPointForInvalidBox(const Point2& p)
527  {
528  // Individual bounds may have been set already. Keep them!
529  if(minX==DBL_MAX) minX=p.x();
530  if(minY==DBL_MAX) minY=p.y();
531  if(maxX==-DBL_MAX) maxX=p.x();
532  if(maxY==-DBL_MAX) maxY=p.y();
533  updateMaxAbs();
534  bValid=true;
535  }
536  friend std::ostream &operator<<(std::ostream &stream, const Bbox2& pC);
537 protected:
538  double minX,minY;
539  double maxX,maxY;
540  double maxAbsCoord;
541  bool bValid;
542  GeomTest* pGeomTest;
543  bool updateMaxAbs();
544  friend Bbox2 intersection(const Bbox2& a,const Bbox2& b);
545 };
546 
553 Bbox2 getBox(std::vector<Point2>& vP);
554 
561  Bbox2 getBox(std::vector<Point2*>& vP);
562 
575 inline Bbox2 intersection(const Bbox2& a, const Bbox2& b)
576 {
577  Bbox2 ret(a);
578  if(b.minX > ret.minX) ret.minX=b.minX;
579  if(b.maxX < ret.maxX) ret.maxX=b.maxX;
580  if(b.minY > ret.minY) ret.minY=b.minY;
581  if(b.maxY < ret.maxY) ret.maxY=b.maxY;
582  if( (ret.minX>ret.maxX) || (ret.minY>ret.maxY) )
583  {
584  ret.bValid=false;
585  ret.minX=DBL_MAX;
586  ret.minY=DBL_MAX;
587  ret.maxX=-DBL_MAX;
588  ret.maxY=-DBL_MAX;
589  }
590  else
591  {
592  ret.bValid = true;
593  }
594  return ret;
595 }
596 
597 
605 inline std::ostream &operator<<(std::ostream &stream, const Bbox2& pC)
606 {
607  stream<<"Bbox2: xy("<<pC.minX<<","<<pC.minY<<") -> xy("<<pC.maxX<<","<<pC.maxY<<"), rangeX="<<pC.getRangeX()<<", rangeY="<<pC.getRangeY();
608  return stream;
609 }
610 
611 
612 
613 } // (namespace)
Bbox2 getBox(std::vector< Point2 > &vP)
Computes the bounding box of a set of points.
Bbox2 intersection(const Bbox2 &a, const Bbox2 &b)
Computes the intersection of two bounding boxes.
Definition: Bbox2.h:575
std::ostream & operator<<(std::ostream &stream, const Bbox2 &pC)
Prints the bounding box to a stream.
Definition: Bbox2.h:605
A 2D axis-aligned bounding box.
Definition: Bbox2.h:39
double getMaxCoord() const
Retrieves the largest coordinate value.
Definition: Bbox2.h:347
void doubleTheBox()
Doubles the size of the bounding box.
bool add(std::vector< Point2 * >::const_iterator start_it, std::vector< Point2 * >::const_iterator end_it)
Adds points to the bounding box.
Definition: Bbox2.h:130
void setMaxX(double val)
Sets the maxX value.
Definition: Bbox2.h:452
void getOffsetCorners(double offset, std::vector< Point2 > &vBoxCorners) const
Retrieves the offset corners of the bounding box.
void getBoundary(std::vector< Segment2 > &vBoundary) const
Retrieves the boundary of the bounding box.
bool add(const Point2 &p)
Adds a single point to the bounding box.
Definition: Bbox2.h:231
void getCorners(std::vector< Point2 > &vBoxCorners) const
Retrieves the corners of the bounding box.
bool isInBox(const Point2 &p) const
Checks if a point is inside the bounding box.
bool add(std::vector< Point2 >::const_iterator start_it, std::vector< Point2 >::const_iterator end_it)
Adds points to the bounding box.
Definition: Bbox2.h:161
void reset()
Resets the bounds.
Definition: Bbox2.h:62
double getRangeX() const
Retrieves the X-range of the bounding box.
Definition: Bbox2.h:358
Bbox2 operator+(const Bbox2 &b)
Adds another bounding box to this one.
double getMaxRange() const
Retrieves the largest range of the bounding box.
Definition: Bbox2.h:378
double get_minX() const
Retrieves the minX value.
Definition: Bbox2.h:391
Point2 computeCenter() const
Computes the 2D midpoint of the bounding box.
void setMaxY(double val)
Sets the maxY value.
Definition: Bbox2.h:476
double get_maxX() const
Retrieves the maxX value.
Definition: Bbox2.h:404
double get_maxY() const
Retrieves the maxY value.
Definition: Bbox2.h:411
void setMinY(double val)
Sets the minY value.
Definition: Bbox2.h:464
Point2 getMaxPoint() const
Retrieves the maximum corner point of the bounding box.
Definition: Bbox2.h:322
void setMinX(double val)
Sets the minX value.
Definition: Bbox2.h:440
void inflateIfDegenerate(double val)
Inflates the bounding box if degenerate.
Definition: Bbox2.h:506
bool isValid() const
Checks if the bounds are valid.
Definition: Bbox2.h:77
bool doIntersect(const Bbox2 &other) const
Checks if this bounding box intersects with another.
double getRangeY() const
Retrieves the Y-range of the bounding box.
Definition: Bbox2.h:368
Bbox2(GeomTest *pGeomTest_=NULL)
Default constructor.
Definition: Bbox2.h:47
bool add(size_t numPoints, double *coordinates)
Adds points to the bounding box.
Definition: Bbox2.h:191
Point2 getMinPoint() const
Retrieves the minimum corner point of the bounding box.
Definition: Bbox2.h:306
double getMinCoord() const
Retrieves the smallest coordinate value.
Definition: Bbox2.h:337
void enlargeRanges(double factor, bool bUseMaxRange, double minRange)
Enlarges the bounding box's ranges symmetrically.
double get_minY() const
Retrieves the minY value.
Definition: Bbox2.h:397
void getBounds(double &minX_, double &maxX_, double &minY_, double &maxY_) const
Retrieves the bounding box coordinates.
Represents a 2D point.
Definition: Point2.h:76
void xy(double &x_, double &y_) const
Get the x- and y- coordinates of the Point2.
Definition: Point2.h:291