]> git.mxchange.org Git - simgear.git/blob - simgear/math/SGSphere.hxx
da0c641a310775366d3c0f6e25984b93c9f4f3fc
[simgear.git] / simgear / math / SGSphere.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 SGSphere_H
19 #define SGSphere_H
20
21
22 template<typename T>
23 class SGSphere {
24 public:
25
26 #ifdef __GNUC__
27 // Avoid "_center not initialized" warnings.
28 #   pragma GCC diagnostic push
29 #   pragma GCC diagnostic ignored "-Wuninitialized"
30 #endif
31
32   SGSphere() :
33      /*
34       * Do not initialize _center to save unneeded initialization time.
35       * Fix 'may be used uninitialized' warnings locally instead
36       */
37 //  _center(0.0, 0.0, 0.0),
38     _radius(-1)
39   { }
40   SGSphere(const SGVec3<T>& center, const T& radius) :
41     _center(center),
42     _radius(radius)
43   { }
44   template<typename S>
45   explicit SGSphere(const SGSphere<S>& sphere) :
46     _center(sphere.getCenter()),
47     _radius(sphere.getRadius())
48   { }
49
50 #ifdef __GNUC__
51   // Restore warning settings.
52 #   pragma GCC diagnostic pop
53 #endif
54
55   const SGVec3<T>& getCenter() const
56   { return _center; }
57   void setCenter(const SGVec3<T>& center)
58   { _center = center; }
59
60   const T& getRadius() const
61   { return _radius; }
62   void setRadius(const T& radius)
63   { _radius = radius; }
64   T getRadius2() const
65   { return _radius*_radius; }
66
67   bool empty() const
68   { return !valid(); }
69
70   bool valid() const
71   { return 0 <= _radius; }
72
73   void clear()
74   { _radius = -1; }
75
76   void expandBy(const SGVec3<T>& v)
77   {
78     if (empty()) {
79       _center = v;
80       _radius = 0;
81       return;
82     }
83
84     T dist2 = distSqr(_center, v);
85     if (dist2 <= getRadius2())
86       return;
87
88     T dist = sqrt(dist2);
89     T newRadius = T(0.5)*(_radius + dist);
90     _center += ((newRadius - _radius)/dist)*(v - _center);
91     _radius = newRadius;
92   }
93
94   void expandBy(const SGSphere<T>& s)
95   {
96     if (s.empty())
97       return;
98
99     if (empty()) {
100       _center = s.getCenter();
101       _radius = s.getRadius();
102       return;
103     }
104
105     T dist = length(_center - s.getCenter());
106     if (dist <= SGLimits<T>::min()) {
107       _radius = SGMisc<T>::max(_radius, s._radius);
108       return;
109     }
110
111     // already included
112     if (dist + s.getRadius() <= _radius)
113       return;
114
115     // new one includes all
116     if (dist + _radius <= s.getRadius()) {
117       _center = s.getCenter();
118       _radius = s.getRadius();
119       return;
120     }
121
122     T newRadius = T(0.5)*(_radius + dist + s.getRadius());
123     T ratio = (newRadius - _radius) / dist;
124     _radius = newRadius;
125
126     _center[0] += ratio*(s._center[0] - _center[0]);
127     _center[1] += ratio*(s._center[1] - _center[1]);
128     _center[2] += ratio*(s._center[2] - _center[2]);
129   }
130
131   void expandBy(const SGBox<T>& box)
132   {
133     if (box.empty())
134       return;
135
136     if (empty()) {
137       _center = box.getCenter();
138       _radius = T(0.5)*length(box.getSize());
139       return;
140     }
141
142     SGVec3<T> boxCenter = box.getCenter();
143     SGVec3<T> corner;
144     for (unsigned i = 0; i < 3; ++i) {
145       if (_center[i] < boxCenter[i])
146         corner[i] = box.getMax()[i];
147       else
148         corner[i] = box.getMin()[i];
149     }
150     expandBy(corner);
151   }
152
153 private:
154   SGVec3<T> _center;
155   T _radius;
156 };
157
158 /// Output to an ostream
159 template<typename char_type, typename traits_type, typename T>
160 inline
161 std::basic_ostream<char_type, traits_type>&
162 operator<<(std::basic_ostream<char_type, traits_type>& s,
163            const SGSphere<T>& sphere)
164 {
165   return s << "center = " << sphere.getCenter()
166            << ", radius = " << sphere.getRadius();
167 }
168
169 #endif