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