22 #ifndef CRNMath_HEADER
23 #define CRNMath_HEADER
30 #if defined(CRN_PF_ANDROID)
31 inline double log2(
double n)
33 return log(n) / log(2.0);
38 # define M_PI 3.14159265358979323846
47 template<
typename T>
inline const T&
Max(
const T &a,
const T &b) {
return std::max(a, b); }
49 template<
typename T>
inline const T&
Min(
const T &a,
const T &b) {
return std::min(a, b); }
51 template<
typename T>
inline const T&
Max(
const T &a,
const T &b,
const T&c) {
return std::max(std::max(a, b), c); }
53 template<
typename T>
inline const T&
Min(
const T &a,
const T &b,
const T&c) {
return std::min(std::min(a, b), c); }
59 template<
typename T> constexpr
int SignOf(
const T &x) noexcept(noexcept(x<0)) {
return (x < 0) ? -1 : 1; }
61 template<
typename T> constexpr
SumType<T> Sqr(
const T&v) noexcept(noexcept(v*v)) {
return v * v; }
63 template<
typename T> constexpr T
Abs(
const T &v) noexcept(noexcept(v<0)&&noexcept(-v)) {
return (v < 0) ? -v : v; }
65 template<
typename T>
inline T
Abs(
const std::complex<T> &v) noexcept(noexcept(std::abs(std::complex<T>(0)))) {
return std::abs(v); }
67 template<
typename T>
inline T
AbsMax(
const T &a,
const T &b) noexcept(noexcept(
Abs(a))) {
return Abs(a) >=
Abs(b) ? a : b; }
69 template<
typename T>
inline T
AbsMin(
const T &a,
const T &b) noexcept(noexcept(
Abs(a))) {
return Abs(a) <
Abs(b) ? a : b; }
75 template<
typename T>
inline const T&
Cap(
const T &v,
const T &min,
const T &max) {
return Min(max,
Max(min, v)); }
78 inline double Gauss(
double x,
double sigma) {
return exp((- x * x) / (2.0 * sigma * sigma)); }
81 inline double MeanGauss(
double x,
double sigma) {
return (
Gauss(x, sigma) +
Gauss(x + 0.5, sigma) +
Gauss(x - 0.5, sigma)) / 3.0; }
84 inline double Pythagoras(
double a,
double b) noexcept {
double aa =
Abs(a), ab =
Abs(b);
return (aa > ab) ? aa * sqrt(1.0 +
Sqr(ab / aa)) : ((ab == 0.0) ? 0.0 : ab * sqrt(1.0 +
Sqr(aa / ab))); }
86 inline double Pythagoras(
int a,
int b) noexcept {
double aa =
Abs(a), ab =
Abs(b);
return (aa > ab) ? aa * sqrt(1.0 +
Sqr(ab / aa)) : ((ab == 0.0) ? 0.0 : ab * sqrt(1.0 +
Sqr(aa / ab))); }
89 inline double Pythagoras(
double a,
double b,
double c) noexcept {
return sqrt(
Sqr(a) +
Sqr(b) +
Sqr(c)); }
92 template <
class ITER>
inline double Pythagoras(ITER it_begin, ITER it_end)
94 auto cumul_squares = 0.0;
96 for (ITER it = it_begin; it != it_end; ++it)
97 cumul_squares +=
Sqr(*it);
99 return sqrt(cumul_squares);
103 template <
class ITER>
inline void Scale(ITER it_begin, ITER it_end,
const double s)
105 for (ITER it = it_begin; it != it_end; ++it)
164 typename std::enable_if<std::is_arithmetic<T>::value,
int>::type = 0
168 return double(
Abs(o1 - o2));
typename TypeInfo< T >::SumType SumType
Direction ToDirection(const Angle< Unit > &ang) noexcept
T AbsMax(const T &a, const T &b) noexcept(noexcept(Abs(a)))
Returns the value that has the maximal absolute value.
Orientation
An enumeration of orientations.
const T & Cap(const T &v, const T &min, const T &max)
Bounds a value to an interval.
double MeanGauss(double x, double sigma)
Computes Gauss function at x for a given standard deviation (centered in 0) – to use with matrices...
const T & Max(const T &a, const T &b)
Returns the max of two values.
Direction
An enumeration of directions.
A convenience class for angles units.
Orientation ToOrientation(Direction d) noexcept
T AbsMinSameSign(const T &a, const T &b) noexcept(noexcept(Abs(a))&&noexcept(SignOf(a)))
Returns the value that has the minimal absolute value if the values have the same sign...
void Scale(ITER it_begin, ITER it_end, const double s)
Scale a collection of numbers.
decltype(std::declval< T >()+std::declval< T >()) SumType
A safe type to compute a sum (e.g.: a larger integer type)
constexpr TypeInfo< T >::SumType Twice(const T &v) noexcept(noexcept(v+v))
Returns the double of a value.
constexpr SumType< T > Sqr(const T &v) noexcept(noexcept(v *v))
Returns the square of a value.
double Gauss(double x, double sigma)
Computes Gauss function at x for a given standard deviation (centered in 0)
double Distance(const Int &i1, const Int &i2) noexcept
constexpr int SignOf(const T &x) noexcept(noexcept(x< 0))
Returns the sign (-1 or 1) of a value.
T AbsMaxSameSign(const T &a, const T &b) noexcept(noexcept(Abs(a))&&noexcept(SignOf(a)))
Returns the value that has the maximal absolute value if the values have the same sign...
void Abs(Image< T > &img, typename std::enable_if< std::is_arithmetic< T >::value >::type *dummy=nullptr) noexcept
Replaces each pixel by its absolute value.
DistanceType
An enumeration of distances.
const T & Min(const T &a, const T &b)
Returns the min of two values.
T AbsMin(const T &a, const T &b) noexcept(noexcept(Abs(a)))
Returns the value that has the minimal absolute value.
#define CRN_DECLARE_ENUM_OPERATORS(Type)
constexpr TypeInfo< T >::SumType Thrice(const T &v) noexcept(noexcept(v+v))
Returns three times a value.
double Pythagoras(double a, double b) noexcept
Computes sqrt(a²+b²) without destructive underflow or overflow.