You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

246 lines
5.0KB

  1. #pragma once
  2. #include <math.h>
  3. namespace rack {
  4. /** Limits a value between a minimum and maximum
  5. If min > max for some reason, returns min
  6. */
  7. inline float clampf(float x, float min, float max) {
  8. if (x > max)
  9. x = max;
  10. if (x < min)
  11. x = min;
  12. return x;
  13. }
  14. /** If the magnitude of x if less than eps, return 0 */
  15. inline float chopf(float x, float eps) {
  16. if (x < eps && x > -eps)
  17. return 0.0;
  18. return x;
  19. }
  20. inline float mapf(float x, float xMin, float xMax, float yMin, float yMax) {
  21. return yMin + (x - xMin) / (xMax - xMin) * (yMax - yMin);
  22. }
  23. inline float crossf(float a, float b, float frac) {
  24. return (1.0 - frac) * a + frac * b;
  25. }
  26. inline int mini(int a, int b) {
  27. return a < b ? a : b;
  28. }
  29. inline int maxi(int a, int b) {
  30. return a > b ? a : b;
  31. }
  32. inline float quadraticBipolar(float x) {
  33. float x2 = x*x;
  34. return x >= 0.0 ? x2 : -x2;
  35. }
  36. inline float cubic(float x) {
  37. // optimal with --fast-math
  38. return x*x*x;
  39. }
  40. inline float quarticBipolar(float x) {
  41. float x2 = x*x;
  42. float x4 = x2*x2;
  43. return x >= 0.0 ? x4 : -x4;
  44. }
  45. inline float quintic(float x) {
  46. // optimal with --fast-math
  47. return x*x*x*x*x;
  48. }
  49. // Euclidean modulus, always returns 0 <= mod < base for positive base
  50. // Assumes this architecture's division is non-Euclidean
  51. inline int eucMod(int a, int base) {
  52. int mod = a % base;
  53. return mod < 0 ? mod + base : mod;
  54. }
  55. inline float getf(const float *p, float v = 0.0) {
  56. return p ? *p : v;
  57. }
  58. inline void setf(float *p, float v) {
  59. if (p)
  60. *p = v;
  61. }
  62. /** Linearly interpolate an array `p` with index `x`
  63. Assumes that the array at `p` is of length at least ceil(x)+1.
  64. */
  65. inline float interpf(const float *p, float x) {
  66. int xi = x;
  67. float xf = x - xi;
  68. return crossf(p[xi], p[xi+1], xf);
  69. }
  70. ////////////////////
  71. // 2D float vector
  72. ////////////////////
  73. struct Vec {
  74. float x, y;
  75. Vec() : x(0.0), y(0.0) {}
  76. Vec(float x, float y) : x(x), y(y) {}
  77. Vec neg() {
  78. return Vec(-x, -y);
  79. }
  80. Vec plus(Vec b) {
  81. return Vec(x + b.x, y + b.y);
  82. }
  83. Vec minus(Vec b) {
  84. return Vec(x - b.x, y - b.y);
  85. }
  86. Vec mult(float s) {
  87. return Vec(x * s, y * s);
  88. }
  89. Vec div(float s) {
  90. return Vec(x / s, y / s);
  91. }
  92. float dot(Vec b) {
  93. return x * b.x + y * b.y;
  94. }
  95. float norm() {
  96. return hypotf(x, y);
  97. }
  98. Vec min(Vec b) {
  99. return Vec(fminf(x, b.x), fminf(y, b.y));
  100. }
  101. Vec max(Vec b) {
  102. return Vec(fmaxf(x, b.x), fmaxf(y, b.y));
  103. }
  104. Vec round() {
  105. return Vec(roundf(x), roundf(y));
  106. }
  107. };
  108. struct Rect {
  109. Vec pos;
  110. Vec size;
  111. Rect() {}
  112. Rect(Vec pos, Vec size) : pos(pos), size(size) {}
  113. /** Returns whether this Rect contains another Rect, inclusive on the top/left, non-inclusive on the bottom/right */
  114. bool contains(Vec v) {
  115. return pos.x <= v.x && v.x < pos.x + size.x
  116. && pos.y <= v.y && v.y < pos.y + size.y;
  117. }
  118. /** Returns whether this Rect overlaps with another Rect */
  119. bool intersects(Rect r) {
  120. return (pos.x + size.x > r.pos.x && r.pos.x + r.size.x > pos.x)
  121. && (pos.y + size.y > r.pos.y && r.pos.y + r.size.y > pos.y);
  122. }
  123. Vec getCenter() {
  124. return pos.plus(size.mult(0.5));
  125. }
  126. Vec getTopRight() {
  127. return pos.plus(Vec(size.x, 0.0));
  128. }
  129. Vec getBottomLeft() {
  130. return pos.plus(Vec(0.0, size.y));
  131. }
  132. Vec getBottomRight() {
  133. return pos.plus(size);
  134. }
  135. /** Clamps the position to fix inside a bounding box */
  136. Rect clamp(Rect bound) {
  137. Rect r;
  138. r.size = size;
  139. r.pos.x = clampf(pos.x, bound.pos.x, bound.pos.x + bound.size.x - size.x);
  140. r.pos.y = clampf(pos.y, bound.pos.y, bound.pos.y + bound.size.y - size.y);
  141. return r;
  142. }
  143. };
  144. ////////////////////
  145. // Simple FFT implementation
  146. ////////////////////
  147. // Derived from the Italian Wikipedia article for FFT
  148. // https://it.wikipedia.org/wiki/Trasformata_di_Fourier_veloce
  149. // If you need speed, use KissFFT, pffft, etc instead.
  150. inline int log2i(int n) {
  151. int i = 0;
  152. while (n >>= 1) {
  153. i++;
  154. }
  155. return i;
  156. }
  157. inline bool isPowerOf2(int n) {
  158. return n > 0 && (n & (n-1)) == 0;
  159. }
  160. /*
  161. inline int reverse(int N, int n) //calculating revers number
  162. {
  163. int j, p = 0;
  164. for(j = 1; j <= log2i(N); j++) {
  165. if(n & (1 << (log2i(N) - j)))
  166. p |= 1 << (j - 1);
  167. }
  168. return p;
  169. }
  170. inline void ordina(complex<double>* f1, int N) //using the reverse order in the array
  171. {
  172. complex<double> f2[MAX];
  173. for(int i = 0; i < N; i++)
  174. f2[i] = f1[reverse(N, i)];
  175. for(int j = 0; j < N; j++)
  176. f1[j] = f2[j];
  177. }
  178. inline void transform(complex<double>* f, int N)
  179. {
  180. ordina(f, N); //first: reverse order
  181. complex<double> *W;
  182. W = (complex<double> *)malloc(N / 2 * sizeof(complex<double>));
  183. W[1] = polar(1., -2. * M_PI / N);
  184. W[0] = 1;
  185. for(int i = 2; i < N / 2; i++)
  186. W[i] = pow(W[1], i);
  187. int n = 1;
  188. int a = N / 2;
  189. for(int j = 0; j < log2i(N); j++) {
  190. for(int i = 0; i < N; i++) {
  191. if(!(i & n)) {
  192. complex<double> temp = f[i];
  193. complex<double> Temp = W[(i * a) % (n * a)] * f[i + n];
  194. f[i] = temp + Temp;
  195. f[i + n] = temp - Temp;
  196. }
  197. }
  198. n *= 2;
  199. a = a / 2;
  200. }
  201. }
  202. inline void FFT(complex<double>* f, int N, double d)
  203. {
  204. transform(f, N);
  205. for(int i = 0; i < N; i++)
  206. f[i] *= d; //multiplying by step
  207. }
  208. */
  209. } // namespace rack