Fade2.5D Documentation pages v1.86
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 "Point2.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 
52 
53 
60  bool isValid() const
61  {
62  return minX<DBL_MAX;
63  }
64 
69  void getCorners(std::vector<Point2>& vBoxCorners) const;
70 
76  void getOffsetCorners(double offset,std::vector<Point2>& vBoxCorners) const;
77 
78 
83  bool doIntersect(const Bbox2& other) const;
84 
91  bool add(std::vector<Point2*>::const_iterator start_it,std::vector<Point2*>::const_iterator end_it)
92  {
93  if(start_it==end_it) return false;
94  if(bValid)
95  {
96  double oldMinX(minX),oldMinY(minY),oldMaxX(maxX),oldMaxY(maxY);
97  for(;start_it!=end_it;++start_it) treatPointForValidBox(**start_it);
98  if(oldMinX!=minX || oldMinY!=minY || oldMaxX!=maxX || oldMaxY!=maxY ) return true;
99  return false;
100  }
101  else
102  {
103  treatPointForInvalidBox(**start_it);
104  ++start_it;
105  for(;start_it!=end_it;++start_it) treatPointForValidBox(**start_it);
106  return true;
107  }
108  }
115  bool add(std::vector<Point2>::const_iterator start_it,std::vector<Point2>::const_iterator end_it)
116  {
117  if(start_it==end_it) return false;
118  if(bValid)
119  {
120  double oldMinX(minX),oldMinY(minY),oldMaxX(maxX),oldMaxY(maxY);
121  for(;start_it!=end_it;++start_it) treatPointForValidBox(*start_it);
122  if(oldMinX!=minX || oldMinY!=minY || oldMaxX!=maxX || oldMaxY!=maxY ) return true;
123  return false;
124  }
125  else
126  {
127  treatPointForInvalidBox(*start_it);
128  ++start_it;
129  for(;start_it!=end_it;++start_it) treatPointForValidBox(*start_it);
130  return true;
131  }
132  }
133 
140  bool add(size_t numPoints,double * coordinates)
141  {
142 #if GEOM_PSEUDO3D==GEOM_TRUE
143  const int NUMCOMPONENTS(3);
144 #else
145  const int NUMCOMPONENTS(2);
146 #endif
147 
148  if(numPoints==0) return false;
149  double oldMinX(minX),oldMinY(minY),oldMaxX(maxX),oldMaxY(maxY);
150  double firstX(coordinates[0]);
151  double firstY(coordinates[1]);
152  if(firstX<minX) minX=firstX;
153  if(firstX>maxX) maxX=firstX;
154  if(firstY<minY) minY=firstY;
155  if(firstY>maxY) maxY=firstY;
156 
157  for(size_t i=0;i<numPoints;++i)
158  {
159  double x(coordinates[NUMCOMPONENTS*i]);
160  double y(coordinates[NUMCOMPONENTS*i+1]);
161  if(x<minX) minX=x;
162  else if(x>maxX) maxX=x;
163  if(y<minY) minY=y;
164  else if(y>maxY) maxY=y;
165  }
166  bValid=true;
167  if(oldMinX!=minX || oldMinY!=minY || oldMaxX!=maxX || oldMaxY!=maxY ) return true;
168  else return false;
169  }
170 
177  bool add(const Point2& p)
178  {
179  //std::cout<<"Add point: "<<p<<std::endl;
180  if(bValid)
181  {
182  double oldMinX(minX),oldMinY(minY),oldMaxX(maxX),oldMaxY(maxY);
183  treatPointForValidBox(p);
184  if(oldMinX!=minX || oldMinY!=minY || oldMaxX!=maxX || oldMaxY!=maxY ) return true;
185  else return false;
186  }
187  else
188  {
189  treatPointForInvalidBox(p);
190  return true;
191  }
192  }
193 
198  bool isInBox(const Point2& p) const;
199 
203  Point2 computeCenter() const;
204 
210  Bbox2 operator+(const Bbox2& b);
211 
212 
213 #if GEOM_PSEUDO3D==GEOM_TRUE
214 
218 #else
219 
223 #endif
224 
226  {
227 #if GEOM_PSEUDO3D==GEOM_TRUE
228  return Point2(minX,minY,0);
229 #else
230  return Point2(minX,minY);
231 #endif
232  }
233 
234 
235 #if GEOM_PSEUDO3D==GEOM_TRUE
236 
240 #else
241 
245 #endif
246 
248  {
249 #if GEOM_PSEUDO3D==GEOM_TRUE
250  return Point2(maxX,maxY,0);
251 #else
252  return Point2(maxX,maxY);
253 #endif
254  }
255 
261  double getMinCoord() const
262  {
263  return (std::min)(minX,minY);
264  }
270  double getMaxCoord() const
271  {
272  return (std::max)(maxX,maxY);
273  }
274 
280  double getRangeX() const
281  {
282  return maxX-minX;
283  }
288  double getRangeY() const
289  {
290  return maxY-minY;
291  }
292 
298  double getMaxRange() const
299  {
300  double range0=getRangeX();
301  double range1=getRangeY();
302  if(range0>range1) return range0;
303  return range1;
304  }
305 
310  double get_minX() const {return minX;}
315  double get_minY() const {return minY;}
320  double get_maxX() const {return maxX;}
325  double get_maxY() const {return maxY;}
326 
330  void getBounds(double& minX_,double& maxX_,double& minY_,double& maxY_) const;
331 
332 
338  void doubleTheBox();
339 
342  void setMinX(double val)
343  {
344  minX=val;
345  if(minX<=maxX && minY<=maxY) bValid=true;
346  }
349  void setMaxX(double val)
350  {
351  maxX=val;
352  if(minX<=maxX && minY<=maxY) bValid=true;
353  }
356  void setMinY(double val)
357  {
358  minY=val;
359  if(minX<=maxX && minY<=maxY) bValid=true;
360  }
363  void setMaxY(double val)
364  {
365  maxY=val;
366  if(minX<=maxX && minY<=maxY) bValid=true;
367  }
368 
369  void enlargeRanges(double factor);
377  void inflateIfDegenerate(double val)
378  {
379  if(bValid)
380  {
381  if(minX==maxX) maxX+=val;
382  if(minY==maxY) maxY+=val;
383  }
384  }
385 protected:
386  inline void treatPointForValidBox(const Point2& p)
387  {
388  double x,y;
389  p.xy(x,y);
390  if(x<minX) minX=x;
391  else if(x>maxX) maxX=x;
392  if(y<minY) minY=y;
393  else if(y>maxY) maxY=y;
394  }
395  inline void treatPointForInvalidBox(const Point2& p)
396  {
397  // Individual bounds may have been set already. Keep them!
398  if(minX==DBL_MAX) minX=p.x();
399  if(minY==DBL_MAX) minY=p.y();
400  if(maxX==-DBL_MAX) maxX=p.x();
401  if(maxY==-DBL_MAX) maxY=p.y();
402  bValid=true;
403  }
404  friend std::ostream &operator<<(std::ostream &stream, const Bbox2& pC);
405 protected:
406  double minX,minY;
407  double maxX,maxY;
408  bool bValid;
409  friend std::ostream &operator<<(std::ostream &stream, Bbox2& pC);
410  GeomTest* pGeomTest;
411 };
412 
413 Bbox2 getBox(std::vector<Point2>& vP);
414 Bbox2 getBox(std::vector<Point2*>& vP);
415 
416 inline std::ostream &operator<<(std::ostream &stream, const Bbox2& pC)
417 {
418  stream<<"Bbox2: ("<<pC.minX<<","<<pC.minY<<") -> ("<<pC.maxX<<","<<pC.maxY<<"), rangeX="<<pC.getRangeX()<<", rangeY="<<pC.getRangeY();
419  return stream;
420 }
421 
422 
423 
424 } // (namespace)
GEOM_FADE25D::Bbox2::add
bool add(std::vector< Point2 >::const_iterator start_it, std::vector< Point2 >::const_iterator end_it)
Add points.
Definition: Bbox2.h:115
GEOM_FADE25D::Bbox2::inflateIfDegenerate
void inflateIfDegenerate(double val)
Inflate if Degenerate.
Definition: Bbox2.h:377
GEOM_FADE25D::Bbox2::getRangeX
double getRangeX() const
Get x-range.
Definition: Bbox2.h:280
GEOM_FADE25D::Bbox2::getMaxRange
double getMaxRange() const
Get max range.
Definition: Bbox2.h:298
GEOM_FADE25D::Bbox2::get_maxY
double get_maxY() const
Get maxY.
Definition: Bbox2.h:325
GEOM_FADE25D::Bbox2::setMaxY
void setMaxY(double val)
Set maxY.
Definition: Bbox2.h:363
GEOM_FADE25D::Bbox2::isValid
bool isValid() const
Check if the bounds are valid.
Definition: Bbox2.h:60
GEOM_FADE25D::Bbox2::get_maxX
double get_maxX() const
Get maxX.
Definition: Bbox2.h:320
GEOM_FADE25D::Bbox2::getMinCoord
double getMinCoord() const
Get minimum coordinate.
Definition: Bbox2.h:261
GEOM_FADE25D::Bbox2::get_minX
double get_minX() const
Get minX.
Definition: Bbox2.h:310
GEOM_FADE25D::Bbox2::Bbox2
Bbox2(GeomTest *pGeomTest_=NULL)
Constructor.
Definition: Bbox2.h:45
GEOM_FADE25D::Bbox2::setMinX
void setMinX(double val)
Set minX.
Definition: Bbox2.h:342
GEOM_FADE25D::Bbox2::get_minY
double get_minY() const
Get minY.
Definition: Bbox2.h:315
GEOM_FADE25D::Bbox2::setMaxX
void setMaxX(double val)
Set maxX.
Definition: Bbox2.h:349
GEOM_FADE25D::Bbox2::getMaxCoord
double getMaxCoord() const
Get maximum coordinate.
Definition: Bbox2.h:270
GEOM_FADE25D::Bbox2::setMinY
void setMinY(double val)
Set minY.
Definition: Bbox2.h:356
GEOM_FADE25D::Point2
Point.
Definition: Point2.h:40
GEOM_FADE25D::Bbox2::add
bool add(std::vector< Point2 * >::const_iterator start_it, std::vector< Point2 * >::const_iterator end_it)
Add points.
Definition: Bbox2.h:91
GEOM_FADE25D::Bbox2::getRangeY
double getRangeY() const
Get y-range.
Definition: Bbox2.h:288
GEOM_FADE25D::Point2::xy
void xy(double &x_, double &y_) const
Get the x- and y-coordinate.
Definition: Point2.h:243
GEOM_FADE25D::Bbox2
Bbox2 is an axis aligned 2D bounding box.
Definition: Bbox2.h:36
GEOM_FADE25D::Bbox2::add
bool add(size_t numPoints, double *coordinates)
Add points.
Definition: Bbox2.h:140
GEOM_FADE25D::Bbox2::add
bool add(const Point2 &p)
Add a point.
Definition: Bbox2.h:177
GEOM_FADE25D::Bbox2::getMaxPoint
Point2 getMaxPoint() const
Get the max point.
Definition: Bbox2.h:247
GEOM_FADE25D::Bbox2::getMinPoint
Point2 getMinPoint() const
Get the min point.
Definition: Bbox2.h:225