]> git.mxchange.org Git - simgear.git/blob - simgear/math/SGBox.hxx
Fix various compiler warnings.
[simgear.git] / simgear / math / SGBox.hxx
1 // Copyright (C) 2006  Mathias Froehlich - Mathias.Froehlich@web.de
2 //
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.
7 //
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.
12 //
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.
16 //
17
18 #ifndef SGBox_H
19 #define SGBox_H
20
21 template<typename T>
22 class SGBox {
23 public:
24   SGBox() :
25     _min(SGLimits<T>::max(), SGLimits<T>::max(), SGLimits<T>::max()),
26     _max(-SGLimits<T>::max(), -SGLimits<T>::max(), -SGLimits<T>::max())
27   { }
28   void setMin(const SGVec3<T>& min)
29   { _min = min; }
30   const SGVec3<T>& getMin() const
31   { return _min; }
32
33   void setMax(const SGVec3<T>& max)
34   { _max = max; }
35   const SGVec3<T>& getMax() const
36   { return _max; }
37
38   // Only works for floating point types
39   SGVec3<T> getCenter() const
40   { return T(0.5)*(_min + _max); }
41
42   // Only valid for nonempty boxes
43   SGVec3<T> getSize() const
44   { return _max - _min; }
45
46   T getVolume() const
47   {
48     if (empty())
49       return 0;
50     return (_max[0] - _min[0])*(_max[1] - _min[1])*(_max[2] - _min[2]);
51   }
52
53   const bool empty() const
54   { return !valid(); }
55
56   bool valid() const
57   {
58     if (_max[0] < _min[0])
59       return false;
60     if (_max[1] < _min[1])
61       return false;
62     if (_max[2] < _min[2])
63       return false;
64     return true;
65   }
66
67   void clear()
68   {
69     _min[0] = SGLimits<T>::max();
70     _min[1] = SGLimits<T>::max();
71     _min[2] = SGLimits<T>::max();
72     _max[0] = -SGLimits<T>::max();
73     _max[1] = -SGLimits<T>::max();
74     _max[2] = -SGLimits<T>::max();
75   }
76
77   void expandBy(const SGVec3<T>& v)
78   { _min = min(_min, v); _max = max(_max, v); }
79
80   void expandBy(const SGBox<T>& b)
81   { _min = min(_min, b._min); _max = max(_max, b._max); }
82
83   // Note that this only works if the box is nonmepty
84   unsigned getBroadestAxis() const
85   {
86     SGVec3d size = getSize();
87     if (size[1] <= size[0] && size[2] <= size[0])
88       return 0;
89     else if (size[2] <= size[1])
90       return 1;
91     else
92       return 2;
93   }
94
95 private:
96   SGVec3<T> _min;
97   SGVec3<T> _max;
98 };
99
100 /// Output to an ostream
101 template<typename char_type, typename traits_type, typename T>
102 inline
103 std::basic_ostream<char_type, traits_type>&
104 operator<<(std::basic_ostream<char_type, traits_type>& s, const SGBox<T>& box)
105 { return s << "min = " << box.getMin() << ", max = " << box.getMax(); }
106
107 #endif