View Javadoc
1   package org.opentrafficsim.road.gtu.lane.perception.mental;
2   
3   import java.util.Iterator;
4   import java.util.LinkedHashMap;
5   import java.util.Map;
6   
7   import org.djunits.value.vdouble.scalar.Length;
8   import org.opentrafficsim.base.parameters.ParameterException;
9   import org.opentrafficsim.base.parameters.Parameters;
10  import org.opentrafficsim.core.gtu.GtuException;
11  import org.opentrafficsim.core.gtu.RelativePosition;
12  import org.opentrafficsim.road.gtu.lane.LaneBasedGtu;
13  import org.opentrafficsim.road.gtu.lane.perception.LanePerception;
14  import org.opentrafficsim.road.gtu.lane.perception.RelativeLane;
15  import org.opentrafficsim.road.network.lane.object.Distraction;
16  
17  /**
18   * Task-demand for road-side distraction.
19   * <p>
20   * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
21   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
22   * </p>
23   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
24   * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
25   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
26   */
27  public class TaskRoadSideDistraction extends AbstractTask
28  {
29  
30      /** Odometer values at distraction. */
31      private Map<Distraction, Double> odos = new LinkedHashMap<>();
32  
33      /** Constructor. */
34      public TaskRoadSideDistraction()
35      {
36          super("road-side distraction");
37      }
38  
39      /** {@inheritDoc} */
40      @Override
41      public double calculateTaskDemand(final LanePerception perception, final LaneBasedGtu gtu, final Parameters parameters)
42              throws ParameterException, GtuException
43      {
44          double odo = gtu.getOdometer().si;
45  
46          for (RelativeLane lane : perception.getLaneStructure().getRootCrossSection())
47          {
48              for (org.opentrafficsim.road.gtu.lane.perception.structure.LaneStructure.Entry<
49                      Distraction> distraction : perception.getLaneStructure().getDownstreamObjects(lane, Distraction.class,
50                              RelativePosition.DRIVER, false))
51              {
52                  this.odos.put(distraction.object(), odo + distraction.distance().si);
53              }
54          }
55  
56          // loop over all distractions in odos
57          Iterator<Distraction> it = this.odos.keySet().iterator();
58          double demand = 0.0;
59          while (it.hasNext())
60          {
61              Distraction next = it.next();
62              Double distraction = next.getDistraction(Length.instantiateSI(odo - this.odos.get(next)));
63              if (distraction == null)
64              {
65                  it.remove();
66              }
67              else
68              {
69                  demand += distraction;
70              }
71          }
72          return demand;
73      }
74  
75  }