Skip to content

Commit

Permalink
Test time labels directly
Browse files Browse the repository at this point in the history
  • Loading branch information
michaz committed Apr 6, 2020
1 parent 97b5d80 commit 8eed548
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 56 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package com.graphhopper.isochrone.algorithm;

import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.locationtech.jts.geom.Envelope;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.triangulate.quadedge.QuadEdge;
import org.locationtech.jts.triangulate.quadedge.QuadEdgeSubdivision;
import org.locationtech.jts.triangulate.quadedge.Vertex;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class QuadEdgeSubdivisionTest {

@Test
Expand Down Expand Up @@ -49,7 +50,7 @@ public void createQuadEdgeSubdivisionFromScratch() {
ContourBuilder contourBuilder = new ContourBuilder(quadEdgeSubdivision.getEdges());

Geometry geometry = contourBuilder.computeIsoline(0.5);
Assert.assertEquals("MULTIPOLYGON (((1 0, 0.5 -0.5, 1 -2, 1.5 -0.5, 1 0, 1 0)))", geometry.toString());
assertEquals("MULTIPOLYGON (((1 0, 0.5 -0.5, 1 -2, 1.5 -0.5, 1 0, 1 0)))", geometry.toString());
}

@Test
Expand Down Expand Up @@ -90,7 +91,7 @@ public void createQuadEdgeSubdivisionFromTriangleList() {
ContourBuilder contourBuilder = new ContourBuilder(triangulation.getEdges());

Geometry geometry = contourBuilder.computeIsoline(0.5);
Assert.assertEquals("MULTIPOLYGON (((0.5 -0.5, 1 -2, 1.5 -0.5, 1 0, 0.5 -0.5)))", geometry.toString());
assertEquals("MULTIPOLYGON (((0.5 -0.5, 1 -2, 1.5 -0.5, 1 0, 0.5 -0.5)))", geometry.toString());
}

@Test
Expand Down Expand Up @@ -135,26 +136,26 @@ public void createQuadEdgeSubdivisionFromTriangleList2() {
ContourBuilder contourBuilder = new ContourBuilder(triangulation.getEdges());

Geometry geometry = contourBuilder.computeIsoline(0.5);
Assert.assertEquals("MULTIPOLYGON (((0.5 -0.5, 1 -2, 1.5 -0.5, 1 0, 0.5 -0.5)))", geometry.toString());
assertEquals("MULTIPOLYGON (((0.5 -0.5, 1 -2, 1.5 -0.5, 1 0, 0.5 -0.5)))", geometry.toString());
}

private void assertVertex(QuadEdge ee1, QuadEdge ee2, QuadEdge ee3) {
Assert.assertEquals(ee2, ee1.oNext());
Assert.assertEquals(ee3, ee2.oNext());
Assert.assertEquals(ee1, ee3.oNext());
assertEquals(ee2, ee1.oNext());
assertEquals(ee3, ee2.oNext());
assertEquals(ee1, ee3.oNext());
}

private void assertVertex(QuadEdge ee1, QuadEdge ee2, QuadEdge ee3, QuadEdge ee4) {
Assert.assertEquals(ee2, ee1.oNext());
Assert.assertEquals(ee3, ee2.oNext());
Assert.assertEquals(ee4, ee3.oNext());
Assert.assertEquals(ee1, ee4.oNext());
assertEquals(ee2, ee1.oNext());
assertEquals(ee3, ee2.oNext());
assertEquals(ee4, ee3.oNext());
assertEquals(ee1, ee4.oNext());
}

private void assertTriangle(QuadEdge e1, QuadEdge e2, QuadEdge e3) {
Assert.assertEquals(e2, e1.lNext());
Assert.assertEquals(e3, e2.lNext());
Assert.assertEquals(e1, e3.lNext());
assertEquals(e2, e1.lNext());
assertEquals(e3, e2.lNext());
assertEquals(e1, e3.lNext());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@
import com.graphhopper.storage.RAMDirectory;
import com.graphhopper.util.GHUtility;
import com.graphhopper.util.PMap;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static org.junit.Assert.assertEquals;
import static java.util.Comparator.comparing;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;

/**
* @author Peter Karich
Expand All @@ -28,13 +28,13 @@ public class ShortestPathTreeTest {
private final FlagEncoder carEncoder = encodingManager.getEncoder("car");
private GraphHopperStorage graph;

@Before
@BeforeEach
public void setUp() {
graph = new GraphHopperStorage(new RAMDirectory(), encodingManager, false);
graph.create(1000);
}

@After
@AfterEach
public void tearDown() {
graph.close();
}
Expand Down Expand Up @@ -70,41 +70,22 @@ private void fillTestGraph(Graph graph) {
@Test
public void testSearch60Seconds() {
fillTestGraph(graph);
List<ShortestPathTree.IsoLabelWithCoordinates> result = new ArrayList<>();
ShortestPathTree instance = new ShortestPathTree(graph, new FastestWeighting(carEncoder, new PMap()), false);
instance.setTimeLimit(60);
List<Set<Integer>> res = searchFromNode0Into5Buckets(instance);
assertEquals("[[0, 4], [6], [1, 7], [5], [2, 3]]", res.toString());
instance.search(0, result::add);
result.sort(comparing(label -> label.nodeId));
assertEquals(8, result.size());
assertAll(
() -> assertEquals(0, result.get(0).timeMillis),
() -> assertEquals(25200, result.get(1).timeMillis),
() -> assertEquals(54000, result.get(2).timeMillis),
() -> assertEquals(55800, result.get(3).timeMillis),
() -> assertEquals(9000, result.get(4).timeMillis),
() -> assertEquals(36000, result.get(5).timeMillis),
() -> assertEquals(18000, result.get(6).timeMillis),
() -> assertEquals(27000, result.get(7).timeMillis)
);
}

@Test
public void testSearch30Seconds() {
fillTestGraph(graph);
ShortestPathTree instance = new ShortestPathTree(graph, new FastestWeighting(carEncoder, new PMap()), false);
instance.setTimeLimit(30);
List<Set<Integer>> res = searchFromNode0Into5Buckets(instance);
assertEquals("[[0], [4], [], [6], [1, 7]]", res.toString());
}

private List<Set<Integer>> searchFromNode0Into5Buckets(ShortestPathTree instance) {
final double bucketSize = instance.limit / 5;
final List<Set<Integer>> list = new ArrayList<>(5);

for (int i = 0; i < 5; i++) {
list.add(new HashSet<>());
}

instance.search(0, isoLabelWithCoordinates -> {
int bucketIndex = (int) (isoLabelWithCoordinates.timeMillis / bucketSize);
if (bucketIndex < 0) {
throw new IllegalArgumentException("edge cannot have negative explore value " + isoLabelWithCoordinates.nodeId + ", " + isoLabelWithCoordinates);
} else if (bucketIndex == 5) {
bucketIndex = 5 - 1;
} else if (bucketIndex > 5) {
return;
}

list.get(bucketIndex).add(isoLabelWithCoordinates.nodeId);
});
return list;
}
}

0 comments on commit 8eed548

Please sign in to comment.