00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #ifndef _BBOX_H_
00028 #define _BBOX_H_
00029
00030 #include <3D/Point.h>
00031
00032 class BBox {
00033 public:
00034 BBox() {}
00035 BBox(const Point& min, const Point& max) { setValue(min, max); }
00036
00037 const Point& getCenter() const { return center; }
00038 const Vector& getExtent() const { return extent; }
00039
00040 void setCenter(const Point& p) { center = p; }
00041 void setExtent(const Vector& v) { extent = v; }
00042
00043 void setValue(const Point& min, const Point& max) {
00044 extent = (max - min) / 2;
00045 center = min + extent;
00046 }
00047
00048 void enclose(const BBox& a, const BBox& b) {
00049 Point lower(min(a.getLower(X), b.getLower(X)),
00050 min(a.getLower(Y), b.getLower(Y)),
00051 min(a.getLower(Z), b.getLower(Z)));
00052 Point upper(max(a.getUpper(X), b.getUpper(X)),
00053 max(a.getUpper(Y), b.getUpper(Y)),
00054 max(a.getUpper(Z), b.getUpper(Z)));
00055 setValue(lower, upper);
00056 }
00057
00058 void setEmpty() {
00059 center.setValue(0, 0, 0);
00060 extent.setValue(-INFINITY_, -INFINITY_, -INFINITY_);
00061 }
00062
00063 void include (const Point& p) {
00064 Point lower(min(getLower(X), p[X]),
00065 min(getLower(Y), p[Y]),
00066 min(getLower(Z), p[Z]));
00067 Point upper(max(getUpper(X), p[X]),
00068 max(getUpper(Y), p[Y]),
00069 max(getUpper(Z), p[Z]));
00070 setValue(lower, upper);
00071 }
00072
00073 void include (const BBox& b) { enclose(*this, b); }
00074
00075 Scalar getLower(int i) const { return center[i] - extent[i]; }
00076 Scalar getUpper(int i) const { return center[i] + extent[i]; }
00077
00078 Scalar size() const { return max(max(extent[X], extent[Y]), extent[Z]); }
00079 int longestAxis() const { return extent.closestAxis(); }
00080
00081 friend bool intersect(const BBox& a, const BBox& b);
00082
00083 private:
00084 Point center;
00085 Vector extent;
00086 };
00087
00088 inline bool intersect(const BBox& a, const BBox& b) {
00089 return fabs(a.center[X] - b.center[X]) <= a.extent[X] + b.extent[X] &&
00090 fabs(a.center[Y] - b.center[Y]) <= a.extent[Y] + b.extent[Y] &&
00091 fabs(a.center[Z] - b.center[Z]) <= a.extent[Z] + b.extent[Z];
00092 }
00093
00094 #endif
00095
00096