Had to reimplement getValue to not use TreeMap/SortedMap apis unavailable in Froyo. Added some comments so it should be easier to read. Test cases continue to pass.
This commit is contained in:
parent
51f98b71aa
commit
52f0e0cf90
@ -1,7 +1,7 @@
|
|||||||
package net.sf.openrocket.util;
|
package net.sf.openrocket.util;
|
||||||
|
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.Map;
|
import java.util.SortedMap;
|
||||||
import java.util.TreeMap;
|
import java.util.TreeMap;
|
||||||
|
|
||||||
public class LinearInterpolator implements Cloneable {
|
public class LinearInterpolator implements Cloneable {
|
||||||
@ -60,32 +60,57 @@ public class LinearInterpolator implements Cloneable {
|
|||||||
|
|
||||||
|
|
||||||
public double getValue(double x) {
|
public double getValue(double x) {
|
||||||
Map.Entry<Double,Double> e1, e2;
|
|
||||||
double x1, x2;
|
double x1, x2;
|
||||||
double y1, y2;
|
Double y1, y2;
|
||||||
|
// Froyo does not support floorEntry, firstEntry or higherEntry. We instead have to
|
||||||
|
// resort to using other more awkward methods.
|
||||||
|
|
||||||
e1 = sortMap.floorEntry(x);
|
y1 = sortMap.get(x);
|
||||||
|
|
||||||
if (e1 == null) {
|
if ( y1 != null ) {
|
||||||
// x smaller than any value in the set
|
// Wow, x was a key in the map. Such luck.
|
||||||
e1 = sortMap.firstEntry();
|
return y1.doubleValue();
|
||||||
if (e1 == null) {
|
}
|
||||||
|
|
||||||
|
// we now know that x is not in the map, so we need to find the lower and higher keys.
|
||||||
|
|
||||||
|
// let's just make certain that our map is not empty.
|
||||||
|
if ( sortMap.isEmpty() ) {
|
||||||
throw new IllegalStateException("No points added yet to the interpolator.");
|
throw new IllegalStateException("No points added yet to the interpolator.");
|
||||||
}
|
}
|
||||||
return e1.getValue();
|
|
||||||
|
// firstKey in the map - cannot be null since the map is not empty.
|
||||||
|
Double firstKey = sortMap.firstKey();
|
||||||
|
|
||||||
|
// x is smaller than the first entry in the map.
|
||||||
|
if ( x < firstKey.doubleValue() ) {
|
||||||
|
y1 = sortMap.get(firstKey);
|
||||||
|
return y1.doubleValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
x1 = e1.getKey();
|
// floor key is the largest key smaller than x - since we have at least one key,
|
||||||
e2 = sortMap.higherEntry(x1);
|
// and x>=firstKey, we know that floorKey != null.
|
||||||
|
Double floorKey = sortMap.subMap(firstKey, x).lastKey();
|
||||||
|
|
||||||
if (e2 == null) {
|
x1 = floorKey.doubleValue();
|
||||||
// x larger than any value in the set
|
y1 = sortMap.get(floorKey);
|
||||||
return e1.getValue();
|
|
||||||
|
// Now we need to find the key that is greater or equal to x
|
||||||
|
SortedMap<Double,Double> tailMap = sortMap.tailMap(x);
|
||||||
|
|
||||||
|
// Check if x is bigger than all the entries.
|
||||||
|
if ( tailMap.isEmpty() ) {
|
||||||
|
return y1.doubleValue();
|
||||||
|
}
|
||||||
|
Double ceilKey = tailMap.firstKey();
|
||||||
|
|
||||||
|
// Check if x is bigger than all the entries.
|
||||||
|
if ( ceilKey == null ) {
|
||||||
|
return y1.doubleValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
x2 = e2.getKey();
|
x2 = ceilKey.doubleValue();
|
||||||
y1 = e1.getValue();
|
y2 = sortMap.get(ceilKey);
|
||||||
y2 = e2.getValue();
|
|
||||||
|
|
||||||
return (x - x1)/(x2-x1) * (y2-y1) + y1;
|
return (x - x1)/(x2-x1) * (y2-y1) + y1;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user