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.

236 lines
4.8KB

  1. #pragma once
  2. #include <stdint.h>
  3. #include <stdlib.h>
  4. #include <math.h>
  5. namespace rack {
  6. ////////////////////
  7. // integer functions
  8. ////////////////////
  9. inline int mini(int a, int b) {
  10. return a < b ? a : b;
  11. }
  12. inline int maxi(int a, int b) {
  13. return a > b ? a : b;
  14. }
  15. /** Limits a value between a minimum and maximum */
  16. inline int clampi(int x, int min, int max) {
  17. return x > max ? max : x < min ? min : x;
  18. }
  19. inline int absi(int a) {
  20. return a >= 0 ? a : -a;
  21. }
  22. // Euclidean modulus, always returns 0 <= mod < base for positive base
  23. // Assumes this architecture's division is non-Euclidean
  24. inline int eucmodi(int a, int base) {
  25. int mod = a % base;
  26. return mod < 0 ? mod + base : mod;
  27. }
  28. inline int log2i(int n) {
  29. int i = 0;
  30. while (n >>= 1) {
  31. i++;
  32. }
  33. return i;
  34. }
  35. inline bool ispow2i(int n) {
  36. return n > 0 && (n & (n - 1)) == 0;
  37. }
  38. ////////////////////
  39. // float functions
  40. ////////////////////
  41. /** Returns 1.0 for positive numbers and -1.0 for negative numbers (including positive/negative zero) */
  42. inline float sgnf(float x) {
  43. return copysignf(1.0, x);
  44. }
  45. inline float eucmodf(float a, float base) {
  46. float mod = fmodf(a, base);
  47. return mod < 0.0 ? mod + base : mod;
  48. }
  49. /** Limits a value between a minimum and maximum
  50. If min < max, returns max
  51. */
  52. inline float clampf(float x, float min, float max) {
  53. return fmaxf(fminf(x, max), min);
  54. }
  55. /** If the magnitude of x if less than eps, return 0 */
  56. inline float chopf(float x, float eps) {
  57. return -eps < x && x < eps ? 0.0 : x;
  58. }
  59. inline float rescalef(float x, float xMin, float xMax, float yMin, float yMax) {
  60. return yMin + (x - xMin) / (xMax - xMin) * (yMax - yMin);
  61. }
  62. inline float crossf(float a, float b, float frac) {
  63. return a + frac * (b - a);
  64. }
  65. inline float quadraticBipolar(float x) {
  66. float x2 = x*x;
  67. return x >= 0.0 ? x2 : -x2;
  68. }
  69. inline float cubic(float x) {
  70. return x*x*x;
  71. }
  72. inline float quarticBipolar(float x) {
  73. return x >= 0.0 ? x*x*x*x : -x*x*x*x;
  74. }
  75. inline float quintic(float x) {
  76. // optimal with --fast-math
  77. return x*x*x*x*x;
  78. }
  79. inline float sqrtBipolar(float x) {
  80. return x >= 0.0 ? sqrtf(x) : -sqrtf(-x);
  81. }
  82. /** This is pretty much a scaled sinh */
  83. inline float exponentialBipolar(float b, float x) {
  84. const float a = b - 1.0 / b;
  85. return (powf(b, x) - powf(b, -x)) / a;
  86. }
  87. inline float sincf(float x) {
  88. if (x == 0.0)
  89. return 1.0;
  90. x *= M_PI;
  91. return sinf(x) / x;
  92. }
  93. inline float getf(const float *p, float v = 0.0) {
  94. return p ? *p : v;
  95. }
  96. inline void setf(float *p, float v) {
  97. if (p)
  98. *p = v;
  99. }
  100. /** Linearly interpolate an array `p` with index `x`
  101. Assumes that the array at `p` is of length at least floor(x)+1.
  102. */
  103. inline float interpf(const float *p, float x) {
  104. int xi = x;
  105. float xf = x - xi;
  106. return crossf(p[xi], p[xi+1], xf);
  107. }
  108. /** Complex multiply c = a * b
  109. It is of course acceptable to reuse arguments
  110. i.e. cmultf(&ar, &ai, ar, ai, br, bi)
  111. */
  112. inline void cmultf(float *cr, float *ci, float ar, float ai, float br, float bi) {
  113. *cr = ar * br - ai * bi;
  114. *ci = ar * bi + ai * br;
  115. }
  116. ////////////////////
  117. // 2D float vector
  118. ////////////////////
  119. struct Vec {
  120. float x, y;
  121. Vec() : x(0.0), y(0.0) {}
  122. Vec(float x, float y) : x(x), y(y) {}
  123. Vec neg() {
  124. return Vec(-x, -y);
  125. }
  126. Vec plus(Vec b) {
  127. return Vec(x + b.x, y + b.y);
  128. }
  129. Vec minus(Vec b) {
  130. return Vec(x - b.x, y - b.y);
  131. }
  132. Vec mult(float s) {
  133. return Vec(x * s, y * s);
  134. }
  135. Vec div(float s) {
  136. return Vec(x / s, y / s);
  137. }
  138. float dot(Vec b) {
  139. return x * b.x + y * b.y;
  140. }
  141. float norm() {
  142. return hypotf(x, y);
  143. }
  144. Vec min(Vec b) {
  145. return Vec(fminf(x, b.x), fminf(y, b.y));
  146. }
  147. Vec max(Vec b) {
  148. return Vec(fmaxf(x, b.x), fmaxf(y, b.y));
  149. }
  150. Vec round() {
  151. return Vec(roundf(x), roundf(y));
  152. }
  153. bool isZero() {
  154. return x == 0.0 && y == 0.0;
  155. }
  156. };
  157. struct Rect {
  158. Vec pos;
  159. Vec size;
  160. Rect() {}
  161. Rect(Vec pos, Vec size) : pos(pos), size(size) {}
  162. static Rect fromMinMax(Vec min, Vec max) {
  163. return Rect(min, max.minus(min));
  164. }
  165. /** Returns whether this Rect contains another Rect, inclusive on the top/left, non-inclusive on the bottom/right */
  166. bool contains(Vec v) {
  167. return pos.x <= v.x && v.x < pos.x + size.x
  168. && pos.y <= v.y && v.y < pos.y + size.y;
  169. }
  170. /** Returns whether this Rect overlaps with another Rect */
  171. bool intersects(Rect r) {
  172. return (pos.x + size.x > r.pos.x && r.pos.x + r.size.x > pos.x)
  173. && (pos.y + size.y > r.pos.y && r.pos.y + r.size.y > pos.y);
  174. }
  175. Vec getCenter() {
  176. return pos.plus(size.mult(0.5));
  177. }
  178. Vec getTopRight() {
  179. return pos.plus(Vec(size.x, 0.0));
  180. }
  181. Vec getBottomLeft() {
  182. return pos.plus(Vec(0.0, size.y));
  183. }
  184. Vec getBottomRight() {
  185. return pos.plus(size);
  186. }
  187. /** Clamps the position to fix inside a bounding box */
  188. Rect clamp(Rect bound) {
  189. Rect r;
  190. r.size = size;
  191. r.pos.x = clampf(pos.x, bound.pos.x, bound.pos.x + bound.size.x - size.x);
  192. r.pos.y = clampf(pos.y, bound.pos.y, bound.pos.y + bound.size.y - size.y);
  193. return r;
  194. }
  195. };
  196. } // namespace rack