Fade2.5D Documentation pages v2.17.3
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 hasOn(const Point2& p) const
131  {
132  return p.x()>=minX && p.x()<=maxX && p.y()>=minY && p.y()<=maxY;
133  }
134 
135 
144  bool add(std::vector<Point2*>::const_iterator start_it,std::vector<Point2*>::const_iterator end_it)
145  {
146  if(start_it==end_it) return false;
147  if(bValid)
148  {
149  double oldMinX(minX),oldMinY(minY),oldMaxX(maxX),oldMaxY(maxY);
150  for(;start_it!=end_it;++start_it) treatPointForValidBox(**start_it);
151  updateMaxAbs();
152  if(oldMinX!=minX || oldMinY!=minY || oldMaxX!=maxX || oldMaxY!=maxY ) return true;
153  return false;
154  }
155  else
156  {
157  treatPointForInvalidBox(**start_it);
158  ++start_it;
159  for(;start_it!=end_it;++start_it) treatPointForValidBox(**start_it);
160  updateMaxAbs();
161  return true;
162  }
163 
164  }
165 
175  bool add(std::vector<Point2>::const_iterator start_it,std::vector<Point2>::const_iterator end_it)
176  {
177  if(start_it==end_it) return false;
178  if(bValid)
179  {
180  double oldMinX(minX),oldMinY(minY),oldMaxX(maxX),oldMaxY(maxY);
181  for(;start_it!=end_it;++start_it) treatPointForValidBox(*start_it);
182  updateMaxAbs();
183  if(oldMinX!=minX || oldMinY!=minY || oldMaxX!=maxX || oldMaxY!=maxY ) return true;
184  return false;
185  }
186  else
187  {
188  treatPointForInvalidBox(*start_it);
189  ++start_it;
190  for(;start_it!=end_it;++start_it) treatPointForValidBox(*start_it);
191  updateMaxAbs();
192  return true;
193  }
194  }
195 
205  bool add(size_t numPoints,double * coordinates)
206  {
207 #if GEOM_PSEUDO3D==GEOM_TRUE
208  const int NUMCOMPONENTS(3);
209 #else
210  const int NUMCOMPONENTS(2);
211 #endif
212 
213  if(numPoints==0) return false;
214  double oldMinX(minX),oldMinY(minY),oldMaxX(maxX),oldMaxY(maxY);
215  double firstX(coordinates[0]);
216  double firstY(coordinates[1]);
217  if(firstX<minX) minX=firstX;
218  if(firstX>maxX) maxX=firstX;
219  if(firstY<minY) minY=firstY;
220  if(firstY>maxY) maxY=firstY;
221 
222  for(size_t i=0;i<numPoints;++i)
223  {
224  double x(coordinates[NUMCOMPONENTS*i]);
225  double y(coordinates[NUMCOMPONENTS*i+1]);
226  if(x<minX) minX=x;
227  else if(x>maxX) maxX=x;
228  if(y<minY) minY=y;
229  else if(y>maxY) maxY=y;
230  }
231  bValid=true;
232  updateMaxAbs();
233  if(oldMinX!=minX || oldMinY!=minY || oldMaxX!=maxX || oldMaxY!=maxY ) return true;
234  else return false;
235  }
236 
245  bool add(const Point2& p)
246  {
247  //GCOUT<<"Add point: "<<p<<std::endl;
248  if(bValid)
249  {
250  double oldMinX(minX),oldMinY(minY),oldMaxX(maxX),oldMaxY(maxY);
251  treatPointForValidBox(p);
252  updateMaxAbs();
253  if(oldMinX!=minX || oldMinY!=minY || oldMaxX!=maxX || oldMaxY!=maxY ) return true;
254  else return false;
255  }
256  else
257  {
258  treatPointForInvalidBox(p);
259  updateMaxAbs();
260  return true;
261  }
262  }
263 
272  bool add(const Bbox2& other)
273  {
274  bool bRet(false);
275  if(other.minX<minX) {minX=other.minX;bRet=true;}
276  if(other.minY<minY) {minY=other.minY;bRet=true;}
277  if(other.maxX>maxX) {maxX=other.maxX;bRet=true;}
278  if(other.maxY>maxY) {maxY=other.maxY;bRet=true;}
279  updateMaxAbs();
280  return bRet;
281  }
282 
291  bool isInBox(const Point2& p) const;
292 
293 
302 
311  Bbox2 operator+(const Bbox2& b);
312 
313 
322  {
323 #if GEOM_PSEUDO3D==GEOM_TRUE
324  return Point2(minX,minY,0);
325 #else
326  return Point2(minX,minY);
327 #endif
328  }
329 
330 
338  {
339 #if GEOM_PSEUDO3D==GEOM_TRUE
340  return Point2(maxX,maxY,0);
341 #else
342  return Point2(maxX,maxY);
343 #endif
344  }
345 
352  double getMinCoord() const
353  {
354  return (std::min)(minX,minY);
355  }
356 
362  double getMaxCoord() const
363  {
364  return (std::max)(maxX,maxY);
365  }
366 
367 
373  double getRangeX() const
374  {
375  return maxX-minX;
376  }
377 
383  double getRangeY() const
384  {
385  return maxY-minY;
386  }
387 
393  double getMaxRange() const
394  {
395  double range0=getRangeX();
396  double range1=getRangeY();
397  if(range0>range1) return range0;
398  return range1;
399  }
400 
406  double get_minX() const {return minX;}
412  double get_minY() const {return minY;}
413 
419  double get_maxX() const {return maxX;}
420 
426  double get_maxY() const {return maxY;}
427 
438  void getBounds(double& minX_,double& maxX_,double& minY_,double& maxY_) const;
439 
440 
441 
448  void doubleTheBox();
449 
455  void setMinX(double val)
456  {
457  minX=val;
458  updateMaxAbs();
459  if(minX<=maxX && minY<=maxY) bValid=true;
460  }
461 
467  void setMaxX(double val)
468  {
469  maxX=val;
470  updateMaxAbs();
471  if(minX<=maxX && minY<=maxY) bValid=true;
472  }
473 
479  void setMinY(double val)
480  {
481  minY=val;
482  updateMaxAbs();
483  if(minX<=maxX && minY<=maxY) bValid=true;
484  }
485 
491  void setMaxY(double val)
492  {
493  maxY=val;
494  updateMaxAbs();
495  if(minX<=maxX && minY<=maxY) bValid=true;
496  }
497 
498 
510  void enlargeRanges(double factor,bool bUseMaxRange,double minRange);
511 
521  void inflateIfDegenerate(double val)
522  {
523  if(bValid)
524  {
525  if(minX==maxX) maxX+=val;
526  if(minY==maxY) maxY+=val;
527  }
528  updateMaxAbs();
529  }
530 protected:
531  inline void treatPointForValidBox(const Point2& p)
532  {
533  double x,y;
534  p.xy(x,y);
535  if(x<minX) minX=x;
536  else if(x>maxX) maxX=x;
537  if(y<minY) minY=y;
538  else if(y>maxY) maxY=y;
539  updateMaxAbs();
540  }
541  inline void treatPointForInvalidBox(const Point2& p)
542  {
543  // Individual bounds may have been set already. Keep them!
544  if(minX==DBL_MAX) minX=p.x();
545  if(minY==DBL_MAX) minY=p.y();
546  if(maxX==-DBL_MAX) maxX=p.x();
547  if(maxY==-DBL_MAX) maxY=p.y();
548  updateMaxAbs();
549  bValid=true;
550  }
551  friend std::ostream &operator<<(std::ostream &stream, const Bbox2& pC);
552 protected:
553  double minX,minY;
554  double maxX,maxY;
555  double maxAbsCoord;
556  bool bValid;
557  GeomTest* pGeomTest;
558  bool updateMaxAbs();
559  friend Bbox2 intersection(const Bbox2& a,const Bbox2& b);
560 };
561 
568 Bbox2 getBox(std::vector<Point2>& vP);
569 
576  Bbox2 getBox(std::vector<Point2*>& vP);
577 
590 inline Bbox2 intersection(const Bbox2& a, const Bbox2& b)
591 {
592  Bbox2 ret(a);
593  if(b.minX > ret.minX) ret.minX=b.minX;
594  if(b.maxX < ret.maxX) ret.maxX=b.maxX;
595  if(b.minY > ret.minY) ret.minY=b.minY;
596  if(b.maxY < ret.maxY) ret.maxY=b.maxY;
597  if( (ret.minX>ret.maxX) || (ret.minY>ret.maxY) )
598  {
599  ret.bValid=false;
600  ret.minX=DBL_MAX;
601  ret.minY=DBL_MAX;
602  ret.maxX=-DBL_MAX;
603  ret.maxY=-DBL_MAX;
604  }
605  else
606  {
607  ret.bValid = true;
608  }
609  return ret;
610 }
611 
612 
620 inline std::ostream &operator<<(std::ostream &stream, const Bbox2& pC)
621 {
622  stream<<"Bbox2: xy("<<pC.minX<<","<<pC.minY<<") -> xy("<<pC.maxX<<","<<pC.maxY<<"), rangeX="<<pC.getRangeX()<<", rangeY="<<pC.getRangeY();
623  return stream;
624 }
625 
626 
627 
628 } // (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:590
std::ostream & operator<<(std::ostream &stream, const Bbox2 &pC)
Prints the bounding box to a stream.
Definition: Bbox2.h:620
A 2D axis-aligned bounding box.
Definition: Bbox2.h:39
Point2 getMaxPoint() const
Retrieves the maximum corner point of the bounding box.
Definition: Bbox2.h:337
double getMaxCoord() const
Retrieves the largest coordinate value.
Definition: Bbox2.h:362
bool add(size_t numPoints, double *coordinates)
Adds points to the bounding box.
Definition: Bbox2.h:205
void reset()
Resets the bounds.
Definition: Bbox2.h:62
bool add(const Bbox2 &other)
Adds another bounding box to this one.
Definition: Bbox2.h:272
Bbox2 operator+(const Bbox2 &b)
Adds another bounding box to this one.
Bbox2(GeomTest *pGeomTest_=NULL)
Default constructor.
Definition: Bbox2.h:47
void setMaxX(double val)
Sets the maxX value.
Definition: Bbox2.h:467
void setMaxY(double val)
Sets the maxY value.
Definition: Bbox2.h:491
void getCorners(std::vector< Point2 > &vBoxCorners) const
Retrieves the corners of the bounding box.
bool add(const Point2 &p)
Adds a single point to the bounding box.
Definition: Bbox2.h:245
double getRangeX() const
Retrieves the X-range of the bounding box.
Definition: Bbox2.h:373
Point2 computeCenter() const
Computes the 2D midpoint 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:144
double getMaxRange() const
Retrieves the largest range of the bounding box.
Definition: Bbox2.h:393
void getOffsetCorners(double offset, std::vector< Point2 > &vBoxCorners) const
Retrieves the offset corners of the bounding box.
bool doIntersect(const Bbox2 &other) const
Checks if this bounding box intersects with another.
void doubleTheBox()
Doubles the size of the bounding box.
void getBounds(double &minX_, double &maxX_, double &minY_, double &maxY_) const
Retrieves the bounding box coordinates.
double getRangeY() const
Retrieves the Y-range of the bounding box.
Definition: Bbox2.h:383
double get_minX() const
Retrieves the minX value.
Definition: Bbox2.h:406
void inflateIfDegenerate(double val)
Inflates the bounding box if degenerate.
Definition: Bbox2.h:521
void setMinX(double val)
Sets the minX value.
Definition: Bbox2.h:455
double getMinCoord() const
Retrieves the smallest coordinate value.
Definition: Bbox2.h:352
bool hasOn(const Point2 &p) const
Checks if a query point is included.
Definition: Bbox2.h:130
double get_maxY() const
Retrieves the maxY value.
Definition: Bbox2.h:426
bool isValid() const
Checks if the bounds are valid.
Definition: Bbox2.h:77
void setMinY(double val)
Sets the minY value.
Definition: Bbox2.h:479
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:175
Point2 getMinPoint() const
Retrieves the minimum corner point of the bounding box.
Definition: Bbox2.h:321
bool isInBox(const Point2 &p) const
Checks if a point is inside the bounding box.
double get_maxX() const
Retrieves the maxX value.
Definition: Bbox2.h:419
void getBoundary(std::vector< Segment2 > &vBoundary) const
Retrieves the boundary of the bounding box.
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:412
Represents a 2.5D 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
double y() const
Get the y-coordinate of the Point2.
Definition: Point2.h:250
double x() const
Get the x-coordinate of the Point2.
Definition: Point2.h:239