1 package org.opentrafficsim.road.gtu.generator;
2
3 import java.util.Set;
4 import java.util.SortedSet;
5
6 import org.djunits.unit.DurationUnit;
7 import org.djunits.unit.LengthUnit;
8 import org.djunits.value.vdouble.scalar.Duration;
9 import org.djunits.value.vdouble.scalar.Length;
10 import org.djunits.value.vdouble.scalar.Speed;
11 import org.opentrafficsim.core.gtu.GtuException;
12 import org.opentrafficsim.core.network.NetworkException;
13 import org.opentrafficsim.road.gtu.generator.LaneBasedGtuGenerator.Placement;
14 import org.opentrafficsim.road.gtu.generator.LaneBasedGtuGenerator.RoomChecker;
15 import org.opentrafficsim.road.gtu.generator.characteristics.LaneBasedGtuCharacteristics;
16 import org.opentrafficsim.road.gtu.lane.perception.headway.HeadwayGtu;
17 import org.opentrafficsim.road.network.lane.LanePosition;
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 public class TtcRoomChecker implements RoomChecker
35 {
36
37
38 private final Duration ttc;
39
40
41
42
43
44 public TtcRoomChecker(final Duration ttc)
45 {
46 this.ttc = ttc;
47 }
48
49
50 @Override
51 public final Placement canPlace(final SortedSet<HeadwayGtu> leaders, final LaneBasedGtuCharacteristics characteristics,
52 final Duration since, final Set<LanePosition> initialPosition) throws NetworkException, GtuException
53 {
54 Speed speedLimit = initialPosition.iterator().next().getLane().getSpeedLimit(characteristics.getGtuType());
55 Speed desiredSpeedProxy = Speed.min(characteristics.getMaximumSpeed(), speedLimit);
56 if (leaders.isEmpty())
57 {
58 return new Placement(desiredSpeedProxy, initialPosition);
59 }
60 HeadwayGtu leader = leaders.first();
61 Speed speed = Speed.min(leader.getSpeed(), desiredSpeedProxy);
62 for (LanePosition dlp : initialPosition)
63 {
64 if (dlp.getLane().getType().isCompatible(characteristics.getGtuType()))
65 {
66 speed = Speed.min(speed, dlp.getLane().getSpeedLimit(characteristics.getGtuType()));
67 }
68 }
69 if ((speed.le(leader.getSpeed()) || leader.getDistance().divide(speed.minus(leader.getSpeed())).gt(this.ttc)) && leader
70 .getDistance().gt(speed.times(new Duration(1.0, DurationUnit.SI)).plus(new Length(3.0, LengthUnit.SI))))
71 {
72 return new Placement(speed, initialPosition);
73 }
74 return Placement.NO;
75 }
76
77
78
79
80
81 public final Duration getTtc()
82 {
83 return this.ttc;
84 }
85
86
87 @Override
88 public final String toString()
89 {
90 return "TTCRoomChecker [ttc=" + this.ttc + "]";
91 }
92
93 }