mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-23 01:21:42 +00:00
[wpiutil] Remove old InterpolatingTreeMap location (#6560)
This commit is contained in:
@@ -1,104 +0,0 @@
|
||||
// Copyright (c) FIRST and other WPILib contributors.
|
||||
// Open Source Software; you can modify and/or share it under the terms of
|
||||
// the WPILib BSD license file in the root directory of this project.
|
||||
|
||||
package edu.wpi.first.util;
|
||||
|
||||
import java.util.TreeMap;
|
||||
|
||||
/**
|
||||
* Interpolating Tree Maps are used to get values at points that are not defined by making a guess
|
||||
* from points that are defined. This uses linear interpolation.
|
||||
*
|
||||
* @param <K> Key type.
|
||||
* @param <V> Value type.
|
||||
* @deprecated Use {@link edu.wpi.first.math.interpolation.InterpolatingDoubleTreeMap} instead
|
||||
*/
|
||||
@Deprecated(forRemoval = true, since = "2024")
|
||||
public class InterpolatingTreeMap<K extends Number, V extends Number> {
|
||||
private final TreeMap<K, V> m_map = new TreeMap<>();
|
||||
|
||||
/** Default constructor. */
|
||||
public InterpolatingTreeMap() {}
|
||||
|
||||
/**
|
||||
* Inserts a key-value pair.
|
||||
*
|
||||
* @param key The key.
|
||||
* @param value The value.
|
||||
*/
|
||||
public void put(K key, V value) {
|
||||
m_map.put(key, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the value associated with a given key.
|
||||
*
|
||||
* <p>If there's no matching key, the value returned will be a linear interpolation between the
|
||||
* keys before and after the provided one.
|
||||
*
|
||||
* @param key The key.
|
||||
* @return The value associated with the given key.
|
||||
*/
|
||||
public Double get(K key) {
|
||||
V val = m_map.get(key);
|
||||
if (val == null) {
|
||||
K ceilingKey = m_map.ceilingKey(key);
|
||||
K floorKey = m_map.floorKey(key);
|
||||
|
||||
if (ceilingKey == null && floorKey == null) {
|
||||
return null;
|
||||
}
|
||||
if (ceilingKey == null) {
|
||||
return m_map.get(floorKey).doubleValue();
|
||||
}
|
||||
if (floorKey == null) {
|
||||
return m_map.get(ceilingKey).doubleValue();
|
||||
}
|
||||
V floor = m_map.get(floorKey);
|
||||
V ceiling = m_map.get(ceilingKey);
|
||||
|
||||
return interpolate(floor, ceiling, inverseInterpolate(ceilingKey, key, floorKey));
|
||||
} else {
|
||||
return val.doubleValue();
|
||||
}
|
||||
}
|
||||
|
||||
/** Clears the contents. */
|
||||
public void clear() {
|
||||
m_map.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the value interpolated between val1 and val2 by the interpolant d.
|
||||
*
|
||||
* @param val1 The lower part of the interpolation range.
|
||||
* @param val2 The upper part of the interpolation range.
|
||||
* @param d The interpolant in the range [0, 1].
|
||||
* @return The interpolated value.
|
||||
*/
|
||||
private double interpolate(V val1, V val2, double d) {
|
||||
double dydx = val2.doubleValue() - val1.doubleValue();
|
||||
return dydx * d + val1.doubleValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return where within interpolation range [0, 1] q is between down and up.
|
||||
*
|
||||
* @param up Upper part of interpolation range.
|
||||
* @param q Query.
|
||||
* @param down Lower part of interpolation range.
|
||||
* @return Interpolant in range [0, 1].
|
||||
*/
|
||||
private double inverseInterpolate(K up, K q, K down) {
|
||||
double upperToLower = up.doubleValue() - down.doubleValue();
|
||||
if (upperToLower <= 0) {
|
||||
return 0.0;
|
||||
}
|
||||
double queryToLower = q.doubleValue() - down.doubleValue();
|
||||
if (queryToLower <= 0) {
|
||||
return 0.0;
|
||||
}
|
||||
return queryToLower / upperToLower;
|
||||
}
|
||||
}
|
||||
@@ -1,135 +0,0 @@
|
||||
// Copyright (c) FIRST and other WPILib contributors.
|
||||
// Open Source Software; you can modify and/or share it under the terms of
|
||||
// the WPILib BSD license file in the root directory of this project.
|
||||
|
||||
package edu.wpi.first.util;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@SuppressWarnings("removal")
|
||||
class InterpolatingTreeMapTest {
|
||||
@Test
|
||||
void testInterpolationDouble() {
|
||||
InterpolatingTreeMap<Double, Double> table = new InterpolatingTreeMap<>();
|
||||
|
||||
table.put(125.0, 450.0);
|
||||
table.put(200.0, 510.0);
|
||||
table.put(268.0, 525.0);
|
||||
table.put(312.0, 550.0);
|
||||
table.put(326.0, 650.0);
|
||||
|
||||
// Key below minimum gives the smallest value
|
||||
assertEquals(450.0, table.get(100.0));
|
||||
|
||||
// Minimum key gives exact value
|
||||
assertEquals(450.0, table.get(125.0));
|
||||
|
||||
// Key gives interpolated value
|
||||
assertEquals(480.0, table.get(162.5));
|
||||
|
||||
// Key at right of interpolation range gives exact value
|
||||
assertEquals(510.0, table.get(200.0));
|
||||
|
||||
// Maximum key gives exact value
|
||||
assertEquals(650.0, table.get(326.0));
|
||||
|
||||
// Key above maximum gives largest value
|
||||
assertEquals(650.0, table.get(400.0));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testInterpolationClear() {
|
||||
InterpolatingTreeMap<Double, Double> table = new InterpolatingTreeMap<>();
|
||||
|
||||
table.put(125.0, 450.0);
|
||||
table.put(200.0, 510.0);
|
||||
table.put(268.0, 525.0);
|
||||
table.put(312.0, 550.0);
|
||||
table.put(326.0, 650.0);
|
||||
|
||||
// Key below minimum gives the smallest value
|
||||
assertEquals(450.0, table.get(100.0));
|
||||
|
||||
// Minimum key gives exact value
|
||||
assertEquals(450.0, table.get(125.0));
|
||||
|
||||
// Key gives interpolated value
|
||||
assertEquals(480.0, table.get(162.5));
|
||||
|
||||
// Key at right of interpolation range gives exact value
|
||||
assertEquals(510.0, table.get(200.0));
|
||||
|
||||
// Maximum key gives exact value
|
||||
assertEquals(650.0, table.get(326.0));
|
||||
|
||||
// Key above maximum gives largest value
|
||||
assertEquals(650.0, table.get(400.0));
|
||||
|
||||
table.clear();
|
||||
|
||||
table.put(100.0, 250.0);
|
||||
table.put(200.0, 500.0);
|
||||
|
||||
assertEquals(375.0, table.get(150.0));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testInterpolationInteger() {
|
||||
InterpolatingTreeMap<Integer, Integer> table = new InterpolatingTreeMap<>();
|
||||
|
||||
table.put(125, 450);
|
||||
table.put(200, 510);
|
||||
table.put(268, 525);
|
||||
table.put(312, 550);
|
||||
table.put(326, 650);
|
||||
|
||||
// Key below minimum gives the smallest value
|
||||
assertEquals(450.0, table.get(100));
|
||||
|
||||
// Minimum key gives exact value
|
||||
assertEquals(450.0, table.get(125));
|
||||
|
||||
// Key gives interpolated value
|
||||
assertEquals(479.6, table.get(162));
|
||||
|
||||
// Key at right of interpolation range gives exact value
|
||||
assertEquals(510.0, table.get(200));
|
||||
|
||||
// Maximum key gives exact value
|
||||
assertEquals(650.0, table.get(326));
|
||||
|
||||
// Key above maximum gives largest value
|
||||
assertEquals(650.0, table.get(400));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testInterpolationLong() {
|
||||
InterpolatingTreeMap<Long, Long> table = new InterpolatingTreeMap<>();
|
||||
|
||||
table.put(125L, 450L);
|
||||
table.put(200L, 510L);
|
||||
table.put(268L, 525L);
|
||||
table.put(312L, 550L);
|
||||
table.put(326L, 650L);
|
||||
|
||||
// Key below minimum gives the smallest value
|
||||
assertEquals(450.0, table.get(100L));
|
||||
|
||||
// Minimum key gives exact value
|
||||
assertEquals(450.0, table.get(125L));
|
||||
|
||||
// Key gives interpolated value
|
||||
assertEquals(479.6, table.get(162L));
|
||||
|
||||
// Key at right of interpolation range gives exact value
|
||||
assertEquals(510.0, table.get(200L));
|
||||
|
||||
// Maximum key gives exact value
|
||||
assertEquals(650.0, table.get(326L));
|
||||
|
||||
// Key above maximum gives largest value
|
||||
assertEquals(650.0, table.get(400L));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user