View Javadoc
1   package org.opentrafficsim.road.gtu.lane.object;
2   
3   import java.util.HashMap;
4   import java.util.HashSet;
5   import java.util.Map;
6   import java.util.Set;
7   
8   import javax.media.j3d.Bounds;
9   import javax.naming.NamingException;
10  import javax.vecmath.Point3d;
11  
12  import nl.tudelft.simulation.dsol.SimRuntimeException;
13  import nl.tudelft.simulation.dsol.formalisms.eventscheduling.SimEvent;
14  import nl.tudelft.simulation.language.d3.BoundingBox;
15  
16  import org.djunits.unit.LengthUnit;
17  import org.djunits.value.vdouble.scalar.Length;
18  import org.djunits.value.vdouble.scalar.Speed;
19  import org.djunits.value.vdouble.scalar.Time;
20  import org.opentrafficsim.core.dsol.OTSDEVSSimulatorInterface;
21  import org.opentrafficsim.core.dsol.OTSSimTimeDouble;
22  import org.opentrafficsim.core.geometry.OTSGeometryException;
23  import org.opentrafficsim.core.gtu.AbstractGTU;
24  import org.opentrafficsim.core.gtu.GTUDirectionality;
25  import org.opentrafficsim.core.gtu.GTUException;
26  import org.opentrafficsim.core.gtu.GTUType;
27  import org.opentrafficsim.core.gtu.RelativePosition;
28  import org.opentrafficsim.core.gtu.RelativePosition.TYPE;
29  import org.opentrafficsim.core.gtu.behavioralcharacteristics.BehavioralCharacteristics;
30  import org.opentrafficsim.core.network.NetworkException;
31  import org.opentrafficsim.core.network.OTSNetwork;
32  import org.opentrafficsim.road.gtu.lane.LaneBasedGTU;
33  import org.opentrafficsim.road.gtu.lane.tactical.LaneBasedTacticalPlanner;
34  import org.opentrafficsim.road.gtu.strategical.LaneBasedStrategicalPlanner;
35  import org.opentrafficsim.road.network.lane.CrossSectionElement;
36  import org.opentrafficsim.road.network.lane.CrossSectionLink;
37  import org.opentrafficsim.road.network.lane.Lane;
38  
39  /**
40   * <p>
41   * Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
42   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
43   * </p>
44   * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
45   * initial version Jan 6, 2016 <br>
46   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
47   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
48   */
49  public class AbstractTrafficLight extends AbstractGTU implements LaneBasedGTU
50  {
51      /** */
52      private static final long serialVersionUID = 1L;
53  
54      /** The lane of the block. */
55      private final Lane laneTL;
56  
57      /** The position of the block on the lane. */
58      private final Length positionTL;
59  
60      /** Light blocked or not? */
61      private boolean blocked = true;
62  
63      /** Blocking GTU type. */
64      private static final GTUType BLOCK_GTU;
65  
66      /** the dummy contour positions. */
67      private static final Set<RelativePosition> CONTOUR_POINTS = new HashSet<>();
68  
69      /** Relative position (0,0,0). */
70      public static final Map<RelativePosition.TYPE, RelativePosition> RELATIVE_POSITIONS = new HashMap<>();
71  
72      static
73      {
74          BLOCK_GTU = new GTUType("BLOCK");
75          RELATIVE_POSITIONS.put(RelativePosition.FRONT, new RelativePosition(Length.ZERO, Length.ZERO, Length.ZERO,
76                  RelativePosition.FRONT));
77          RELATIVE_POSITIONS.put(RelativePosition.REAR, new RelativePosition(Length.ZERO, Length.ZERO, Length.ZERO,
78                  RelativePosition.REAR));
79          RELATIVE_POSITIONS.put(RelativePosition.REFERENCE, RelativePosition.REFERENCE_POSITION);
80          RELATIVE_POSITIONS.put(RelativePosition.CENTER, RelativePosition.REFERENCE_POSITION);
81      }
82  
83      /**
84       * @param name the name or id of the traffic light
85       * @param lane The lane where the block has to be put
86       * @param position the position on the lane as a length
87       * @param simulator the simulator to avoid NullPointerExceptions
88       * @param network the network that the GTU is initially registered in
89       * @throws GTUException when GTU cannot be created.
90       * @throws NamingException if an error occurs when adding the animation handler
91       * @throws NetworkException when the GTU cannot be placed on the given lane
92       * @throws OTSGeometryException x
93       * @throws SimRuntimeException x
94       */
95      public AbstractTrafficLight(final String name, final Lane lane, final Length position,
96              final OTSDEVSSimulatorInterface simulator, final OTSNetwork network) throws GTUException, NetworkException,
97              NamingException, SimRuntimeException, OTSGeometryException
98      {
99          super(name, BLOCK_GTU, simulator, network);
100         this.positionTL = position;
101         this.laneTL = lane;
102 
103         // register the block on the lanes
104         lane.addGTU(this, position);
105     }
106 
107     /**
108      * @param blocked set blocked
109      */
110     public final void setBlocked(final boolean blocked)
111     {
112         try
113         {
114             if (this.blocked && !blocked)
115             {
116                 // remove ourselves from the lane
117                 this.laneTL.removeGTU(this);
118             }
119             else if (!this.blocked && blocked)
120             {
121                 // add ourselves to the lane
122                 this.laneTL.addGTU(this, this.positionTL);
123             }
124             this.blocked = blocked;
125         }
126         catch (GTUException exception)
127         {
128             exception.printStackTrace();
129         }
130     }
131 
132     /**
133      * @return blocked
134      */
135     public final boolean isBlocked()
136     {
137         return this.blocked;
138     }
139 
140     /**
141      * @return lane
142      */
143     public final Lane getLane()
144     {
145         return this.laneTL;
146     }
147 
148     /**
149      * @return positionTL
150      */
151     public final Length getPositionTL()
152     {
153         return this.positionTL;
154     }
155     
156     /* ========================================================================================================= */
157 
158     /** {@inheritDoc} */
159     @Override
160     public final Length getLength()
161     {
162         return Length.ZERO;
163     }
164 
165     /** {@inheritDoc} */
166     @Override
167     public final Length getWidth()
168     {
169         return Length.ZERO;
170     }
171 
172     /** {@inheritDoc} */
173     @Override
174     public final Speed getMaximumSpeed()
175     {
176         return Speed.ZERO;
177     }
178 
179     /** {@inheritDoc} */
180     @Override
181     public final RelativePosition getFront()
182     {
183         return RELATIVE_POSITIONS.get(RelativePosition.FRONT);
184     }
185 
186     /** {@inheritDoc} */
187     @Override
188     public final RelativePosition getRear()
189     {
190         return RELATIVE_POSITIONS.get(RelativePosition.REAR);
191     }
192 
193     /** {@inheritDoc} */
194     @Override
195     public final RelativePosition getCenter()
196     {
197         return RELATIVE_POSITIONS.get(RelativePosition.CENTER);
198     }
199 
200     /** {@inheritDoc} */
201     @Override
202     public final Map<TYPE, RelativePosition> getRelativePositions()
203     {
204         return RELATIVE_POSITIONS;
205     }
206 
207     /** {@inheritDoc} */
208     @Override
209     public final Bounds getBounds()
210     {
211         double dx = 2;
212         double dy = 1;
213         return new BoundingBox(new Point3d(-dx, -dy, 0.0), new Point3d(dx, dy, 0.0));
214     }
215 
216     /** {@inheritDoc} */
217     @Override
218     public final BehavioralCharacteristics getBehavioralCharacteristics()
219     {
220         return null;
221     }
222 
223     /** {@inheritDoc} */
224     @Override
225     public final Map<Lane, GTUDirectionality> getLanes()
226     {
227         return null;
228     }
229 
230     /** {@inheritDoc} */
231     @Override
232     public void enterLane(final Lane lane, final Length position, final GTUDirectionality gtuDirection) throws GTUException
233     {
234         // do nothing
235     }
236 
237     /** {@inheritDoc} */
238     @Override
239     public void leaveLane(final Lane lane)
240     {
241         // do nothing
242     }
243 
244     /** {@inheritDoc} */
245     @Override
246     public final Map<Lane, Length> positions(final RelativePosition relativePosition) throws GTUException
247     {
248         Map<Lane, Length> map = new HashMap<Lane, Length>();
249         map.put(this.laneTL, this.positionTL);
250         return map;
251     }
252 
253     /** {@inheritDoc} */
254     @Override
255     public final Map<Lane, Length> positions(final RelativePosition relativePosition, final Time when) throws GTUException
256     {
257         return positions(relativePosition);
258     }
259 
260     /** {@inheritDoc} */
261     @Override
262     public final Length position(final Lane lane, final RelativePosition relativePosition) throws GTUException
263     {
264         if (this.laneTL.equals(lane))
265         {
266             return this.positionTL;
267         }
268         throw new GTUException("TrafficLight " + this.getId() + " not on lane " + lane);
269     }
270 
271     /** {@inheritDoc} */
272     @Override
273     public final Length position(final Lane lane, final RelativePosition relativePosition, final Time when) throws GTUException
274     {
275         return position(lane, relativePosition);
276     }
277 
278     /** {@inheritDoc} */
279     @Override
280     public final Map<Lane, Double> fractionalPositions(final RelativePosition relativePosition) throws GTUException
281     {
282         Map<Lane, Double> map = new HashMap<Lane, Double>();
283         map.put(this.laneTL, this.positionTL.getSI() / this.laneTL.getLength().getSI());
284         return map;
285     }
286 
287     /** {@inheritDoc} */
288     @Override
289     public final Map<Lane, Double> fractionalPositions(final RelativePosition relativePosition, final Time when)
290             throws GTUException
291     {
292         return fractionalPositions(relativePosition);
293     }
294 
295     /** {@inheritDoc} */
296     @Override
297     public final double fractionalPosition(final Lane lane, final RelativePosition relativePosition) throws GTUException
298     {
299         if (this.laneTL.equals(lane))
300         {
301             return this.positionTL.getSI() / this.laneTL.getLength().getSI();
302         }
303         throw new GTUException("TrafficLight " + this.getId() + " not on lane " + lane);
304     }
305 
306     /** {@inheritDoc} */
307     @Override
308     public final double fractionalPosition(final Lane lane, final RelativePosition relativePosition, final Time when)
309             throws GTUException
310     {
311         return fractionalPosition(lane, relativePosition);
312     }
313 
314     /** {@inheritDoc} */
315     @Override
316     public final Length projectedPosition(final Lane projectionLane, final RelativePosition relativePosition, final Time when)
317             throws GTUException
318     {
319         CrossSectionLink link = projectionLane.getParentLink();
320         for (CrossSectionElement cse : link.getCrossSectionElementList())
321         {
322             if (cse instanceof Lane)
323             {
324                 Lane cseLane = (Lane) cse;
325                 if (cseLane.equals(projectionLane))
326                 {
327                     double fractionalPosition = fractionalPosition(cseLane, relativePosition, when);
328                     return new Length(projectionLane.getLength().getSI() * fractionalPosition, LengthUnit.SI);
329                 }
330             }
331         }
332         throw new GTUException("TrafficLight " + getId() + " is not on any lane of Link " + link);
333     }
334 
335     /** {@inheritDoc} */
336     @Override
337     public final LaneBasedStrategicalPlanner getStrategicalPlanner()
338     {
339         return (LaneBasedStrategicalPlanner) super.getStrategicalPlanner();
340     }
341 
342     /** {@inheritDoc} */
343     @Override
344     public final LaneBasedTacticalPlanner getTacticalPlanner()
345     {
346         return (LaneBasedTacticalPlanner) super.getTacticalPlanner();
347     }
348 
349     /** {@inheritDoc} */
350     @Override
351     public final void addTrigger(final Lane lane, final SimEvent<OTSSimTimeDouble> event)
352     {
353         // Nothing to do as this is not really a GTU.
354     }
355 
356     /** {@inheritDoc} */
357     @Override
358     public final Set<RelativePosition> getContourPoints()
359     {
360         return CONTOUR_POINTS;
361     }
362 
363     /** {@inheritDoc} */
364     @SuppressWarnings("checkstyle:designforextension")
365     @Override
366     public String toString()
367     {
368         return "AbstractTrafficLight [laneTL=" + this.laneTL + ", positionTL=" + this.positionTL + ", blocked=" + this.blocked
369                 + "]";
370     }
371 }