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.

224 lines
4.2KB

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