/usr/include/simgear/math/SGSphere.hxx is in libsimgear-dev 3.0.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | // Copyright (C) 2006 Mathias Froehlich - Mathias.Froehlich@web.de
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Library General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
#ifndef SGSphere_H
#define SGSphere_H
template<typename T>
class SGSphere {
public:
#ifdef __GNUC__
// Avoid "_center not initialized" warnings.
# pragma GCC diagnostic ignored "-Wuninitialized"
#endif
SGSphere() :
/*
* Do not initialize _center to save unneeded initialization time.
* Fix 'may be used uninitialized' warnings locally instead
*/
// _center(0.0, 0.0, 0.0),
_radius(-1)
{ }
SGSphere(const SGVec3<T>& center, const T& radius) :
_center(center),
_radius(radius)
{ }
template<typename S>
explicit SGSphere(const SGSphere<S>& sphere) :
_center(sphere.getCenter()),
_radius(sphere.getRadius())
{ }
#ifdef __GNUC__
// Restore warning settings.
# pragma GCC diagnostic warning "-Wuninitialized"
#endif
const SGVec3<T>& getCenter() const
{ return _center; }
void setCenter(const SGVec3<T>& center)
{ _center = center; }
const T& getRadius() const
{ return _radius; }
void setRadius(const T& radius)
{ _radius = radius; }
T getRadius2() const
{ return _radius*_radius; }
bool empty() const
{ return !valid(); }
bool valid() const
{ return 0 <= _radius; }
void clear()
{ _radius = -1; }
/// Return true if this is inside sphere
bool inside(const SGSphere<T>& sphere) const
{
if (empty())
return false;
if (sphere.empty())
return false;
T dist = sphere.getRadius() - getRadius();
return distSqr(getCenter(), sphere.getCenter()) <= dist*dist;
}
void expandBy(const SGVec3<T>& v)
{
if (empty()) {
_center = v;
_radius = 0;
return;
}
T dist2 = distSqr(_center, v);
if (dist2 <= getRadius2())
return;
T dist = sqrt(dist2);
T newRadius = T(0.5)*(_radius + dist);
_center += ((newRadius - _radius)/dist)*(v - _center);
_radius = newRadius;
}
void expandBy(const SGSphere<T>& s)
{
if (s.empty())
return;
if (empty()) {
_center = s.getCenter();
_radius = s.getRadius();
return;
}
T dist = length(_center - s.getCenter());
if (dist <= SGLimits<T>::min()) {
_radius = SGMisc<T>::max(_radius, s._radius);
return;
}
// already included
if (dist + s.getRadius() <= _radius)
return;
// new one includes all
if (dist + _radius <= s.getRadius()) {
_center = s.getCenter();
_radius = s.getRadius();
return;
}
T newRadius = T(0.5)*(_radius + dist + s.getRadius());
T ratio = (newRadius - _radius) / dist;
_radius = newRadius;
_center[0] += ratio*(s._center[0] - _center[0]);
_center[1] += ratio*(s._center[1] - _center[1]);
_center[2] += ratio*(s._center[2] - _center[2]);
}
void expandBy(const SGBox<T>& box)
{
if (box.empty())
return;
if (empty()) {
_center = box.getCenter();
_radius = T(0.5)*length(box.getSize());
return;
}
SGVec3<T> boxCenter = box.getCenter();
SGVec3<T> corner;
for (unsigned i = 0; i < 3; ++i) {
if (_center[i] < boxCenter[i])
corner[i] = box.getMax()[i];
else
corner[i] = box.getMin()[i];
}
expandBy(corner);
}
private:
SGVec3<T> _center;
T _radius;
};
/// Output to an ostream
template<typename char_type, typename traits_type, typename T>
inline
std::basic_ostream<char_type, traits_type>&
operator<<(std::basic_ostream<char_type, traits_type>& s,
const SGSphere<T>& sphere)
{
return s << "center = " << sphere.getCenter()
<< ", radius = " << sphere.getRadius();
}
#endif
|