/usr/include/simgear/math/SGMisc.hxx is in libsimgear-dev 1:2018.1.1+dfsg-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 | // 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 SGMisc_H
#define SGMisc_H
template<typename T>
class SGMisc {
public:
static T pi() { return T(3.1415926535897932384626433832795029L); }
static T twopi() { return 2*T(3.1415926535897932384626433832795029L); }
static T min(const T& a, const T& b)
{ return a < b ? a : b; }
static T min(const T& a, const T& b, const T& c)
{ return min(min(a, b), c); }
static T min(const T& a, const T& b, const T& c, const T& d)
{ return min(min(min(a, b), c), d); }
static T max(const T& a, const T& b)
{ return a > b ? a : b; }
static T max(const T& a, const T& b, const T& c)
{ return max(max(a, b), c); }
static T max(const T& a, const T& b, const T& c, const T& d)
{ return max(max(max(a, b), c), d); }
// clip the value of a to be in the range between and including _min and _max
static T clip(const T& a, const T& _min, const T& _max)
{ return max(_min, min(_max, a)); }
/// Add two (integer) values taking care of overflows.
static T addClipOverflow(T a, T b)
{
if( b > 0 )
{
if( SGLimits<T>::max() - b < a )
return SGLimits<T>::max();
}
else
{
if( SGLimits<T>::min() - b > a )
return SGLimits<T>::min();
}
return a + b;
}
/// Add two (integer) values in place, taking care of overflows.
static T& addClipOverflowInplace(T& a, T b)
{
return a = addClipOverflow(a, b);
}
/**
* Seek a variable towards a target value with given rate and timestep
*
* @param var Variable or eg. SGPropObj
* @param target Target value
* @param rate Max. change rate/sec
* @param dt Time step (sec)
*/
template<class Var>
static T seek(Var& var, T target, T rate, T dt)
{
if( var < target )
return var = min(var + rate * dt, target);
else
return var = max(var - rate * dt, target);
}
/**
* Get @c base raised to the power of @c N
*
* @tparam N Exponent
* @param base Base
*/
template<int N>
static T pow(T base)
{
return (N < 0)
? (1. / pow<-N>(base))
: ( ((N & 1) ? base : 1)
* ((N > 1) ? pow<N / 2>(base * base) : 1)
);
}
static int sign(const T& a)
{
if (a < -SGLimits<T>::min())
return -1;
else if (SGLimits<T>::min() < a)
return 1;
else
return 0;
}
static T rad2deg(const T& val)
{ return val*180/pi(); }
static T deg2rad(const T& val)
{ return val*pi()/180; }
// normalize the value to be in a range between [min, max[
static T
normalizePeriodic(const T& min, const T& max, const T& value)
{
T range = max - min;
if (range < SGLimits<T>::min())
return min;
T normalized = value - range*floor((value - min)/range);
// two security checks that can only happen due to roundoff
if (normalized <= min)
return min;
if (max <= normalized)
return min;
return normalized;
}
// normalize the angle to be in a range between [-pi, pi[
static T
normalizeAngle(const T& angle)
{ return normalizePeriodic(-pi(), pi(), angle); }
// normalize the angle to be in a range between [0, 2pi[
static T
normalizeAngle2(const T& angle)
{ return normalizePeriodic(0, twopi(), angle); }
static T round(const T& v)
{ return floor(v + T(0.5)); }
static int roundToInt(const T& v)
{ return int(round(v)); }
// Linear interpolation between two arbitrary typed values
template<typename S>
static S lerp(const S& val0, const S& val1, const T& t)
{ return val0*(T(1) - t) + val1*t; }
/// Returns true if v is a NaN value
/// Use with care: allways code that you do not need to use that!
static bool isNaN(const T& v)
{
return std::isnan(v);
}
static bool eq(const T& a, const T& b, const T& epsilon = SGLimits<T>::epsilon())
{ return std::abs(a - b) < epsilon; }
static bool neq(const T& a, const T& b, const T& epsilon = SGLimits<T>::epsilon())
{ return !eq(a, b, epsilon); }
};
#endif
|