View Javadoc
1   package org.opentrafficsim.road.gtu.lane.perception.mental.channel;
2   
3   import java.util.Iterator;
4   
5   import org.djunits.value.vdouble.scalar.Length;
6   import org.djutils.exceptions.Throw;
7   import org.djutils.exceptions.Try;
8   import org.opentrafficsim.base.parameters.ParameterException;
9   import org.opentrafficsim.base.parameters.ParameterTypeLength;
10  import org.opentrafficsim.base.parameters.ParameterTypes;
11  import org.opentrafficsim.core.gtu.RelativePosition;
12  import org.opentrafficsim.core.gtu.perception.AbstractPerceptionCategory;
13  import org.opentrafficsim.core.network.LateralDirectionality;
14  import org.opentrafficsim.core.network.route.Route;
15  import org.opentrafficsim.road.gtu.lane.LaneBasedGtu;
16  import org.opentrafficsim.road.gtu.lane.perception.LanePerception;
17  import org.opentrafficsim.road.gtu.lane.perception.PerceptionCollectable;
18  import org.opentrafficsim.road.gtu.lane.perception.PerceptionReiterable;
19  import org.opentrafficsim.road.gtu.lane.perception.RelativeLane;
20  import org.opentrafficsim.road.gtu.lane.perception.categories.DirectIntersectionPerception;
21  import org.opentrafficsim.road.gtu.lane.perception.categories.IntersectionPerception;
22  import org.opentrafficsim.road.gtu.lane.perception.categories.neighbors.Anticipation;
23  import org.opentrafficsim.road.gtu.lane.perception.categories.neighbors.Estimation;
24  import org.opentrafficsim.road.gtu.lane.perception.categories.neighbors.PerceivedGtuType;
25  import org.opentrafficsim.road.gtu.lane.perception.categories.neighbors.PerceivedGtuType.AnticipationPerceivedGtuType;
26  import org.opentrafficsim.road.gtu.lane.perception.object.PerceivedConflict;
27  import org.opentrafficsim.road.gtu.lane.perception.object.PerceivedTrafficLight;
28  import org.opentrafficsim.road.gtu.lane.perception.structure.NavigatingIterable.Entry;
29  import org.opentrafficsim.road.gtu.lane.tactical.lmrs.PerceivedTrafficLightChannel;
30  import org.opentrafficsim.road.network.lane.conflict.Conflict;
31  import org.opentrafficsim.road.network.lane.object.trafficlight.TrafficLight;
32  
33  /**
34   * This class is highly similar to {@link DirectIntersectionPerception} but takes perception delay for conflicting GTUs and
35   * traffic light colors from perception channels.
36   * <p>
37   * Copyright (c) 2024-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
38   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
39   * </p>
40   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
41   */
42  public class IntersectionPerceptionChannel extends AbstractPerceptionCategory<LaneBasedGtu, LanePerception>
43          implements IntersectionPerception
44  {
45  
46      /** Mental module. */
47      private final ChannelMental mental;
48  
49      /** Estimation. */
50      private final Estimation estimation;
51  
52      /** Anticipation. */
53      private final Anticipation anticipation;
54  
55      /** Look ahead parameter type. */
56      protected static final ParameterTypeLength LOOKAHEAD = ParameterTypes.LOOKAHEAD;
57  
58      /**
59       * Constructor.
60       * @param perception perception.
61       * @param estimation estimation.
62       * @param anticipation anticipation.
63       */
64      public IntersectionPerceptionChannel(final LanePerception perception, final Estimation estimation,
65              final Anticipation anticipation)
66      {
67          super(perception);
68          Throw.when(!(getPerception().getMental().get() instanceof ChannelMental), IllegalArgumentException.class,
69                  "Mental module is not channel based.");
70          this.mental = (ChannelMental) getPerception().getMental().get();
71          this.estimation = estimation;
72          this.anticipation = anticipation;
73      }
74  
75      @Override
76      public final PerceptionCollectable<PerceivedTrafficLight, TrafficLight> getTrafficLights(final RelativeLane lane)
77      {
78          return computeIfAbsent("trafficLights", () -> computeTrafficLights(lane), lane);
79      }
80  
81      @Override
82      public final PerceptionCollectable<PerceivedConflict, Conflict> getConflicts(final RelativeLane lane)
83      {
84          return computeIfAbsent("conflicts", () -> computeConflicts(lane), lane);
85      }
86  
87      @Override
88      public final boolean isAlongsideConflictLeft()
89      {
90          return computeIfAbsent("alongside", () -> computeConflictAlongside(LateralDirectionality.LEFT),
91                  LateralDirectionality.LEFT);
92      }
93  
94      @Override
95      public final boolean isAlongsideConflictRight()
96      {
97          return computeIfAbsent("alongside", () -> computeConflictAlongside(LateralDirectionality.RIGHT),
98                  LateralDirectionality.RIGHT);
99      }
100 
101     /**
102      * Compute traffic lights.
103      * @param lane lane
104      * @return PerceptionCollectable of traffic lights
105      */
106     private PerceptionCollectable<PerceivedTrafficLight, TrafficLight> computeTrafficLights(final RelativeLane lane)
107     {
108         Iterable<Entry<TrafficLight>> iterable = Try.assign(() ->
109         {
110             return getPerception().getLaneStructure().getDownstreamObjects(lane, TrafficLight.class, RelativePosition.FRONT,
111                     true);
112         }, "Unable to get downstream traffic lights from LaneStructure");
113         Route route =
114                 Try.assign(() -> getPerception().getGtu().getStrategicalPlanner().getRoute().get(), "Unable to obtain route");
115         return new PerceptionReiterable<>(getGtu(), iterable, (trafficLight, distance) ->
116         {
117             return new PerceivedTrafficLightChannel(trafficLight, distance,
118                     trafficLight.canTurnOnRed(route, getGtu().getType()),
119                     () -> IntersectionPerceptionChannel.this.mental.getPerceptionDelay(ChannelTask.FRONT));
120         });
121     }
122 
123     /**
124      * Compute conflicts.
125      * @param lane lane
126      * @return PerceptionCollectable of conflicts
127      */
128     private PerceptionCollectable<PerceivedConflict, Conflict> computeConflicts(final RelativeLane lane)
129     {
130         Iterable<Entry<Conflict>> iterable = Try.assign(() -> getPerception().getLaneStructure().getDownstreamObjects(lane,
131                 Conflict.class, RelativePosition.FRONT, true), "Unable to get downstream conflicts from LaneStructure");
132         return new PerceptionReiterable<>(getGtu(), iterable, (conflict, distance) ->
133         {
134             Length lookAhead =
135                     Try.assign(() -> getGtu().getParameters().getParameter(LOOKAHEAD), "Parameter LOOKAHEAD not present.");
136             // TODO visibility
137             Length conflictingVisibility = lookAhead;
138             PerceivedConflict perceivedConflict;
139             PerceivedGtuType perceivedGtuType = new AnticipationPerceivedGtuType(IntersectionPerceptionChannel.this.estimation,
140                     IntersectionPerceptionChannel.this.anticipation,
141                     () -> IntersectionPerceptionChannel.this.mental.getPerceptionDelay(conflict));
142             perceivedConflict = PerceivedConflict.of(getGtu(), conflict, perceivedGtuType, distance, conflictingVisibility);
143             return perceivedConflict;
144         });
145     }
146 
147     /**
148      * Compute whether there is a conflict alongside.
149      * @param lat lateral directionality
150      * @return whether there is a conflict alongside
151      */
152     private boolean computeConflictAlongside(final LateralDirectionality lat)
153     {
154         try
155         {
156             RelativeLane lane = new RelativeLane(lat, 1);
157             if (getPerception().getLaneStructure().exists(lane))
158             {
159                 Iterator<Entry<Conflict>> conflicts = getPerception().getLaneStructure()
160                         .getUpstreamObjects(lane, Conflict.class, RelativePosition.FRONT).iterator();
161                 if (conflicts.hasNext())
162                 {
163                     Entry<Conflict> entry = conflicts.next();
164                     return entry.distance().si < entry.object().getLength().si + getGtu().getLength().si;
165                 }
166             }
167             return false;
168         }
169         catch (ParameterException exception)
170         {
171             throw new RuntimeException("Unexpected exception while computing conflict alongside.", exception);
172         }
173     }
174 
175     @Override
176     public final String toString()
177     {
178         return "IntersectionPerceptionChannel " + cacheAsString();
179     }
180 
181 }