Fade2.5D Documentation pages v1.90
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  bValid(false),pGeomTest(pGeomTest_)
49  {
50  }
51  ~Bbox2();
52 
53 
54 
61  bool isValid() const
62  {
63  return minX<DBL_MAX;
64  }
65 
70  void getCorners(std::vector<Point2>& vBoxCorners) const;
71 
76  void getBoundary(std::vector<Segment2>& vBoundary) const;
77 
83  void getOffsetCorners(double offset,std::vector<Point2>& vBoxCorners) const;
84 
85 
90  bool doIntersect(const Bbox2& other) const;
91 
98  bool add(std::vector<Point2*>::const_iterator start_it,std::vector<Point2*>::const_iterator end_it)
99  {
100  if(start_it==end_it) return false;
101  if(bValid)
102  {
103  double oldMinX(minX),oldMinY(minY),oldMaxX(maxX),oldMaxY(maxY);
104  for(;start_it!=end_it;++start_it) treatPointForValidBox(**start_it);
105  if(oldMinX!=minX || oldMinY!=minY || oldMaxX!=maxX || oldMaxY!=maxY ) return true;
106  return false;
107  }
108  else
109  {
110  treatPointForInvalidBox(**start_it);
111  ++start_it;
112  for(;start_it!=end_it;++start_it) treatPointForValidBox(**start_it);
113  return true;
114  }
115  }
122  bool add(std::vector<Point2>::const_iterator start_it,std::vector<Point2>::const_iterator end_it)
123  {
124  if(start_it==end_it) return false;
125  if(bValid)
126  {
127  double oldMinX(minX),oldMinY(minY),oldMaxX(maxX),oldMaxY(maxY);
128  for(;start_it!=end_it;++start_it) treatPointForValidBox(*start_it);
129  if(oldMinX!=minX || oldMinY!=minY || oldMaxX!=maxX || oldMaxY!=maxY ) return true;
130  return false;
131  }
132  else
133  {
134  treatPointForInvalidBox(*start_it);
135  ++start_it;
136  for(;start_it!=end_it;++start_it) treatPointForValidBox(*start_it);
137  return true;
138  }
139  }
140 
147  bool add(size_t numPoints,double * coordinates)
148  {
149 #if GEOM_PSEUDO3D==GEOM_TRUE
150  const int NUMCOMPONENTS(3);
151 #else
152  const int NUMCOMPONENTS(2);
153 #endif
154 
155  if(numPoints==0) return false;
156  double oldMinX(minX),oldMinY(minY),oldMaxX(maxX),oldMaxY(maxY);
157  double firstX(coordinates[0]);
158  double firstY(coordinates[1]);
159  if(firstX<minX) minX=firstX;
160  if(firstX>maxX) maxX=firstX;
161  if(firstY<minY) minY=firstY;
162  if(firstY>maxY) maxY=firstY;
163 
164  for(size_t i=0;i<numPoints;++i)
165  {
166  double x(coordinates[NUMCOMPONENTS*i]);
167  double y(coordinates[NUMCOMPONENTS*i+1]);
168  if(x<minX) minX=x;
169  else if(x>maxX) maxX=x;
170  if(y<minY) minY=y;
171  else if(y>maxY) maxY=y;
172  }
173  bValid=true;
174  if(oldMinX!=minX || oldMinY!=minY || oldMaxX!=maxX || oldMaxY!=maxY ) return true;
175  else return false;
176  }
177 
184  bool add(const Point2& p)
185  {
186  //std::cout<<"Add point: "<<p<<std::endl;
187  if(bValid)
188  {
189  double oldMinX(minX),oldMinY(minY),oldMaxX(maxX),oldMaxY(maxY);
190  treatPointForValidBox(p);
191  if(oldMinX!=minX || oldMinY!=minY || oldMaxX!=maxX || oldMaxY!=maxY ) return true;
192  else return false;
193  }
194  else
195  {
196  treatPointForInvalidBox(p);
197  return true;
198  }
199  }
200 
205  bool isInBox(const Point2& p) const;
206 
211 
217  Bbox2 operator+(const Bbox2& b);
218 
219 
220 #if GEOM_PSEUDO3D==GEOM_TRUE
225 #else
230 #endif
231 
233  {
234 #if GEOM_PSEUDO3D==GEOM_TRUE
235  return Point2(minX,minY,0);
236 #else
237  return Point2(minX,minY);
238 #endif
239  }
240 
241 
242 #if GEOM_PSEUDO3D==GEOM_TRUE
247 #else
252 #endif
253 
255  {
256 #if GEOM_PSEUDO3D==GEOM_TRUE
257  return Point2(maxX,maxY,0);
258 #else
259  return Point2(maxX,maxY);
260 #endif
261  }
262 
268  double getMinCoord() const
269  {
270  return (std::min)(minX,minY);
271  }
277  double getMaxCoord() const
278  {
279  return (std::max)(maxX,maxY);
280  }
281 
287  double getRangeX() const
288  {
289  return maxX-minX;
290  }
295  double getRangeY() const
296  {
297  return maxY-minY;
298  }
299 
305  double getMaxRange() const
306  {
307  double range0=getRangeX();
308  double range1=getRangeY();
309  if(range0>range1) return range0;
310  return range1;
311  }
312 
317  double get_minX() const {return minX;}
322  double get_minY() const {return minY;}
327  double get_maxX() const {return maxX;}
332  double get_maxY() const {return maxY;}
333 
337  void getBounds(double& minX_,double& maxX_,double& minY_,double& maxY_) const;
338 
339 
345  void doubleTheBox();
346 
349  void setMinX(double val)
350  {
351  minX=val;
352  if(minX<=maxX && minY<=maxY) bValid=true;
353  }
356  void setMaxX(double val)
357  {
358  maxX=val;
359  if(minX<=maxX && minY<=maxY) bValid=true;
360  }
363  void setMinY(double val)
364  {
365  minY=val;
366  if(minX<=maxX && minY<=maxY) bValid=true;
367  }
370  void setMaxY(double val)
371  {
372  maxY=val;
373  if(minX<=maxX && minY<=maxY) bValid=true;
374  }
375 
376  void enlargeRanges(double factor);
384  void inflateIfDegenerate(double val)
385  {
386  if(bValid)
387  {
388  if(minX==maxX) maxX+=val;
389  if(minY==maxY) maxY+=val;
390  }
391  }
392 protected:
393  inline void treatPointForValidBox(const Point2& p)
394  {
395  double x,y;
396  p.xy(x,y);
397  if(x<minX) minX=x;
398  else if(x>maxX) maxX=x;
399  if(y<minY) minY=y;
400  else if(y>maxY) maxY=y;
401  }
402  inline void treatPointForInvalidBox(const Point2& p)
403  {
404  // Individual bounds may have been set already. Keep them!
405  if(minX==DBL_MAX) minX=p.x();
406  if(minY==DBL_MAX) minY=p.y();
407  if(maxX==-DBL_MAX) maxX=p.x();
408  if(maxY==-DBL_MAX) maxY=p.y();
409  bValid=true;
410  }
411  friend std::ostream &operator<<(std::ostream &stream, const Bbox2& pC);
412 protected:
413  double minX,minY;
414  double maxX,maxY;
415  bool bValid;
416 
417  GeomTest* pGeomTest;
418 
419 };
420 
425 Bbox2 getBox(std::vector<Point2>& vP);
430 Bbox2 getBox(std::vector<Point2*>& vP);
436 inline std::ostream &operator<<(std::ostream &stream, const Bbox2& pC)
437 {
438  stream<<"Bbox2: xy("<<pC.minX<<","<<pC.minY<<") -> xy("<<pC.maxX<<","<<pC.maxY<<"), rangeX="<<pC.getRangeX()<<", rangeY="<<pC.getRangeY();
439  return stream;
440 }
441 
442 
443 
444 } // (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:436
Bbox2 is an axis aligned 2D bounding box.
Definition: Bbox2.h:37
Point2 getMaxPoint() const
Get the max point.
Definition: Bbox2.h:254
double getMaxCoord() const
Get maximum coordinate.
Definition: Bbox2.h:277
bool add(size_t numPoints, double *coordinates)
Add points.
Definition: Bbox2.h:147
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:356
void setMaxY(double val)
Set maxY.
Definition: Bbox2.h:370
void getCorners(std::vector< Point2 > &vBoxCorners) const
Get corners.
bool add(const Point2 &p)
Add a point.
Definition: Bbox2.h:184
double getRangeX() const
Get x-range.
Definition: Bbox2.h:287
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:98
double getMaxRange() const
Get max range.
Definition: Bbox2.h:305
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:295
double get_minX() const
Get minX.
Definition: Bbox2.h:317
void inflateIfDegenerate(double val)
Inflate if Degenerate.
Definition: Bbox2.h:384
void setMinX(double val)
Set minX.
Definition: Bbox2.h:349
double getMinCoord() const
Get minimum coordinate.
Definition: Bbox2.h:268
double get_maxY() const
Get maxY.
Definition: Bbox2.h:332
bool isValid() const
Check if the bounds are valid.
Definition: Bbox2.h:61
void setMinY(double val)
Set minY.
Definition: Bbox2.h:363
bool add(std::vector< Point2 >::const_iterator start_it, std::vector< Point2 >::const_iterator end_it)
Add points.
Definition: Bbox2.h:122
Point2 getMinPoint() const
Get the min point.
Definition: Bbox2.h:232
bool isInBox(const Point2 &p) const
Point-in-Box Test.
double get_maxX() const
Get maxX.
Definition: Bbox2.h:327
void getBoundary(std::vector< Segment2 > &vBoundary) const
Get boundary.
double get_minY() const
Get minY.
Definition: Bbox2.h:322
Point.
Definition: Point2.h:43
void xy(double &x_, double &y_) const
Get the x- and y-coordinate.
Definition: Point2.h:245