LaneBlock.java

  1. package org.opentrafficsim.core.gtu.lane;

  2. import java.rmi.RemoteException;
  3. import java.util.HashMap;
  4. import java.util.LinkedHashMap;
  5. import java.util.Map;
  6. import java.util.Set;
  7. import java.util.UUID;

  8. import javax.media.j3d.Bounds;
  9. import javax.naming.NamingException;
  10. import javax.vecmath.Point3d;

  11. import nl.tudelft.simulation.dsol.animation.D2.Renderable2D;
  12. import nl.tudelft.simulation.language.d3.BoundingBox;
  13. import nl.tudelft.simulation.language.d3.DirectedPoint;

  14. import org.opentrafficsim.core.dsol.OTSAnimatorInterface;
  15. import org.opentrafficsim.core.dsol.OTSDEVSSimulatorInterface;
  16. import org.opentrafficsim.core.gtu.AbstractGTU;
  17. import org.opentrafficsim.core.gtu.GTUException;
  18. import org.opentrafficsim.core.gtu.GTUType;
  19. import org.opentrafficsim.core.gtu.RelativePosition;
  20. import org.opentrafficsim.core.gtu.RelativePosition.TYPE;
  21. import org.opentrafficsim.core.gtu.animation.DefaultBlockAnimation;
  22. import org.opentrafficsim.core.gtu.animation.LaneChangeUrgeGTUColorer.LaneChangeDistanceAndDirection;
  23. import org.opentrafficsim.core.gtu.following.GTUFollowingModel;
  24. import org.opentrafficsim.core.gtu.following.HeadwayGTU;
  25. import org.opentrafficsim.core.network.LateralDirectionality;
  26. import org.opentrafficsim.core.network.NetworkException;
  27. import org.opentrafficsim.core.network.lane.Lane;
  28. import org.opentrafficsim.core.network.route.CompleteRoute;
  29. import org.opentrafficsim.core.network.route.CompleteRouteNavigator;

  30. /**
  31.  * Special GTU that cannot move, but it can be seen by other GTUs.
  32.  * <p>
  33.  * Copyright (c) 2013-2015 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  34.  * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  35.  * <p>
  36.  * @version $Revision: 1378 $, $LastChangedDate: 2015-09-03 13:38:01 +0200 (Thu, 03 Sep 2015) $, by $Author: averbraeck $,
  37.  *          initial version 15 jul. 2015 <br>
  38.  * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  39.  * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  40.  */
  41. public class LaneBlock extends AbstractGTU implements LaneBasedGTU
  42. {
  43.     /** */
  44.     private static final long serialVersionUID = 20150624L;

  45.     /** the simulator. */
  46.     private OTSDEVSSimulatorInterface simulator;

  47.     /** animation. */
  48.     private Renderable2D animation;

  49.     /** the lane of the block. */
  50.     private final Lane lane;

  51.     /** the position of the block on the lane. */
  52.     private final Length.Rel position;

  53.     /** the cached location for animation. */
  54.     private DirectedPoint location = null;

  55.     /** the cached bounds for animation. */
  56.     private Bounds bounds = null;

  57.     /** blocking GTU type. */
  58.     public static final GTUType BLOCK_GTU;

  59.     /** null length. */
  60.     private static final Length.Rel LENGTH_REL_0 = new Length.Rel(0.0, METER);

  61.     /** null length. */
  62.     private static final Length.Abs LENGTH_ABS_0 = new Length.Abs(0.0, METER);

  63.     /** null speed. */
  64.     private static final Speed.Abs SPEED_ABS_0 = new Speed.Abs(0.0, METER_PER_SECOND);

  65.     /** null time. */
  66.     private static final Time.Abs TIME_ABS_0 = new Time.Abs(0.0, SECOND);

  67.     /** null acceleration. */
  68.     private static final Acceleration.Abs ACCELERATION_ABS_0 = new Acceleration.Abs(0.0, METER_PER_SECOND_2);

  69.     /** the front, back, and reference positions; all at the same place. */
  70.     private static final Map<RelativePosition.TYPE, RelativePosition> RELATIVE_POSITIONS = new LinkedHashMap<>();

  71.     static
  72.     {
  73.         BLOCK_GTU = GTUType.makeGTUType("BLOCK");

  74.         RELATIVE_POSITIONS.put(RelativePosition.FRONT, new RelativePosition(LENGTH_REL_0, LENGTH_REL_0, LENGTH_REL_0,
  75.             RelativePosition.FRONT));
  76.         RELATIVE_POSITIONS.put(RelativePosition.REAR, new RelativePosition(LENGTH_REL_0, LENGTH_REL_0, LENGTH_REL_0,
  77.             RelativePosition.REAR));
  78.         RELATIVE_POSITIONS.put(RelativePosition.REFERENCE, RelativePosition.REFERENCE_POSITION);
  79.     }

  80.     /**
  81.      * @param lane The lane where the block has to be put
  82.      * @param position the position on the lane as a length
  83.      * @param simulator the simulator to avoid NullPointerExceptions
  84.      * @param animationClass Class&lt;? extends Renderable2D&gt;; the class for animation or null if no animation
  85.      * @throws GTUException when GTU cannot be created.
  86.      * @throws NamingException if an error occurs when adding the animation handler
  87.      * @throws RemoteException when the simulator cannot be reached
  88.      * @throws NetworkException when the GTU cannot be placed on the given lane
  89.      */
  90.     public LaneBlock(final Lane lane, final Length.Rel position, final OTSDEVSSimulatorInterface simulator,
  91.         final Class<? extends Renderable2D> animationClass) throws GTUException, RemoteException, NetworkException,
  92.         NamingException
  93.     {
  94.         super(UUID.randomUUID().toString(), BLOCK_GTU, new CompleteRouteNavigator(new CompleteRoute("")));
  95.         this.simulator = simulator;
  96.         this.position = position;
  97.         this.lane = lane;

  98.         // register the block on the lanes
  99.         lane.addGTU(this, position);

  100.         // animation
  101.         new DefaultBlockAnimation(this, this.simulator);
  102.         if (simulator instanceof OTSAnimatorInterface && animationClass != null)
  103.         {
  104.             // TODO use animationClass
  105.         }

  106.     }

  107.     /**
  108.      * @return lane
  109.      */
  110.     public final Lane getLane()
  111.     {
  112.         return this.lane;
  113.     }

  114.     /** {@inheritDoc} */
  115.     @Override
  116.     public final Length.Rel getLength()
  117.     {
  118.         return LENGTH_REL_0;
  119.     }

  120.     /** {@inheritDoc} */
  121.     @Override
  122.     public final Length.Rel getWidth()
  123.     {
  124.         return LENGTH_REL_0;
  125.     }

  126.     /** {@inheritDoc} */
  127.     @Override
  128.     public final Speed.Abs getMaximumVelocity()
  129.     {
  130.         return SPEED_ABS_0;
  131.     }

  132.     /** {@inheritDoc} */
  133.     @Override
  134.     public final OTSDEVSSimulatorInterface getSimulator()
  135.     {
  136.         return this.simulator;
  137.     }

  138.     /** {@inheritDoc} */
  139.     @Override
  140.     public final RelativePosition getFront()
  141.     {
  142.         return RELATIVE_POSITIONS.get(RelativePosition.FRONT);
  143.     }

  144.     /** {@inheritDoc} */
  145.     @Override
  146.     public final RelativePosition getRear()
  147.     {
  148.         return RELATIVE_POSITIONS.get(RelativePosition.FRONT);
  149.     }

  150.     /** {@inheritDoc} */
  151.     @Override
  152.     public final Speed.Abs getVelocity() throws RemoteException
  153.     {
  154.         return SPEED_ABS_0;
  155.     }

  156.     /** {@inheritDoc} */
  157.     @Override
  158.     public final Map<TYPE, RelativePosition> getRelativePositions()
  159.     {
  160.         return RELATIVE_POSITIONS;
  161.     }

  162.     /** {@inheritDoc} */
  163.     @Override
  164.     public final void destroy()
  165.     {
  166.         // nothing to do.
  167.     }

  168.     /** {@inheritDoc} */
  169.     @Override
  170.     public final Acceleration.Abs getAcceleration() throws RemoteException
  171.     {
  172.         return ACCELERATION_ABS_0;
  173.     }

  174.     /** {@inheritDoc} */
  175.     @Override
  176.     public final DirectedPoint getLocation() throws RemoteException
  177.     {
  178.         if (this.location == null)
  179.         {
  180.             try
  181.             {
  182.                 this.location = this.lane.getCenterLine().getLocation(this.position);
  183.                 this.location.z = this.lane.getLocation().z + 0.01;
  184.             }
  185.             catch (NetworkException exception)
  186.             {
  187.                 exception.printStackTrace();
  188.                 return null;
  189.             }
  190.         }
  191.         return this.location;
  192.     }

  193.     /** {@inheritDoc} */
  194.     @Override
  195.     public final Bounds getBounds() throws RemoteException
  196.     {
  197.         if (this.bounds == null)
  198.         {
  199.             this.bounds =
  200.                 new BoundingBox(new Point3d(-0.4, -this.lane.getWidth(0.0).getSI() * 0.4, 0.0), new Point3d(0.4, this.lane
  201.                     .getWidth(0.0).getSI() * 0.4, this.lane.getLocation().z + 0.01));
  202.         }
  203.         return this.bounds;
  204.     }

  205.     /** {@inheritDoc} */
  206.     @Override
  207.     public final Length.Abs getOdometer() throws RemoteException
  208.     {
  209.         return LENGTH_ABS_0;
  210.     }

  211.     /** {@inheritDoc} */
  212.     @Override
  213.     public final Speed.Abs getLongitudinalVelocity() throws RemoteException
  214.     {
  215.         return SPEED_ABS_0;
  216.     }

  217.     /** {@inheritDoc} */
  218.     @Override
  219.     public final Speed.Abs getLongitudinalVelocity(final Time.Abs when)
  220.     {
  221.         return SPEED_ABS_0;
  222.     }

  223.     /** {@inheritDoc} */
  224.     @Override
  225.     public final Acceleration.Abs getAcceleration(final Time.Abs when)
  226.     {
  227.         return ACCELERATION_ABS_0;
  228.     }

  229.     /** {@inheritDoc} */
  230.     @Override
  231.     public final Speed.Abs getLateralVelocity()
  232.     {
  233.         return SPEED_ABS_0;
  234.     }

  235.     /** {@inheritDoc} */
  236.     @Override
  237.     public final Time.Abs getLastEvaluationTime()
  238.     {
  239.         return TIME_ABS_0;
  240.     }

  241.     /** {@inheritDoc} */
  242.     @Override
  243.     public final Time.Abs getNextEvaluationTime()
  244.     {
  245.         return TIME_ABS_0;
  246.     }

  247.     /** {@inheritDoc} */
  248.     @Override
  249.     public final void enterLane(final Lane lane, final Length.Rel position) throws NetworkException
  250.     {
  251.         // do nothing
  252.     }

  253.     /** {@inheritDoc} */
  254.     @Override
  255.     public final void leaveLane(final Lane lane)
  256.     {
  257.         // do nothing
  258.     }

  259.     /** {@inheritDoc} */
  260.     @Override
  261.     public final Map<Lane, Length.Rel> positions(final RelativePosition relativePosition) throws NetworkException,
  262.         RemoteException
  263.     {
  264.         Map<Lane, Length.Rel> map = new HashMap<Lane, Length.Rel>();
  265.         map.put(this.lane, this.position);
  266.         return map;
  267.     }

  268.     /** {@inheritDoc} */
  269.     @Override
  270.     public final Map<Lane, Length.Rel> positions(final RelativePosition relativePosition, final Time.Abs when)
  271.         throws NetworkException, RemoteException
  272.     {
  273.         return positions(relativePosition);
  274.     }

  275.     /** {@inheritDoc} */
  276.     @Override
  277.     public final Length.Rel position(final Lane lane, final RelativePosition relativePosition) throws NetworkException,
  278.         RemoteException
  279.     {
  280.         if (this.lane.equals(lane))
  281.         {
  282.             return this.position;
  283.         }
  284.         throw new NetworkException("BLOCK GTU not on lane " + lane);
  285.     }

  286.     /** {@inheritDoc} */
  287.     @Override
  288.     public final Length.Rel position(final Lane lane, final RelativePosition relativePosition, final Time.Abs when)
  289.         throws NetworkException, RemoteException
  290.     {
  291.         return position(lane, relativePosition);
  292.     }

  293.     /** {@inheritDoc} */
  294.     @Override
  295.     public final Map<Lane, Double> fractionalPositions(final RelativePosition relativePosition) throws NetworkException,
  296.         RemoteException
  297.     {
  298.         Map<Lane, Double> map = new HashMap<Lane, Double>();
  299.         map.put(this.lane, this.position.getSI() / this.lane.getLength().getSI());
  300.         return map;
  301.     }

  302.     /** {@inheritDoc} */
  303.     @Override
  304.     public Map<Lane, Double> fractionalPositions(RelativePosition relativePosition, Time.Abs when) throws NetworkException,
  305.         RemoteException
  306.     {
  307.         Map<Lane, Double> result = new HashMap<Lane, Double>();
  308.         result.put(this.lane, this.position.getSI() / this.lane.getLength().getSI());
  309.         return result;
  310.     }

  311.     /** {@inheritDoc} */
  312.     @Override
  313.     public double fractionalPosition(Lane lane, RelativePosition relativePosition, Time.Abs when) throws NetworkException,
  314.         RemoteException
  315.     {
  316.         return this.position.getSI() / lane.getLength().getSI();
  317.     }

  318.     /** {@inheritDoc} */
  319.     @Override
  320.     public double fractionalPosition(Lane lane, RelativePosition relativePosition) throws NetworkException, RemoteException
  321.     {
  322.         return this.position.getSI() / lane.getLength().getSI();
  323.     }

  324.     /** {@inheritDoc} */
  325.     @Override
  326.     public Length.Rel projectedPosition(Lane projectionLane, RelativePosition relativePosition, Time.Abs when)
  327.         throws NetworkException, RemoteException
  328.     {
  329.         return null;
  330.     }

  331.     /** {@inheritDoc} */
  332.     @Override
  333.     public HeadwayGTU headway(Length.Rel maxDistance) throws RemoteException, NetworkException
  334.     {
  335.         return null;
  336.     }

  337.     /** {@inheritDoc} */
  338.     @Override
  339.     public HeadwayGTU headway(Lane lane, Length.Rel maxDistance) throws RemoteException, NetworkException
  340.     {
  341.         return null;
  342.     }

  343.     /** {@inheritDoc} */
  344.     @Override
  345.     public Set<LaneBasedGTU> parallel(Lane lane, Time.Abs when) throws RemoteException, NetworkException
  346.     {
  347.         return null;
  348.     }

  349.     /** {@inheritDoc} */
  350.     @Override
  351.     public Set<LaneBasedGTU> parallel(LateralDirectionality lateralDirection, Time.Abs when) throws RemoteException,
  352.         NetworkException
  353.     {
  354.         return null;
  355.     }

  356.     /** {@inheritDoc} */
  357.     @Override
  358.     public Lane bestAccessibleAdjacentLane(Lane currentLane, LateralDirectionality lateralDirection,
  359.         Length.Rel longitudinalPosition)
  360.     {
  361.         return null;
  362.     }

  363.     /** {@inheritDoc} */
  364.     @Override
  365.     public Time.Abs timeAtDistance(Length.Rel distance)
  366.     {
  367.         return null;
  368.     }

  369.     /** {@inheritDoc} */
  370.     @Override
  371.     public Time.Rel deltaTimeForDistance(Length.Rel distance)
  372.     {
  373.         return null;
  374.     }

  375.     /** {@inheritDoc} */
  376.     @Override
  377.     public GTUFollowingModel getGTUFollowingModel()
  378.     {
  379.         return null;
  380.     }

  381.     /** {@inheritDoc} */
  382.     @Override
  383.     public LaneChangeDistanceAndDirection getLaneChangeDistanceAndDirection()
  384.     {
  385.         return null;
  386.     }

  387.     /** {@inheritDoc} */
  388.     @Override
  389.     public String toString()
  390.     {
  391.         return "LaneBlock [lane=" + this.lane + ", position=" + this.position + "]";
  392.     }

  393. }