Skip to content

Commit

Permalink
Revert "Speed up path simplification with path details/instructions (#…
Browse files Browse the repository at this point in the history
…1782)"

This reverts commit 2ebac80
  • Loading branch information
easbar committed Nov 19, 2019
1 parent 781b1db commit 6cc92df
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 583 deletions.
2 changes: 1 addition & 1 deletion api/src/main/java/com/graphhopper/util/PointList.java
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ public void parse2DJSON(String str) {
if (latlon.trim().length() == 0)
continue;

String[] ll = latlon.split(",");
String ll[] = latlon.split(",");
String lat = ll[1].replace("]", "").trim();
add(Double.parseDouble(lat), Double.parseDouble(ll[0].trim()), Double.NaN);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,6 @@ public boolean isEmpty() {
return size() == 0;
}

public String getIntervalString() {
return "[" + fromOffset + ", " + toOffset + "]";
}

@Override
public double getLatitude(int index) {
if (index > getSize())
Expand Down
59 changes: 35 additions & 24 deletions core/src/main/java/com/graphhopper/util/DouglasPeucker.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,22 +64,16 @@ public int simplify(PointList points) {
return simplify(points, 0, points.size() - 1);
}

public int simplify(PointList points, int fromIndex, int lastIndex) {
return simplify(points, fromIndex, lastIndex, true);
}

/**
* Simplifies a part of the <code>points</code>. The <code>fromIndex</code> and <code>lastIndex</code>
* are guaranteed to be kept.
*
* @param points The PointList to simplify
* @param fromIndex Start index to simplify, should be <= <code>lastIndex</code>
* @param fromIndex Start index to simplify, should be >= <code>lastIndex</code>
* @param lastIndex Simplify up to this index
* @param compress Whether or not the <code>points</code> shall be compressed or not, if set to false no points
* are actually removed, but instead their lat/lon/ele is only set to NaN
* @return The number of removed points
*/
public int simplify(PointList points, int fromIndex, int lastIndex, boolean compress) {
public int simplify(PointList points, int fromIndex, int lastIndex) {
int removed = 0;
int size = lastIndex - fromIndex;
if (approx) {
Expand All @@ -95,12 +89,43 @@ public int simplify(PointList points, int fromIndex, int lastIndex, boolean comp
removed = subSimplify(points, fromIndex, lastIndex);
}

if (removed > 0 && compress)
removeNaN(points);
if (removed > 0)
compressNew(points, removed);

return removed;
}

/**
* compress list: move points into EMPTY slots
*/
void compressNew(PointList points, int removed) {
int freeIndex = -1;
for (int currentIndex = 0; currentIndex < points.getSize(); currentIndex++) {
if (Double.isNaN(points.getLatitude(currentIndex))) {
if (freeIndex < 0)
freeIndex = currentIndex;

continue;
} else if (freeIndex < 0) {
continue;
}

points.set(freeIndex, points.getLatitude(currentIndex), points.getLongitude(currentIndex), points.getElevation(currentIndex));
points.set(currentIndex, Double.NaN, Double.NaN, Double.NaN);
// find next free index
int max = currentIndex;
int searchIndex = freeIndex + 1;
freeIndex = currentIndex;
for (; searchIndex < max; searchIndex++) {
if (Double.isNaN(points.getLatitude(searchIndex))) {
freeIndex = searchIndex;
break;
}
}
}
points.trimToSize(points.getSize() - removed);
}

// keep the points of fromIndex and lastIndex
int subSimplify(PointList points, int fromIndex, int lastIndex) {
if (lastIndex - fromIndex < 2) {
Expand Down Expand Up @@ -142,18 +167,4 @@ int subSimplify(PointList points, int fromIndex, int lastIndex) {
return counter;
}

/**
* Fills all entries of the point list that are NaN with the subsequent values (and therefore shortens the list)
*/
static void removeNaN(PointList pointList) {
int curr = 0;
for (int i = 0; i < pointList.size(); i++) {
if (!Double.isNaN(pointList.getLatitude(i))) {
pointList.set(curr, pointList.getLatitude(i), pointList.getLongitude(i), pointList.getElevation(i));
curr++;
}
}
pointList.trimToSize(curr);
}

}
7 changes: 4 additions & 3 deletions core/src/main/java/com/graphhopper/util/PathMerger.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public class PathMerger {
private DouglasPeucker douglasPeucker = DP;
private boolean calcPoints = true;
private PathDetailsBuilderFactory pathBuilderFactory;
private List<String> requestedPathDetails = Collections.emptyList();
private List<String> requestedPathDetails = Collections.EMPTY_LIST;
private double favoredHeading = Double.NaN;

public PathMerger(Graph graph, Weighting weighting) {
Expand Down Expand Up @@ -117,7 +117,7 @@ public void doWork(PathWrapper altRsp, List<Path> paths, EncodingManager encodin
if (!il.isEmpty()) {
fullInstructions.addAll(il);

// for all paths except the last replace the FinishInstruction with a ViaInstruction
// for all paths except the last replace the FinishInstruction with a ViaInstructionn
if (pathIndex + 1 < paths.size()) {
ViaInstruction newInstr = new ViaInstruction(fullInstructions.get(fullInstructions.size() - 1));
newInstr.setViaCount(pathIndex + 1);
Expand Down Expand Up @@ -167,7 +167,8 @@ public void doWork(PathWrapper altRsp, List<Path> paths, EncodingManager encodin
setTime(fullTimeInMillis);

if (allFound && simplifyResponse && (calcPoints || enableInstructions)) {
PathSimplification.simplify(altRsp, douglasPeucker, enableInstructions);
PathSimplification ps = new PathSimplification(altRsp, douglasPeucker, enableInstructions);
ps.simplify();
}
}

Expand Down
Loading

0 comments on commit 6cc92df

Please sign in to comment.