First Commit

This commit is contained in:
Marius Metzger
2015-01-03 23:25:21 +01:00
commit cbd96d57f4
64 changed files with 23728 additions and 0 deletions

View File

@@ -0,0 +1,71 @@
package eu.crushedpixel.replaymod.interpolation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.List;
public abstract class BasicSpline {
public void calcNaturalCubic(List valueCollection, Field val, Collection<Cubic> cubicCollection) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
int num = valueCollection.size()-1;
double[] gamma = new double[num+1];
double[] delta = new double[num+1];
double[] D = new double[num+1];
int i;
/*
We solve the equation
[2 1 ] [D[0]] [3(x[1] - x[0]) ]
|1 4 1 | |D[1]| |3(x[2] - x[0]) |
| 1 4 1 | | . | = | . |
| ..... | | . | | . |
| 1 4 1| | . | |3(x[n] - x[n-2])|
[ 1 2] [D[n]] [3(x[n] - x[n-1])]
by using row operations to convert the matrix to upper triangular
and then back sustitution. The D[i] are the derivatives at the knots.
*/
gamma[0] = 1.0f / 2.0f;
for(i=1; i< num; i++) {
gamma[i] = 1.0f/(4.0f - gamma[i-1]);
}
gamma[num] = 1.0f/(2.0f - gamma[num-1]);
Double p0 = val.getDouble(valueCollection.get(0));
Double p1 = val.getDouble(valueCollection.get(1));
delta[0] = 3.0f * (p1 - p0) * gamma[0];
for(i=1; i< num; i++) {
p0 = val.getDouble(valueCollection.get(i-1));
p1 = val.getDouble(valueCollection.get(i+1));
delta[i] = (3.0f * (p1 - p0) - delta[i - 1]) * gamma[i];
}
p0 = val.getDouble(valueCollection.get(num-1));
p1 = val.getDouble(valueCollection.get(num));
delta[num] = (3.0f * (p1 - p0) - delta[num - 1]) * gamma[num];
D[num] = delta[num];
for(i=num-1; i >= 0; i--) {
D[i] = delta[i] - gamma[i] * D[i+1];
}
//now compute the coefficients of the cubics
cubicCollection.clear();
for(i=0; i<num; i++) {
p0 = val.getDouble(valueCollection.get(i));
p1 = val.getDouble(valueCollection.get(i+1));
cubicCollection.add(new Cubic(
p0,
D[i],
3*(p1 - p0) - 2*D[i] - D[i+1],
2*(p0 - p1) + D[i] + D[i+1]
)
);
}
}
}

View File

@@ -0,0 +1,16 @@
package eu.crushedpixel.replaymod.interpolation;
public class Cubic {
private double a,b,c,d;
public Cubic(double p0, double d2, double e, double f) {
this.a =p0;
this.b =d2;
this.c =e;
this.d =f;
}
public double eval(double u) {
return (((d*u) + c)*u + b)*u + a;
}
}

View File

@@ -0,0 +1,42 @@
package eu.crushedpixel.replaymod.interpolation;
import java.util.ArrayList;
import java.util.List;
import akka.japi.Pair;
public abstract class LinearInterpolation<K> {
protected List<K> points = new ArrayList<K>();
public abstract K getPoint(float position);
public void addPoint(K point) {
points.add(point);
}
public void clearPoints() {
points = new ArrayList<K>();
}
protected Pair<Float, Pair<K, K>> getCurrentPoints(float position) {
if(points.size() == 0) return null;
float pos = position * (points.size()-1);
int cubicNum = Math.round(pos);
float cubicPos = (pos - cubicNum);
if(cubicNum == points.size()-1) {
cubicNum--;
cubicPos++;
}
if(cubicNum < 0) {
return new Pair<Float, Pair<K,K>>(cubicPos, new Pair<K,K>(points.get(cubicNum+1), points.get(cubicNum+1)));
}
return new Pair<Float, Pair<K,K>>(cubicPos, new Pair<K,K>(points.get(cubicNum), points.get(cubicNum+1)));
}
protected double getInterpolatedValue(double val1, double val2, float perc) {
return val1+((val2-val1)*perc);
}
}

View File

@@ -0,0 +1,29 @@
package eu.crushedpixel.replaymod.interpolation;
import akka.japi.Pair;
import eu.crushedpixel.replaymod.holders.Position;
public class LinearPoint extends LinearInterpolation<Position> {
@Override
public Position getPoint(float position) {
Pair<Float, Pair<Position, Position>> pair = getCurrentPoints(position);
if(pair == null) return null;
float perc = pair.first();
Position first = pair.second().first();
Position second = pair.second().second();
double x = getInterpolatedValue(first.getX(), second.getX(), perc);
double y = getInterpolatedValue(first.getY(), second.getY(), perc);
double z = getInterpolatedValue(first.getZ(), second.getZ(), perc);
float pitch = (float)getInterpolatedValue(first.getPitch(), second.getPitch(), perc);
float yaw = (float)getInterpolatedValue(first.getYaw(), second.getYaw(), perc);
Position inter = new Position(x, y, z, pitch, yaw);
return inter;
}
}

View File

@@ -0,0 +1,22 @@
package eu.crushedpixel.replaymod.interpolation;
import akka.japi.Pair;
import eu.crushedpixel.replaymod.holders.Position;
public class LinearTimestamp extends LinearInterpolation<Integer> {
@Override
public Integer getPoint(float position) {
Pair<Float, Pair<Integer, Integer>> pair = getCurrentPoints(position);
if(pair == null) return null;
float perc = pair.first();
int first = pair.second().first();
int second = pair.second().second();
int val = (int)getInterpolatedValue(first, second, perc);
return val;
}
}

View File

@@ -0,0 +1,92 @@
package eu.crushedpixel.replaymod.interpolation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Vector;
import com.sun.javafx.geom.Vec3d;
import eu.crushedpixel.replaymod.holders.Position;
public class SplinePoint extends BasicSpline{
private Vector<Position> points;
private Vector<Cubic> xCubics;
private Vector<Cubic> yCubics;
private Vector<Cubic> zCubics;
private Vector<Cubic> pitchCubics;
private Vector<Cubic> yawCubics;
private Field vectorX;
private Field vectorY;
private Field vectorZ;
private Field vectorPitch;
private Field vectorYaw;
private static final Object[] EMPTYOBJ = new Object[] { };
public SplinePoint() {
this.points = new Vector<Position>();
this.xCubics = new Vector<Cubic>();
this.yCubics = new Vector<Cubic>();
this.zCubics = new Vector<Cubic>();
this.pitchCubics = new Vector<Cubic>();
this.yawCubics = new Vector<Cubic>();
try {
vectorX = Position.class.getDeclaredField("x");
vectorY = Position.class.getDeclaredField("y");
vectorZ = Position.class.getDeclaredField("z");
vectorPitch = Position.class.getDeclaredField("pitch");
vectorYaw = Position.class.getDeclaredField("yaw");
vectorX.setAccessible(true);
vectorY.setAccessible(true);
vectorZ.setAccessible(true);
vectorPitch.setAccessible(true);
vectorYaw.setAccessible(true);
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
}
public void addPoint(Position point) {
this.points.add(point);
}
public Vector<Position> getPoints() {
return points;
}
public void calcSpline() {
try {
calcNaturalCubic(points, vectorX, xCubics);
calcNaturalCubic(points, vectorY, yCubics);
calcNaturalCubic(points, vectorZ, zCubics);
calcNaturalCubic(points, vectorPitch, pitchCubics);
calcNaturalCubic(points, vectorYaw, yawCubics);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
public Position getPoint(float position) {
position = position * xCubics.size();
int cubicNum = (int)Math.min(xCubics.size()-1, position);
float cubicPos = (position - cubicNum);
return new Position(xCubics.get(cubicNum).eval(cubicPos),
yCubics.get(cubicNum).eval(cubicPos),
zCubics.get(cubicNum).eval(cubicPos),
(float)pitchCubics.get(cubicNum).eval(cubicPos),
(float)yawCubics.get(cubicNum).eval(cubicPos));
}
}