5package org.wpilib.math.util;
7import org.wpilib.math.geometry.Translation2d;
8import org.wpilib.math.geometry.Translation3d;
9import org.wpilib.math.linalg.Vector;
12public final class MathUtil {
14 throw new AssertionError(
"utility class");
26 public static double lerp(
double a,
double b,
double t) {
27 return a + (b - a) * t;
39 public static double inverseLerp(
double a,
double b,
double q) {
43 if (Math.abs(a - b) < 1e-9) {
46 return (q - a) / (b - a);
59 public static double applyDeadband(
double value,
double deadband,
double maxMagnitude) {
60 if (Math.abs(value) < deadband) {
83 return (1 + deadband / (maxMagnitude - deadband)) * (value - deadband);
104 return (1 + deadband / (maxMagnitude - deadband)) * (value + deadband);
132 Vector<R> value,
double deadband,
double maxMagnitude) {
133 if (value.
norm() < 1e-9) {
134 return value.times(0);
170 public static double copyDirectionPow(
double value,
double exponent,
double maxMagnitude) {
171 return Math.copySign(Math.pow(Math.abs(value) / maxMagnitude, exponent), value) * maxMagnitude;
205 Vector<R> value,
double exponent,
double maxMagnitude) {
206 if (value.
norm() < 1e-9) {
207 return value.times(0);
233 public static double inputModulus(
double input,
double minimumInput,
double maximumInput) {
234 double modulus = maximumInput - minimumInput;
237 int numMax = (int) ((input - minimumInput) / modulus);
238 input -= numMax * modulus;
241 int numMin = (int) ((input - maximumInput) / modulus);
242 input -= numMin * modulus;
265 public static boolean isNear(
double expected,
double actual,
double tolerance) {
267 throw new IllegalArgumentException(
"Tolerance must be a non-negative number!");
269 return Math.abs(expected - actual) < tolerance;
290 double expected,
double actual,
double tolerance,
double min,
double max) {
292 throw new IllegalArgumentException(
"Tolerance must be a non-negative number!");
295 double errorBound = (max - min) / 2.0;
296 double error = MathUtil.inputModulus(expected - actual, -errorBound, errorBound);
297 return Math.abs(error) < tolerance;
311 if (maxVelocity < 0) {
312 Exception e =
new IllegalArgumentException();
314 "maxVelocity must be a non-negative number, got " + maxVelocity, e.getStackTrace());
322 if (dist > maxVelocity * dt) {
324 return current.
plus(diff.
times(maxVelocity * dt / dist));
340 if (maxVelocity < 0) {
341 Exception e =
new IllegalArgumentException();
343 "maxVelocity must be a non-negative number, got " + maxVelocity, e.getStackTrace());
351 if (dist > maxVelocity * dt) {
353 return current.
plus(diff.
times(maxVelocity * dt / dist));
Translation2d minus(Translation2d other)
Translation2d plus(Translation2d other)
Translation2d times(double scalar)
Translation3d plus(Translation3d other)
Translation3d times(double scalar)
Translation3d minus(Translation3d other)
static void reportError(String error, StackTraceElement[] stackTrace)
static Translation3d slewRateLimit(Translation3d current, Translation3d next, double dt, double maxVelocity)
static double copyDirectionPow(double value, double exponent)
static boolean isNear(double expected, double actual, double tolerance, double min, double max)
static double angleModulus(double angleRadians)
static double inverseLerp(double a, double b, double q)
static double lerp(double a, double b, double t)
static< R extends Num > Vector< R > copyDirectionPow(Vector< R > value, double exponent, double maxMagnitude)
static< R extends Num > Vector< R > applyDeadband(Vector< R > value, double deadband)
static< R extends Num > Vector< R > applyDeadband(Vector< R > value, double deadband, double maxMagnitude)
static Translation2d slewRateLimit(Translation2d current, Translation2d next, double dt, double maxVelocity)
static boolean isNear(double expected, double actual, double tolerance)
static double applyDeadband(double value, double deadband)
static double copyDirectionPow(double value, double exponent, double maxMagnitude)
static< R extends Num > Vector< R > copyDirectionPow(Vector< R > value, double exponent)
static double applyDeadband(double value, double deadband, double maxMagnitude)
static double inputModulus(double input, double minimumInput, double maximumInput)