MAV'RIC
maths.h
1 /*******************************************************************************
2  * Copyright (c) 2009-2014, MAV'RIC Development Team
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its contributors
16  * may be used to endorse or promote products derived from this software without
17  * specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  ******************************************************************************/
31 
32 /*******************************************************************************
33  * \file maths.h
34  *
35  * \author MAV'RIC Team
36  * \author Felix Schill
37  * \author Geraud L'Eplattenier
38  *
39  * \brief Useful math functions
40  *
41  ******************************************************************************/
42 
43 
44 #ifndef MATHS_H
45 #define MATHS_H
46 
47 #ifdef __cplusplus
48 extern "C"
49 {
50 #endif
51 
52 #include <stdint.h>
53 #include <math.h>
54 
55 #define PI 3.141592653589793f
56 
57 
61 #define MATH_DEG_TO_RAD (PI/180.0f)
62 
63 
67 #define MATH_RAD_TO_DEG (180.0f/PI)
68 
69 
76 #define SQR(in) \
77  ((in)*(in))
78 
79 
86 float static inline maths_deg_to_rad(float i)
87 {
88  return MATH_DEG_TO_RAD * i;
89 }
90 
91 
98 float static inline maths_rad_to_deg(float i)
99 {
100  return MATH_RAD_TO_DEG * i;
101 }
102 
103 
111 float static inline maths_calc_smaller_angle(float angle)
112 {
113  float out=angle;
114  while (out<-PI) out += 2.0f * PI;
115  while (out>=PI) out -= 2.0f * PI;
116  return out;
117 }
118 
119 
127 float static inline maths_fast_sqrt(float number)
128 {
129  union
130  {
131  float f;
132  int32_t l;
133  }i;
134 
135  float x, y;
136  const float f = 1.5f;
137 
138  x = number * 0.5f;
139  i.f = number;
140  i.l = 0x5f3759df - ( i.l >> 1 );
141  y = i.f;
142  y = y * ( f - ( x * y * y ) );
143  y = y * ( f - ( x * y * y ) ); // repeat newton iteration for more accuracy
144  return number * y;
145 }
146 
147 
155 float static inline maths_fast_sqrt_1(float input)
156 {
157  if (input<0)
158  {
159  return 0.0f;
160  }
161 
162  float result = 1.0f;
163 
164  result = 0.5f * (result + (input / result));
165  result = 0.5f * (result + (input / result));
166 
167  return result;
168 }
169 
170 
178 static inline float maths_f_abs(const float a)
179 {
180  if (a >= 0.0f)
181  {
182  return a;
183  }
184  else
185  {
186  return -a;
187  }
188 }
189 
190 
199 static inline float maths_f_min(const float a, const float b)
200 {
201  if (a <= b)
202  {
203  return a;
204  }
205  else
206  {
207  return b;
208  }
209 }
210 
211 
220 static inline float maths_f_max(const float a, const float b){
221  if (a >= b)
222  {
223  return a;
224  }
225  else
226  {
227  return b;
228  }
229 }
230 
231 
242 static float inline maths_clip(float input_value, float clip_value)
243 {
244  if (input_value>clip_value) return clip_value;
245  if (input_value<-clip_value) return -clip_value;
246  return input_value;
247 }
248 
249 
258 static float inline maths_soft_zone(float x, float soft_zone_width)
259 {
260  if (soft_zone_width < 0.0000001f)
261  {
262  return x;
263  }
264  else
265  {
266  return x * x * x / ( SQR(soft_zone_width) + SQR(x) );
267  }
268 };
269 
270 
277 static float inline maths_sigmoid(float x)
278 {
279  return (x / maths_fast_sqrt(1 + SQR(x)));
280 };
281 
282 
289 static float inline maths_center_window_2(float x)
290 {
291  return 1.0f / (1 + SQR(x));
292 }
293 
294 
301 static float inline maths_center_window_4(float x)
302 {
303  return 1.0f / (1 + SQR(SQR(x)));
304 }
305 
306 
318 static float inline maths_median_filter_3x(float a, float b, float c)
319 {
320  float middle;
321 
322  if ((a <= b) && (a <= c))
323  {
324  middle = (b <= c) ? b : c;
325  }
326  else if ((b <= a) && (b <= c))
327  {
328  middle = (a <= c) ? a : c;
329  }
330  else
331  {
332  middle = (a <= b) ? a : b;
333  }
334 
335  return middle;
336 }
337 
338 
352 static inline float maths_interpolate(float x, float x1, float x2, float y1, float y2)
353 {
354  if (x1 == x2)
355  {
356  return y1;
357  }
358  else
359  {
360  float y = y1 + (y2 - y1) * (x - x1) / (x2 - x1);
361  return y;
362  }
363 }
364 
365 
366 #ifdef __cplusplus
367 }
368 #endif
369 
370 #endif /* MATHS_H */