[wpiutil,wpimath] Add generic InterpolatingTreeMap (#5372)

This commit is contained in:
Ryan Blue
2023-08-04 00:46:17 -04:00
committed by GitHub
parent d83a6edc20
commit 90e37a129f
10 changed files with 330 additions and 17 deletions

View File

@@ -146,6 +146,26 @@ public final class MathUtil {
return startValue + (endValue - startValue) * MathUtil.clamp(t, 0, 1);
}
/**
* Return where within interpolation range [0, 1] q is between startValue and endValue.
*
* @param startValue Lower part of interpolation range.
* @param endValue Upper part of interpolation range.
* @param q Query.
* @return Interpolant in range [0, 1].
*/
public static double inverseInterpolate(double startValue, double endValue, double q) {
double totalRange = endValue - startValue;
if (totalRange <= 0) {
return 0.0;
}
double queryToStart = q - startValue;
if (queryToStart <= 0) {
return 0.0;
}
return queryToStart / totalRange;
}
/**
* Checks if the given value matches an expected value within a certain tolerance.
*