org.openlr.encoder.ShortestPathProcessor Maven / Gradle / Ivy
The newest version!
package org.openlr.encoder;
import org.openlr.map.Line;
import org.openlr.map.Path;
import org.openlr.map.ShortestPathFinder;
import java.util.List;
import java.util.Optional;
import java.util.OptionalDouble;
class ShortestPathProcessor implements IntermediateProcessor {
private final ShortestPathFinder shortestPathFinder;
ShortestPathProcessor(ShortestPathFinder shortestPathFinder) {
this.shortestPathFinder = shortestPathFinder;
}
@Override
public > OptionalDouble getDistanceToNext(Path path) {
Optional> optionalShortestPath = shortestPathFinder.find(path.getStart(), path.getEnd(), path.getLength());
if (optionalShortestPath.isEmpty()) {
return OptionalDouble.empty();
}
Path shortestPath = optionalShortestPath.get();
if (shortestPath.equals(path)) {
return OptionalDouble.empty();
}
List pathLines = path.getLines();
List shortestPathLines = shortestPath.getLines();
double lengthToDeviation = 0;
for (int i = 0; i < pathLines.size() && i < shortestPathLines.size(); i++) {
L line = pathLines.get(i);
if (!line.equals(shortestPathLines.get(i))) {
break;
}
lengthToDeviation += line.getLength();
}
double distance = lengthToDeviation - path.getPositiveOffset();
return OptionalDouble.of(distance);
}
}