11package org .opentripplanner .ext .carpooling .routing ;
22
33import static org .opentripplanner .ext .carpooling .util .GraphPathUtils .calculateCumulativeDurations ;
4+ import static org .opentripplanner .ext .carpooling .util .GraphPathUtils .calculateDuration ;
45
56import java .time .Duration ;
67import java .util .ArrayList ;
@@ -57,24 +58,18 @@ public InsertionEvaluator(
5758 }
5859
5960 /**
60- * Routes all baseline segments and caches the results .
61+ * Routes all segments of routePoints .
6162 *
6263 * @return Array of routed segments, or null if any segment fails to route
6364 */
6465 @ SuppressWarnings ("unchecked" )
65- private GraphPath <State , Edge , Vertex >[] routeBaselineSegments (List <WgsCoordinate > routePoints ) {
66+ private GraphPath <State , Edge , Vertex >[] routeSegments (List <WgsCoordinate > routePoints ) {
6667 GraphPath <State , Edge , Vertex >[] segments = new GraphPath [routePoints .size () - 1 ];
6768
6869 for (int i = 0 ; i < routePoints .size () - 1 ; i ++) {
6970 var fromCoord = routePoints .get (i );
7071 var toCoord = routePoints .get (i + 1 );
71- GenericLocation from = GenericLocation .fromCoordinate (
72- fromCoord .latitude (),
73- fromCoord .longitude ()
74- );
75- GenericLocation to = GenericLocation .fromCoordinate (toCoord .latitude (), toCoord .longitude ());
76-
77- GraphPath <State , Edge , Vertex > segment = routingFunction .route (from , to , linkingContext );
72+ GraphPath <State , Edge , Vertex > segment = routeSegment (fromCoord , toCoord );
7873 if (segment == null ) {
7974 LOG .debug ("Baseline routing failed for segment {} → {}" , i , i + 1 );
8075 return null ;
@@ -86,6 +81,18 @@ private GraphPath<State, Edge, Vertex>[] routeBaselineSegments(List<WgsCoordinat
8681 return segments ;
8782 }
8883
84+ private GraphPath <State , Edge , Vertex > routeSegment (WgsCoordinate from , WgsCoordinate to ) {
85+ GenericLocation fromGenericLocation = GenericLocation .fromCoordinate (
86+ from .latitude (),
87+ from .longitude ()
88+ );
89+ GenericLocation toGenericLocation = GenericLocation .fromCoordinate (
90+ to .latitude (),
91+ to .longitude ()
92+ );
93+ return routingFunction .route (fromGenericLocation , toGenericLocation , linkingContext );
94+ }
95+
8996 /**
9097 * Evaluates pre-filtered insertion positions using A* routing.
9198 * <p>
@@ -107,17 +114,30 @@ public InsertionCandidate findBestInsertion(
107114 WgsCoordinate passengerPickup ,
108115 WgsCoordinate passengerDropoff
109116 ) {
110- GraphPath <State , Edge , Vertex >[] baselineSegments = routeBaselineSegments (trip .routePoints ());
117+ GraphPath <State , Edge , Vertex >[] baselineSegments = routeSegments (trip .routePoints ());
111118 if (baselineSegments == null ) {
112119 LOG .warn ("Could not route baseline for trip {}" , trip .getId ());
113120 return null ;
114121 }
115122
116123 Duration [] cumulativeDurations = calculateCumulativeDurations (baselineSegments );
117124
125+ GraphPath <State , Edge , Vertex > pathBetweenOriginAndDestination = routeSegment (
126+ trip .stops ().getFirst ().getCoordinate (),
127+ trip .stops ().getLast ().getCoordinate ()
128+ );
129+
130+ if (pathBetweenOriginAndDestination == null ) {
131+ LOG .warn ("Could not create route between start and stop for trip {}" , trip .getId ());
132+ return null ;
133+ }
134+
135+ Duration durationBetweenOriginAndDestination = calculateDuration (
136+ pathBetweenOriginAndDestination
137+ );
138+
118139 InsertionCandidate bestCandidate = null ;
119140 Duration minAdditionalDuration = INITIAL_ADDITIONAL_DURATION ;
120- Duration baselineDuration = cumulativeDurations [cumulativeDurations .length - 1 ];
121141
122142 for (InsertionPosition position : viablePositions ) {
123143 InsertionCandidate candidate = evaluateInsertion (
@@ -128,7 +148,7 @@ public InsertionCandidate findBestInsertion(
128148 passengerDropoff ,
129149 baselineSegments ,
130150 cumulativeDurations ,
131- baselineDuration
151+ durationBetweenOriginAndDestination
132152 );
133153
134154 if (candidate == null ) {
@@ -168,7 +188,7 @@ private InsertionCandidate evaluateInsertion(
168188 WgsCoordinate passengerDropoff ,
169189 GraphPath <State , Edge , Vertex >[] baselineSegments ,
170190 Duration [] originalCumulativeDurations ,
171- Duration baselineDuration
191+ Duration durationBetweenOriginAndDestination
172192 ) {
173193 // Build modified route segments by reusing cached baseline segments
174194 List <GraphPath <State , Edge , Vertex >> modifiedSegments = buildModifiedSegments (
@@ -217,7 +237,7 @@ private InsertionCandidate evaluateInsertion(
217237 pickupPos ,
218238 dropoffPos ,
219239 modifiedSegments ,
220- baselineDuration ,
240+ durationBetweenOriginAndDestination ,
221241 totalDuration
222242 );
223243 }
0 commit comments