1 package org.opentrafficsim.road.gtu.lane.perception.headway; 2 3 import org.djunits.value.vdouble.scalar.Acceleration; 4 import org.djunits.value.vdouble.scalar.Length; 5 import org.djunits.value.vdouble.scalar.Speed; 6 import org.djutils.exceptions.Throw; 7 import org.opentrafficsim.core.gtu.GtuException; 8 9 /** 10 * Container for a reference to information about a (lane based) GTU and a headway. The Headway can store information about GTUs 11 * or objects ahead of the reference GTU, behind the reference GTU, or (partially) parallel to the reference GTU. In addition to 12 * the (perceived) headway, several other pieces of information can be stored, such as (perceived) speed, (perceived) 13 * acceleration, (perceived) turn indicators, and (perceived) braking lights. <br> 14 * Special care must be taken in curves when perceiving headway of a GTU or object on an adjacent lane.The question is whether 15 * we perceive the parallel or ahead/behind based on a line perpendicular to the front/back of the GTU (rectangular), or 16 * perpendicular to the center line of the lane (wedge-shaped in case of a curve). The difficulty of a wedge-shaped situation is 17 * that reciprocity might be violated: in case of a clothoid, for instance, it is not sure that the point on the center line 18 * when projected from lane 1 to lane 2 is the same as the projection from lane 2 to lane 1. The same holds for shapes with 19 * sharp bends. Therefore, algorithms implementing headway should only project the <i>reference point</i> of the reference GTU 20 * on the center line of the adjacent lane, and then calculate the forward position and backward position on the adjacent lane 21 * based on the reference point. Still, our human perception of what is parallel and what not, is not reflected by fractional 22 * positions. 23 * <p> 24 * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br> 25 * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>. 26 * </p> 27 * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a> 28 * @author <a href="https://github.com/peter-knoppers">Peter Knoppers</a> 29 * @author <a href="https://github.com/wjschakel">Wouter Schakel</a> 30 */ 31 public abstract class AbstractHeadwayCopy extends AbstractHeadway 32 { 33 /** */ 34 private static final long serialVersionUID = 20160410L; 35 36 /** The id of the other object for comparison purposes, cannot be null. */ 37 private final String id; 38 39 /** The (perceived) length of the other object. Can be null if unknown. */ 40 private final Length length; 41 42 /** The (perceived) speed of the other object. v>0 seen from driver chair. Can be null if unknown. */ 43 private final Speed speed; 44 45 /** The (perceived) acceleration of the other object. Can be null if unknown. */ 46 private final Acceleration acceleration; 47 48 /** The object type. */ 49 private final ObjectType objectType; 50 51 /** 52 * Construct a new Headway information object, for an object in front, behind, or in parallel with us. <br> 53 * @param objectType the perceived object type, can be null if object type unknown. 54 * @param id the id of the object for comparison purposes, can not be null. 55 * @param distance the distance to the other object 56 * @param length the length of the other object, can be null if not applicable. 57 * @param overlapFront the front-front distance to the other object 58 * @param overlap the 'center' overlap with the other object 59 * @param overlapRear the rear-rear distance to the other object 60 * @param speed the (perceived) speed of the other object; can be null if unknown. 61 * @param acceleration the (perceived) acceleration of the other object; can be null if unknown. 62 * @throws GtuException when id is null, or parameters are inconsistent 63 */ 64 @SuppressWarnings("checkstyle:parameternumber") 65 private AbstractHeadwayCopy(final ObjectType objectType, final String id, final Length distance, final Length length, 66 final Speed speed, final Acceleration acceleration, final Length overlapFront, final Length overlap, 67 final Length overlapRear) throws GtuException 68 { 69 super(distance, overlapFront, overlap, overlapRear); 70 Throw.when(id == null, GtuException.class, "Object id of a headway cannot be null"); 71 this.id = id; 72 73 this.objectType = objectType; 74 this.length = length; 75 this.speed = speed; 76 this.acceleration = acceleration; 77 } 78 79 /** 80 * Construct a new Headway information object, for a moving object ahead of us or behind us. 81 * @param objectType the perceived object type, can be null if object type unknown. 82 * @param id the id of the object for comparison purposes, can not be null. 83 * @param distance if this constructor is used, distance cannot be null. 84 * @param speed the (perceived) speed of the other object; can be null if unknown. 85 * @param acceleration the (perceived) acceleration of the other object; can be null if unknown. 86 * @throws GtuException when id is null, or parameters are inconsistent 87 */ 88 public AbstractHeadwayCopy(final ObjectType objectType, final String id, final Length distance, final Speed speed, 89 final Acceleration acceleration) throws GtuException 90 { 91 this(objectType, id, distance, null, speed, acceleration, null, null, null); 92 } 93 94 /** 95 * Construct a new Headway information object, for a non-moving object ahead of us or behind us. 96 * @param objectType the perceived object type, can be null if object type unknown. 97 * @param id the id of the object for comparison purposes, can not be null. 98 * @param distance if this constructor is used, distance cannot be null. 99 * @throws GtuException when id is null, or parameters are inconsistent 100 */ 101 public AbstractHeadwayCopy(final ObjectType objectType, final String id, final Length distance) throws GtuException 102 { 103 this(objectType, id, distance, null, Speed.ZERO, Acceleration.ZERO, null, null, null); 104 } 105 106 /** 107 * Construct a new Headway information object, for a moving object parallel with us. 108 * @param objectType the perceived object type, can be null if object type unknown. 109 * @param id the id of the object for comparison purposes, can not be null. 110 * @param overlapFront the front-front distance to the other object; if this constructor is used, this value cannot be null. 111 * @param overlap the 'center' overlap with the other object; if this constructor is used, this value cannot be null. 112 * @param overlapRear the rear-rear distance to the other object; if this constructor is used, this value cannot be null. 113 * @param speed the (perceived) speed of the other object; can be null if unknown. 114 * @param acceleration the (perceived) acceleration of the other object; can be null if unknown. 115 * @throws GtuException when id is null, or parameters are inconsistent 116 */ 117 @SuppressWarnings("checkstyle:parameternumber") 118 public AbstractHeadwayCopy(final ObjectType objectType, final String id, final Length overlapFront, final Length overlap, 119 final Length overlapRear, final Speed speed, final Acceleration acceleration) throws GtuException 120 { 121 this(objectType, id, null, null, speed, acceleration, overlapFront, overlap, overlapRear); 122 } 123 124 /** 125 * Construct a new Headway information object, for a non-moving object parallel with us. 126 * @param objectType the perceived object type, can be null if object type unknown. 127 * @param id the id of the object for comparison purposes, can not be null. 128 * @param overlapFront the front-front distance to the other object; if this constructor is used, this value cannot be null. 129 * @param overlap the 'center' overlap with the other object; if this constructor is used, this value cannot be null. 130 * @param overlapRear the rear-rear distance to the other object; if this constructor is used, this value cannot be null. 131 * @throws GtuException when id is null, or parameters are inconsistent 132 */ 133 public AbstractHeadwayCopy(final ObjectType objectType, final String id, final Length overlapFront, final Length overlap, 134 final Length overlapRear) throws GtuException 135 { 136 this(objectType, id, null, null, null, null, overlapFront, overlap, overlapRear); 137 } 138 139 /** 140 * Construct a new Headway information object, for a moving object ahead of us or behind us. 141 * @param objectType the perceived object type, can be null if object type unknown. 142 * @param id the id of the object for comparison purposes, can not be null. 143 * @param distance if this constructor is used, distance cannot be null. 144 * @param length if this constructor is used, length cannot be null. 145 * @param speed the (perceived) speed of the other object; can be null if unknown. 146 * @param acceleration the (perceived) acceleration of the other object; can be null if unknown. 147 * @throws GtuException when id is null, or parameters are inconsistent 148 */ 149 public AbstractHeadwayCopy(final ObjectType objectType, final String id, final Length distance, final Length length, 150 final Speed speed, final Acceleration acceleration) throws GtuException 151 { 152 this(objectType, id, distance, length, speed, acceleration, null, null, null); 153 Throw.whenNull(length, "Length may not be null."); 154 } 155 156 /** 157 * Construct a new Headway information object, for a non-moving object ahead of us or behind us. 158 * @param objectType the perceived object type, can be null if object type unknown. 159 * @param id the id of the object for comparison purposes, can not be null. 160 * @param distance if this constructor is used, distance cannot be null. 161 * @param length if this constructor is used, length cannot be null. 162 * @throws GtuException when id is null, or parameters are inconsistent 163 */ 164 public AbstractHeadwayCopy(final ObjectType objectType, final String id, final Length distance, final Length length) 165 throws GtuException 166 { 167 this(objectType, id, distance, length, null, null, null, null, null); 168 Throw.whenNull(length, "Length may not be null."); 169 } 170 171 /** 172 * Construct a new Headway information object, for a moving object parallel with us. 173 * @param objectType the perceived object type, can be null if object type unknown. 174 * @param id the id of the object for comparison purposes, can not be null. 175 * @param overlapFront the front-front distance to the other object; if this constructor is used, this value cannot be null. 176 * @param overlap the 'center' overlap with the other object; if this constructor is used, this value cannot be null. 177 * @param overlapRear the rear-rear distance to the other object; if this constructor is used, this value cannot be null. 178 * @param length if this constructor is used, length cannot be null. 179 * @param speed the (perceived) speed of the other object; can be null if unknown. 180 * @param acceleration the (perceived) acceleration of the other object; can be null if unknown. 181 * @throws GtuException when id is null, or parameters are inconsistent 182 */ 183 @SuppressWarnings("checkstyle:parameternumber") 184 public AbstractHeadwayCopy(final ObjectType objectType, final String id, final Length overlapFront, final Length overlap, 185 final Length overlapRear, final Length length, final Speed speed, final Acceleration acceleration) 186 throws GtuException 187 { 188 this(objectType, id, null, length, speed, acceleration, overlapFront, overlap, overlapRear); 189 Throw.whenNull(length, "Length may not be null."); 190 } 191 192 /** 193 * Construct a new Headway information object, for a non-moving object parallel with us. 194 * @param objectType the perceived object type, can be null if object type unknown. 195 * @param id the id of the object for comparison purposes, can not be null. 196 * @param overlapFront the front-front distance to the other object; if this constructor is used, this value cannot be null. 197 * @param overlap the 'center' overlap with the other object; if this constructor is used, this value cannot be null. 198 * @param overlapRear the rear-rear distance to the other object; if this constructor is used, this value cannot be null. 199 * @param length if this constructor is used, length cannot be null. 200 * @throws GtuException when id is null, or parameters are inconsistent 201 */ 202 public AbstractHeadwayCopy(final ObjectType objectType, final String id, final Length overlapFront, final Length overlap, 203 final Length overlapRear, final Length length) throws GtuException 204 { 205 this(objectType, id, null, length, null, null, overlapFront, overlap, overlapRear); 206 Throw.whenNull(length, "Length may not be null."); 207 } 208 209 @Override 210 public final String getId() 211 { 212 return this.id; 213 } 214 215 @Override 216 public final Length getLength() 217 { 218 return this.length; 219 } 220 221 @Override 222 public final Speed getSpeed() 223 { 224 return this.speed; 225 } 226 227 @Override 228 public final ObjectType getObjectType() 229 { 230 return this.objectType; 231 } 232 233 @Override 234 public final Acceleration getAcceleration() 235 { 236 return this.acceleration; 237 } 238 239 @Override 240 public int hashCode() 241 { 242 final int prime = 31; 243 int result = super.hashCode(); 244 result = prime * result + ((this.acceleration == null) ? 0 : this.acceleration.hashCode()); 245 result = prime * result + ((this.id == null) ? 0 : this.id.hashCode()); 246 result = prime * result + ((this.length == null) ? 0 : this.length.hashCode()); 247 result = prime * result + ((this.objectType == null) ? 0 : this.objectType.hashCode()); 248 result = prime * result + ((this.speed == null) ? 0 : this.speed.hashCode()); 249 return result; 250 } 251 252 @Override 253 public boolean equals(final Object obj) 254 { 255 if (this == obj) 256 { 257 return true; 258 } 259 if (!super.equals(obj)) 260 { 261 return false; 262 } 263 if (getClass() != obj.getClass()) 264 { 265 return false; 266 } 267 AbstractHeadwayCopy other = (AbstractHeadwayCopy) obj; 268 if (this.acceleration == null) 269 { 270 if (other.acceleration != null) 271 { 272 return false; 273 } 274 } 275 else if (!this.acceleration.equals(other.acceleration)) 276 { 277 return false; 278 } 279 if (this.id == null) 280 { 281 if (other.id != null) 282 { 283 return false; 284 } 285 } 286 else if (!this.id.equals(other.id)) 287 { 288 return false; 289 } 290 if (this.length == null) 291 { 292 if (other.length != null) 293 { 294 return false; 295 } 296 } 297 else if (!this.length.equals(other.length)) 298 { 299 return false; 300 } 301 if (this.objectType != other.objectType) 302 { 303 return false; 304 } 305 if (this.speed == null) 306 { 307 if (other.speed != null) 308 { 309 return false; 310 } 311 } 312 else if (!this.speed.equals(other.speed)) 313 { 314 return false; 315 } 316 return true; 317 } 318 319 }