Skip to content

Commit dcddd7f

Browse files
committed
halfhp#66 adds basic unit tests for CatmullRomInterpolator
1 parent 98ee573 commit dcddd7f

File tree

2 files changed

+62
-3
lines changed

2 files changed

+62
-3
lines changed

androidplot-core/src/main/java/com/androidplot/xy/CatmullRomInterpolator.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import java.util.List;
2121

2222
/**
23-
* A primitive implementation of Catmull-Rom interpolation, based on the information found at:
23+
* An implementation of Catmull-Rom interpolation, based on the information found at:
2424
* http://stackoverflow.com/questions/9489736/catmull-rom-curve-with-no-cusps-and-no-self-intersections/19283471#19283471
2525
*/
2626
public class CatmullRomInterpolator implements Interpolator<CatmullRomInterpolator.Params> {
@@ -196,7 +196,7 @@ public List<XYCoords> interpolate(XYSeries series, Params params) {
196196
* @return the list of coordinates that define the CatmullRom curve
197197
* between the points defined by index+1 and index+2.
198198
*/
199-
public List<XYCoords> interpolate(XYSeries series, int index, Params params) {
199+
protected List<XYCoords> interpolate(XYSeries series, int index, Params params) {
200200
List<XYCoords> result = new ArrayList<>();
201201
double[] x = new double[4];
202202
double[] y = new double[4];
@@ -251,7 +251,7 @@ public List<XYCoords> interpolate(XYSeries series, int index, Params params) {
251251
* position between p1 and p2 to interpolate the value.
252252
* @return
253253
*/
254-
public static double interpolate(double[] p, double[] time, double t) {
254+
protected static double interpolate(double[] p, double[] time, double t) {
255255
double L01 = p[0] * (time[1] - t) / (time[1] - time[0]) + p[1] * (t - time[0]) / (time[1] - time[0]);
256256
double L12 = p[1] * (time[2] - t) / (time[2] - time[1]) + p[2] * (t - time[1]) / (time[2] - time[1]);
257257
double L23 = p[2] * (time[3] - t) / (time[3] - time[2]) + p[3] * (t - time[2]) / (time[3] - time[2]);
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.androidplot.xy;
2+
3+
import org.junit.Test;
4+
5+
import java.util.List;
6+
7+
import static junit.framework.Assert.assertEquals;
8+
import static org.mockito.Mockito.mock;
9+
10+
public class CatmullRomInterpolatorTest {
11+
12+
@Test(expected = IllegalArgumentException.class)
13+
public void interpolate_invalidPointsPerSegment_throwsIllegalArgumentException() {
14+
final XYSeries series = mock(XYSeries.class);
15+
final CatmullRomInterpolator.Params params =
16+
new CatmullRomInterpolator.Params(1, CatmullRomInterpolator.Type.Centripetal);
17+
18+
new CatmullRomInterpolator().interpolate(series, params);
19+
}
20+
21+
@Test(expected = IllegalArgumentException.class)
22+
public void interpolate_twoElementSeries_throwsIllegalArgumentException() {
23+
final XYSeries series = new SimpleXYSeries(SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, "test", 1, 2);
24+
final CatmullRomInterpolator.Params params =
25+
new CatmullRomInterpolator.Params(2, CatmullRomInterpolator.Type.Centripetal);
26+
27+
new CatmullRomInterpolator().interpolate(series, params);
28+
}
29+
30+
@Test
31+
public void interpolate_threeElementSeriesAndThreePointsPerSegment_producesFivePoints() {
32+
final XYSeries series = new SimpleXYSeries(SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, "test", 1, 2, 3);
33+
final CatmullRomInterpolator.Params params =
34+
new CatmullRomInterpolator.Params(3, CatmullRomInterpolator.Type.Centripetal);
35+
final List<XYCoords> interpolated = new CatmullRomInterpolator().interpolate(series, params);
36+
37+
assertEquals(5, interpolated.size());
38+
39+
// control points should exactly match input:
40+
assertEquals(1, interpolated.get(0).y);
41+
assertEquals(2, interpolated.get(2).y);
42+
assertEquals(3, interpolated.get(4).y);
43+
}
44+
45+
@Test
46+
public void interpolate_threeElementSeriesAndFourPointsPerSegment_producesSavenPoints() {
47+
final XYSeries series = new SimpleXYSeries(SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, "test", 1, 2, 3);
48+
final CatmullRomInterpolator.Params params =
49+
new CatmullRomInterpolator.Params(4, CatmullRomInterpolator.Type.Centripetal);
50+
final List<XYCoords> interpolated = new CatmullRomInterpolator().interpolate(series, params);
51+
52+
assertEquals(7, interpolated.size());
53+
54+
// control points should exactly match input:
55+
assertEquals(1, interpolated.get(0).y);
56+
assertEquals(2, interpolated.get(3).y);
57+
assertEquals(3, interpolated.get(6).y);
58+
}
59+
}

0 commit comments

Comments
 (0)