Geom Software - C++ Programming and Geometry Libraries
Fade2D Documentation pages v1.52
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 
34 class CLASS_DECLSPEC Bbox2
35 {
36 public:
42  explicit Bbox2(GeomTest* pGeomTest_=NULL):
43  minX(DBL_MAX),minY(DBL_MAX),
44  maxX(DBL_MIN),maxY(DBL_MIN),
45  bValid(false),pGeomTest(pGeomTest_)
46  {
47  }
48 
53  Bbox2( std::vector<Point2>::const_iterator start_it,
54  std::vector<Point2>::const_iterator end_it,
55  GeomTest* pGeomTest_=NULL)
56  :
57  minX(DBL_MAX),minY(DBL_MAX),
58  maxX(DBL_MIN),maxY(DBL_MIN),
59  pGeomTest(pGeomTest_)
60  {
61  add(start_it,end_it);
62  }
63 
67  bool isValid() const
68  {
69  return minX<DBL_MAX;
70  }
71 
74  void getCorners(std::vector<Point2>& vBoxCorners) const;
75 
76 
81  bool add(std::vector<Point2>::const_iterator start_it,std::vector<Point2>::const_iterator end_it)
82  {
83  if(start_it==end_it) return false;
84  if(bValid)
85  {
86  double oldMinX(minX),oldMinY(minY),oldMaxX(maxX),oldMaxY(maxY);
87  for(;start_it!=end_it;++start_it) treatPointForValidBox(*start_it);
88  if(oldMinX!=minX || oldMinY!=minY || oldMaxX!=maxX || oldMaxY!=maxY ) return true;
89  return false;
90  }
91  else
92  {
93  treatPointForInvalidBox(*start_it);
94  ++start_it;
95  for(;start_it!=end_it;++start_it) treatPointForValidBox(*start_it);
96  return true;
97  }
98  }
99 
104  bool add(size_t numPoints,double * coordinates)
105  {
106 #if GEOM_PSEUDO3D==GEOM_TRUE
107  const int NUMCOMPONENTS(3);
108 #else
109  const int NUMCOMPONENTS(2);
110 #endif
111 
112  if(numPoints==0) return false;
113  double oldMinX(minX),oldMinY(minY),oldMaxX(maxX),oldMaxY(maxY);
114  double x(coordinates[0]);
115  double y(coordinates[1]);
116  if(x<minX) minX=x;
117  if(x>maxX) maxX=x;
118  if(y<minY) minY=y;
119  if(y>maxY) maxY=y;
120 
121  for(size_t i=0;i<numPoints;++i)
122  {
123  double x(coordinates[NUMCOMPONENTS*i]);
124  double y(coordinates[NUMCOMPONENTS*i+1]);
125  if(x<minX) minX=x;
126  else if(x>maxX) maxX=x;
127  if(y<minY) minY=y;
128  else if(y>maxY) maxY=y;
129  }
130  bValid=true;
131  if(oldMinX!=minX || oldMinY!=minY || oldMaxX!=maxX || oldMaxY!=maxY ) return true;
132  else return false;
133  }
134 
139  bool add(const Point2& p)
140  {
141  //std::cout<<"Add point: "<<p<<std::endl;
142  if(bValid)
143  {
144  double oldMinX(minX),oldMinY(minY),oldMaxX(maxX),oldMaxY(maxY);
145  treatPointForValidBox(p);
146  if(oldMinX!=minX || oldMinY!=minY || oldMaxX!=maxX || oldMaxY!=maxY ) return true;
147  else return false;
148  }
149  else
150  {
151  treatPointForInvalidBox(p);
152  return true;
153  }
154  }
155 
162  {
163  if(b.minX<minX) minX=b.minX;
164  if(b.maxX>maxX) maxX=b.maxX;
165  if(b.minY<minY) minY=b.minY;
166  if(b.maxY>maxY) maxY=b.maxY;
167  return *this;
168  }
169 
170 #if GEOM_PSEUDO3D==GEOM_TRUE
171 
174 #else
175 
178 #endif
179 
181  {
182 #if GEOM_PSEUDO3D==GEOM_TRUE
183  return Point2(minX,minY,0);
184 #else
185  return Point2(minX,minY);
186 #endif
187  }
188 
189 
190 #if GEOM_PSEUDO3D==GEOM_TRUE
191 
194 #else
195 
198 #endif
199 
201  {
202 #if GEOM_PSEUDO3D==GEOM_TRUE
203  return Point2(maxX,maxY,0);
204 #else
205  return Point2(maxX,maxY);
206 #endif
207  }
208 
213  double getMinCoord() const
214  {
215  if(minX<minY) return minX;
216  else return minY;
217  }
222  double getMaxCoord() const
223  {
224  if(maxX>maxY) return maxX;
225  else return maxY;
226  }
231  double getRangeX() const
232  {
233  return maxX-minX;
234  }
239  double getRangeY() const
240  {
241  return maxY-minY;
242  }
247  double getMaxRange() const
248  {
249  double range0=getRangeX();
250  double range1=getRangeY();
251  if(range0>range1) std::swap(range0,range1);
252  return range1;
253  }
257  double get_minX() {return minX;}
261  double get_minY() {return minY;}
265  double get_maxX() {return maxX;}
269  double get_maxY() {return maxY;}
270 
275  void doubleTheBox();
276 
277 protected:
281  inline void treatPointForValidBox(const Point2& p)
282  {
283  double x,y;
284  p.xy(x,y);
285  if(x<minX) minX=x;
286  else if(x>maxX) maxX=x;
287  if(y<minY) minY=y;
288  else if(y>maxY) maxY=y;
289  }
290 
294  inline void treatPointForInvalidBox(const Point2& p)
295  {
296  p.xy(minX,minY);
297  p.xy(maxX,maxY);
298  bValid=true;
299  }
300 
301 
302 
303 
304 protected:
305  double minX,minY;
306  double maxX,maxY;
307  bool bValid;
308  friend std::ostream &operator<<(std::ostream &stream, Bbox2& pC);
309  GeomTest* pGeomTest;
310 };
311 
312 
313 inline std::ostream &operator<<(std::ostream &stream, Bbox2& pC)
314 {
315  stream<<"Bbox2: ("<<pC.minX<<","<<pC.minY<<") -> ("<<pC.maxX<<","<<pC.maxY<<"), rangeX="<<pC.getRangeX()<<", rangeY="<<pC.getRangeY();
316  return stream;
317 }
318 
319 
320 
321 } // (namespace)
double getRangeY() const
Get y range.
Definition: Bbox2.h:239
Bbox2(GeomTest *pGeomTest_=NULL)
Create a bounding box Bounds stay uninitialized.
Definition: Bbox2.h:42
Bbox2 operator+(const Bbox2 &b)
Add a bounding box.
Definition: Bbox2.h:161
bool add(const Point2 &p)
Add a point.
Definition: Bbox2.h:139
Bbox2(std::vector< Point2 >::const_iterator start_it, std::vector< Point2 >::const_iterator end_it, GeomTest *pGeomTest_=NULL)
Create a bounding box Bounds initialized to the minimal bounding box of the iterator range of points...
Definition: Bbox2.h:53
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:81
void treatPointForValidBox(const Point2 &p)
Add a point to a valid box.
Definition: Bbox2.h:281
Point2 getMinPoint() const
Get the min point.
Definition: Bbox2.h:180
bool add(size_t numPoints, double *coordinates)
Add points.
Definition: Bbox2.h:104
double get_maxY()
Get maxY.
Definition: Bbox2.h:269
bool isValid() const
Are the bounds valid?
Definition: Bbox2.h:67
double getMaxCoord() const
Get maximum coordinate.
Definition: Bbox2.h:222
Point2 getMaxPoint() const
Get the max point.
Definition: Bbox2.h:200
void treatPointForInvalidBox(const Point2 &p)
Add a point to an uninitialized.
Definition: Bbox2.h:294
double getRangeX() const
Get x range.
Definition: Bbox2.h:231
double getMinCoord() const
Get minimum coordinate.
Definition: Bbox2.h:213
double get_minX()
Get minX.
Definition: Bbox2.h:257
double getMaxRange() const
Get max range.
Definition: Bbox2.h:247
void xy(double &x_, double &y_) const
Get the x- and y-coordinate.
Definition: Point2.h:195
double get_minY()
Get minY.
Definition: Bbox2.h:261
double get_maxX()
Get maxX.
Definition: Bbox2.h:265
Boundingbox.
Definition: Bbox2.h:34
Definition: Bbox2.h:25