Browse Source

Make min, max, and clamp generics

tags/v0.6.0
Andrew Belt 6 years ago
parent
commit
6f36a77e7b
5 changed files with 28 additions and 32 deletions
  1. +22
    -26
      include/util/math.hpp
  2. +1
    -1
      src/app/SVGSwitch.cpp
  3. +2
    -2
      src/audio.cpp
  4. +1
    -1
      src/settings.cpp
  5. +2
    -2
      src/ui/TextField.cpp

+ 22
- 26
include/util/math.hpp View File

@@ -15,24 +15,42 @@ using std::isnormal;
namespace rack {

////////////////////
// basic integer functions
// generic functions
////////////////////

inline int min(int a, int b) {
/** Returns the minimum of `a` and `b` */
template <typename T>
inline T min(T a, T b) {
return (a < b) ? a : b;
}

inline int max(int a, int b) {
/** Returns the maximum of `a` and `b` */
template <typename T>
inline T max(T a, T b) {
return (a > b) ? a : b;
}

/** Limits `x` between `a` and `b`
Assumes a <= b
*/
inline int clamp(int x, int a, int b) {
template <typename T>
inline T clamp(T x, T a, T b) {
return min(max(x, a), b);
}

/** Limits `x` between `a` and `b`
If a > b, switches the two values
*/
template <typename T>
inline T clamp2(T x, T a, T b) {
return clamp(x, min(a, b), max(a, b));
}

////////////////////
// basic integer functions
////////////////////


/** Euclidean modulus, always returns 0 <= mod < base for positive base.
*/
inline int eucmod(int a, int base) {
@@ -58,14 +76,6 @@ inline bool ispow2(int n) {
// basic float functions
////////////////////

inline float min(float a, float b) {
return (a < b) ? a : b;
}

inline float max(float a, float b) {
return (a > b) ? a : b;
}

/** Returns 1.f for positive numbers and -1.f for negative numbers (including positive/negative zero) */
inline float sgn(float x) {
return copysignf(1.0f, x);
@@ -80,20 +90,6 @@ inline bool isNear(float a, float b, float epsilon = 1.0e-6f) {
return fabsf(a - b) <= epsilon;
}

/** Limits `x` between `a` and `b`
Assumes a <= b
*/
inline float clamp(float x, float a, float b) {
return min(max(x, a), b);
}

/** Limits `x` between `a` and `b`
If a > b, switches the two values
*/
inline float clamp2(float x, float a, float b) {
return clamp(x, min(a, b), max(a, b));
}

/** If the magnitude of x if less than eps, return 0 */
inline float chop(float x, float eps) {
return (-eps < x && x < eps) ? 0.0f : x;


+ 1
- 1
src/app/SVGSwitch.cpp View File

@@ -21,7 +21,7 @@ void SVGSwitch::addFrame(std::shared_ptr<SVG> svg) {
void SVGSwitch::onChange(EventChange &e) {
assert(frames.size() > 0);
float valueScaled = rescale(value, minValue, maxValue, 0, frames.size() - 1);
int index = clamp((int) roundf(valueScaled), 0, frames.size() - 1);
int index = clamp((int) roundf(valueScaled), 0, (int) frames.size() - 1);
sw->setSVG(frames[index]);
dirty = true;
ParamWidget::onChange(e);


+ 2
- 2
src/audio.cpp View File

@@ -138,11 +138,11 @@ std::string AudioIO::getDeviceDetail(int device, int offset) {
if (getDeviceInfo(device, &deviceInfo)) {
std::string deviceDetail = stringf("%s (", deviceInfo.name.c_str());
if (offset < (int) deviceInfo.inputChannels)
deviceDetail += stringf("%d-%d in", offset + 1, min(offset + maxChannels, deviceInfo.inputChannels));
deviceDetail += stringf("%d-%d in", offset + 1, min(offset + maxChannels, (int) deviceInfo.inputChannels));
if (offset < (int) deviceInfo.inputChannels && offset < (int) deviceInfo.outputChannels)
deviceDetail += ", ";
if (offset < (int) deviceInfo.outputChannels)
deviceDetail += stringf("%d-%d out", offset + 1, min(offset + maxChannels, deviceInfo.outputChannels));
deviceDetail += stringf("%d-%d out", offset + 1, min(offset + maxChannels, (int) deviceInfo.outputChannels));
deviceDetail += ")";
return deviceDetail;
}


+ 1
- 1
src/settings.cpp View File

@@ -105,7 +105,7 @@ static void settingsFromJson(json_t *rootJ) {
// zoom
json_t *zoomJ = json_object_get(rootJ, "zoom");
if (zoomJ) {
gRackScene->zoomWidget->setZoom(clamp(json_number_value(zoomJ), 0.25f, 4.0f));
gRackScene->zoomWidget->setZoom(clamp((float) json_number_value(zoomJ), 0.25f, 4.0f));
gToolbar->zoomSlider->setValue(json_number_value(zoomJ) * 100.0);
}



+ 2
- 2
src/ui/TextField.cpp View File

@@ -166,8 +166,8 @@ void TextField::onKey(EventKey &e) {
} break;
}

cursor = clamp(cursor, 0, text.size());
selection = clamp(selection, 0, text.size());
cursor = clamp(cursor, 0, (int) text.size());
selection = clamp(selection, 0, (int) text.size());
e.consumed = true;
}



Loading…
Cancel
Save