libcrn  3.9.5
A document image processing library
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CRNMath.h
Go to the documentation of this file.
1 /* Copyright 2006-2016 Yann LEYDIER, CoReNum, INSA-Lyon, Université Paris Descartes, ENS-Lyon
2  *
3  * This file is part of libcrn.
4  *
5  * libcrn is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * libcrn is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with libcrn. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * file: CRNMath.h
19  * \author Yann LEYDIER
20  */
21 
22 #ifndef CRNMath_HEADER
23 #define CRNMath_HEADER
24 
25 #include <CRN.h>
26 #include <cmath>
27 #include <complex>
28 #include <algorithm>
29 
30 #if defined(CRN_PF_ANDROID)
31 inline double log2(double n)
32 {
33  return log(n) / log(2.0);
34 }
35 #endif
36 
37 #ifndef M_PI
38 # define M_PI 3.14159265358979323846
39 #endif
40 
42 namespace crn
43 {
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); }
55  template<typename T> constexpr typename TypeInfo<T>::SumType Twice(const T&v) noexcept(noexcept(v+v)) { return v + v; }
57  template<typename T> constexpr typename TypeInfo<T>::SumType Thrice(const T&v) noexcept(noexcept(v+v)) { return v + v + v; }
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; }
71  template<typename T> inline T AbsMaxSameSign(const T &a, const T &b) noexcept(noexcept(Abs(a))&&noexcept(SignOf(a))) { if (SignOf(a) == SignOf(b)) return Abs(a) >= Abs(b) ? a : b; else return T(0); }
73  template<typename T> inline T AbsMinSameSign(const T &a, const T &b) noexcept(noexcept(Abs(a))&&noexcept(SignOf(a))) { if (SignOf(a) == SignOf(b)) return Abs(a) < Abs(b) ? a : b; else return T(0); }
75  template<typename T> inline const T& Cap(const T &v, const T &min, const T &max) { return Min(max, Max(min, v)); }
76 
78  inline double Gauss(double x, double sigma) { return exp((- x * x) / (2.0 * sigma * sigma)); }
79 
81  inline double MeanGauss(double x, double sigma) { return (Gauss(x, sigma) + Gauss(x + 0.5, sigma) + Gauss(x - 0.5, sigma)) / 3.0; }
82 
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))); }
87 
89  inline double Pythagoras(double a, double b, double c) noexcept { return sqrt(Sqr(a) + Sqr(b) + Sqr(c)); }
90 
92  template <class ITER> inline double Pythagoras(ITER it_begin, ITER it_end)
93  {
94  auto cumul_squares = 0.0;
95 
96  for (ITER it = it_begin; it != it_end; ++it)
97  cumul_squares += Sqr(*it);
98 
99  return sqrt(cumul_squares);
100  }
101 
103  template <class ITER> inline void Scale(ITER it_begin, ITER it_end, const double s)
104  {
105  for (ITER it = it_begin; it != it_end; ++it)
106  (*it) *= s;
107  }
108 
110 }
111 
112 #include <CRNMath/CRNTrigonometry.h>
113 
114 namespace crn
115 {
122  enum class Direction { INVALID = 0, LEFT = 1, RIGHT = 2, TOP = 4, BOTTOM = 8 };
123  template<typename Unit> Direction ToDirection(const Angle<Unit> &ang) noexcept
124  {
125  Angle<ByteAngle> a(ang);
126  if (a == Angle<ByteAngle>::LEFT())
127  {
128  return Direction::LEFT;
129  }
130  else if (a == Angle<ByteAngle>::RIGHT())
131  {
132  return Direction::RIGHT;
133  }
134  else if (a == Angle<ByteAngle>::TOP())
135  {
136  return Direction::TOP;
137  }
138  else if (a == Angle<ByteAngle>::BOTTOM())
139  {
140  return Direction::BOTTOM;
141  }
142  else
143  {
144  return Direction::INVALID;
145  }
146  }
147 
152  enum class Orientation { INVALID = 0, HORIZONTAL, VERTICAL };
153  Orientation ToOrientation(Direction d) noexcept;
154 
159  enum class DistanceType { INVALID = 0, D4, D8, EUCLIDEAN };
160 
162  template<
163  typename T,
164  typename std::enable_if<std::is_arithmetic<T>::value, int>::type = 0
165  >
166  double Distance(T o1, T o2)
167  {
168  return double(Abs(o1 - o2));
169  }
170 
173 }
174 
177 
178 #endif
179 
180 
typename TypeInfo< T >::SumType SumType
Definition: CRNType.h:185
Direction ToDirection(const Angle< Unit > &ang) noexcept
Definition: CRNMath.h:123
T AbsMax(const T &a, const T &b) noexcept(noexcept(Abs(a)))
Returns the value that has the maximal absolute value.
Definition: CRNMath.h:67
Orientation
An enumeration of orientations.
Definition: CRNMath.h:152
const T & Cap(const T &v, const T &min, const T &max)
Bounds a value to an interval.
Definition: CRNMath.h:75
double MeanGauss(double x, double sigma)
Computes Gauss function at x for a given standard deviation (centered in 0) – to use with matrices...
Definition: CRNMath.h:81
const T & Max(const T &a, const T &b)
Returns the max of two values.
Definition: CRNMath.h:47
Direction
An enumeration of directions.
Definition: CRNMath.h:122
A convenience class for angles units.
Orientation ToOrientation(Direction d) noexcept
Definition: CRNMath.cpp:31
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...
Definition: CRNMath.h:73
void Scale(ITER it_begin, ITER it_end, const double s)
Scale a collection of numbers.
Definition: CRNMath.h:103
decltype(std::declval< T >()+std::declval< T >()) SumType
A safe type to compute a sum (e.g.: a larger integer type)
Definition: CRNType.h:179
constexpr TypeInfo< T >::SumType Twice(const T &v) noexcept(noexcept(v+v))
Returns the double of a value.
Definition: CRNMath.h:55
constexpr SumType< T > Sqr(const T &v) noexcept(noexcept(v *v))
Returns the square of a value.
Definition: CRNMath.h:61
double Gauss(double x, double sigma)
Computes Gauss function at x for a given standard deviation (centered in 0)
Definition: CRNMath.h:78
double Distance(const Int &i1, const Int &i2) noexcept
Definition: CRNInt.h:78
constexpr int SignOf(const T &x) noexcept(noexcept(x< 0))
Returns the sign (-1 or 1) of a value.
Definition: CRNMath.h:59
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...
Definition: CRNMath.h:71
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.
Definition: CRNImageGray.h:47
DistanceType
An enumeration of distances.
Definition: CRNMath.h:159
const T & Min(const T &a, const T &b)
Returns the min of two values.
Definition: CRNMath.h:49
T AbsMin(const T &a, const T &b) noexcept(noexcept(Abs(a)))
Returns the value that has the minimal absolute value.
Definition: CRNMath.h:69
#define CRN_DECLARE_ENUM_OPERATORS(Type)
Definition: CRNType.h:275
constexpr TypeInfo< T >::SumType Thrice(const T &v) noexcept(noexcept(v+v))
Returns three times a value.
Definition: CRNMath.h:57
double Pythagoras(double a, double b) noexcept
Computes sqrt(a²+b²) without destructive underflow or overflow.
Definition: CRNMath.h:84