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.

196 lines
3.9KB

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