1 // Copyright (C) 2006 Mathias Froehlich - Mathias.Froehlich@web.de
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Library General Public
5 // License as published by the Free Software Foundation; either
6 // version 2 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Library General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 _min(SGLimits<T>::max(), SGLimits<T>::max(), SGLimits<T>::max()),
26 _max(-SGLimits<T>::max(), -SGLimits<T>::max(), -SGLimits<T>::max())
28 SGBox(const SGVec3<T>& pt) :
32 SGBox(const SGVec3<T>& min, const SGVec3<T>& max) :
37 explicit SGBox(const SGBox<S>& box) :
42 void setMin(const SGVec3<T>& min)
44 const SGVec3<T>& getMin() const
47 void setMax(const SGVec3<T>& max)
49 const SGVec3<T>& getMax() const
52 SGVec3<T> getCorner(unsigned i) const
53 { return SGVec3<T>((i&1) ? _min[0] : _max[0],
54 (i&2) ? _min[1] : _max[1],
55 (i&4) ? _min[2] : _max[2]); }
58 SGVec3<T> getNearestCorner(const SGVec3<S>& pt) const
60 SGVec3<T> center = getCenter();
61 return SGVec3<T>((pt[0] <= center[0]) ? _min[0] : _max[0],
62 (pt[1] <= center[1]) ? _min[1] : _max[1],
63 (pt[2] <= center[2]) ? _min[2] : _max[2]);
66 SGVec3<T> getFarestCorner(const SGVec3<S>& pt) const
68 SGVec3<T> center = getCenter();
69 return SGVec3<T>((pt[0] > center[0]) ? _min[0] : _max[0],
70 (pt[1] > center[1]) ? _min[1] : _max[1],
71 (pt[2] > center[2]) ? _min[2] : _max[2]);
74 // return the closest point to pt still in the box
76 SGVec3<T> getClosestPoint(const SGVec3<S>& pt) const
78 return SGVec3<T>((pt[0] < _min[0]) ? _min[0] : ((_max[0] < pt[0]) ? _max[0] : T(pt[0])),
79 (pt[1] < _min[1]) ? _min[1] : ((_max[1] < pt[1]) ? _max[1] : T(pt[1])),
80 (pt[2] < _min[2]) ? _min[2] : ((_max[2] < pt[2]) ? _max[2] : T(pt[2])));
83 // Only works for floating point types
84 SGVec3<T> getCenter() const
85 { return T(0.5)*(_min + _max); }
87 // Only valid for nonempty boxes
88 SGVec3<T> getSize() const
89 { return _max - _min; }
90 SGVec3<T> getHalfSize() const
91 { return T(0.5)*getSize(); }
97 return (_max[0] - _min[0])*(_max[1] - _min[1])*(_max[2] - _min[2]);
105 if (_max[0] < _min[0])
107 if (_max[1] < _min[1])
109 if (_max[2] < _min[2])
116 _min[0] = SGLimits<T>::max();
117 _min[1] = SGLimits<T>::max();
118 _min[2] = SGLimits<T>::max();
119 _max[0] = -SGLimits<T>::max();
120 _max[1] = -SGLimits<T>::max();
121 _max[2] = -SGLimits<T>::max();
124 void expandBy(const SGVec3<T>& v)
125 { _min = min(_min, v); _max = max(_max, v); }
127 void expandBy(const SGBox<T>& b)
128 { _min = min(_min, b._min); _max = max(_max, b._max); }
130 // Note that this only works if the box is nonmepty
131 unsigned getBroadestAxis() const
133 SGVec3<T> size = getSize();
134 if (size[1] <= size[0] && size[2] <= size[0])
136 else if (size[2] <= size[1])
142 // Note that this only works if the box is nonmepty
143 unsigned getSmallestAxis() const
145 SGVec3<T> size = getSize();
146 if (size[1] >= size[0] && size[2] >= size[0])
148 else if (size[2] >= size[1])
159 /// Output to an ostream
160 template<typename char_type, typename traits_type, typename T>
162 std::basic_ostream<char_type, traits_type>&
163 operator<<(std::basic_ostream<char_type, traits_type>& s, const SGBox<T>& box)
164 { return s << "min = " << box.getMin() << ", max = " << box.getMax(); }