X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=simgear%2Fmath%2FSGBox.hxx;h=8024a1318e3911767828bcebf164a983fe36d573;hb=11479cd8c386d8bf7e1fee7bed60ab4abefc5fad;hp=272c7cab0e81e035c705645fb93c2cd66bc85b7c;hpb=de020ee69524393daf11200aa0a46bfd5aa2409a;p=simgear.git diff --git a/simgear/math/SGBox.hxx b/simgear/math/SGBox.hxx index 272c7cab..8024a131 100644 --- a/simgear/math/SGBox.hxx +++ b/simgear/math/SGBox.hxx @@ -25,6 +25,20 @@ public: _min(SGLimits::max(), SGLimits::max(), SGLimits::max()), _max(-SGLimits::max(), -SGLimits::max(), -SGLimits::max()) { } + SGBox(const SGVec3& pt) : + _min(pt), + _max(pt) + { } + SGBox(const SGVec3& min, const SGVec3& max) : + _min(min), + _max(max) + { } + template + explicit SGBox(const SGBox& box) : + _min(box.getMin()), + _max(box.getMax()) + { } + void setMin(const SGVec3& min) { _min = min; } const SGVec3& getMin() const @@ -35,6 +49,29 @@ public: const SGVec3& getMax() const { return _max; } + SGVec3 getCorner(unsigned i) const + { return SGVec3((i&1) ? _min[0] : _max[0], + (i&2) ? _min[1] : _max[1], + (i&4) ? _min[2] : _max[2]); } + + template + SGVec3 getNearestCorner(const SGVec3& pt) const + { + SGVec3 center = getCenter(); + return SGVec3((pt[0] <= center[0]) ? _min[0] : _max[0], + (pt[1] <= center[1]) ? _min[1] : _max[1], + (pt[2] <= center[2]) ? _min[2] : _max[2]); + } + template + SGVec3 getFarestCorner(const SGVec3& pt) const + { + SGVec3 center = getCenter(); + return SGVec3((pt[0] > center[0]) ? _min[0] : _max[0], + (pt[1] > center[1]) ? _min[1] : _max[1], + (pt[2] > center[2]) ? _min[2] : _max[2]); + } + + // Only works for floating point types SGVec3 getCenter() const { return T(0.5)*(_min + _max); } @@ -42,6 +79,8 @@ public: // Only valid for nonempty boxes SGVec3 getSize() const { return _max - _min; } + SGVec3 getHalfSize() const + { return T(0.5)*getSize(); } T getVolume() const { @@ -83,7 +122,7 @@ public: // Note that this only works if the box is nonmepty unsigned getBroadestAxis() const { - SGVec3d size = getSize(); + SGVec3 size = getSize(); if (size[1] <= size[0] && size[2] <= size[0]) return 0; else if (size[2] <= size[1]) @@ -92,6 +131,18 @@ public: return 2; } + // Note that this only works if the box is nonmepty + unsigned getSmallestAxis() const + { + SGVec3 size = getSize(); + if (size[1] >= size[0] && size[2] >= size[0]) + return 0; + else if (size[2] >= size[1]) + return 1; + else + return 2; + } + private: SGVec3 _min; SGVec3 _max;