Skip to content

Commit

Permalink
Merge pull request #5274 from entur/refactor_copy_properties
Browse files Browse the repository at this point in the history
Refactor copy properties from parent edge to split edge
  • Loading branch information
vpaturet authored Aug 16, 2023
2 parents 5188f73 + 7d11714 commit 503ac5e
Show file tree
Hide file tree
Showing 10 changed files with 384 additions and 149 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
import org.opentripplanner.routing.graph.Graph;
import org.opentripplanner.street.model.edge.Edge;
import org.opentripplanner.street.model.edge.StreetEdge;
import org.opentripplanner.street.model.edge.StreetElevationExtension;
import org.opentripplanner.street.model.edge.StreetElevationExtensionBuilder;
import org.opentripplanner.street.model.vertex.Vertex;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -509,7 +509,12 @@ private Coverage getThreadSpecificCoverageInterpolator() {

private void setEdgeElevationProfile(StreetEdge ee, PackedCoordinateSequence elevPCS) {
try {
StreetElevationExtension.addToEdge(ee, elevPCS, false);
StreetElevationExtensionBuilder
.of(ee)
.withElevationProfile(elevPCS)
.withComputed(false)
.build()
.ifPresent(ee::setElevationExtension);
if (ee.isElevationFlattened()) {
issueStore.add(new ElevationFlattened(ee));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import org.opentripplanner.graph_builder.issues.ElevationProfileFailure;
import org.opentripplanner.street.model.edge.Edge;
import org.opentripplanner.street.model.edge.StreetEdge;
import org.opentripplanner.street.model.edge.StreetElevationExtension;
import org.opentripplanner.street.model.edge.StreetElevationExtensionBuilder;
import org.opentripplanner.street.model.vertex.Vertex;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -254,7 +254,12 @@ private void assignElevationToEdgeIfPossible(Map<Vertex, Double> elevations, Str
PackedCoordinateSequence profile = new PackedCoordinateSequence.Double(coords);

try {
StreetElevationExtension.addToEdge(edge, profile, true);
StreetElevationExtensionBuilder
.of(edge)
.withElevationProfile(profile)
.withComputed(true)
.build()
.ifPresent(edge::setElevationExtension);

if (edge.isElevationFlattened()) {
issueStore.add(new ElevationFlattened(edge));
Expand Down
121 changes: 64 additions & 57 deletions src/main/java/org/opentripplanner/street/model/edge/StreetEdge.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public class StreetEdge
/**
* The speed (meters / sec) at which an automobile can traverse this street segment.
*/
private float carSpeed;
private final float carSpeed;

/**
* The angle at the start of the edge geometry. Internal representation is -180 to +179 integer
Expand Down Expand Up @@ -141,10 +141,11 @@ protected StreetEdge(StreetEdgeBuilder<?> builder) {
this.setWalkSafetyFactor(builder.walkSafetyFactor());
this.name = builder.name();
this.setPermission(builder.permission());
this.setCarSpeed(builder.carSpeed());
this.carSpeed = builder.carSpeed();
LineStringInOutAngles lineStringInOutAngles = LineStringInOutAngles.of(builder.geometry());
inAngle = lineStringInOutAngles.inAngle();
outAngle = lineStringInOutAngles.outAngle();
elevationExtension = builder.streetElevationExtension();
}

public StreetEdgeBuilder<?> toBuilder() {
Expand Down Expand Up @@ -587,18 +588,10 @@ public boolean isLink() {
return BitSetUtils.get(flags, CLASS_LINK);
}

private void setLink(boolean link) {
flags = BitSetUtils.set(flags, CLASS_LINK, link);
}

public float getCarSpeed() {
return carSpeed;
}

private void setCarSpeed(float carSpeed) {
this.carSpeed = carSpeed;
}

public boolean isSlopeOverride() {
return BitSetUtils.get(flags, SLOPEOVERRIDE_FLAG_INDEX);
}
Expand Down Expand Up @@ -690,11 +683,17 @@ public SplitStreetEdge splitDestructively(SplitterVertex v) {
l2 = 1;
}

StreetEdge se1 = seb1.withMilliMeterLength(l1).buildAndConnect();
StreetEdge se2 = seb2.withMilliMeterLength(l2).buildAndConnect();
seb1.withMilliMeterLength(l1);
seb2.withMilliMeterLength(l2);

copyPropertiesToSplitEdge(seb1, 0, l1 / 1000.0);
copyPropertiesToSplitEdge(seb2, l1 / 1000.0, getDistanceMeters());

StreetEdge se1 = seb1.buildAndConnect();
StreetEdge se2 = seb2.buildAndConnect();

copyPropertiesToSplitEdge(se1, 0, se1.getDistanceMeters());
copyPropertiesToSplitEdge(se2, se1.getDistanceMeters(), getDistanceMeters());
copyRentalRestrictionsToSplitEdge(se1);
copyRentalRestrictionsToSplitEdge(se2);

var splitEdges = new SplitStreetEdge(se1, se2);
copyRestrictionsToSplitEdges(this, splitEdges);
Expand All @@ -713,33 +712,33 @@ public SplitStreetEdge splitNonDestructively(
StreetEdge e2 = null;

if (direction == LinkingDirection.OUTGOING || direction == LinkingDirection.BOTH_WAYS) {
e1 =
new TemporaryPartialStreetEdgeBuilder()
.withParentEdge(this)
.withFromVertex((StreetVertex) fromv)
.withToVertex(v)
.withGeometry(geoms.beginning())
.withName(name)
.withBack(isBack())
.buildAndConnect();
copyPropertiesToSplitEdge(e1, 0, e1.getDistanceMeters());
var seb1 = new TemporaryPartialStreetEdgeBuilder()
.withParentEdge(this)
.withFromVertex((StreetVertex) fromv)
.withToVertex(v)
.withGeometry(geoms.beginning())
.withName(name)
.withBack(isBack());
copyPropertiesToSplitEdge(seb1, 0, defaultMillimeterLength(geoms.beginning()) / 1000.0);
e1 = seb1.buildAndConnect();
copyRentalRestrictionsToSplitEdge(e1);
tempEdges.addEdge(e1);
}
if (direction == LinkingDirection.INCOMING || direction == LinkingDirection.BOTH_WAYS) {
e2 =
new TemporaryPartialStreetEdgeBuilder()
.withParentEdge(this)
.withFromVertex(v)
.withToVertex((StreetVertex) tov)
.withGeometry(geoms.ending())
.withName(name)
.withBack(isBack())
.buildAndConnect();
var seb2 = new TemporaryPartialStreetEdgeBuilder()
.withParentEdge(this)
.withFromVertex(v)
.withToVertex((StreetVertex) tov)
.withGeometry(geoms.ending())
.withName(name)
.withBack(isBack());
copyPropertiesToSplitEdge(
e2,
getDistanceMeters() - e2.getDistanceMeters(),
seb2,
getDistanceMeters() - defaultMillimeterLength(geoms.ending()) / 1000.0,
getDistanceMeters()
);
e2 = seb2.buildAndConnect();
copyRentalRestrictionsToSplitEdge(e2);
tempEdges.addEdge(e2);
}

Expand Down Expand Up @@ -773,16 +772,17 @@ public Optional<Edge> createPartialEdge(StreetVertex from, StreetVertex to) {
double lengthRatio = partial.getLength() / parent.getLength();
double length = getDistanceMeters() * lengthRatio;

var tempEdge = new TemporaryPartialStreetEdgeBuilder()
var tpseb = new TemporaryPartialStreetEdgeBuilder()
.withParentEdge(this)
.withFromVertex(from)
.withToVertex(to)
.withGeometry(partial)
.withName(getName())
.withMeterLength(length)
.buildAndConnect();
copyPropertiesToSplitEdge(tempEdge, start, start + length);
return Optional.of(tempEdge);
.withMeterLength(length);
copyPropertiesToSplitEdge(tpseb, start, start + length);
TemporaryPartialStreetEdge se = tpseb.buildAndConnect();
copyRentalRestrictionsToSplitEdge(se);
return Optional.of(se);
} else {
return Optional.empty();
}
Expand Down Expand Up @@ -868,31 +868,38 @@ public void remove() {
super.remove();
}

/**
* Copy inherited properties from a parent edge to a split edge.
*/
protected void copyPropertiesToSplitEdge(
StreetEdge splitEdge,
StreetEdgeBuilder<?> seb,
double fromDistance,
double toDistance
) {
splitEdge.flags = this.flags;
splitEdge.setBicycleSafetyFactor(bicycleSafetyFactor);
splitEdge.setWalkSafetyFactor(walkSafetyFactor);
splitEdge.setLink(isLink());
splitEdge.setCarSpeed(getCarSpeed());
splitEdge.setElevationExtensionUsingParent(this, fromDistance, toDistance);
splitEdge.addRentalRestriction(fromv.rentalRestrictions());
}
seb.withFlags(flags);
seb.withBicycleSafetyFactor(bicycleSafetyFactor);
seb.withWalkSafetyFactor(walkSafetyFactor);
seb.withCarSpeed(carSpeed);

protected void setElevationExtensionUsingParent(
StreetEdge parentEdge,
double fromDistance,
double toDistance
) {
var profile = ElevationUtils.getPartialElevationProfile(
parentEdge.getElevationProfile(),
var partialElevationProfileFromParent = ElevationUtils.getPartialElevationProfile(
getElevationProfile(),
fromDistance,
toDistance
);
StreetElevationExtension.addToEdge(this, profile, true);

StreetElevationExtensionBuilder
.of(seb)
.withDistanceInMeters(defaultMillimeterLength(seb.geometry()) / 1000.)
.withElevationProfile(partialElevationProfileFromParent)
.build()
.ifPresent(seb::withElevationExtension);
}

/**
* Copy inherited rental restrictions from a parent edge to a split edge
*/
protected void copyRentalRestrictionsToSplitEdge(StreetEdge splitEdge) {
splitEdge.addRentalRestriction(fromv.rentalRestrictions());
}

short getFlags() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class StreetEdgeBuilder<B extends StreetEdgeBuilder<B>> {
private float walkSafetyFactor;
private float bicycleSafetyFactor;
private short flags;
private StreetElevationExtension streetElevationExtension;

public StreetEdgeBuilder() {
this.defaultLength = true;
Expand Down Expand Up @@ -225,6 +226,28 @@ public B withNoThruTrafficTraverseMode(TraverseMode noThruTrafficTraverseMode) {
return instance();
}

public boolean slopeOverride() {
return BitSetUtils.get(flags, SLOPEOVERRIDE_FLAG_INDEX);
}

public boolean stairs() {
return BitSetUtils.get(flags, STAIRS_FLAG_INDEX);
}

public B withFlags(short flags) {
this.flags = flags;
return instance();
}

public B withElevationExtension(StreetElevationExtension streetElevationExtension) {
this.streetElevationExtension = streetElevationExtension;
return instance();
}

public StreetElevationExtension streetElevationExtension() {
return streetElevationExtension;
}

@SuppressWarnings("unchecked")
final B instance() {
return (B) this;
Expand Down
Loading

0 comments on commit 503ac5e

Please sign in to comment.