Geom Software - C++ Programming and Geometry Libraries
Fade2.5D Documentation pages v1.63
Bbox2.h
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
17 
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 
58  Bbox2( std::vector<Point2>::const_iterator start_it,
59  std::vector<Point2>::const_iterator end_it,
60  GeomTest* pGeomTest_=NULL)
61  :
62  minX(DBL_MAX),minY(DBL_MAX),
63  maxX(-DBL_MAX),maxY(DBL_MAX),
64  pGeomTest(pGeomTest_)
65  {
66  add(start_it,end_it);
67  }
68 
69 
76  bool isValid() const
77  {
78  return minX<DBL_MAX;
79  }
80 
85  void getCorners(std::vector<Point2>& vBoxCorners) const;
86 
92  void getOffsetCorners(double offset,std::vector<Point2>& vBoxCorners) const;
93 
94 
99  bool doIntersect(const Bbox2& other) const;
100 
107  bool add(std::vector<Point2*>::const_iterator start_it,std::vector<Point2*>::const_iterator end_it)
108  {
109  if(start_it==end_it) return false;
110  if(bValid)
111  {
112  double oldMinX(minX),oldMinY(minY),oldMaxX(maxX),oldMaxY(maxY);
113  for(;start_it!=end_it;++start_it) treatPointForValidBox(**start_it);
114  if(oldMinX!=minX || oldMinY!=minY || oldMaxX!=maxX || oldMaxY!=maxY ) return true;
115  return false;
116  }
117  else
118  {
119  treatPointForInvalidBox(**start_it);
120  ++start_it;
121  for(;start_it!=end_it;++start_it) treatPointForValidBox(**start_it);
122  return true;
123  }
124  }
131  bool add(std::vector<Point2>::const_iterator start_it,std::vector<Point2>::const_iterator end_it)
132  {
133  if(start_it==end_it) return false;
134  if(bValid)
135  {
136  double oldMinX(minX),oldMinY(minY),oldMaxX(maxX),oldMaxY(maxY);
137  for(;start_it!=end_it;++start_it) treatPointForValidBox(*start_it);
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  return true;
147  }
148  }
149 
156  bool add(size_t numPoints,double * coordinates)
157  {
158 #if GEOM_PSEUDO3D==GEOM_TRUE
159  const int NUMCOMPONENTS(3);
160 #else
161  const int NUMCOMPONENTS(2);
162 #endif
163 
164  if(numPoints==0) return false;
165  double oldMinX(minX),oldMinY(minY),oldMaxX(maxX),oldMaxY(maxY);
166  double firstX(coordinates[0]);
167  double firstY(coordinates[1]);
168  if(firstX<minX) minX=firstX;
169  if(firstX>maxX) maxX=firstX;
170  if(firstY<minY) minY=firstY;
171  if(firstY>maxY) maxY=firstY;
172 
173  for(size_t i=0;i<numPoints;++i)
174  {
175  double x(coordinates[NUMCOMPONENTS*i]);
176  double y(coordinates[NUMCOMPONENTS*i+1]);
177  if(x<minX) minX=x;
178  else if(x>maxX) maxX=x;
179  if(y<minY) minY=y;
180  else if(y>maxY) maxY=y;
181  }
182  bValid=true;
183  if(oldMinX!=minX || oldMinY!=minY || oldMaxX!=maxX || oldMaxY!=maxY ) return true;
184  else return false;
185  }
186 
193  bool add(const Point2& p)
194  {
195  //std::cout<<"Add point: "<<p<<std::endl;
196  if(bValid)
197  {
198  double oldMinX(minX),oldMinY(minY),oldMaxX(maxX),oldMaxY(maxY);
199  treatPointForValidBox(p);
200  if(oldMinX!=minX || oldMinY!=minY || oldMaxX!=maxX || oldMaxY!=maxY ) return true;
201  else return false;
202  }
203  else
204  {
205  treatPointForInvalidBox(p);
206  return true;
207  }
208  }
209 
213  bool isInBox(const Point2& p) const;
214 
218  Point2 computeCenter() const;
219 
225  Bbox2 operator+(const Bbox2& b);
226 
227 
228 #if GEOM_PSEUDO3D==GEOM_TRUE
229 
233 #else
234 
238 #endif
239 
241  {
242 #if GEOM_PSEUDO3D==GEOM_TRUE
243  return Point2(minX,minY,0);
244 #else
245  return Point2(minX,minY);
246 #endif
247  }
248 
249 
250 #if GEOM_PSEUDO3D==GEOM_TRUE
251 
255 #else
256 
260 #endif
261 
263  {
264 #if GEOM_PSEUDO3D==GEOM_TRUE
265  return Point2(maxX,maxY,0);
266 #else
267  return Point2(maxX,maxY);
268 #endif
269  }
270 
276  double getMinCoord() const
277  {
278  if(minX<minY) return minX;
279  else return minY;
280  }
286  double getMaxCoord() const
287  {
288  if(maxX>maxY) return maxX;
289  else return maxY;
290  }
291 
297  double getRangeX() const
298  {
299  return maxX-minX;
300  }
305  double getRangeY() const
306  {
307  return maxY-minY;
308  }
309 
315  double getMaxRange() const
316  {
317  double range0=getRangeX();
318  double range1=getRangeY();
319  if(range0>range1) return range0;
320  return range1;
321  }
322 
327  double get_minX() const {return minX;}
332  double get_minY() const {return minY;}
337  double get_maxX() const {return maxX;}
342  double get_maxY() const {return maxY;}
343 
347  void getBounds(double& minX_,double& maxX_,double& minY_,double& maxY_) const;
348 
349 
358  void setMinX(double val)
359  {
360  minX=val;
361  if(minX<=maxX && minY<=maxY) bValid=true;
362  }
365  void setMaxX(double val)
366  {
367  maxX=val;
368  if(minX<=maxX && minY<=maxY) bValid=true;
369  }
372  void setMinY(double val)
373  {
374  minY=val;
375  if(minX<=maxX && minY<=maxY) bValid=true;
376  }
379  void setMaxY(double val)
380  {
381  maxY=val;
382  if(minX<=maxX && minY<=maxY) bValid=true;
383  }
384 
385  void doubleTheBox();
386  void enlargeRanges(double factor);
394  void inflateIfDegenerate(double val)
395  {
396  if(bValid)
397  {
398  if(minX==maxX) maxX+=val;
399  if(minY==maxY) maxY+=val;
400  }
401  }
402 protected:
403  inline void treatPointForValidBox(const Point2& p)
404  {
405  double x,y;
406  p.xy(x,y);
407  if(x<minX) minX=x;
408  else if(x>maxX) maxX=x;
409  if(y<minY) minY=y;
410  else if(y>maxY) maxY=y;
411  }
412  inline void treatPointForInvalidBox(const Point2& p)
413  {
414  // Individual bounds may have been set already. Keep them!
415  if(minX==DBL_MAX) minX=p.x();
416  if(minY==DBL_MAX) minY=p.y();
417  if(maxX==-DBL_MAX) maxX=p.x();
418  if(maxY==-DBL_MAX) maxY=p.y();
419  bValid=true;
420  }
421 
422 protected:
423  double minX,minY;
424  double maxX,maxY;
425  bool bValid;
426  friend std::ostream &operator<<(std::ostream &stream, Bbox2& pC);
427  GeomTest* pGeomTest;
428 };
429 
430 Bbox2 getBox(std::vector<Point2>& vP);
431 Bbox2 getBox(std::vector<Point2*>& vP);
432 
433 inline std::ostream &operator<<(std::ostream &stream, Bbox2& pC)
434 {
435  stream<<"Bbox2: ("<<pC.minX<<","<<pC.minY<<") -> ("<<pC.maxX<<","<<pC.maxY<<"), rangeX="<<pC.getRangeX()<<", rangeY="<<pC.getRangeY();
436  return stream;
437 }
438 
439 
440 
441 } // (namespace)
double getRangeY() const
Get y-range.
Definition: Bbox2.h:305
Bbox2(GeomTest *pGeomTest_=NULL)
Constructor.
Definition: Bbox2.h:45
void setMaxY(double val)
Set maxY.
Definition: Bbox2.h:379
double y() const
Get the y-coordinate.
Definition: Point2.h:185
double get_minX() const
Get minX.
Definition: Bbox2.h:327
double getMinCoord() const
Get minimum coordinate.
Definition: Bbox2.h:276
void setMaxX(double val)
Set maxX.
Definition: Bbox2.h:365
bool add(const Point2 &p)
Add a point.
Definition: Bbox2.h:193
bool add(size_t numPoints, double *coordinates)
Add points.
Definition: Bbox2.h:156
void setMinY(double val)
Set minY.
Definition: Bbox2.h:372
Bbox2 is an axis aligned bounding box.
Definition: Bbox2.h:36
Point2 getMaxPoint() const
Get the max point.
Definition: Bbox2.h:262
double x() const
Get the x-coordinate.
Definition: Point2.h:176
bool isValid() const
Check if the bounds are valid.
Definition: Bbox2.h:76
double get_maxX() const
Get maxX.
Definition: Bbox2.h:337
double getMaxRange() const
Get max range.
Definition: Bbox2.h:315
bool add(std::vector< Point2 >::const_iterator start_it, std::vector< Point2 >::const_iterator end_it)
Add points.
Definition: Bbox2.h:131
Definition: Bbox2.h:23
double get_minY() const
Get minY.
Definition: Bbox2.h:332
Bbox2(std::vector< Point2 >::const_iterator start_it, std::vector< Point2 >::const_iterator end_it, GeomTest *pGeomTest_=NULL)
Constructor.
Definition: Bbox2.h:58
Point2 getMinPoint() const
Get the min point.
Definition: Bbox2.h:240
void setMinX(double val)
Double the box.
Definition: Bbox2.h:358
double getRangeX() const
Get x-range.
Definition: Bbox2.h:297
double getMaxCoord() const
Get maximum coordinate.
Definition: Bbox2.h:286
Point.
Definition: Point2.h:39
bool add(std::vector< Point2 *>::const_iterator start_it, std::vector< Point2 *>::const_iterator end_it)
Add points.
Definition: Bbox2.h:107
void inflateIfDegenerate(double val)
Inflate if Degenerate.
Definition: Bbox2.h:394
double get_maxY() const
Get maxY.
Definition: Bbox2.h:342
void xy(double &x_, double &y_) const
Get the x- and y-coordinate.
Definition: Point2.h:226