1 package org.opentrafficsim.road.gtu.lane.perception; 2 3 import java.util.Set; 4 import java.util.TreeMap; 5 6 import org.djunits.value.vdouble.scalar.Length; 7 import org.opentrafficsim.road.network.lane.object.LaneBasedObject; 8 9 /** 10 * Provides 'friendly' access to the LaneStructure from a GTU point of view. 11 * <p> 12 * Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br> 13 * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>. 14 * </p> 15 * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $, 16 * initial version Sep 9, 2016 <br> 17 * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a> 18 * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a> 19 * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a> 20 */ 21 public interface EnvironmentState 22 { 23 24 /** 25 * Retrieve objects on a lane of a specific type. 26 * @param viewingDirection direction to look at 27 * @param relativeLane lane to look at 28 * @param clazz class of objects to obtain 29 * @param <T> type of the objects 30 * @return Sorted set of objects of requested class 31 */ 32 <T extends LaneBasedObject> TreeMap<Length, Set<T>> getSortedObjects(final ViewingDirection viewingDirection, 33 final RelativeLane relativeLane, final Class<T> clazz); 34 35 /** 36 * Direction to look. 37 * <p> 38 * Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. 39 * <br> 40 * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>. 41 * <p> 42 * @version $Revision$, $LastChangedDate$, by $Author$, initial version 20 okt. 2016 <br> 43 * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a> 44 * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a> 45 * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a> 46 */ 47 enum ViewingDirection 48 { 49 /** Forward direction. */ 50 FORWARD, 51 52 /** Backward direction. */ 53 BACKWARD; 54 } 55 56 /** 57 * <p> 58 * Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. 59 * <br> 60 * BSD-style license. See <a href="http://opentrafficsim.org/docs/current/license.html">OpenTrafficSim License</a>. 61 * <p> 62 * @version $Revision$, $LastChangedDate$, by $Author$, initial version Sep 15, 2016 <br> 63 * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a> 64 * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a> 65 * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a> 66 * @param <T> class of lane based object contained 67 */ 68 class Entry<T extends LaneBasedObject> implements Comparable<Entry<T>> 69 { 70 71 /** Distance to lane based object. */ 72 private final Length distance; 73 74 /** Lane based object. */ 75 private final T laneBasedObject; 76 77 /** 78 * @param distance distance to lane based object 79 * @param laneBasedObject lane based object 80 */ 81 public Entry(final Length distance, final T laneBasedObject) 82 { 83 super(); 84 this.distance = distance; 85 this.laneBasedObject = laneBasedObject; 86 } 87 88 /** 89 * @return distance. 90 */ 91 public final Length getDistance() 92 { 93 return this.distance; 94 } 95 96 /** 97 * @return laneBasedObject. 98 */ 99 public final T getLaneBasedObject() 100 { 101 return this.laneBasedObject; 102 } 103 104 /** {@inheritDoc} */ 105 @Override 106 public final int hashCode() 107 { 108 final int prime = 31; 109 int result = 1; 110 result = prime * result + ((this.distance == null) ? 0 : this.distance.hashCode()); 111 result = prime * result + ((this.laneBasedObject == null) ? 0 : this.laneBasedObject.hashCode()); 112 return result; 113 } 114 115 /** {@inheritDoc} */ 116 @Override 117 public final boolean equals(final Object obj) 118 { 119 if (this == obj) 120 { 121 return true; 122 } 123 if (obj == null) 124 { 125 return false; 126 } 127 if (getClass() != obj.getClass()) 128 { 129 return false; 130 } 131 Entry<?> other = (Entry<?>) obj; 132 if (this.distance == null) 133 { 134 if (other.distance != null) 135 { 136 return false; 137 } 138 } 139 else if (!this.distance.equals(other.distance)) 140 { 141 return false; 142 } 143 if (this.laneBasedObject == null) 144 { 145 if (other.laneBasedObject != null) 146 { 147 return false; 148 } 149 } 150 // laneBasedObject does not implement equals... 151 else if (!this.laneBasedObject.equals(other.laneBasedObject)) 152 { 153 return false; 154 } 155 return true; 156 } 157 158 /** {@inheritDoc} */ 159 @Override 160 public final int compareTo(final Entry<T> arg) 161 { 162 int d = this.distance.compareTo(arg.distance); 163 if (d != 0 || this.laneBasedObject.equals(arg.laneBasedObject)) 164 { 165 return d; // different distance (-1 or 1), or same distance but also equal lane based object (0) 166 } 167 return 1; // same distance, unequal lane based object (1) 168 } 169 170 /** {@inheritDoc} */ 171 @Override 172 public String toString() 173 { 174 return "EnvironmentState.Entry [distance=" + this.distance + ", laneBasedObject=" + this.laneBasedObject + "]"; 175 } 176 177 } 178 }