lundi 30 mai 2016

Calculate Factor unknown

I'm currently trying to add some JUnit teststo my pathdrawing system in order to check if the calculations are right...
Currently I have:

Class to Test(MapRouteDrawer.java):

protected static double[] getCoords(PolynomialSplineFunction curve, double[] index) {
  final double detailLevel = 1.0;//Tweak this value in order to achieve good rendering results
  final double defaultCoordSize = index[index.length - 1];
  final double[] coords = new double[(int)Math.round(detailLevel * defaultCoordSize) + 1];
  final double stepSize = curve.getKnots()[curve.getKnots().length - 1] / coords.length;
  double curValue = 0;
  for (int i = 0; i < coords.length; i ++) {
    coords[i] = curve.value(curValue);//gets y value of specified x value
    curValue += stepSize;
  }
  return coords;
}

protected static double[] getIndex(Point[] points) {
  final double[] index = new double[points.length];
  if(index.length > 0){
    index[0] = 0;
  }
  for (int i = 1; i < points.length; i++) {
    index[i] = index[i - 1] + Math.sqrt(points[i - 1].distance(points[i]));
  }
  return index;
}

TestClass:

private Point[] dummyPoints = new Point[]{new Point(0,0), new Point(100,0), new Point(0,100)};//Some Points for Testing
//This returns a so called index - the squareroot distance between addedn on top of each other
private double[] dummyIndex = MapRouteDrawer.getIndex(dummyPoints);

@Test
public void testCurve(){
  final double[] testYValues = new double[]{20, 40, 90};
  final PolynomialSplineFunction testFunction = new SplineInterpolator().interpolate(dummyIndex, testYValues);//get a Spline-Interpolated curve
  final double[] coords = MapRouteDrawer.getCoords(testFunction, dummyIndex);//Calls the function to test
  final double factor = testFunction.getKnots()[testFunction.getKnots().length - 1] / coords.length;
  assertEquals(testYValues[0] * factor, coords[(int)Math.round(dummyIndex[0])], 1);//Check if the coordinates are equal
  assertEquals(testYValues[1] * factor, coords[(int)Math.round(dummyIndex[1])], 1);
  assertEquals(testYValues[2] * factor, coords[(int)Math.round(dummyIndex[2])], 1);
}

This is working fine, but if you are familiar with JUnit you may notice the delta of 1 which is neccesary in order for this Test to work...
What I'm trying to achieve is this:

@Test
public void testCurve(){
  final double[] testYValues = new double[]{20, 40, 90};
  final PolynomialSplineFunction testFunction = new SplineInterpolator().interpolate(dummyIndex, testYValues);//get a Spline-Interpolated curve
  final double[] coords = MapRouteDrawer.getCoords(testFunction, dummyIndex);//Calls the function to test
  final double factor;//Unknown... should be dynamic, so it does not matter which detail level I chose
  assertEquals(testYValues[0], coords[(int)Math.round(dummyIndex[0])] * stepSize, 0);//Check if the coordinates are equal
  assertEquals(testYValues[1], coords[(int)Math.round(dummyIndex[1])] * stepSize, 0);//e.g. coords[(int)Math.round(dummyIndex[0])] * factor == 20 etc.
  assertEquals(testYValues[2], coords[(int)Math.round(dummyIndex[2])] * stepSize, 0);
}

So that the first value in assertEquals() is 20, 40, 90 etc. and not a weird 21.39587576787686 (or similar) number and the delta is 0 (or 0.01 if there is no other way) and not 1 which I'm currently using

Because I'm using a detail level in my getCoords() method, which should be able to be adjusted without having to change the test, there is a need to make the "factor" in my test related to the coords-size.

How would I calculate the factor in order for the Test to succeed?
Any help is would be very much appreciated

Aucun commentaire:

Enregistrer un commentaire