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