#include "AP_Math.h" #include // a varient of asin() that checks the input ranges and ensures a // valid angle as output. If nan is given as input then zero is // returned. float safe_asin(float v) { if (isnan(v)) { return 0.0f; } if (v >= 1.0f) { return M_PI/2; } if (v <= -1.0f) { return -M_PI/2; } return asinf(v); } // a varient of sqrt() that checks the input ranges and ensures a // valid value as output. If a negative number is given then 0 is // returned. The reasoning is that a negative number for sqrt() in our // code is usually caused by small numerical rounding errors, so the // real input should have been zero float safe_sqrt(float v) { float ret = sqrtf(v); if (isnan(ret)) { return 0; } return ret; } /* linear interpolation based on a variable in a range */ float linear_interpolate(float low_output, float high_output, float var_value, float var_low, float var_high) { if (var_value <= var_low) { return low_output; } if (var_value >= var_high) { return high_output; } float p = (var_value - var_low) / (var_high - var_low); return low_output + p * (high_output - low_output); } template float wrap_180(const T angle, float unit_mod) { const float ang_180 = 180.f * unit_mod; const float ang_360 = 360.f * unit_mod; float res = fmod(static_cast(angle) + ang_180, ang_360); if (res < 0 || is_zero(res)) { res += ang_180; } else { res -= ang_180; } return res; } template float wrap_180(const int angle, float unit_mod); template float wrap_180(const short angle, float unit_mod); template float wrap_180(const float angle, float unit_mod); template float wrap_180(const double angle, float unit_mod); template auto wrap_180_cd(const T angle) -> decltype(wrap_180(angle, 100.f)) { return wrap_180(angle, 100.f); } template auto wrap_180_cd(const float angle) -> decltype(wrap_180(angle, 100.f)); template auto wrap_180_cd(const int angle) -> decltype(wrap_180(angle, 100.f)); template auto wrap_180_cd(const short angle) -> decltype(wrap_180(angle, 100.f)); template auto wrap_180_cd(const double angle) -> decltype(wrap_360(angle, 100.f)); template float wrap_360(const T angle, float unit_mod) { const float ang_360 = 360.f * unit_mod; float res = fmodf(static_cast(angle), ang_360); if (res < 0) { res += ang_360; } return res; } template float wrap_360(const int angle, float unit_mod); template float wrap_360(const short angle, float unit_mod); template float wrap_360(const float angle, float unit_mod); template float wrap_360(const double angle, float unit_mod); template auto wrap_360_cd(const T angle) -> decltype(wrap_360(angle, 100.f)) { return wrap_360(angle, 100.f); } template auto wrap_360_cd(const float angle) -> decltype(wrap_360(angle, 100.f)); template auto wrap_360_cd(const int angle) -> decltype(wrap_360(angle, 100.f)); template auto wrap_360_cd(const short angle) -> decltype(wrap_360(angle, 100.f)); template auto wrap_360_cd(const double angle) -> decltype(wrap_360(angle, 100.f)); template float wrap_PI(const T radian) { float res = fmod(static_cast(radian) + M_PI, M_2PI); if (res < 0 || is_zero(res)) { res += M_PI; } else { res -= M_PI; } return res; } template float wrap_PI(const int radian); template float wrap_PI(const short radian); template float wrap_PI(const float radian); template float wrap_PI(const double radian); template float wrap_2PI(const T radian) { float res = fmodf(static_cast(radian), M_2PI); if (res < 0) { res += M_2PI; } return res; } template float wrap_2PI(const int radian); template float wrap_2PI(const short radian); template float wrap_2PI(const float radian); template float wrap_2PI(const double radian);