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.

math.hpp 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. return x*x*x;
  38. }
  39. inline float quarticBipolar(float x) {
  40. return x >= 0.0 ? x*x*x*x : -x*x*x*x;
  41. }
  42. inline float quintic(float x) {
  43. // optimal with --fast-math
  44. return x*x*x*x*x;
  45. }
  46. inline float sqrtBipolar(float x) {
  47. return x >= 0.0 ? sqrtf(x) : -sqrtf(-x);
  48. }
  49. /** This is pretty much a scaled sinh */
  50. inline float exponentialBipolar(float b, float x) {
  51. const float a = b - 1.0 / b;
  52. return (powf(b, x) - powf(b, -x)) / a;
  53. }
  54. inline float sincf(float x) {
  55. if (x == 0.0)
  56. return 1.0;
  57. x *= M_PI;
  58. return sinf(x) / x;
  59. }
  60. inline float getf(const float *p, float v = 0.0) {
  61. return p ? *p : v;
  62. }
  63. inline void setf(float *p, float v) {
  64. if (p)
  65. *p = v;
  66. }
  67. /** Linearly interpolate an array `p` with index `x`
  68. Assumes that the array at `p` is of length at least ceil(x)+1.
  69. */
  70. inline float interpf(const float *p, float x) {
  71. int xi = x;
  72. float xf = x - xi;
  73. return crossf(p[xi], p[xi+1], xf);
  74. }
  75. // Euclidean modulus, always returns 0 <= mod < base for positive base
  76. // Assumes this architecture's division is non-Euclidean
  77. inline int eucmod(int a, int base) {
  78. int mod = a % base;
  79. return mod < 0 ? mod + base : mod;
  80. }
  81. inline int log2i(int n) {
  82. int i = 0;
  83. while (n >>= 1) {
  84. i++;
  85. }
  86. return i;
  87. }
  88. inline bool ispow2(int n) {
  89. return n > 0 && (n & (n - 1)) == 0;
  90. }
  91. ////////////////////
  92. // 2D float vector
  93. ////////////////////
  94. struct Vec {
  95. float x, y;
  96. Vec() : x(0.0), y(0.0) {}
  97. Vec(float x, float y) : x(x), y(y) {}
  98. Vec neg() {
  99. return Vec(-x, -y);
  100. }
  101. Vec plus(Vec b) {
  102. return Vec(x + b.x, y + b.y);
  103. }
  104. Vec minus(Vec b) {
  105. return Vec(x - b.x, y - b.y);
  106. }
  107. Vec mult(float s) {
  108. return Vec(x * s, y * s);
  109. }
  110. Vec div(float s) {
  111. return Vec(x / s, y / s);
  112. }
  113. float dot(Vec b) {
  114. return x * b.x + y * b.y;
  115. }
  116. float norm() {
  117. return hypotf(x, y);
  118. }
  119. Vec min(Vec b) {
  120. return Vec(fminf(x, b.x), fminf(y, b.y));
  121. }
  122. Vec max(Vec b) {
  123. return Vec(fmaxf(x, b.x), fmaxf(y, b.y));
  124. }
  125. Vec round() {
  126. return Vec(roundf(x), roundf(y));
  127. }
  128. bool isFinite() {
  129. return isfinite(x) && isfinite(y);
  130. }
  131. bool isZero() {
  132. return x == 0.0 && y == 0.0;
  133. }
  134. };
  135. struct Rect {
  136. Vec pos;
  137. Vec size;
  138. Rect() {}
  139. Rect(Vec pos, Vec size) : pos(pos), size(size) {}
  140. /** Returns whether this Rect contains another Rect, inclusive on the top/left, non-inclusive on the bottom/right */
  141. bool contains(Vec v) {
  142. return pos.x <= v.x && v.x < pos.x + size.x
  143. && pos.y <= v.y && v.y < pos.y + size.y;
  144. }
  145. /** Returns whether this Rect overlaps with another Rect */
  146. bool intersects(Rect r) {
  147. return (pos.x + size.x > r.pos.x && r.pos.x + r.size.x > pos.x)
  148. && (pos.y + size.y > r.pos.y && r.pos.y + r.size.y > pos.y);
  149. }
  150. Vec getCenter() {
  151. return pos.plus(size.mult(0.5));
  152. }
  153. Vec getTopRight() {
  154. return pos.plus(Vec(size.x, 0.0));
  155. }
  156. Vec getBottomLeft() {
  157. return pos.plus(Vec(0.0, size.y));
  158. }
  159. Vec getBottomRight() {
  160. return pos.plus(size);
  161. }
  162. /** Clamps the position to fix inside a bounding box */
  163. Rect clamp(Rect bound) {
  164. Rect r;
  165. r.size = size;
  166. r.pos.x = clampf(pos.x, bound.pos.x, bound.pos.x + bound.size.x - size.x);
  167. r.pos.y = clampf(pos.y, bound.pos.y, bound.pos.y + bound.size.y - size.y);
  168. return r;
  169. }
  170. };
  171. } // namespace rack