Fade2.5D Documentation pages v2.03
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 // not clear to you.
14 //
15 // Author: Bernhard Kornberger, bkorn (at) geom.at
16 // http://www.geom.at
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 
32 
36 class CLASS_DECLSPEC Bbox2
37 {
38 public:
45  explicit Bbox2(GeomTest* pGeomTest_=NULL):
46  minX(DBL_MAX),minY(DBL_MAX),
47  maxX(-DBL_MAX),maxY(-DBL_MAX),
48  maxAbsCoord(0),
49  bValid(false),pGeomTest(pGeomTest_)
50  {
51  }
52  ~Bbox2();
53 
54 
59  void reset()
60  {
61  minX=DBL_MAX;
62  minY=DBL_MAX;
63  maxX=-DBL_MAX;
64  maxY=-DBL_MAX;
65  bValid=false;
66  }
67 
68 
75  bool isValid() const
76  {
77  return minX<DBL_MAX;
78  }
79 
84  void getCorners(std::vector<Point2>& vBoxCorners) const;
85 
90  void getBoundary(std::vector<Segment2>& vBoundary) const;
91 
97  void getOffsetCorners(double offset,std::vector<Point2>& vBoxCorners) const;
98 
99 
104  bool doIntersect(const Bbox2& other) const;
105 
111  bool add(std::vector<Point2*>::const_iterator start_it,std::vector<Point2*>::const_iterator end_it)
112  {
113  if(start_it==end_it) return false;
114  if(bValid)
115  {
116  double oldMinX(minX),oldMinY(minY),oldMaxX(maxX),oldMaxY(maxY);
117  for(;start_it!=end_it;++start_it) treatPointForValidBox(**start_it);
118  updateMaxAbs();
119  if(oldMinX!=minX || oldMinY!=minY || oldMaxX!=maxX || oldMaxY!=maxY ) return true;
120  return false;
121  }
122  else
123  {
124  treatPointForInvalidBox(**start_it);
125  ++start_it;
126  for(;start_it!=end_it;++start_it) treatPointForValidBox(**start_it);
127  updateMaxAbs();
128  return true;
129  }
130 
131  }
138  bool add(std::vector<Point2>::const_iterator start_it,std::vector<Point2>::const_iterator end_it)
139  {
140  if(start_it==end_it) return false;
141  if(bValid)
142  {
143  double oldMinX(minX),oldMinY(minY),oldMaxX(maxX),oldMaxY(maxY);
144  for(;start_it!=end_it;++start_it) treatPointForValidBox(*start_it);
145  updateMaxAbs();
146  if(oldMinX!=minX || oldMinY!=minY || oldMaxX!=maxX || oldMaxY!=maxY ) return true;
147  return false;
148  }
149  else
150  {
151  treatPointForInvalidBox(*start_it);
152  ++start_it;
153  for(;start_it!=end_it;++start_it) treatPointForValidBox(*start_it);
154  updateMaxAbs();
155  return true;
156  }
157  }
158 
165  bool add(size_t numPoints,double * coordinates)
166  {
167 #if GEOM_PSEUDO3D==GEOM_TRUE
168  const int NUMCOMPONENTS(3);
169 #else
170  const int NUMCOMPONENTS(2);
171 #endif
172 
173  if(numPoints==0) return false;
174  double oldMinX(minX),oldMinY(minY),oldMaxX(maxX),oldMaxY(maxY);
175  double firstX(coordinates[0]);
176  double firstY(coordinates[1]);
177  if(firstX<minX) minX=firstX;
178  if(firstX>maxX) maxX=firstX;
179  if(firstY<minY) minY=firstY;
180  if(firstY>maxY) maxY=firstY;
181 
182  for(size_t i=0;i<numPoints;++i)
183  {
184  double x(coordinates[NUMCOMPONENTS*i]);
185  double y(coordinates[NUMCOMPONENTS*i+1]);
186  if(x<minX) minX=x;
187  else if(x>maxX) maxX=x;
188  if(y<minY) minY=y;
189  else if(y>maxY) maxY=y;
190  }
191  bValid=true;
192  updateMaxAbs();
193  if(oldMinX!=minX || oldMinY!=minY || oldMaxX!=maxX || oldMaxY!=maxY ) return true;
194  else return false;
195  }
196 
203  bool add(const Point2& p)
204  {
205  //std::cout<<"Add point: "<<p<<std::endl;
206  if(bValid)
207  {
208  double oldMinX(minX),oldMinY(minY),oldMaxX(maxX),oldMaxY(maxY);
209  treatPointForValidBox(p);
210  updateMaxAbs();
211  if(oldMinX!=minX || oldMinY!=minY || oldMaxX!=maxX || oldMaxY!=maxY ) return true;
212  else return false;
213  }
214  else
215  {
216  treatPointForInvalidBox(p);
217  updateMaxAbs();
218  return true;
219  }
220  }
226  bool add(const Bbox2& other)
227  {
228  bool bRet(false);
229  if(other.minX<minX) {minX=other.minX;bRet=true;}
230  if(other.minY<minY) {minY=other.minY;bRet=true;}
231  if(other.maxX>maxX) {maxX=other.maxX;bRet=true;}
232  if(other.maxY>maxY) {maxY=other.maxY;bRet=true;}
233  updateMaxAbs();
234  return bRet;
235  }
236 
241  bool isInBox(const Point2& p) const;
242 
247 
253  Bbox2 operator+(const Bbox2& b);
254 
255 
256 #if GEOM_PSEUDO3D==GEOM_TRUE
261 #else
266 #endif
267 
269  {
270 #if GEOM_PSEUDO3D==GEOM_TRUE
271  return Point2(minX,minY,0);
272 #else
273  return Point2(minX,minY);
274 #endif
275  }
276 
277 
278 #if GEOM_PSEUDO3D==GEOM_TRUE
283 #else
288 #endif
289 
291  {
292 #if GEOM_PSEUDO3D==GEOM_TRUE
293  return Point2(maxX,maxY,0);
294 #else
295  return Point2(maxX,maxY);
296 #endif
297  }
298 
304  double getMinCoord() const
305  {
306  return (std::min)(minX,minY);
307  }
313  double getMaxCoord() const
314  {
315  return (std::max)(maxX,maxY);
316  }
317 
323  double getRangeX() const
324  {
325  return maxX-minX;
326  }
331  double getRangeY() const
332  {
333  return maxY-minY;
334  }
335 
340  double getMaxRange() const
341  {
342  double range0=getRangeX();
343  double range1=getRangeY();
344  if(range0>range1) return range0;
345  return range1;
346  }
347 
352  double get_minX() const {return minX;}
357  double get_minY() const {return minY;}
362  double get_maxX() const {return maxX;}
367  double get_maxY() const {return maxY;}
368 
372  void getBounds(double& minX_,double& maxX_,double& minY_,double& maxY_) const;
373 
374 
380  void doubleTheBox();
381 
384  void setMinX(double val)
385  {
386  minX=val;
387  updateMaxAbs();
388  if(minX<=maxX && minY<=maxY) bValid=true;
389  }
392  void setMaxX(double val)
393  {
394  maxX=val;
395  updateMaxAbs();
396  if(minX<=maxX && minY<=maxY) bValid=true;
397  }
400  void setMinY(double val)
401  {
402  minY=val;
403  updateMaxAbs();
404  if(minX<=maxX && minY<=maxY) bValid=true;
405  }
408  void setMaxY(double val)
409  {
410  maxY=val;
411  updateMaxAbs();
412  if(minX<=maxX && minY<=maxY) bValid=true;
413  }
414 
428  void enlargeRanges(double factor,bool bUseMaxRange,double minRange);
429 
437  void inflateIfDegenerate(double val)
438  {
439  if(bValid)
440  {
441  if(minX==maxX) maxX+=val;
442  if(minY==maxY) maxY+=val;
443  }
444  updateMaxAbs();
445  }
446 protected:
447  inline void treatPointForValidBox(const Point2& p)
448  {
449  double x,y;
450  p.xy(x,y);
451  if(x<minX) minX=x;
452  else if(x>maxX) maxX=x;
453  if(y<minY) minY=y;
454  else if(y>maxY) maxY=y;
455  updateMaxAbs();
456  }
457  inline void treatPointForInvalidBox(const Point2& p)
458  {
459  // Individual bounds may have been set already. Keep them!
460  if(minX==DBL_MAX) minX=p.x();
461  if(minY==DBL_MAX) minY=p.y();
462  if(maxX==-DBL_MAX) maxX=p.x();
463  if(maxY==-DBL_MAX) maxY=p.y();
464  updateMaxAbs();
465  bValid=true;
466  }
467  friend std::ostream &operator<<(std::ostream &stream, const Bbox2& pC);
468 protected:
469  double minX,minY;
470  double maxX,maxY;
471  double maxAbsCoord;
472  bool bValid;
473  GeomTest* pGeomTest;
474  bool updateMaxAbs();
475 };
476 
481 Bbox2 getBox(std::vector<Point2>& vP);
486 Bbox2 getBox(std::vector<Point2*>& vP);
492 inline std::ostream &operator<<(std::ostream &stream, const Bbox2& pC)
493 {
494  stream<<"Bbox2: xy("<<pC.minX<<","<<pC.minY<<") -> xy("<<pC.maxX<<","<<pC.maxY<<"), rangeX="<<pC.getRangeX()<<", rangeY="<<pC.getRangeY();
495  return stream;
496 }
497 
498 
499 
500 } // (namespace)
Bbox2 getBox(std::vector< Point2 > &vP)
Compute the bounding box.
std::ostream & operator<<(std::ostream &stream, const Bbox2 &pC)
Print the box.
Definition: Bbox2.h:492
Bbox2 is an axis aligned 2D bounding box.
Definition: Bbox2.h:37
Point2 getMaxPoint() const
Get the max point.
Definition: Bbox2.h:290
double getMaxCoord() const
Get maximum coordinate.
Definition: Bbox2.h:313
bool add(size_t numPoints, double *coordinates)
Add points.
Definition: Bbox2.h:165
void reset()
Reset the bounds.
Definition: Bbox2.h:59
bool add(const Bbox2 &other)
Add another bounding box.
Definition: Bbox2.h:226
Bbox2 operator+(const Bbox2 &b)
Add a bounding box.
Bbox2(GeomTest *pGeomTest_=NULL)
Constructor.
Definition: Bbox2.h:45
void setMaxX(double val)
Set maxX.
Definition: Bbox2.h:392
void setMaxY(double val)
Set maxY.
Definition: Bbox2.h:408
void getCorners(std::vector< Point2 > &vBoxCorners) const
Get corners.
bool add(const Point2 &p)
Add a point.
Definition: Bbox2.h:203
double getRangeX() const
Get x-range.
Definition: Bbox2.h:323
Point2 computeCenter() const
Compute the 2D midpoint.
bool add(std::vector< Point2 * >::const_iterator start_it, std::vector< Point2 * >::const_iterator end_it)
Add points.
Definition: Bbox2.h:111
double getMaxRange() const
Get max range.
Definition: Bbox2.h:340
void getOffsetCorners(double offset, std::vector< Point2 > &vBoxCorners) const
Get offset corners.
bool doIntersect(const Bbox2 &other) const
Check intersection.
void doubleTheBox()
Double the box.
void getBounds(double &minX_, double &maxX_, double &minY_, double &maxY_) const
Get bounds.
double getRangeY() const
Get y-range.
Definition: Bbox2.h:331
double get_minX() const
Get minX.
Definition: Bbox2.h:352
void inflateIfDegenerate(double val)
Inflate if Degenerate.
Definition: Bbox2.h:437
void setMinX(double val)
Set minX.
Definition: Bbox2.h:384
double getMinCoord() const
Get minimum coordinate.
Definition: Bbox2.h:304
double get_maxY() const
Get maxY.
Definition: Bbox2.h:367
bool isValid() const
Check if the bounds are valid.
Definition: Bbox2.h:75
void setMinY(double val)
Set minY.
Definition: Bbox2.h:400
bool add(std::vector< Point2 >::const_iterator start_it, std::vector< Point2 >::const_iterator end_it)
Add points.
Definition: Bbox2.h:138
Point2 getMinPoint() const
Get the min point.
Definition: Bbox2.h:268
bool isInBox(const Point2 &p) const
Point-in-Box Test.
double get_maxX() const
Get maxX.
Definition: Bbox2.h:362
void getBoundary(std::vector< Segment2 > &vBoundary) const
Get boundary.
void enlargeRanges(double factor, bool bUseMaxRange, double minRange)
Enlarge the x|y-ranges of a bounding box.
double get_minY() const
Get minY.
Definition: Bbox2.h:357
Point.
Definition: Point2.h:43
void xy(double &x_, double &y_) const
Get the x- and y-coordinate.
Definition: Point2.h:245