Skip to content

Commit

Permalink
Test findAllPaths
Browse files Browse the repository at this point in the history
  • Loading branch information
wipfli committed Nov 2, 2024
1 parent 6c2c338 commit 9b4a65b
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ private void add(LineString lineString) {

}

public static Coordinate roundCoordinate(Coordinate cooridnate) {
protected static Coordinate roundCoordinate(Coordinate cooridnate) {
Coordinate result = new Coordinate(cooridnate);

double multiplier = 16.0;
Expand All @@ -67,7 +67,7 @@ public static Coordinate roundCoordinate(Coordinate cooridnate) {
return result;
}

public static List<LineString> split(LineString lineString) {
protected static List<LineString> split(LineString lineString) {
List<LineString> segments = new ArrayList<>();

Coordinate[] coordinates = lineString.getCoordinates();
Expand All @@ -82,7 +82,7 @@ public static List<LineString> split(LineString lineString) {
return segments;
}

public static LineString concat(LineString A, LineString B) {
protected static LineString concat(LineString A, LineString B) {

List<Coordinate> coordinates = new ArrayList<>();
List<Coordinate> coordsA = List.of(A.getCoordinates());
Expand Down Expand Up @@ -175,7 +175,7 @@ private void removeLoops(double loopMinLength) {
}
}

private List<List<LineString>> findAllPaths(Point start, Point end, double maxLength) {
protected List<List<LineString>> findAllPaths(Point start, Point end, double maxLength) {
List<List<LineString>> allPaths = new ArrayList<>();
Queue<List<LineString>> queue = new LinkedList<>();

Expand Down Expand Up @@ -245,7 +245,7 @@ private static double getLength(List<LineString> lines) {
return result;
}

public static boolean hasPointAppearingMoreThanTwice(List<LineString> lineStrings) {
protected static boolean hasPointAppearingMoreThanTwice(List<LineString> lineStrings) {
HashMap<Point, Integer> pointCountMap = new HashMap<>();
for (LineString line : lineStrings) {
Point startPoint = line.getStartPoint();
Expand Down Expand Up @@ -320,50 +320,50 @@ public List<LineString> getMergedLineStrings(double minLength, double loopMinLen

return result;
}
}

class Node {
private Point point;
private List<LineString> edges;

public Node(Point point) {
this.point = point;
this.edges = new ArrayList<>();
}

public Point getPoint() {
return point;
}

public void setPoint(Point point) {
this.point = point;
}

public List<LineString> getEdges() {
return edges;
}

public void addEdge(LineString edge) {
if (!edges.contains(edge) && !edges.contains(edge.reverse())) {
edges.add(edge);
class Node {
private Point point;
private List<LineString> edges;

public Node(Point point) {
this.point = point;
this.edges = new ArrayList<>();
}
}

public void removeEdge(LineString edge) {
if (edges.contains(edge)) {
edges.remove(edge);
} else if (edges.contains(edge.reverse())) {
edges.remove(edge.reverse());
} else {
// nothing to do

public Point getPoint() {
return point;
}
}

@Override
public String toString() {
return "Node{" +
"point=" + point +
", edges=" + edges +
'}';
}

public void setPoint(Point point) {
this.point = point;
}

public List<LineString> getEdges() {
return edges;
}

public void addEdge(LineString edge) {
if (!edges.contains(edge) && !edges.contains(edge.reverse())) {
edges.add(edge);
}
}

public void removeEdge(LineString edge) {
if (edges.contains(edge)) {
edges.remove(edge);
} else if (edges.contains(edge.reverse())) {
edges.remove(edge.reverse());
} else {
// nothing to do
}
}

@Override
public String toString() {
return "Node{" +
"point=" + point +
", edges=" + edges +
'}';
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.onthegomap.planetiler.util;
import static com.onthegomap.planetiler.TestUtils.newLineString;
import static com.onthegomap.planetiler.TestUtils.newPoint;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.List;
Expand Down Expand Up @@ -240,5 +241,75 @@ void testMerge() {
merger.getMergedLineStrings(15, -1)
);
}

@Test
void testHasPointAppearingMoreThanTwice() {

// can revisit starting node once
assertEquals(
false,
LoopLineMerger.hasPointAppearingMoreThanTwice(
List.of(
newLineString(10, 0, 20, 0),
newLineString(20, 0, 20, 10),
newLineString(20, 10, 10, 0)
)
)
);

// cannot revisit starting node and continue on
assertEquals(
true,
LoopLineMerger.hasPointAppearingMoreThanTwice(
List.of(
newLineString(10, 0, 20, 0),
newLineString(20, 0, 20, 10),
newLineString(20, 10, 10, 0),
newLineString(10, 0, 10, 10)
)
)
);
}

@Test
void testFindAllPaths() {
// finds all paths and orders them by length
var merger = new LoopLineMerger();
/** 10 20 30
* 10 o-----o
* |\ |
* | \ |
* 20 o--o--o
*/
merger.add(newLineString(10, 10, 30, 10));
merger.add(newLineString(10, 10, 10, 20));
merger.add(newLineString(10, 10, 20, 20));
merger.add(newLineString(10, 20, 20, 20));
merger.add(newLineString(20, 20, 30, 20));
merger.add(newLineString(30, 20, 30, 10));

var allPaths = merger.findAllPaths(newPoint(10, 10), newPoint(20, 20), 1000);

assertEquals(3, allPaths.size());
assertEquals(
List.of(newLineString(10, 10, 20, 20)),
allPaths.get(0)
);
assertEquals(
List.of(
newLineString(10, 10, 10, 20),
newLineString(10, 20, 20, 20)
),
allPaths.get(1)
);
assertEquals(
List.of(
newLineString(10, 10, 30, 10),
newLineString(30, 10, 30, 20),
newLineString(30, 20, 20, 20)
),
allPaths.get(2)
);
}

}

0 comments on commit 9b4a65b

Please sign in to comment.