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.

257 lines
5.3KB

  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, the limits are switched
  51. */
  52. inline float clampf(float x, float min, float max) {
  53. return fmaxf(fminf(x, fmaxf(min, max)), fminf(min, max));
  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 Rect;
  120. struct Vec {
  121. float x, y;
  122. Vec() : x(0.0), y(0.0) {}
  123. Vec(float x, float y) : x(x), y(y) {}
  124. Vec neg() {
  125. return Vec(-x, -y);
  126. }
  127. Vec plus(Vec b) {
  128. return Vec(x + b.x, y + b.y);
  129. }
  130. Vec minus(Vec b) {
  131. return Vec(x - b.x, y - b.y);
  132. }
  133. Vec mult(float s) {
  134. return Vec(x * s, y * s);
  135. }
  136. Vec mult(Vec b) {
  137. return Vec(x * b.x, y * b.y);
  138. }
  139. Vec div(float s) {
  140. return Vec(x / s, y / s);
  141. }
  142. Vec div(Vec b) {
  143. return Vec(x / b.x, y / b.y);
  144. }
  145. float dot(Vec b) {
  146. return x * b.x + y * b.y;
  147. }
  148. float norm() {
  149. return hypotf(x, y);
  150. }
  151. Vec min(Vec b) {
  152. return Vec(fminf(x, b.x), fminf(y, b.y));
  153. }
  154. Vec max(Vec b) {
  155. return Vec(fmaxf(x, b.x), fmaxf(y, b.y));
  156. }
  157. Vec round() {
  158. return Vec(roundf(x), roundf(y));
  159. }
  160. bool isZero() {
  161. return x == 0.0 && y == 0.0;
  162. }
  163. Vec clamp(Rect bound);
  164. };
  165. struct Rect {
  166. Vec pos;
  167. Vec size;
  168. Rect() {}
  169. Rect(Vec pos, Vec size) : pos(pos), size(size) {}
  170. static Rect fromMinMax(Vec min, Vec max) {
  171. return Rect(min, max.minus(min));
  172. }
  173. /** Returns whether this Rect contains an entire point, inclusive on the top/left, non-inclusive on the bottom/right */
  174. bool contains(Vec v) {
  175. return pos.x <= v.x && v.x < pos.x + size.x
  176. && pos.y <= v.y && v.y < pos.y + size.y;
  177. }
  178. /** Returns whether this Rect contains an entire Rect */
  179. bool contains(Rect r) {
  180. return pos.x <= r.pos.x && r.pos.x + r.size.x <= pos.x + size.x
  181. && pos.y <= r.pos.y && r.pos.y + r.size.y <= pos.y + size.y;
  182. }
  183. /** Returns whether this Rect overlaps with another Rect */
  184. bool intersects(Rect r) {
  185. return (pos.x + size.x > r.pos.x && r.pos.x + r.size.x > pos.x)
  186. && (pos.y + size.y > r.pos.y && r.pos.y + r.size.y > pos.y);
  187. }
  188. Vec getCenter() {
  189. return pos.plus(size.mult(0.5));
  190. }
  191. Vec getTopRight() {
  192. return pos.plus(Vec(size.x, 0.0));
  193. }
  194. Vec getBottomLeft() {
  195. return pos.plus(Vec(0.0, size.y));
  196. }
  197. Vec getBottomRight() {
  198. return pos.plus(size);
  199. }
  200. /** Clamps the position to fix inside a bounding box */
  201. Rect clamp(Rect bound) {
  202. Rect r;
  203. r.size = size;
  204. r.pos.x = clampf(pos.x, bound.pos.x, bound.pos.x + bound.size.x - size.x);
  205. r.pos.y = clampf(pos.y, bound.pos.y, bound.pos.y + bound.size.y - size.y);
  206. return r;
  207. }
  208. };
  209. inline Vec Vec::clamp(Rect bound) {
  210. return Vec(
  211. clampf(x, bound.pos.x, bound.pos.x + bound.size.x),
  212. clampf(y, bound.pos.y, bound.pos.y + bound.size.y));
  213. }
  214. } // namespace rack