X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=simgear%2Fsg_inlines.h;h=8e4585d479876f8d39db37146deb9a7f535a9df1;hb=a0bdec284624820feb0a96a06c0c38e2f07d5e4e;hp=0fc8676113eb5e1f8eb9731cf2f64151684f9808;hpb=4326d560e96e42893d9048cc6ee6625d7351c054;p=simgear.git diff --git a/simgear/sg_inlines.h b/simgear/sg_inlines.h index 0fc86761..8e4585d4 100644 --- a/simgear/sg_inlines.h +++ b/simgear/sg_inlines.h @@ -28,37 +28,77 @@ #ifndef _SG_INLINES_H #define _SG_INLINES_H +// return the sign of a value template -inline const int SG_SIGN(const T x) { +inline int SG_SIGN(const T x) { return x < T(0) ? -1 : 1; } +// return the minimum of two values template -inline const T SG_MIN2(const T a, const T b) { +inline T SG_MIN2(const T a, const T b) { return a < b ? a : b; } // return the minimum of three values template -inline const T SG_MIN3( const T a, const T b, const T c) { +inline T SG_MIN3( const T a, const T b, const T c) { return (a < b ? SG_MIN2 (a, c) : SG_MIN2 (b, c)); } +// return the maximum of two values template -inline const T SG_MAX2(const T a, const T b) { +inline T SG_MAX2(const T a, const T b) { return a > b ? a : b; } // return the maximum of three values template -inline const T SG_MAX3 (const T a, const T b, const T c) { +inline T SG_MAX3 (const T a, const T b, const T c) { return (a > b ? SG_MAX2 (a, c) : SG_MAX2 (b, c)); } -// +// return the minimum and maximum of three values +template +inline void SG_MIN_MAX3 ( T &min, T &max, const T a, const T b, const T c) { + if( a > b ) { + if( a > c ) { + max = a; + min = SG_MIN2 (b, c); + } else { + max = c; + min = SG_MIN2 (a, b); + } + } else { + if( b > c ) { + max = b; + min = SG_MIN2 (a, c); + } else { + max = c; + min = SG_MIN2 (a, b); + } + } +} + +// swap two values template inline void SG_SWAP( T &a, T &b) { T c = a; a = b; b = c; } +// clamp a value to lie between min and max +template +inline void SG_CLAMP_RANGE(T &x, const T min, const T max ) { + if ( x < min ) { x = min; } + if ( x > max ) { x = max; } +} + +// normalize a value to lie between min and max +template +inline void SG_NORMALIZE_RANGE( T &val, const T min, const T max ) { + T step = max - min; + while( val >= max ) val -= step; + while( val < min ) val += step; +}; + #endif // _SG_INLINES_H