X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=simgear%2Fmath%2FSGMisc.hxx;h=8eaa124e27704eca149bf7f8c912fcd661670aba;hb=b99f53fda3b06378668bdccd4a9d07161f263366;hp=b8ba7e8d9c764b45d6d0daa4d9206105d24b6be6;hpb=9be14a63b1b593b02b5cc3e82edb7a266ae3cbe7;p=simgear.git diff --git a/simgear/math/SGMisc.hxx b/simgear/math/SGMisc.hxx index b8ba7e8d..8eaa124e 100644 --- a/simgear/math/SGMisc.hxx +++ b/simgear/math/SGMisc.hxx @@ -10,21 +10,20 @@ // 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 Library General Public -// License along with this library; if not, write to the -// Free Software Foundation, Inc., 59 Temple Place - Suite 330, -// Boston, MA 02111-1307, USA. +// 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 -#include - template 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) @@ -37,6 +36,11 @@ public: { 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)); } + static int sign(const T& a) { if (a < -SGLimits::min()) @@ -52,7 +56,42 @@ public: static T deg2rad(const T& val) { return val*pi()/180; } -#ifndef NDEBUG + // 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::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 + 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) @@ -69,10 +108,6 @@ public: return !(v == v); #endif } -#endif }; -typedef SGMisc SGMiscf; -typedef SGMisc SGMiscd; - #endif