Skip to content

add test for calculation of square segment distance- coverage 100% #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Simplification of a 2D-polyline or a 3D-polyline.
* requires Java 5
* Maven Build
* JUnit-tested
* 94% lines covered
* 100% lines covered
* reference data is created by "original" JavaScript implementation (Version 1.1.0)

# Example #
Expand Down
18 changes: 12 additions & 6 deletions src/main/java/com/goebl/simplify/Simplify3D.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public double getSquareDistance(T p1, T p2) {

@Override
public double getSquareSegmentDistance(T p0, T p1, T p2) {
double x0, y0, z0, x1, y1, z1, x2, y2, z2, dx, dy, dz, t;
double x0, y0, z0, x1, y1, z1, x2, y2, z2;

x1 = pointExtractor.getX(p1);
y1 = pointExtractor.getY(p1);
Expand All @@ -77,14 +77,20 @@ public double getSquareSegmentDistance(T p0, T p1, T p2) {
y0 = pointExtractor.getY(p0);
z0 = pointExtractor.getZ(p0);

dx = x2 - x1;
dy = y2 - y1;
dz = z2 - z1;
return calculateSquareSegmentDistance(x0, y0, z0, x1, y1, z1, x2, y2, z2);
}

static double calculateSquareSegmentDistance(
double x0, double y0, double z0,
double x1, double y1, double z1,
double x2, double y2, double z2) {
double dx = x2 - x1;
double dy = y2 - y1;
double dz = z2 - z1;

if (dx != 0.0d || dy != 0.0d || dz != 0.0d) {
t = ((x0 - x1) * dx + (y0 - y1) * dy + (z0 - z1) * dz)
double t = ((x0 - x1) * dx + (y0 - y1) * dy + (z0 - z1) * dz)
/ (dx * dx + dy * dy + dz * dz);

if (t > 1.0d) {
x1 = x2;
y1 = y2;
Expand Down
15 changes: 15 additions & 0 deletions src/test/java/com/goebl/simplify/Simplify3DTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class Simplify3DTest {
private static final float[][] POINTS_3D = new float[][]{
{3.14f, 5.2f, 4f}, {5.7f, 8.1f, 5f}, {4.6f, -1.3f, 6f}
};
private static final double DELTA = 0.00001d;

@Test
public void testCustomPoint3DExtractor() {
Expand Down Expand Up @@ -59,6 +60,20 @@ public void testDefaultPointExtractor() {
Assert.assertEquals("array should be simplified", 2, simplified.length);
}

@Test
public void testCalculateSquareSegmentDistance() {
double res = Simplify3D.calculateSquareSegmentDistance(
2, 1, 1,
1, 1, 1,
3, 3, 1);
Assert.assertEquals(0.5d, res, DELTA);
double resBis = Simplify3D.calculateSquareSegmentDistance(
-0.1, -0.2, -0.3,
-0.7, -0.8, -0.9,
-0.4, -0.5, -0.6);
Assert.assertEquals(0.27d, resBis, DELTA);
}

private static class MyPoint implements Point3D {
double x;
double y;
Expand Down
6 changes: 5 additions & 1 deletion src/test/java/com/goebl/simplify/SimplifyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,10 @@ static Point[] readPoints(String fileName) throws Exception {
List<MyPoint> pointList = new ArrayList<MyPoint>();
File file = new File("src/test/resources", fileName);
InputStream is = null;
BufferedReader reader = null;
try {
is = new FileInputStream(file);
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
String line;
while ((line = reader.readLine()) != null) {
if (line.trim().length() == 0) {
Expand All @@ -125,6 +126,9 @@ static Point[] readPoints(String fileName) throws Exception {
pointList.add(new MyPoint(x, y));
}
} finally {
if (reader != null) {
reader.close();
}
if (is != null) {
is.close();
}
Expand Down