CatmullRomInterpolator implements Interpolator<CatmullRomInterpolator.Params> { @Override public List<XYCoords> interpolate(XYSeries series, Params params) { if (params.getPointPerSegment() < 2) { throw new IllegalArgumentException( "pointsPerSegment must be greater than 2, since 2 points is just the linear segment."); } if (series.size() < 3) { throw new IllegalArgumentException("Cannot interpolate a series with fewer than 3 vertices."); } double dx = series.getX(1).doubleValue() - series.getX(0).doubleValue(); double dy = series.getY(1).doubleValue() - series.getY(0).doubleValue(); double x1 = series.getX(0).doubleValue() - dx; double y1 = series.getY(0).doubleValue() - dy; XYCoords start = new XYCoords(x1, y1); int n = series.size() -1; dx = series.getX(n).doubleValue() - series.getX(n-1).doubleValue(); dy = series.getY(n).doubleValue() - series.getY(n - 1).doubleValue(); double xn = series.getX(n).doubleValue() + dx; double yn = series.getY(n).doubleValue() + dy; XYCoords end = new XYCoords(xn, yn); ExtrapolatedXYSeries extrapolatedXYSeries = new ExtrapolatedXYSeries(series, start, end); List<XYCoords> result = new ArrayList<>(); for (int i = 0; i < extrapolatedXYSeries.size() - 3; i++) { List<XYCoords> points = interpolate(extrapolatedXYSeries, i, params); if (result.size() > 0) { points.remove(0); } result.addAll(points); } return result; }  @Override List<XYCoords> interpolate(XYSeries series, Params params);  }
@Test(expected = IllegalArgumentException.class) public void interpolate_invalidPointsPerSegment_throwsIllegalArgumentException() { final XYSeries series = mock(XYSeries.class); final CatmullRomInterpolator.Params params = new CatmullRomInterpolator.Params(1, CatmullRomInterpolator.Type.Centripetal); new CatmullRomInterpolator().interpolate(series, params); }
@Test(expected = IllegalArgumentException.class) public void interpolate_twoElementSeries_throwsIllegalArgumentException() { final XYSeries series = new SimpleXYSeries(SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, "test", 1, 2); final CatmullRomInterpolator.Params params = new CatmullRomInterpolator.Params(2, CatmullRomInterpolator.Type.Centripetal); new CatmullRomInterpolator().interpolate(series, params); }
@Test public void interpolate_threeElementSeriesAndThreePointsPerSegment_producesFivePoints() { final XYSeries series = new SimpleXYSeries(SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, "test", 1, 2, 3); final CatmullRomInterpolator.Params params = new CatmullRomInterpolator.Params(3, CatmullRomInterpolator.Type.Centripetal); final List<XYCoords> interpolated = new CatmullRomInterpolator().interpolate(series, params); assertEquals(5, interpolated.size()); assertEquals(1, interpolated.get(0).y); assertEquals(2, interpolated.get(2).y); assertEquals(3, interpolated.get(4).y); }
@Test public void interpolate_threeElementSeriesAndFourPointsPerSegment_producesSavenPoints() { final XYSeries series = new SimpleXYSeries(SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, "test", 1, 2, 3); final CatmullRomInterpolator.Params params = new CatmullRomInterpolator.Params(4, CatmullRomInterpolator.Type.Centripetal); final List<XYCoords> interpolated = new CatmullRomInterpolator().interpolate(series, params); assertEquals(7, interpolated.size()); assertEquals(1, interpolated.get(0).y); assertEquals(2, interpolated.get(3).y); assertEquals(3, interpolated.get(6).y); }