Geom Software - C++ Programming and Geometry Libraries
Fade3D Documentation pages v0.99
/home/geom/repo/dev/geomDev/dt3/dt3Library/Bbox3.h
1 #pragma once
2 //#include "common.h"
3 #include "Point3.h"
4 //#include "GeomTest3.h"
5 
6 namespace FADE3D {
7 
8 
9 class GeomTest3; // FWD
10 
13 class CLASS_DECLSPEC Bbox3
14 {
15  friend class HC3;
16 public:
17 
24  :
25  minX(DBL_MAX),minY(DBL_MAX),minZ(DBL_MAX),
26  maxX(-DBL_MAX),maxY(-DBL_MAX),maxZ(-DBL_MAX)
27  {
28  }
33  explicit Bbox3( const std::vector<Point3>& vPoints)
34  :
35  minX(DBL_MAX),minY(DBL_MAX),minZ(DBL_MAX),
36  maxX(-DBL_MAX),maxY(-DBL_MAX),maxZ(-DBL_MAX)
37  {
38  add(vPoints);
39  }
46  bool isValid()
47  {
48  return minX<DBL_MAX;
49  }
56  bool add(const std::vector<Point3>& vPoints)
57  {
58  if(vPoints.empty()) return false;
59 
60  // Initialize if we had an invalid box, return true
61  if(!isValid())
62  {
63  vPoints[0].xyz(minX,minY,minZ);
64  vPoints[0].xyz(maxX,maxY,maxZ);
65  for(size_t i=1;i<vPoints.size();++i)
66  {
67  treatPointForValidBox(vPoints[i]);
68  }
69  return true;
70  }
71 
72  // Otherwise just add and check if the box changes
73  double oldMinX(minX),oldMinY(minY),oldMinZ(minZ),oldMaxX(maxX),oldMaxY(maxY),oldMaxZ(maxZ);
74  for(size_t i=0;i<vPoints.size();++i)
75  {
76  treatPointForValidBox(vPoints[i]);
77  }
78 
79  if( oldMinX!=minX ||
80  oldMinY!=minY ||
81  oldMinZ!=minZ ||
82  oldMaxX!=maxX ||
83  oldMaxY!=maxY ||
84  oldMaxZ!=maxZ)
85  {
86  return true;
87  }
88  return false;
89  }
90 
97  bool add(const Point3& p)
98  {
99  if(isValid())
100  {
101  double oldMinX(minX),oldMinY(minY),oldMinZ(minZ),oldMaxX(maxX),oldMaxY(maxY),oldMaxZ(maxZ);
102  treatPointForValidBox(p);
103  if( oldMinX!=minX ||
104  oldMinY!=minY ||
105  oldMinZ!=minZ ||
106  oldMaxX!=maxX ||
107  oldMaxY!=maxY ||
108  oldMaxZ!=maxZ)
109  {
110  return true;
111  }
112  return false;
113  }
114  else
115  {
116  p.xyz(minX,minY,minZ);
117  p.xyz(maxX,maxY,maxZ);
118  return true;
119  }
120  }
121 
123  // factor=1 means that half the range is subtracted from the lower
124  // bound and half the range is added to the upper bound
125  void enlargeBounds(double factor)
126  {
127  //std::cout<<"doubleTheBox has been called"<<std::endl;
128  //std::cout<<"doubleTheBox, oldVals: minX="<<minX<<", minY="<<minY<<", minZ="<<minZ<<std::endl;
129  //std::cout<<" maxX="<<maxX<<", maxY="<<maxY<<", maxZ="<<maxZ<<std::endl;
130  double addValX=factor*getRangeX()/2.0;
131  double addValY=factor*getRangeY()/2.0;
132  double addValZ=factor*getRangeZ()/2.0;
133  minX-=addValX;
134  maxX+=addValX;
135  minY-=addValY;
136  maxY+=addValY;
137  minZ-=addValZ;
138  maxZ+=addValZ;
139  //std::cout<<" newVals: minX="<<minX<<", minY="<<minY<<", minZ="<<minZ<<std::endl;
140  //std::cout<<" maxX="<<maxX<<", maxY="<<maxY<<", maxZ="<<maxZ<<std::endl;
141  }
142 
149  {
150  if(b.minX<minX) minX=b.minX;
151  if(b.maxX>maxX) maxX=b.maxX;
152  if(b.minY<minY) minY=b.minY;
153  if(b.maxY>maxY) maxY=b.maxY;
154  if(b.minZ<minZ) minZ=b.minZ;
155  if(b.maxZ>maxZ) maxZ=b.maxZ;
156  return *this;
157  }
158 
164  {
165  return Point3(minX,minY,minZ);
166  }
172  {
173  return Point3(maxX,maxY,maxZ);
174  }
179  double getMinCoord()
180  {
181  if(minX<minY)
182  {
183  if(minX<minZ) return minX;
184  else return minZ;
185  }
186  else
187  {
188  if(minY<minZ) return minY;
189  else return minZ;
190  }
191  }
196  double getMaxCoord()
197  {
198  if(maxX>maxY)
199  {
200  if(maxX>maxZ) return maxX;
201  else return maxZ;
202  }
203  else
204  {
205  if(maxY>maxZ) return maxY;
206  else return maxZ;
207  }
208  }
213  double getRangeX() const
214  {
215  return maxX-minX;
216  }
221  double getRangeY() const
222  {
223  return maxY-minY;
224  }
229  double getRangeZ() const
230  {
231  return maxZ-minZ;
232  }
237  double getMaxRange() const
238  {
239  double range0=getRangeX();
240  double range1=getRangeY();
241  double range2=getRangeZ();
242  if(range0>range1) std::swap(range0,range1);
243  if(range1>range2) std::swap(range1,range2);
244  return range2;
245  }
249  double getMinX() const{return minX;}
253  double getMinY() const{return minY;}
257  double getMinZ() const{return minZ;}
261  double getMaxX() const{return maxX;}
265  double getMaxY() const{return maxY;}
269  double getMaxZ() const{return maxZ;}
270 
272  void debug()
273  {
274  std::cout<<"Bbox3: ("<<minX<<","<<minY<<","<<minZ<<") -> ("<<maxX<<","<<maxY<<","<<maxZ<<")"<<std::endl;
275  }
276 
277 protected:
279  inline void treatPointForValidBox(const Point3& p)
280  {
281  double x,y,z;
282  p.xyz(x,y,z);
283  if(x<minX) minX=x;
284  else if(x>maxX) maxX=x;
285  if(y<minY) minY=y;
286  else if(y>maxY) maxY=y;
287  if(z<minZ) minZ=z;
288  else if(z>maxZ) maxZ=z;
289  }
290  double minX,minY,minZ;
291  double maxX,maxY,maxZ;
292 
293 
294  friend std::ostream &operator<<(std::ostream &stream, Bbox3& pC);
295 };
296 
297 
298 inline std::ostream &operator<<(std::ostream &stream, Bbox3& pC)
299 {
300  stream<<"Bbox3: ("<<pC.minX<<","<<pC.minY<<","<<pC.minZ<<") -> ("<<pC.maxX<<","<<pC.maxY<<","<<pC.maxZ<<"), rangeX="<<pC.getRangeX()<<", rangeY="<<pC.getRangeY()<<", rangeZ="<<pC.getRangeZ();
301  return stream;
302 }
303 
304 
305 } // NAMESPACE FADE3D
double getMinX() const
Get the minimal x coordinate.
Definition: Bbox3.h:249
double getMaxRange() const
Get the maximum range.
Definition: Bbox3.h:237
double getMaxZ() const
Get the maximal z coordinate.
Definition: Bbox3.h:269
Definition: Ball3.h:16
double getMinZ() const
Get the minimal z coordinate.
Definition: Bbox3.h:257
Vertex.
Definition: Point3.h:32
double getRangeY() const
Get the y-range.
Definition: Bbox3.h:221
Point3 getMaxPoint()
Get the maximum corner.
Definition: Bbox3.h:171
void xyz(double &x_, double &y_, double &z_) const
Access all coordinates at once.
bool isValid()
Check if the bounding box is valid.
Definition: Bbox3.h:46
double getMaxY() const
Get the maximal y coordinate.
Definition: Bbox3.h:265
Bbox3(const std::vector< Point3 > &vPoints)
Constructor.
Definition: Bbox3.h:33
Axis-aligned minimal 3D bounding box.
Definition: Bbox3.h:13
double getMaxCoord()
Get the largest coordinate.
Definition: Bbox3.h:196
double getMinY() const
Get the minimal y coordinate.
Definition: Bbox3.h:253
Point3 getMinPoint()
Get the minimum corner.
Definition: Bbox3.h:163
bool add(const std::vector< Point3 > &vPoints)
Add points to the bounding box.
Definition: Bbox3.h:56
double getMaxX() const
Get the maximal x coordinate.
Definition: Bbox3.h:261
Bbox3 operator+(Bbox3 &b)
Add another bounding box.
Definition: Bbox3.h:148
double getRangeZ() const
Get the z-range.
Definition: Bbox3.h:229
Bbox3()
Constructor.
Definition: Bbox3.h:23
double getMinCoord()
Get the smallest coordinate.
Definition: Bbox3.h:179
bool add(const Point3 &p)
Add a point to the bounding box.
Definition: Bbox3.h:97
double getRangeX() const
Get the x-range.
Definition: Bbox3.h:213