Geom Software - C++ Programming and Geometry Libraries
Fade2D Documentation pages v1.58
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:
43  explicit Bbox2(GeomTest* pGeomTest_=NULL):
44  minX(DBL_MAX),minY(DBL_MAX),
45  maxX(DBL_MIN),maxY(DBL_MIN),
46  bValid(false),pGeomTest(pGeomTest_)
47  {
48  }
49 
55  Bbox2( std::vector<Point2>::const_iterator start_it,
56  std::vector<Point2>::const_iterator end_it,
57  GeomTest* pGeomTest_=NULL)
58  :
59  minX(DBL_MAX),minY(DBL_MAX),
60  maxX(DBL_MIN),maxY(DBL_MIN),
61  pGeomTest(pGeomTest_)
62  {
63  add(start_it,end_it);
64  }
65 
71  bool isValid() const
72  {
73  return minX<DBL_MAX;
74  }
75 
80  void getCorners(std::vector<Point2>& vBoxCorners) const;
81 
82 
89  bool add(std::vector<Point2>::const_iterator start_it,std::vector<Point2>::const_iterator end_it)
90  {
91  if(start_it==end_it) return false;
92  if(bValid)
93  {
94  double oldMinX(minX),oldMinY(minY),oldMaxX(maxX),oldMaxY(maxY);
95  for(;start_it!=end_it;++start_it) treatPointForValidBox(*start_it);
96  if(oldMinX!=minX || oldMinY!=minY || oldMaxX!=maxX || oldMaxY!=maxY ) return true;
97  return false;
98  }
99  else
100  {
101  treatPointForInvalidBox(*start_it);
102  ++start_it;
103  for(;start_it!=end_it;++start_it) treatPointForValidBox(*start_it);
104  return true;
105  }
106  }
107 
114  bool add(size_t numPoints,double * coordinates)
115  {
116 #if GEOM_PSEUDO3D==GEOM_TRUE
117  const int NUMCOMPONENTS(3);
118 #else
119  const int NUMCOMPONENTS(2);
120 #endif
121 
122  if(numPoints==0) return false;
123  double oldMinX(minX),oldMinY(minY),oldMaxX(maxX),oldMaxY(maxY);
124  double x(coordinates[0]);
125  double y(coordinates[1]);
126  if(x<minX) minX=x;
127  if(x>maxX) maxX=x;
128  if(y<minY) minY=y;
129  if(y>maxY) maxY=y;
130 
131  for(size_t i=0;i<numPoints;++i)
132  {
133  double x(coordinates[NUMCOMPONENTS*i]);
134  double y(coordinates[NUMCOMPONENTS*i+1]);
135  if(x<minX) minX=x;
136  else if(x>maxX) maxX=x;
137  if(y<minY) minY=y;
138  else if(y>maxY) maxY=y;
139  }
140  bValid=true;
141  if(oldMinX!=minX || oldMinY!=minY || oldMaxX!=maxX || oldMaxY!=maxY ) return true;
142  else return false;
143  }
144 
151  bool add(const Point2& p)
152  {
153  //std::cout<<"Add point: "<<p<<std::endl;
154  if(bValid)
155  {
156  double oldMinX(minX),oldMinY(minY),oldMaxX(maxX),oldMaxY(maxY);
157  treatPointForValidBox(p);
158  if(oldMinX!=minX || oldMinY!=minY || oldMaxX!=maxX || oldMaxY!=maxY ) return true;
159  else return false;
160  }
161  else
162  {
163  treatPointForInvalidBox(p);
164  return true;
165  }
166  }
167 
174  {
175  if(b.minX<minX) minX=b.minX;
176  if(b.maxX>maxX) maxX=b.maxX;
177  if(b.minY<minY) minY=b.minY;
178  if(b.maxY>maxY) maxY=b.maxY;
179  return *this;
180  }
181 
182 
183 #if GEOM_PSEUDO3D==GEOM_TRUE
184 
188 #else
189 
193 #endif
194 
196  {
197 #if GEOM_PSEUDO3D==GEOM_TRUE
198  return Point2(minX,minY,0);
199 #else
200  return Point2(minX,minY);
201 #endif
202  }
203 
204 
205 #if GEOM_PSEUDO3D==GEOM_TRUE
206 
210 #else
211 
215 #endif
216 
218  {
219 #if GEOM_PSEUDO3D==GEOM_TRUE
220  return Point2(maxX,maxY,0);
221 #else
222  return Point2(maxX,maxY);
223 #endif
224  }
225 
231  double getMinCoord() const
232  {
233  if(minX<minY) return minX;
234  else return minY;
235  }
241  double getMaxCoord() const
242  {
243  if(maxX>maxY) return maxX;
244  else return maxY;
245  }
246 
252  double getRangeX() const
253  {
254  return maxX-minX;
255  }
260  double getRangeY() const
261  {
262  return maxY-minY;
263  }
264 
270  double getMaxRange() const
271  {
272  double range0=getRangeX();
273  double range1=getRangeY();
274  if(range0>range1) return range0;
275  return range1;
276  }
277 
282  double get_minX() {return minX;}
287  double get_minY() {return minY;}
292  double get_maxX() {return maxX;}
297  double get_maxY() {return maxY;}
298 
305  void doubleTheBox();
306 
307 protected:
308  inline void treatPointForValidBox(const Point2& p)
309  {
310  double x,y;
311  p.xy(x,y);
312  if(x<minX) minX=x;
313  else if(x>maxX) maxX=x;
314  if(y<minY) minY=y;
315  else if(y>maxY) maxY=y;
316  }
317  inline void treatPointForInvalidBox(const Point2& p)
318  {
319  p.xy(minX,minY);
320  p.xy(maxX,maxY);
321  bValid=true;
322  }
323 
324 protected:
325  double minX,minY;
326  double maxX,maxY;
327  bool bValid;
328  friend std::ostream &operator<<(std::ostream &stream, Bbox2& pC);
329  GeomTest* pGeomTest;
330 };
331 
332 
333 inline std::ostream &operator<<(std::ostream &stream, Bbox2& pC)
334 {
335  stream<<"Bbox2: ("<<pC.minX<<","<<pC.minY<<") -> ("<<pC.maxX<<","<<pC.maxY<<"), rangeX="<<pC.getRangeX()<<", rangeY="<<pC.getRangeY();
336  return stream;
337 }
338 
339 
340 
341 } // (namespace)
double getMaxRange() const
Get max range.
Definition: Bbox2.h:270
double getMinCoord() const
Get minimum coordinate.
Definition: Bbox2.h:231
Bbox2(GeomTest *pGeomTest_=NULL)
Constructor.
Definition: Bbox2.h:43
Bbox2 operator+(const Bbox2 &b)
Add a bounding box.
Definition: Bbox2.h:173
bool add(const Point2 &p)
Add a point.
Definition: Bbox2.h:151
double getRangeY() const
Get y-range.
Definition: Bbox2.h:260
Bbox2(std::vector< Point2 >::const_iterator start_it, std::vector< Point2 >::const_iterator end_it, GeomTest *pGeomTest_=NULL)
Constructor.
Definition: Bbox2.h:55
Point.
Definition: Point2.h:39
void xy(double &x_, double &y_) const
Get the x- and y-coordinate.
Definition: Point2.h:213
bool add(std::vector< Point2 >::const_iterator start_it, std::vector< Point2 >::const_iterator end_it)
Add points.
Definition: Bbox2.h:89
Point2 getMaxPoint() const
Get the max point.
Definition: Bbox2.h:217
bool add(size_t numPoints, double *coordinates)
Add points.
Definition: Bbox2.h:114
double get_maxY()
Get maxY.
Definition: Bbox2.h:297
Point2 getMinPoint() const
Get the min point.
Definition: Bbox2.h:195
bool isValid() const
Are the bounds valid?
Definition: Bbox2.h:71
double getMaxCoord() const
Get maximum coordinate.
Definition: Bbox2.h:241
double get_minX()
Get minX.
Definition: Bbox2.h:282
double get_minY()
Get minY.
Definition: Bbox2.h:287
double get_maxX()
Get maxX.
Definition: Bbox2.h:292
Bbox2 is an axis aligned bounding box.
Definition: Bbox2.h:36
double getRangeX() const
Get x-range.
Definition: Bbox2.h:252
Definition: Bbox2.h:25