22 template <
typename num_t,
typename return_t = num_t>
23 inline return_t
square(
const num_t x)
25 return static_cast<return_t
>(x * x);
33 return std::sqrt(x * x + y * y);
36 #ifdef DEG2RAD // functions are preferred over macros 43 #define M_PI 3.14159265358979323846 47 constexpr
inline double DEG2RAD(
const double x) {
return x *
M_PI / 180.0; }
49 constexpr
inline float DEG2RAD(
const float x)
51 return x * float(
M_PI) / 180.0f;
54 constexpr
inline double DEG2RAD(
const int x) {
return x *
M_PI / 180.0; }
56 constexpr
inline double RAD2DEG(
const double x) {
return x * 180.0 /
M_PI; }
58 constexpr
inline float RAD2DEG(
const float x)
60 return x * 180.0f / float(
M_PI);
63 #define M_PIl 3.14159265358979323846264338327950288L 64 #define M_2PIl (2.0L * 3.14159265358979323846264338327950288L) 67 constexpr
inline long double DEG2RAD(
const long double x)
69 return x *
M_PIl / 180.0;
72 constexpr
inline long double RAD2DEG(
const long double x)
74 return x * 180.0 /
M_PIl;
79 #define DEG2RAD DEG2RAD 80 #define RAD2DEG RAD2DEG 83 constexpr
inline double operator"" _deg(
long double v)
92 return x < 0 ? -1 : 1;
99 return (x == 0 || x == -0) ? 0 :
sign(x);
103 template <
typename T>
117 template <
typename T>
120 return std::max(a, b) - std::min(a, b);
123 template <
typename T>
124 inline const T
min3(
const T&
A,
const T& B,
const T& C)
126 return std::min<T>(
A, std::min<T>(B, C));
128 template <
typename T>
129 inline const T
max3(
const T&
A,
const T& B,
const T& C)
131 return std::max<T>(
A, std::max<T>(B, C));
135 template <
typename T>
138 return x > 0 ?
static_cast<int>(floor(static_cast<double>(x)))
139 : static_cast<int>(ceil(static_cast<double>(x)));
144 template <
typename T,
typename K>
147 if (test_val < var) var =
static_cast<T
>(test_val);
151 template <
typename T,
typename K>
154 if (test_val > var) var =
static_cast<T
>(test_val);
158 template <
typename T>
159 inline void saturate(T& var,
const T sat_min,
const T sat_max)
161 if (var > sat_max) var = sat_max;
162 if (var < sat_min) var = sat_min;
166 template <
typename T>
167 inline T
saturate_val(
const T& value,
const T sat_min,
const T sat_max)
170 if (var > sat_max) var = sat_max;
171 if (var < sat_min) var = sat_min;
183 if (n <= 1)
throw std::invalid_argument(
"round2up: Overflow!");
189 inline float d2f(
const double d) {
return static_cast<float>(d); }
193 inline uint8_t
f2u8(
const float f) {
return static_cast<uint8_t
>(f * 255); }
196 inline float u8tof(
const uint8_t v) {
return v / 255.0f; }
void keep_min(T &var, const K test_val)
If the second argument is below the first one, set the first argument to this lower value...
int fix(T x)
Rounds toward zero.
const T min3(const T &A, const T &B, const T &C)
void saturate(T &var, const T sat_min, const T sat_max)
Saturate the value of var (the variable gets modified) so it does not get out of [min,max].
float d2f(const double d)
shortcut for static_cast<float>(double)
constexpr double DEG2RAD(const double x)
Degrees to radians.
int sign(T x)
Returns the sign of X as "1" or "-1".
T round2up(T val)
Round up to the nearest power of two of a given number.
T hypot_fast(const T x, const T y)
Faster version of std::hypot(), to use when overflow is not an issue and we prefer fast code...
void keep_max(T &var, const K test_val)
If the second argument is above the first one, set the first argument to this higher value...
T lowestPositive(const T a, const T b)
Returns the smallest positive number among a and b.
return_t square(const num_t x)
Inline function for the square of a number.
uint8_t f2u8(const float f)
converts a float [0,1] into an uint8_t [0,255] (without checking for out of bounds) ...
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
constexpr double RAD2DEG(const double x)
Radians to degrees.
float u8tof(const uint8_t v)
converts a uint8_t [0,255] into a float [0,1]
T saturate_val(const T &value, const T sat_min, const T sat_max)
Like saturate() but it returns the value instead of modifying the variable.
const T max3(const T &A, const T &B, const T &C)
T abs_diff(const T a, const T b)
Efficient and portable evaluation of the absolute difference of two unsigned integer values (but will...
int signWithZero(T x)
Returns the sign of X as "0", "1" or "-1".