1 package org.opentrafficsim.road.gtu.lane.perception; 2 3 import java.util.Map; 4 import java.util.SortedSet; 5 6 import org.djunits.value.vdouble.scalar.Length; 7 import org.opentrafficsim.core.gtu.GtuException; 8 import org.opentrafficsim.core.gtu.GtuType; 9 import org.opentrafficsim.core.gtu.RelativePosition; 10 import org.opentrafficsim.core.network.route.Route; 11 import org.opentrafficsim.road.gtu.lane.LaneBasedGtu; 12 import org.opentrafficsim.road.network.lane.LanePosition; 13 import org.opentrafficsim.road.network.lane.object.LaneBasedObject; 14 15 /** 16 * Interface for lane structures. 17 * <p> 18 * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br> 19 * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>. 20 * </p> 21 * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a> 22 * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a> 23 * @author <a href="https://github.com/wjschakel">Wouter Schakel</a> 24 */ 25 public interface LaneStructure 26 { 27 28 /** 29 * Updates the underlying structure shifting the root position to the input. 30 * @param pos LanePosition; current position of the GTU 31 * @param route Route; current route of the GTU 32 * @param gtuType GtuType; GTU type 33 * @throws GtuException on a problem while updating the structure 34 */ 35 void update(LanePosition pos, Route route, GtuType gtuType) throws GtuException; 36 37 /** 38 * Returns the root record. 39 * @return LaneRecord; root record 40 */ 41 LaneStructureRecord getRootRecord(); 42 43 /** 44 * Returns the extended cross-section, which includes all lanes for which a first record is present. 45 * @return SortedSet; the cross-section 46 */ 47 SortedSet<RelativeLane> getExtendedCrossSection(); 48 49 /** 50 * Returns the first record on the given lane. This is often a record in the current cross section, but it may be one 51 * downstream for a lane that starts further downstream. 52 * @param lane RelativeLane; lane 53 * @return first record on the given lane, or {@code null} if no such record 54 */ 55 LaneStructureRecord getFirstRecord(RelativeLane lane); 56 57 /** 58 * Retrieve objects of a specific type. Returns objects over a maximum length of the look ahead distance downstream from the 59 * relative position, or as far as the lane structure goes. 60 * @param clazz Class<T>; class of objects to find 61 * @param gtu LaneBasedGtu; gtu 62 * @param pos RelativePosition.TYPE; relative position to start search from 63 * @param <T> type of objects to find 64 * @return Map; sorted set of objects of requested type per lane 65 * @throws GtuException if lane is not in current set 66 */ 67 <T extends LaneBasedObject> Map<RelativeLane, SortedSet<Entry<T>>> getDownstreamObjects(Class<T> clazz, LaneBasedGtu gtu, 68 RelativePosition.Type pos) throws GtuException; 69 70 /** 71 * Retrieve objects on a lane of a specific type. Returns objects over a maximum length of the look ahead distance 72 * downstream from the relative position, or as far as the lane structure goes. 73 * @param lane RelativeLane; lane 74 * @param clazz Class<T>; class of objects to find 75 * @param gtu LaneBasedGtu; gtu 76 * @param pos RelativePosition.TYPE; relative position to start search from 77 * @param <T> type of objects to find 78 * @return SortedSet; sorted set of objects of requested type 79 * @throws GtuException if lane is not in current set 80 */ 81 <T extends LaneBasedObject> SortedSet<Entry<T>> getDownstreamObjects(RelativeLane lane, Class<T> clazz, LaneBasedGtu gtu, 82 RelativePosition.Type pos) throws GtuException; 83 84 /** 85 * Retrieve objects of a specific type. Returns objects over a maximum length of the look ahead distance downstream from the 86 * relative position, or as far as the lane structure goes. Objects on links not on the route are ignored. 87 * @param clazz Class<T>; class of objects to find 88 * @param gtu LaneBasedGtu; gtu 89 * @param pos RelativePosition.TYPE; relative position to start search from 90 * @param <T> type of objects to find 91 * @param route Route; the route 92 * @return SortedSet; sorted set of objects of requested type per lane 93 * @throws GtuException if lane is not in current set 94 */ 95 <T extends LaneBasedObject> Map<RelativeLane, SortedSet<Entry<T>>> getDownstreamObjectsOnRoute(Class<T> clazz, 96 LaneBasedGtu gtu, RelativePosition.Type pos, Route route) throws GtuException; 97 98 /** 99 * Retrieve objects on a lane of a specific type. Returns objects over a maximum length of the look ahead distance 100 * downstream from the relative position, or as far as the lane structure goes. Objects on links not on the route are 101 * ignored. 102 * @param lane RelativeLane; lane 103 * @param clazz Class<T>; class of objects to find 104 * @param gtu LaneBasedGtu; gtu 105 * @param pos RelativePosition.TYPE; relative position to start search from 106 * @param <T> type of objects to find 107 * @param route Route; the route 108 * @return SortedSet; sorted set of objects of requested type 109 * @throws GtuException if lane is not in current set 110 */ 111 <T extends LaneBasedObject> SortedSet<Entry<T>> getDownstreamObjectsOnRoute(RelativeLane lane, Class<T> clazz, 112 LaneBasedGtu gtu, RelativePosition.Type pos, Route route) throws GtuException; 113 114 /** 115 * Retrieve objects on a lane of a specific type. Returns upstream objects from the relative position for as far as the lane 116 * structure goes. Distances to upstream objects are given as positive values. 117 * @param lane RelativeLane; lane 118 * @param clazz Class<T>; class of objects to find 119 * @param gtu LaneBasedGtu; gtu 120 * @param pos RelativePosition.TYPE; relative position to start search from 121 * @param <T> type of objects to find 122 * @return SortedSet; sorted set of objects of requested type 123 * @throws GtuException if lane is not in current set 124 */ 125 <T extends LaneBasedObject> SortedSet<Entry<T>> getUpstreamObjects(RelativeLane lane, Class<T> clazz, LaneBasedGtu gtu, 126 RelativePosition.Type pos) throws GtuException; 127 128 /** 129 * Wrapper to hold lane-based object and it's distance. 130 * <p> 131 * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. 132 * <br> 133 * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>. 134 * </p> 135 * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a> 136 * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a> 137 * @author <a href="https://github.com/wjschakel">Wouter Schakel</a> 138 * @param <T> class of lane based object contained 139 */ 140 class Entry<T extends LaneBasedObject> implements Comparable<Entry<T>> 141 { 142 143 /** Distance to lane based object. */ 144 private final Length distance; 145 146 /** Lane based object. */ 147 private final T laneBasedObject; 148 149 /** 150 * @param distance Length; distance to lane based object 151 * @param laneBasedObject T; lane based object 152 */ 153 public Entry(final Length distance, final T laneBasedObject) 154 { 155 this.distance = distance; 156 this.laneBasedObject = laneBasedObject; 157 } 158 159 /** 160 * @return distance. 161 */ 162 public final Length getDistance() 163 { 164 return this.distance; 165 } 166 167 /** 168 * @return laneBasedObject. 169 */ 170 public final T getLaneBasedObject() 171 { 172 return this.laneBasedObject; 173 } 174 175 /** {@inheritDoc} */ 176 @Override 177 public final int hashCode() 178 { 179 final int prime = 31; 180 int result = 1; 181 result = prime * result + ((this.distance == null) ? 0 : this.distance.hashCode()); 182 result = prime * result + ((this.laneBasedObject == null) ? 0 : this.laneBasedObject.hashCode()); 183 return result; 184 } 185 186 /** {@inheritDoc} */ 187 @Override 188 public final boolean equals(final Object obj) 189 { 190 if (this == obj) 191 { 192 return true; 193 } 194 if (obj == null) 195 { 196 return false; 197 } 198 if (getClass() != obj.getClass()) 199 { 200 return false; 201 } 202 Entry<?> other = (Entry<?>) obj; 203 if (this.distance == null) 204 { 205 if (other.distance != null) 206 { 207 return false; 208 } 209 } 210 else if (!this.distance.equals(other.distance)) 211 { 212 return false; 213 } 214 if (this.laneBasedObject == null) 215 { 216 if (other.laneBasedObject != null) 217 { 218 return false; 219 } 220 } 221 // laneBasedObject does not implement equals... 222 else if (!this.laneBasedObject.equals(other.laneBasedObject)) 223 { 224 return false; 225 } 226 return true; 227 } 228 229 /** {@inheritDoc} */ 230 @Override 231 public final int compareTo(final Entry<T> arg) 232 { 233 int d = this.distance.compareTo(arg.distance); 234 if (d != 0 || this.laneBasedObject.equals(arg.laneBasedObject)) 235 { 236 return d; // different distance (-1 or 1), or same distance but also equal lane based object (0) 237 } 238 return 1; // same distance, unequal lane based object (1) 239 } 240 241 /** {@inheritDoc} */ 242 @Override 243 public final String toString() 244 { 245 return "LaneStructure.Entry [distance=" + this.distance + ", laneBasedObject=" + this.laneBasedObject + "]"; 246 } 247 248 } 249 250 }