View Javadoc
1   package org.opentrafficsim.road.gtu.lane;
2   
3   import java.rmi.RemoteException;
4   import java.util.HashMap;
5   import java.util.LinkedHashMap;
6   import java.util.Map;
7   import java.util.Set;
8   import java.util.UUID;
9   
10  import javax.media.j3d.Bounds;
11  import javax.naming.NamingException;
12  import javax.vecmath.Point3d;
13  
14  import nl.tudelft.simulation.dsol.animation.D2.Renderable2D;
15  import nl.tudelft.simulation.language.d3.BoundingBox;
16  import nl.tudelft.simulation.language.d3.DirectedPoint;
17  
18  import org.opentrafficsim.core.dsol.OTSAnimatorInterface;
19  import org.opentrafficsim.core.dsol.OTSDEVSSimulatorInterface;
20  import org.opentrafficsim.core.gtu.AbstractGTU;
21  import org.opentrafficsim.core.gtu.GTUException;
22  import org.opentrafficsim.core.gtu.GTUType;
23  import org.opentrafficsim.core.gtu.RelativePosition;
24  import org.opentrafficsim.core.gtu.RelativePosition.TYPE;
25  import org.opentrafficsim.core.network.LateralDirectionality;
26  import org.opentrafficsim.core.network.NetworkException;
27  import org.opentrafficsim.core.network.route.CompleteRoute;
28  import org.opentrafficsim.core.network.route.CompleteRouteNavigator;
29  import org.opentrafficsim.road.gtu.animation.DefaultBlockAnimation;
30  import org.opentrafficsim.road.gtu.animation.LaneChangeUrgeGTUColorer.LaneChangeDistanceAndDirection;
31  import org.opentrafficsim.road.gtu.following.GTUFollowingModel;
32  import org.opentrafficsim.road.gtu.following.HeadwayGTU;
33  import org.opentrafficsim.road.network.lane.Lane;
34  
35  /**
36   * Special GTU that cannot move, but it can be seen by other GTUs.
37   * <p>
38   * Copyright (c) 2013-2015 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
39   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
40   * <p>
41   * @version $Revision: 1401 $, $LastChangedDate: 2015-09-14 01:33:02 +0200 (Mon, 14 Sep 2015) $, by $Author: averbraeck $,
42   *          initial version 15 jul. 2015 <br>
43   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
44   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
45   */
46  public class LaneBlock extends AbstractGTU implements LaneBasedGTU
47  {
48      /** */
49      private static final long serialVersionUID = 20150624L;
50  
51      /** the simulator. */
52      private OTSDEVSSimulatorInterface simulator;
53  
54      /** animation. */
55      private Renderable2D animation;
56  
57      /** the lane of the block. */
58      private final Lane lane;
59  
60      /** the position of the block on the lane. */
61      private final Length.Rel position;
62  
63      /** the cached location for animation. */
64      private DirectedPoint location = null;
65  
66      /** the cached bounds for animation. */
67      private Bounds bounds = null;
68  
69      /** blocking GTU type. */
70      public static final GTUType BLOCK_GTU;
71  
72      /** null length. */
73      private static final Length.Rel LENGTH_REL_0 = new Length.Rel(0.0, METER);
74  
75      /** null length. */
76      private static final Length.Abs LENGTH_ABS_0 = new Length.Abs(0.0, METER);
77  
78      /** null speed. */
79      private static final Speed.Abs SPEED_ABS_0 = new Speed.Abs(0.0, METER_PER_SECOND);
80  
81      /** null time. */
82      private static final Time.Abs TIME_ABS_0 = new Time.Abs(0.0, SECOND);
83  
84      /** null acceleration. */
85      private static final Acceleration.Abs ACCELERATION_ABS_0 = new Acceleration.Abs(0.0, METER_PER_SECOND_2);
86  
87      /** the front, back, and reference positions; all at the same place. */
88      private static final Map<RelativePosition.TYPE, RelativePosition> RELATIVE_POSITIONS = new LinkedHashMap<>();
89  
90      static
91      {
92          BLOCK_GTU = GTUType.makeGTUType("BLOCK");
93  
94          RELATIVE_POSITIONS.put(RelativePosition.FRONT, new RelativePosition(LENGTH_REL_0, LENGTH_REL_0, LENGTH_REL_0,
95              RelativePosition.FRONT));
96          RELATIVE_POSITIONS.put(RelativePosition.REAR, new RelativePosition(LENGTH_REL_0, LENGTH_REL_0, LENGTH_REL_0,
97              RelativePosition.REAR));
98          RELATIVE_POSITIONS.put(RelativePosition.REFERENCE, RelativePosition.REFERENCE_POSITION);
99      }
100 
101     /**
102      * @param lane The lane where the block has to be put
103      * @param position the position on the lane as a length
104      * @param simulator the simulator to avoid NullPointerExceptions
105      * @param animationClass Class&lt;? extends Renderable2D&gt;; the class for animation or null if no animation
106      * @throws GTUException when GTU cannot be created.
107      * @throws NamingException if an error occurs when adding the animation handler
108      * @throws NetworkException when the GTU cannot be placed on the given lane
109      */
110     public LaneBlock(final Lane lane, final Length.Rel position, final OTSDEVSSimulatorInterface simulator,
111         final Class<? extends Renderable2D> animationClass) throws GTUException, NetworkException, NamingException
112     {
113         super(UUID.randomUUID().toString(), BLOCK_GTU, new CompleteRouteNavigator(new CompleteRoute("")));
114         this.simulator = simulator;
115         this.position = position;
116         this.lane = lane;
117 
118         // register the block on the lanes
119         lane.addGTU(this, position);
120 
121         // animation
122         try
123         {
124             new DefaultBlockAnimation(this, this.simulator);
125             if (simulator instanceof OTSAnimatorInterface && animationClass != null)
126             {
127                 // TODO use animationClass
128             }
129         }
130         catch (RemoteException exception)
131         {
132             exception.printStackTrace();
133         }
134     }
135 
136     /**
137      * @return lane
138      */
139     public final Lane getLane()
140     {
141         return this.lane;
142     }
143 
144     /** {@inheritDoc} */
145     @Override
146     public final Length.Rel getLength()
147     {
148         return LENGTH_REL_0;
149     }
150 
151     /** {@inheritDoc} */
152     @Override
153     public final Length.Rel getWidth()
154     {
155         return LENGTH_REL_0;
156     }
157 
158     /** {@inheritDoc} */
159     @Override
160     public final Speed.Abs getMaximumVelocity()
161     {
162         return SPEED_ABS_0;
163     }
164 
165     /** {@inheritDoc} */
166     @Override
167     public final OTSDEVSSimulatorInterface getSimulator()
168     {
169         return this.simulator;
170     }
171 
172     /** {@inheritDoc} */
173     @Override
174     public final RelativePosition getFront()
175     {
176         return RELATIVE_POSITIONS.get(RelativePosition.FRONT);
177     }
178 
179     /** {@inheritDoc} */
180     @Override
181     public final RelativePosition getRear()
182     {
183         return RELATIVE_POSITIONS.get(RelativePosition.FRONT);
184     }
185 
186     /** {@inheritDoc} */
187     @Override
188     public final Speed.Abs getVelocity()
189     {
190         return SPEED_ABS_0;
191     }
192 
193     /** {@inheritDoc} */
194     @Override
195     public final Map<TYPE, RelativePosition> getRelativePositions()
196     {
197         return RELATIVE_POSITIONS;
198     }
199 
200     /** {@inheritDoc} */
201     @Override
202     public final void destroy()
203     {
204         // nothing to do.
205     }
206 
207     /** {@inheritDoc} */
208     @Override
209     public final Acceleration.Abs getAcceleration()
210     {
211         return ACCELERATION_ABS_0;
212     }
213 
214     /** {@inheritDoc} */
215     @Override
216     public final DirectedPoint getLocation()
217     {
218         if (this.location == null)
219         {
220             try
221             {
222                 this.location = this.lane.getCenterLine().getLocation(this.position);
223                 this.location.z = this.lane.getLocation().z + 0.01;
224             }
225             catch (NetworkException exception)
226             {
227                 exception.printStackTrace();
228                 return null;
229             }
230         }
231         return this.location;
232     }
233 
234     /** {@inheritDoc} */
235     @Override
236     public final Bounds getBounds()
237     {
238         if (this.bounds == null)
239         {
240             this.bounds =
241                 new BoundingBox(new Point3d(-0.4, -this.lane.getWidth(0.0).getSI() * 0.4, 0.0), new Point3d(0.4,
242                     this.lane.getWidth(0.0).getSI() * 0.4, this.lane.getLocation().z + 0.01));
243         }
244         return this.bounds;
245     }
246 
247     /** {@inheritDoc} */
248     @Override
249     public final Length.Abs getOdometer()
250     {
251         return LENGTH_ABS_0;
252     }
253 
254     /** {@inheritDoc} */
255     @Override
256     public final Speed.Abs getLongitudinalVelocity()
257     {
258         return SPEED_ABS_0;
259     }
260 
261     /** {@inheritDoc} */
262     @Override
263     public final Speed.Abs getLongitudinalVelocity(final Time.Abs when)
264     {
265         return SPEED_ABS_0;
266     }
267 
268     /** {@inheritDoc} */
269     @Override
270     public final Acceleration.Abs getAcceleration(final Time.Abs when)
271     {
272         return ACCELERATION_ABS_0;
273     }
274 
275     /** {@inheritDoc} */
276     @Override
277     public final Speed.Abs getLateralVelocity()
278     {
279         return SPEED_ABS_0;
280     }
281 
282     /** {@inheritDoc} */
283     @Override
284     public final Time.Abs getLastEvaluationTime()
285     {
286         return TIME_ABS_0;
287     }
288 
289     /** {@inheritDoc} */
290     @Override
291     public final Time.Abs getNextEvaluationTime()
292     {
293         return TIME_ABS_0;
294     }
295 
296     /** {@inheritDoc} */
297     @Override
298     public final void enterLane(final Lane lane, final Length.Rel position) throws NetworkException
299     {
300         // do nothing
301     }
302 
303     /** {@inheritDoc} */
304     @Override
305     public final void leaveLane(final Lane lane)
306     {
307         // do nothing
308     }
309 
310     /** {@inheritDoc} */
311     @Override
312     public final Map<Lane, Length.Rel> positions(final RelativePosition relativePosition) throws NetworkException
313     {
314         Map<Lane, Length.Rel> map = new HashMap<Lane, Length.Rel>();
315         map.put(this.lane, this.position);
316         return map;
317     }
318 
319     /** {@inheritDoc} */
320     @Override
321     public final Map<Lane, Length.Rel> positions(final RelativePosition relativePosition, final Time.Abs when)
322         throws NetworkException
323     {
324         return positions(relativePosition);
325     }
326 
327     /** {@inheritDoc} */
328     @Override
329     public final Length.Rel position(final Lane lane, final RelativePosition relativePosition) throws NetworkException
330     {
331         if (this.lane.equals(lane))
332         {
333             return this.position;
334         }
335         throw new NetworkException("BLOCK GTU not on lane " + lane);
336     }
337 
338     /** {@inheritDoc} */
339     @Override
340     public final Length.Rel position(final Lane lane, final RelativePosition relativePosition, final Time.Abs when)
341         throws NetworkException
342     {
343         return position(lane, relativePosition);
344     }
345 
346     /** {@inheritDoc} */
347     @Override
348     public final Map<Lane, Double> fractionalPositions(final RelativePosition relativePosition) throws NetworkException
349     {
350         Map<Lane, Double> map = new HashMap<Lane, Double>();
351         map.put(this.lane, this.position.getSI() / this.lane.getLength().getSI());
352         return map;
353     }
354 
355     /** {@inheritDoc} */
356     @Override
357     public Map<Lane, Double> fractionalPositions(RelativePosition relativePosition, Time.Abs when)
358         throws NetworkException
359     {
360         Map<Lane, Double> result = new HashMap<Lane, Double>();
361         result.put(this.lane, this.position.getSI() / this.lane.getLength().getSI());
362         return result;
363     }
364 
365     /** {@inheritDoc} */
366     @Override
367     public double fractionalPosition(Lane lane, RelativePosition relativePosition, Time.Abs when)
368         throws NetworkException
369     {
370         return this.position.getSI() / lane.getLength().getSI();
371     }
372 
373     /** {@inheritDoc} */
374     @Override
375     public double fractionalPosition(Lane lane, RelativePosition relativePosition) throws NetworkException
376     {
377         return this.position.getSI() / lane.getLength().getSI();
378     }
379 
380     /** {@inheritDoc} */
381     @Override
382     public Length.Rel projectedPosition(Lane projectionLane, RelativePosition relativePosition, Time.Abs when)
383         throws NetworkException
384     {
385         return null;
386     }
387 
388     /** {@inheritDoc} */
389     @Override
390     public HeadwayGTU headway(Length.Rel maxDistance) throws NetworkException
391     {
392         return null;
393     }
394 
395     /** {@inheritDoc} */
396     @Override
397     public HeadwayGTU headway(Lane lane, Length.Rel maxDistance) throws NetworkException
398     {
399         return null;
400     }
401 
402     /** {@inheritDoc} */
403     @Override
404     public Set<LaneBasedGTU> parallel(Lane lane, Time.Abs when) throws NetworkException
405     {
406         return null;
407     }
408 
409     /** {@inheritDoc} */
410     @Override
411     public Set<LaneBasedGTU> parallel(LateralDirectionality lateralDirection, Time.Abs when) throws NetworkException
412     {
413         return null;
414     }
415 
416     /** {@inheritDoc} */
417     @Override
418     public Lane bestAccessibleAdjacentLane(Lane currentLane, LateralDirectionality lateralDirection,
419         Length.Rel longitudinalPosition)
420     {
421         return null;
422     }
423 
424     /** {@inheritDoc} */
425     @Override
426     public Time.Abs timeAtDistance(Length.Rel distance)
427     {
428         return null;
429     }
430 
431     /** {@inheritDoc} */
432     @Override
433     public Time.Rel deltaTimeForDistance(Length.Rel distance)
434     {
435         return null;
436     }
437 
438     /** {@inheritDoc} */
439     @Override
440     public GTUFollowingModel getGTUFollowingModel()
441     {
442         return null;
443     }
444 
445     /** {@inheritDoc} */
446     @Override
447     public LaneChangeDistanceAndDirection getLaneChangeDistanceAndDirection()
448     {
449         return null;
450     }
451 
452     /** {@inheritDoc} */
453     @Override
454     public String toString()
455     {
456         return "LaneBlock [lane=" + this.lane + ", position=" + this.position + "]";
457     }
458 
459 }