fast_math.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #ifndef _Fast_Math_H__
  2. #define _Fast_Math_H__
  3. #include <arm_math.h>
  4. #include "libs/utils.h"
  5. // Constants
  6. #define ONE_BY_SQRT3 (0.57735026919f) // 1/sqrt(3)
  7. #define TWO_BY_SQRT3 (2.0f * 0.57735026919f)
  8. #define SQRT3_BY_2 (0.86602540378f)
  9. #define SQRT3 (1.73205080757f)
  10. #define SQRT2_BY_SQRT3 (0.8164966f)
  11. #define TWO_BY_THREE (0.66667f)
  12. #define M_PI (3.14159265f)
  13. #define ONE_BY_SQRT3_Q14 (9459L) //0.57735026919 * 16384.0F
  14. #define SQRT3_BY_2_Q14 (14189L)//0.86602540378 * 16384.0F
  15. #define TWO_BY_SQRT3_Q14 (18918L)
  16. #ifndef SQ
  17. #define SQ(x) ((x)*(x))
  18. #endif
  19. #define NORM2_f(x,y) (sqrtf(SQ(x) + SQ(y)))
  20. #define sqrtsub2_f(x,y) (sqrtf(SQ(x) - SQ(y)))
  21. // nan and infinity check for floats
  22. #define UTILS_IS_INF(x) ((x) == (1.0F / 0.0F) || (x) == (-1.0F / 0.0F))
  23. #define UTILS_IS_NAN(x) ((x) != (x))
  24. #define UTILS_NAN_ZERO(x) (x = UTILS_IS_NAN(x) ? 0.0F : x)
  25. void fast_sincos(float angle, float *sin, float *cos);
  26. void arm_sin_cos(float angle, float *s, float *c);
  27. static __INLINE int32_t sclamp(int32_t v, int32_t minv, int32_t maxv) {
  28. if (v < minv) {
  29. return minv;
  30. }else if (v > maxv) {
  31. return maxv;
  32. }
  33. return v;
  34. }
  35. static __INLINE float fclamp(float v, float minv, float maxv) {
  36. if (v < minv) {
  37. return minv;
  38. }else if (v > maxv) {
  39. return maxv;
  40. }
  41. return v;
  42. }
  43. static void fast_norm_angle(float *angle) {
  44. *angle = fmodf(*angle, 360.0f);
  45. if (*angle < 0.0f) {
  46. *angle += 360.0f;
  47. }
  48. }
  49. static __INLINE float f_map(float x, float in_min, float in_max, float out1, float out2) {
  50. if (out1 > out2) { /* 递增map */
  51. return out1 - (x - in_min) * (out1 - out2) / (in_max - in_min);
  52. }else { /* 递减map */
  53. return (x - in_min) * (out2 - out1) / (in_max - in_min) + out1;
  54. }
  55. }
  56. static __INLINE void step_towards(float *value, float goal, float step) {
  57. if (*value < goal) {
  58. if ((*value + step) < goal) {
  59. *value += step;
  60. } else {
  61. *value = goal;
  62. }
  63. } else if (*value > goal) {
  64. if ((*value - step) > goal) {
  65. *value -= step;
  66. } else {
  67. *value = goal;
  68. }
  69. }
  70. }
  71. static __INLINE void step_towards_s16(s16 *value, s16 goal, s16 step) {
  72. if (*value < goal) {
  73. if ((*value + step) < goal) {
  74. *value += step;
  75. } else {
  76. *value = goal;
  77. }
  78. } else if (*value > goal) {
  79. if ((*value - step) > goal) {
  80. *value -= step;
  81. } else {
  82. *value = goal;
  83. }
  84. }
  85. }
  86. static __INLINE s16 line_intp(s16 x, s16 x_l, s16 x_h, s16 y_l, s16 y_h) {
  87. float r = (float)(x - x_l) /(float)(x_h - x_l);
  88. return (s16)(r * (y_h - y_l)) + y_l;
  89. }
  90. /**
  91. * Fast atan2
  92. *
  93. * See based on https://math.stackexchange.com/a/1105038/81278
  94. *
  95. * @param y
  96. * y
  97. *
  98. * @param x
  99. * x
  100. *
  101. * @return
  102. * The angle in radians
  103. */
  104. static __INLINE float fast_atan2(float y, float x) {
  105. // a := min (|x|, |y|) / max (|x|, |y|)
  106. float abs_y = ABS(y);
  107. float abs_x = ABS(x);
  108. // inject FLT_MIN in denominator to avoid division by zero
  109. float a = min(abs_x, abs_y) / (MAX(abs_x, abs_y) + 1e-20f);
  110. // s := a * a
  111. float s = a * a;
  112. // r := ((-0.0464964749 * s + 0.15931422) * s - 0.327622764) * s * a + a
  113. float r = ((-0.0464964749f * s + 0.15931422f) * s - 0.327622764f) * s * a + a;
  114. // if |y| > |x| then r := 1.57079637 - r
  115. if (abs_y > abs_x)
  116. r = 1.57079637f - r;
  117. // if x < 0 then r := 3.14159274 - r
  118. if (x < 0.0f)
  119. r = 3.14159274f - r;
  120. // if y < 0 then r := -r
  121. if (y < 0.0f)
  122. r = -r;
  123. return r;
  124. }
  125. static __INLINE void saturate_vector_2d(float *x, float *y, float max) {
  126. float mag = NORM2_f(*x, *y);
  127. max = fabsf(max);
  128. if (mag < 1e-4f) {
  129. mag = 1e-4f;
  130. }
  131. if (mag > max) {
  132. const float f = max / mag;
  133. *x *= f;
  134. *y *= f;
  135. }
  136. }
  137. static void normal_sincosf(float angle, float *sin, float *cos) {
  138. *sin = arm_sin_f32(angle);
  139. *cos = arm_cos_f32(angle);
  140. }
  141. #define degree_2_pi(d) ((float)(d) * M_PI / 180.0f)
  142. #define pi_2_degree(d) ((float)(d) * 180.0f / M_PI)
  143. #define norm_angle_rad(a) {while (a >= M_PI*2) a-=M_PI*2;while (a < 0) a +=M_PI*2;};
  144. #define norm_angle_deg(a) {while (a >= 360.0f) a-=360.0f;while (a < 0) a +=360.0f;};
  145. #define INVALID_ANGLE 0x3DFF
  146. #define SIGN(x) (((x) < 0.0f) ? -1.0f : 1.0f)
  147. /**
  148. * A simple low pass filter.
  149. *
  150. * @param value
  151. * The filtered value.
  152. *
  153. * @param sample
  154. * Next sample.
  155. *
  156. * @param filter_constant
  157. * Filter constant. Range 0.0 to 1.0, where 1.0 gives the unfiltered value.
  158. */
  159. /* 前向差分离散化 */
  160. #define LowPass_Filter(value, sample, filter_constant) (value = ((float)sample - (float)value) * filter_constant + value)
  161. /* 后向差分离散化 */
  162. #define do_lpf(value, sample, filter_constant) ((sample * filter_constant + value)/(1.0f + filter_constant))
  163. #endif /* _Fast_Math_H__ */