TestOpenDriveParserNoRTINew.java

  1. package org.opentrafficsim.road.network.factory;

  2. import java.awt.Color;
  3. import java.awt.geom.Rectangle2D;
  4. import java.io.IOException;
  5. import java.net.SocketException;
  6. import java.net.URL;
  7. import java.rmi.RemoteException;
  8. import java.util.ArrayList;
  9. import java.util.Arrays;
  10. import java.util.HashSet;
  11. import java.util.List;
  12. import java.util.Set;

  13. import javax.naming.NamingException;
  14. import javax.swing.JPanel;
  15. import javax.swing.SwingUtilities;
  16. import javax.xml.parsers.ParserConfigurationException;

  17. import nl.javel.gisbeans.io.esri.CoordinateTransform;
  18. import nl.tudelft.simulation.dsol.SimRuntimeException;
  19. import nl.tudelft.simulation.dsol.animation.D2.GisRenderable2D;
  20. import nl.tudelft.simulation.dsol.animation.D2.Renderable2D;
  21. import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
  22. import nl.tudelft.simulation.jstats.distributions.DistConstant;
  23. import nl.tudelft.simulation.jstats.distributions.DistExponential;
  24. import nl.tudelft.simulation.jstats.distributions.DistTriangular;
  25. import nl.tudelft.simulation.jstats.distributions.DistUniform;
  26. import nl.tudelft.simulation.jstats.streams.MersenneTwister;
  27. import nl.tudelft.simulation.jstats.streams.StreamInterface;
  28. import nl.tudelft.simulation.language.d3.DirectedPoint;
  29. import nl.tudelft.simulation.language.io.URLResource;

  30. import org.djunits.unit.AccelerationUnit;
  31. import org.djunits.unit.LengthUnit;
  32. import org.djunits.unit.SpeedUnit;
  33. import org.djunits.unit.TimeUnit;
  34. import org.djunits.value.vdouble.scalar.Acceleration;
  35. import org.djunits.value.vdouble.scalar.DoubleScalar;
  36. import org.djunits.value.vdouble.scalar.Duration;
  37. import org.djunits.value.vdouble.scalar.Length;
  38. import org.djunits.value.vdouble.scalar.Speed;
  39. import org.djunits.value.vdouble.scalar.Time;
  40. import org.opentrafficsim.core.dsol.OTSDEVSSimulatorInterface;
  41. import org.opentrafficsim.core.dsol.OTSModelInterface;
  42. import org.opentrafficsim.core.dsol.OTSSimTimeDouble;
  43. import org.opentrafficsim.core.geometry.Bezier;
  44. import org.opentrafficsim.core.geometry.OTSGeometryException;
  45. import org.opentrafficsim.core.geometry.OTSLine3D;
  46. import org.opentrafficsim.core.geometry.OTSPoint3D;
  47. import org.opentrafficsim.core.gtu.GTUDirectionality;
  48. import org.opentrafficsim.core.gtu.GTUException;
  49. import org.opentrafficsim.core.gtu.GTUType;
  50. import org.opentrafficsim.core.gtu.animation.AccelerationGTUColorer;
  51. import org.opentrafficsim.core.gtu.animation.GTUColorer;
  52. import org.opentrafficsim.core.gtu.animation.IDGTUColorer;
  53. import org.opentrafficsim.core.gtu.animation.SwitchableGTUColorer;
  54. import org.opentrafficsim.core.gtu.animation.VelocityGTUColorer;
  55. import org.opentrafficsim.core.gtu.behavioralcharacteristics.BehavioralCharacteristics;
  56. import org.opentrafficsim.core.network.Link;
  57. import org.opentrafficsim.core.network.LinkType;
  58. import org.opentrafficsim.core.network.LongitudinalDirectionality;
  59. import org.opentrafficsim.core.network.NetworkException;
  60. import org.opentrafficsim.core.network.Node;
  61. import org.opentrafficsim.core.network.OTSNetwork;
  62. import org.opentrafficsim.core.network.OTSNode;
  63. import org.opentrafficsim.core.network.route.CompleteRoute;
  64. import org.opentrafficsim.core.network.route.Route;
  65. import org.opentrafficsim.core.units.distributions.ContinuousDistDoubleScalar;
  66. import org.opentrafficsim.road.gtu.lane.LaneBasedIndividualGTU;
  67. import org.opentrafficsim.road.gtu.lane.perception.LanePerceptionFull;
  68. import org.opentrafficsim.road.gtu.lane.tactical.LaneBasedGTUFollowingLaneChangeTacticalPlanner;
  69. import org.opentrafficsim.road.gtu.lane.tactical.following.IDMPlusOld;
  70. import org.opentrafficsim.road.gtu.strategical.LaneBasedStrategicalPlanner;
  71. import org.opentrafficsim.road.gtu.strategical.route.LaneBasedStrategicalRoutePlanner;
  72. import org.opentrafficsim.road.network.factory.opendrive.LaneAnimationOD;
  73. import org.opentrafficsim.road.network.factory.opendrive.OpenDriveNetworkLaneParser;
  74. import org.opentrafficsim.road.network.factory.opendrive.communicationRTI.ReceiverThread;
  75. import org.opentrafficsim.road.network.lane.CrossSectionElement;
  76. import org.opentrafficsim.road.network.lane.CrossSectionLink;
  77. import org.opentrafficsim.road.network.lane.DirectedLanePosition;
  78. import org.opentrafficsim.road.network.lane.Lane;
  79. import org.opentrafficsim.road.network.lane.NoTrafficLane;
  80. import org.opentrafficsim.road.network.lane.changing.LaneKeepingPolicy;
  81. import org.opentrafficsim.simulationengine.AbstractWrappableAnimation;
  82. import org.opentrafficsim.simulationengine.OTSSimulationException;
  83. import org.opentrafficsim.simulationengine.properties.AbstractProperty;
  84. import org.opentrafficsim.simulationengine.properties.PropertyException;
  85. import org.xml.sax.SAXException;

  86. /**
  87.  * <p>
  88.  * Copyright (c) 2013-2015 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  89.  * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  90.  * <p>
  91.  * $LastChangedDate: 2015-08-05 15:55:21 +0200 (Wed, 05 Aug 2015) $, @version $Revision: 1199 $, by $Author: averbraeck $,
  92.  * initial version Oct 17, 2014 <br>
  93.  * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  94.  */
  95. public class TestOpenDriveParserNoRTINew extends AbstractWrappableAnimation
  96. {
  97.     /**
  98.      * Main program.
  99.      * @param args String[]; the command line arguments (not used)
  100.      * @throws SimRuntimeException should never happen
  101.      */
  102.     public static void main(final String[] args) throws SimRuntimeException
  103.     {
  104.         SwingUtilities.invokeLater(new Runnable()
  105.         {
  106.             @Override
  107.             public void run()
  108.             {
  109.                 try
  110.                 {
  111.                     TestOpenDriveParserNoRTINew xmlModel = new TestOpenDriveParserNoRTINew();
  112.                     // 1 hour simulation run for testing
  113.                     xmlModel.buildAnimator(new Time(0.0, TimeUnit.SECOND), new Duration(0.0, TimeUnit.SECOND),
  114.                             new Duration(60.0, TimeUnit.MINUTE), new ArrayList<AbstractProperty<?>>(), null, true);
  115.                 }
  116.                 catch (SimRuntimeException | NamingException | OTSSimulationException | PropertyException exception)
  117.                 {
  118.                     exception.printStackTrace();
  119.                 }
  120.             }
  121.         });
  122.     }

  123.     /** {@inheritDoc} */
  124.     @Override
  125.     public final String shortName()
  126.     {
  127.         return "TestOpenDriveModel";
  128.     }

  129.     /** {@inheritDoc} */
  130.     @Override
  131.     public final String description()
  132.     {
  133.         return "TestOpenDriveModel";
  134.     }

  135.     /** {@inheritDoc} */
  136.     @Override
  137.     public final void stopTimersThreads()
  138.     {
  139.         super.stopTimersThreads();
  140.     }

  141.     /** {@inheritDoc} */
  142.     @Override
  143.     protected final JPanel makeCharts()
  144.     {
  145.         return null;
  146.     }

  147.     /** {@inheritDoc} */
  148.     @Override
  149.     protected final OTSModelInterface makeModel(final GTUColorer colorer)
  150.     {
  151.         return new TestOpenDriveModel();
  152.     }

  153.     /** {@inheritDoc} */
  154.     @Override
  155.     protected final Rectangle2D.Double makeAnimationRectangle()
  156.     {
  157.         return new Rectangle2D.Double(-1000, -1000, 2000, 2000);
  158.     }

  159.     /** {@inheritDoc} */
  160.     @Override
  161.     public String toString()
  162.     {
  163.         return "TestOpenDriveParserNoRTINew []";
  164.     }

  165.     /**
  166.      * Model to test the XML parser.
  167.      * <p>
  168.      * Copyright (c) 2013-2015 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. <br>
  169.      * All rights reserved. BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim
  170.      * License</a>.
  171.      * <p>
  172.      * $LastChangedDate: 2015-08-05 15:55:21 +0200 (Wed, 05 Aug 2015) $, @version $Revision: 1199 $, by $Author: averbraeck $,
  173.      * initial version un 27, 2015 <br>
  174.      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  175.      * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  176.      */
  177.     class TestOpenDriveModel implements OTSModelInterface
  178.     {
  179.         /** */
  180.         private static final long serialVersionUID = 20150811L;

  181.         /** The simulator. */
  182.         private OTSDEVSSimulatorInterface simulator;

  183.         private List<LaneBasedIndividualGTU> rtiCars;

  184.         /** */
  185.         private ContinuousDistDoubleScalar.Rel<Speed, SpeedUnit> initialSpeedDist;

  186.         /** */
  187.         private ContinuousDistDoubleScalar.Rel<Duration, TimeUnit> iatDist;

  188.         /** */
  189.         private ContinuousDistDoubleScalar.Rel<Length, LengthUnit> lengthDist;

  190.         /** */
  191.         private ContinuousDistDoubleScalar.Rel<Length, LengthUnit> widthDist;

  192.         /** */
  193.         private ContinuousDistDoubleScalar.Rel<Speed, SpeedUnit> maxSpeedDist;

  194.         /** */
  195.         private ContinuousDistDoubleScalar.Rel<Length, LengthUnit> initialPosDist;

  196.         /** */
  197.         private GTUType carType;

  198.         /** Last id */
  199.         private int lastId = 0;

  200.         /** */
  201.         private StreamInterface stream;

  202.         /**
  203.              *
  204.              */
  205.         public TestOpenDriveModel()
  206.         {
  207.             super();
  208.             this.stream = new MersenneTwister(1);
  209.             this.initialSpeedDist = new ContinuousDistDoubleScalar.Rel<>(new DistConstant(this.stream, 0.0), SpeedUnit.SI);
  210.             this.iatDist = new ContinuousDistDoubleScalar.Rel<>(new DistExponential(this.stream, 30.0), TimeUnit.SECOND);
  211.             this.lengthDist = new ContinuousDistDoubleScalar.Rel<>(new DistUniform(this.stream, 4.0, 5.0), LengthUnit.METER);
  212.             this.widthDist = new ContinuousDistDoubleScalar.Rel<>(new DistConstant(this.stream, 2.0), LengthUnit.METER);
  213.             this.maxSpeedDist =
  214.                     new ContinuousDistDoubleScalar.Rel<>(new DistTriangular(this.stream, 30.0, 35.0, 40.0),
  215.                             SpeedUnit.MILE_PER_HOUR);
  216.             this.initialPosDist =
  217.                     new ContinuousDistDoubleScalar.Rel<>(new DistUniform(this.stream, 0.0, 1.0), LengthUnit.METER);
  218.             this.carType = new GTUType("Car");
  219.         }

  220.         /** {@inheritDoc} */
  221.         @Override
  222.         public final void constructModel(
  223.                 final SimulatorInterface<DoubleScalar.Abs<TimeUnit>, DoubleScalar.Rel<TimeUnit>, OTSSimTimeDouble> pSimulator)
  224.                 throws SimRuntimeException
  225.         {
  226.             this.simulator = (OTSDEVSSimulatorInterface) pSimulator;

  227.             this.rtiCars = new ArrayList<LaneBasedIndividualGTU>();

  228.             // URL url = URLResource.getResource("/NASAames.xodr");
  229.             URL url = URLResource.getResource("/testod.xodr");
  230.             this.simulator.setPauseOnError(false);
  231.             OpenDriveNetworkLaneParser nlp = new OpenDriveNetworkLaneParser(this.simulator);
  232.             OTSNetwork network = null;
  233.             try
  234.             {
  235.                 network = nlp.build(url);
  236.             }
  237.             catch (NetworkException | ParserConfigurationException | SAXException | IOException | NamingException
  238.                     | GTUException | OTSGeometryException exception)
  239.             {
  240.                 exception.printStackTrace();
  241.             }

  242.             URL gisURL = URLResource.getResource("/gis/map.xml");
  243.             System.err.println("GIS-map file: " + gisURL.toString());

  244.             double latCenter = nlp.getHeaderTag().getOriginLat().si, lonCenter = nlp.getHeaderTag().getOriginLong().si;

  245.             CoordinateTransform latLonToXY = new CoordinateTransformLonLatToXY(lonCenter, latCenter);
  246.             new GisRenderable2D(this.simulator, gisURL, latLonToXY);

  247.             // new ReceiverThread(this.simulator).run();

  248.             /** Repair the network... */
  249.             try
  250.             {
  251.                 destroyLink(nlp, network, "3766109");
  252.                 destroyLink(nlp, network, "3766110");
  253.                 destroyLink(nlp, network, "3766111");

  254.                 Lane lane109 =
  255.                         makeLane(network, "3766068.1", "3766068.0", "3", "3766059.7", "3766059.150", "2", "3766109", "-1",
  256.                                 LinkType.ALL, LaneKeepingPolicy.KEEP_LANE);
  257.                 Renderable2D animation109 = new LaneAnimationOD(lane109, this.simulator, Color.gray);
  258.                 nlp.animationMap.put(lane109, animation109);

  259.                 Lane lane110 =
  260.                         makeLane(network, "3766068.1", "3766068.0", "4", "3766059.7", "3766059.150", "3", "3766110", "-1",
  261.                                 LinkType.ALL, LaneKeepingPolicy.KEEP_LANE);
  262.                 Renderable2D animation110 = new LaneAnimationOD(lane110, this.simulator, Color.gray);
  263.                 nlp.animationMap.put(lane110, animation110);

  264.                 Lane lane111 =
  265.                         makeLane(network, "3766068.1", "3766068.0", "5", "3766059.7", "3766059.150", "4", "3766111", "-1",
  266.                                 LinkType.ALL, LaneKeepingPolicy.KEEP_LANE);
  267.                 Renderable2D animation111 = new LaneAnimationOD(lane111, this.simulator, Color.gray);
  268.                 nlp.animationMap.put(lane111, animation111);

  269.                 destroyLink(nlp, network, "3766175");
  270.                 destroyLink(nlp, network, "3766176");
  271.                 destroyLink(nlp, network, "3766177");

  272.                 Lane lane175 =
  273.                         makeLane(network, "3766059.1", "3766059.0", "3", "3766054.5", "3766054.191", "2", "3766175", "-1",
  274.                                 LinkType.ALL, LaneKeepingPolicy.KEEP_LANE);
  275.                 Renderable2D animation175 = new LaneAnimationOD(lane175, this.simulator, Color.gray);
  276.                 nlp.animationMap.put(lane175, animation175);

  277.                 Lane lane176 =
  278.                         makeLane(network, "3766059.1", "3766059.0", "4", "3766054.5", "3766054.191", "3", "3766176", "-1",
  279.                                 LinkType.ALL, LaneKeepingPolicy.KEEP_LANE);
  280.                 Renderable2D animation176 = new LaneAnimationOD(lane176, this.simulator, Color.gray);
  281.                 nlp.animationMap.put(lane176, animation176);

  282.                 Lane lane177 =
  283.                         makeLane(network, "3766059.1", "3766059.0", "5", "3766054.5", "3766054.191", "4", "3766177", "-1",
  284.                                 LinkType.ALL, LaneKeepingPolicy.KEEP_LANE);
  285.                 Renderable2D animation177 = new LaneAnimationOD(lane177, this.simulator, Color.gray);
  286.                 nlp.animationMap.put(lane177, animation177);

  287.                 Lane lane191x =
  288.                         makeLane(network, "3766054.5", "3766054.191", "-6", "3766059.1", "3766059.0", "-4", "3766191x", "-1",
  289.                                 LinkType.ALL, LaneKeepingPolicy.KEEP_LANE);
  290.                 Renderable2D animation191x = new LaneAnimationOD(lane191x, this.simulator, Color.gray);
  291.                 nlp.animationMap.put(lane191x, animation191x);

  292.             }
  293.             catch (OTSGeometryException | NetworkException | NamingException | RemoteException e)
  294.             {
  295.                 e.printStackTrace();
  296.             }

  297.             /*-
  298.             CompleteRoute cr1 = null, cr2 = null, cr3 = null, cr4 = null, cr5 = null, cr6 = null;

  299.             List<Node> nodesVia1 = new ArrayList<Node>();
  300.             nodesVia1.add(link2.getStartNode());
  301.             nodesVia1.add(link3.getEndNode());
  302.             nodesVia1.add(link4.getStartNode());
  303.             nodesVia1.add(link5.getEndNode());
  304.             nodesVia1.add(link7.getEndNode());
  305.             nodesVia1.add(link8.getStartNode());
  306.             try
  307.             {
  308.                 cr1 =
  309.                     network.getShortestRouteBetween(GTUType.ALL, link1.getStartNode(), link1.getStartNode(), nodesVia1);
  310.                 Collections.reverse(nodesVia1);
  311.                 cr2 =
  312.                     network.getShortestRouteBetween(GTUType.ALL, link1.getStartNode(), link1.getStartNode(), nodesVia1);
  313.             }
  314.             catch (NetworkException exception)
  315.             {
  316.                 exception.printStackTrace();
  317.             }

  318.             List<Node> nodesVia2 = new ArrayList<Node>();
  319.             nodesVia2.add(link3.getEndNode());
  320.             nodesVia2.add(link5.getEndNode());
  321.             try
  322.             {
  323.                 cr3 =
  324.                     network.getShortestRouteBetween(GTUType.ALL, link3.getStartNode(), link3.getStartNode(), nodesVia2);
  325.                 Collections.reverse(nodesVia2);
  326.                 cr4 =
  327.                     network.getShortestRouteBetween(GTUType.ALL, link3.getStartNode(), link3.getStartNode(), nodesVia2);
  328.             }
  329.             catch (NetworkException exception)
  330.             {
  331.                 exception.printStackTrace();
  332.             }

  333.             List<Node> nodesVia3 = new ArrayList<Node>();
  334.             nodesVia3.add(link7.getEndNode());
  335.             nodesVia3.add(link8.getEndNode());
  336.             try
  337.             {
  338.                 cr5 =
  339.                     network.getShortestRouteBetween(GTUType.ALL, link6.getStartNode(), link6.getStartNode(), nodesVia3);
  340.                 Collections.reverse(nodesVia3);
  341.                 cr6 =
  342.                     network.getShortestRouteBetween(GTUType.ALL, link6.getStartNode(), link6.getStartNode(), nodesVia3);
  343.             }
  344.             catch (NetworkException exception)
  345.             {
  346.                 exception.printStackTrace();
  347.             }
  348.              */

  349.             // build the graph
  350.             network.buildGraph(this.carType);

  351.             // generate 2 GTUs on 70.1 (parking lot NRC SV)
  352.             try
  353.             {
  354.                 CrossSectionLink link70_1 = (CrossSectionLink) network.getLink("3766070.1");
  355.                 Lane lane70_1m1 = getLane(link70_1, "-1"); // get the rightmost lane (-1) to drive north
  356.                 Lane lane70_1p1 = getLane(link70_1, "1"); // get the rightmost lane (1) to drive south

  357.                 // create a route around NRC SV
  358.                 Node n70_0 = network.getNode("3766070.0");
  359.                 Node n70_23 = network.getNode("3766070.23");
  360.                 Node n45_0 = network.getNode("3766045.0");
  361.                 Node n45_22 = network.getNode("3766045.22");
  362.                 Node n65_0 = network.getNode("3766065.0");
  363.                 Node n65_8 = network.getNode("3766065.8");
  364.                 Route route7070r =
  365.                         new CompleteRoute("Right around NRC-SV", this.carType, network.getShortestRouteBetween(this.carType,
  366.                                 n70_0, n70_0, Arrays.asList(new Node[] { n45_0, n65_0 })).getNodes());
  367.                 putCar(lane70_1m1, route7070r, network, GTUDirectionality.DIR_PLUS);
  368.                 Route route7070l =
  369.                         new CompleteRoute("Left around NRC-SV", this.carType, network.getShortestRouteBetween(this.carType,
  370.                                 n70_0, n70_0, Arrays.asList(new Node[] { n65_8, n45_22, n70_23 })).getNodes());
  371.                 putCar(lane70_1p1, route7070l, network, GTUDirectionality.DIR_MINUS);
  372.             }
  373.             catch (NetworkException | GTUException | NamingException | OTSGeometryException exception)
  374.             {
  375.                 exception.printStackTrace();
  376.             }

  377.             // generate 10 GTUs on 68 driving the upper circle.
  378.             try
  379.             {
  380.                 List<CrossSectionLink> links68 = new ArrayList<>();
  381.                 links68.add((CrossSectionLink) network.getLink("3766068.1"));
  382.                 links68.add((CrossSectionLink) network.getLink("3766068.2"));
  383.                 links68.add((CrossSectionLink) network.getLink("3766068.3"));
  384.                 links68.add((CrossSectionLink) network.getLink("3766068.4"));
  385.                 links68.add((CrossSectionLink) network.getLink("3766068.5"));

  386.                 // create a right route for top circle
  387.                 Node n68_0 = network.getNode("3766068.0");
  388.                 Node n38_88 = network.getNode("3766038.88");
  389.                 Node n43_0 = network.getNode("3766043.0");
  390.                 Node n45_0 = network.getNode("3766045.0");
  391.                 Node n65_0 = network.getNode("3766065.0");
  392.                 Node n64_17 = network.getNode("3766064.17");
  393.                 Route route6868r =
  394.                         new CompleteRoute("Right top", this.carType, network.getShortestRouteBetween(this.carType, n68_0,
  395.                                 n68_0, Arrays.asList(new Node[] { n38_88, n43_0, n45_0, n65_0, n64_17 })).getNodes());
  396.                 System.out.println(route6868r);
  397.                 for (int i = 0; i < 5; i++)
  398.                 {
  399.                     putCar(randLane(links68, LongitudinalDirectionality.DIR_PLUS), route6868r, network,
  400.                             GTUDirectionality.DIR_PLUS);
  401.                 }

  402.                 // create a left route for top circle
  403.                 Node n38_0 = network.getNode("3766038.0");
  404.                 Node n43_53 = network.getNode("3766043.53");
  405.                 Node n45_22 = network.getNode("3766045.22");
  406.                 Node n65_8 = network.getNode("3766065.8");
  407.                 Node n64_0 = network.getNode("3766064.0");
  408.                 Node n68_158 = network.getNode("3766068.158");
  409.                 Route route6868l =
  410.                         new CompleteRoute("Right top", this.carType, network.getShortestRouteBetween(this.carType, n68_158,
  411.                                 n68_158, Arrays.asList(new Node[] { n64_0, n65_8, n45_22, n43_53, n38_0 })).getNodes());
  412.                 System.out.println(route6868l);
  413.                 for (int i = 0; i < 5; i++)
  414.                 {
  415.                     putCar(randLane(links68, LongitudinalDirectionality.DIR_MINUS), route6868l, network,
  416.                             GTUDirectionality.DIR_MINUS);
  417.                 }

  418.             }
  419.             catch (NetworkException | GTUException | NamingException | OTSGeometryException exception)
  420.             {
  421.                 exception.printStackTrace();
  422.             }

  423.             // XXX dirty hack...
  424.             while (this.rtiCars.size() < 52)
  425.             {
  426.                 this.rtiCars.add(this.rtiCars.get(this.rtiCars.size() - 1));

  427.             }
  428.             /*-
  429.             for (int i = 0; i < 10; i++)
  430.             {
  431.                 Lane lane = null;
  432.                 GTUDirectionality dir = GTUDirectionality.DIR_PLUS;
  433.                 for (CrossSectionElement cse : link1.getCrossSectionElementList())
  434.                 {
  435.                     if (cse instanceof Lane && !(cse instanceof NoTrafficLane))
  436.                     {
  437.                         lane = (Lane) cse;
  438.                         dir =
  439.                             lane.getDirectionality(carType).isForwardOrBoth() ? GTUDirectionality.DIR_PLUS
  440.                                 : GTUDirectionality.DIR_MINUS;
  441.                         break;
  442.                     }
  443.                 }
  444.                 // int i = 1;
  445.                 LaneBasedDrivingCharacteristics drivingCharacteristics =
  446.                     new LaneBasedDrivingCharacteristics(new IDMPlus(), new Altruistic());
  447.                 LaneBasedStrategicalPlanner sPlanner =
  448.                     new LaneBasedStrategicalRoutePlanner(drivingCharacteristics,
  449.                         new LaneBasedGTUFollowingLaneChangeTacticalPlanner(), cr2);

  450.                 System.out.println("Car " + i + " - generated on lane " + lane + " with sn="
  451.                     + lane.getParentLink().getStartNode() + " and en=" + lane.getParentLink().getEndNode()
  452.                     + ", route = " + cr2);

  453.                 LanePerceptionFull perception = new LanePerceptionFull();
  454.                 DirectedLanePosition directedLanePosition =
  455.                     new DirectedLanePosition(lane,
  456.                         initialPosDist.draw().multiplyBy(lane.getCenterLine().getLengthSI()), dir);
  457.                 Set<DirectedLanePosition> lanepositionSet = new HashSet<DirectedLanePosition>();
  458.                 lanepositionSet.add(directedLanePosition);
  459.                 Length carLength = lengthDist.draw();

  460.                 try
  461.                 {
  462.                     LaneBasedIndividualCar car =
  463.                         new LaneBasedIndividualCar(String.valueOf(i), carType, lanepositionSet, new Speed(0.0,
  464.                             SpeedUnit.METER_PER_SECOND), carLength, widthDist.draw(), maxSpeedDist.draw(),
  465.                             this.simulator, sPlanner, perception, network);
  466.                     this.rtiCars.add(car);

  467.                 }
  468.                 catch (NamingException | NetworkException | GTUException | OTSGeometryException exception)
  469.                 {
  470.                     exception.printStackTrace();
  471.                 }
  472.             }
  473.              */

  474.             /*-
  475.             List<CompleteRoute> cRoutes = new ArrayList<>();
  476.             cRoutes.add(cr1);
  477.             cRoutes.add(cr2);
  478.             cRoutes.add(cr3);
  479.             cRoutes.add(cr4);
  480.             cRoutes.add(cr5);
  481.             cRoutes.add(cr6);
  482.             Random routeRandom = new Random();

  483.             List<CrossSectionLink> links = new ArrayList<>();
  484.             links.add(link1);
  485.             links.add(link2);
  486.             links.add(link3);
  487.             links.add(link4);
  488.             links.add(link5);
  489.             links.add(link6);
  490.             links.add(link7);
  491.             links.add(link8);

  492.             for (int i = 0; i < 52; i++)
  493.             {
  494.                 CompleteRoute cr = cRoutes.get(routeRandom.nextInt(6));

  495.                 CrossSectionLink link;
  496.                 while (true)
  497.                 {
  498.                     link = links.get(routeRandom.nextInt(8));
  499.                     if (cr.getNodes().contains(link.getStartNode()))
  500.                         break;
  501.                 }

  502.                 GTUDirectionality dir = GTUDirectionality.DIR_PLUS;
  503.                 Lane lane = null;

  504.                 while (true)
  505.                 {
  506.                     CrossSectionElement cse =
  507.                         link.getCrossSectionElementList().get(
  508.                             routeRandom.nextInt(link.getCrossSectionElementList().size()));
  509.                     if (cse instanceof Lane && !(cse instanceof NoTrafficLane))
  510.                     {
  511.                         lane = (Lane) cse;
  512.                         break;

  513.                     }
  514.                 }

  515.                 if (lane.getDirectionality(carType).equals(LongitudinalDirectionality.DIR_MINUS))
  516.                 {
  517.                     dir = GTUDirectionality.DIR_MINUS;
  518.                 }

  519.                 LaneBasedBehavioralCharacteristics drivingCharacteristics =
  520.                     new LaneBasedBehavioralCharacteristics(new IDMPlusOld(), new Altruistic());
  521.                 LaneBasedStrategicalPlanner sPlanner =
  522.                     new LaneBasedStrategicalRoutePlanner(drivingCharacteristics,
  523.                         new LaneBasedGTUFollowingLaneChangeTacticalPlanner(), cRoutes.get(routeRandom.nextInt(6)));
  524.                 LanePerceptionFull perception = new LanePerceptionFull();

  525.                 DirectedLanePosition directedLanePosition = null;
  526.                 try
  527.                 {
  528.                     directedLanePosition =
  529.                         new DirectedLanePosition(lane, initialPosDist.draw().multiplyBy(
  530.                             lane.getCenterLine().getLengthSI()), dir);
  531.                 }
  532.                 catch (GTUException exception1)
  533.                 {
  534.                     exception1.printStackTrace();
  535.                 }
  536.                 Set<DirectedLanePosition> lanepositionSet = new HashSet<DirectedLanePosition>();
  537.                 lanepositionSet.add(directedLanePosition);

  538.                 Length carLength = lengthDist.draw();
  539.                 double genPosSI = directedLanePosition.getPosition().getSI();
  540.                 double lengthSI = lane.getLength().getSI();
  541.                 double frontNew = (genPosSI + carLength.getSI()) / lengthSI;
  542.                 double rearNew = genPosSI / lengthSI;

  543.                 boolean isEnoughSpace = true;

  544.                 for (LaneBasedGTU gtu : lane.getGtuList())
  545.                 {
  546.                     double frontGTU = 0;
  547.                     try
  548.                     {
  549.                         frontGTU = gtu.fractionalPosition(lane, gtu.getFront());
  550.                     }
  551.                     catch (GTUException exception)
  552.                     {
  553.                         exception.printStackTrace();
  554.                     }
  555.                     double rearGTU = 0;
  556.                     try
  557.                     {
  558.                         rearGTU = gtu.fractionalPosition(lane, gtu.getRear());
  559.                     }
  560.                     catch (GTUException exception)
  561.                     {
  562.                         exception.printStackTrace();
  563.                     }
  564.                     if ((frontNew >= rearGTU && frontNew <= frontGTU) || (rearNew >= rearGTU && rearNew <= frontGTU)
  565.                         || (frontGTU >= rearNew && frontGTU <= frontNew) || (rearGTU >= rearNew && rearGTU <= frontNew))
  566.                         isEnoughSpace = false;
  567.                 }

  568.                 if (isEnoughSpace)
  569.                 {
  570.                     try
  571.                     {
  572.                         LaneBasedIndividualGTU car =
  573.                             new LaneBasedIndividualGTU(String.valueOf(i), carType, lanepositionSet, new Speed(0.0,
  574.                                 SpeedUnit.METER_PER_SECOND), carLength, widthDist.draw(), maxSpeedDist.draw(),
  575.                                 this.simulator, sPlanner, perception, network);
  576.                         this.rtiCars.add(car);

  577.                     }
  578.                     catch (NamingException | NetworkException | GTUException | OTSGeometryException exception)
  579.                     {
  580.                         exception.printStackTrace();
  581.                     }
  582.                 }
  583.                 else
  584.                 {
  585.                     i = i - 1;
  586.                 }

  587.             }
  588.              */

  589.             try
  590.             {
  591.                 new Thread(new ReceiverThread(this.simulator, this.carType, this.rtiCars, network)).start();
  592.             }
  593.             catch (SocketException exception1)
  594.             {
  595.                 exception1.printStackTrace();
  596.             }

  597.         }

  598.         private Lane randLane(final List<CrossSectionLink> links, final LongitudinalDirectionality dir)
  599.         {
  600.             // choose a random link with a random lane on that link in the right direction
  601.             CrossSectionLink link = links.get(this.stream.nextInt(0, links.size() - 1));
  602.             List<Lane> lanes = new ArrayList<>();
  603.             for (CrossSectionElement cse : link.getCrossSectionElementList())
  604.             {
  605.                 if (cse instanceof Lane)
  606.                 {
  607.                     Lane lane = (Lane) cse;
  608.                     if (lane.getDirectionality(this.carType).equals(dir) || lane.getDirectionality(this.carType).isBoth())
  609.                     {
  610.                         lanes.add(lane);
  611.                     }
  612.                 }
  613.             }
  614.             for (int i = 0; i < 10; i++)
  615.             {
  616.                 Lane lane = lanes.get(this.stream.nextInt(0, lanes.size() - 1));
  617.                 if (lane.getGtuList().isEmpty())
  618.                 {
  619.                     return lane;
  620.                 }
  621.             }
  622.             return lanes.get(this.stream.nextInt(0, lanes.size() - 1));
  623.         }

  624.         private void putCar(Lane lane, Route route, OTSNetwork network, GTUDirectionality dir) throws GTUException,
  625.                 NamingException, NetworkException, SimRuntimeException, OTSGeometryException
  626.         {
  627.             BehavioralCharacteristics behavioralCharacteristics = new BehavioralCharacteristics();
  628.             // LaneBasedBehavioralCharacteristics drivingCharacteristics =
  629.             // new LaneBasedBehavioralCharacteristics(new IDMPlusOld(), new Altruistic());
  630.             LaneBasedStrategicalPlanner sPlanner =
  631.                     new LaneBasedStrategicalRoutePlanner(behavioralCharacteristics,
  632.                             new LaneBasedGTUFollowingLaneChangeTacticalPlanner(new IDMPlusOld()), route);
  633.             LanePerceptionFull perception = new LanePerceptionFull();

  634.             DirectedLanePosition directedLanePosition =
  635.                     new DirectedLanePosition(lane, this.initialPosDist.draw().multiplyBy(lane.getCenterLine().getLengthSI()),
  636.                             dir);
  637.             Set<DirectedLanePosition> lanepositionSet = new HashSet<DirectedLanePosition>();
  638.             lanepositionSet.add(directedLanePosition);
  639.             Length carLength = this.lengthDist.draw();
  640.             LaneBasedIndividualGTU car =
  641.                     new LaneBasedIndividualGTU("" + (++this.lastId), this.carType, lanepositionSet, new Speed(0.0,
  642.                             SpeedUnit.METER_PER_SECOND), carLength, this.widthDist.draw(), this.maxSpeedDist.draw(),
  643.                             this.simulator, sPlanner, perception, network);
  644.             this.rtiCars.add(car);
  645.         }

  646.         private Lane getLane(CrossSectionLink link, String id) throws NetworkException
  647.         {
  648.             for (CrossSectionElement cse : link.getCrossSectionElementList())
  649.             {
  650.                 if (cse instanceof Lane && !(cse instanceof NoTrafficLane) && cse.getId().equals(id))
  651.                 {
  652.                     return (Lane) cse;
  653.                 }
  654.             }
  655.             throw new NetworkException("Could not find Lane " + id + " in link " + link);
  656.         }

  657.         /**
  658.          * Destroy the animation of the link and underlying cross section elements
  659.          * @param nlp the parser with the animation map
  660.          * @param network the network in which the link is registered
  661.          * @param linkId the link to destroy
  662.          * @throws NamingException in case destroying fails
  663.          * @throws NetworkException in case link cannot be found in the network
  664.          */
  665.         private void destroyLink(final OpenDriveNetworkLaneParser nlp, final OTSNetwork network, final String linkId)
  666.                 throws NamingException, NetworkException
  667.         {
  668.             Link link = network.getLink(linkId);
  669.             link.getStartNode().removeLink(link);
  670.             link.getEndNode().removeLink(link);
  671.             network.removeLink(link);
  672.             if (link instanceof CrossSectionLink)
  673.             {
  674.                 for (CrossSectionElement cse : ((CrossSectionLink) link).getCrossSectionElementList())
  675.                 {
  676.                     if (nlp.animationMap.containsKey(cse))
  677.                     {
  678.                         nlp.animationMap.get(cse).destroy();
  679.                     }
  680.                 }
  681.             }
  682.             if (nlp.animationMap.containsKey(link))
  683.             {
  684.                 nlp.animationMap.get(link).destroy();
  685.             }
  686.         }

  687.         /**
  688.          * Create an extra link to "repair" the network
  689.          * @param network network
  690.          * @param sLinkStr start link id
  691.          * @param sNodeStr start node id
  692.          * @param sLaneStr start lane id
  693.          * @param eLinkStr end link id
  694.          * @param eNodeStr end node id
  695.          * @param eLaneStr end lane id
  696.          * @param linkId the id of the new link
  697.          * @param laneId the id of the new lane
  698.          * @param linkType the type of the new link
  699.          * @param laneKeepingPolicy the lane keeping policy of the new link
  700.          * @return the created lane
  701.          * @throws OTSGeometryException when points cannot be found or line cannot be constructed
  702.          * @throws NetworkException when lane cannot be constructed
  703.          */
  704.         private Lane makeLane(final OTSNetwork network, final String sLinkStr, final String sNodeStr, final String sLaneStr,
  705.                 final String eLinkStr, final String eNodeStr, final String eLaneStr, final String linkId, final String laneId,
  706.                 final LinkType linkType, final LaneKeepingPolicy laneKeepingPolicy) throws OTSGeometryException,
  707.                 NetworkException
  708.         {
  709.             CrossSectionLink sLink = (CrossSectionLink) network.getLink(sLinkStr);
  710.             OTSNode sNode = (OTSNode) network.getNode(sNodeStr);
  711.             Lane sLane = (Lane) sLink.getCrossSectionElement(sLaneStr);
  712.             CrossSectionLink eLink = (CrossSectionLink) network.getLink(eLinkStr);
  713.             OTSNode eNode = (OTSNode) network.getNode(eNodeStr);
  714.             Lane eLane = (Lane) eLink.getCrossSectionElement(eLaneStr);
  715.             DirectedPoint sp, ep;
  716.             Length beginWidth, endWidth;
  717.             if (sLink.getStartNode().equals(sNode))
  718.             {
  719.                 OTSPoint3D p1 = sLane.getCenterLine().get(1);
  720.                 OTSPoint3D p2 = sLane.getCenterLine().get(0);
  721.                 sp = new DirectedPoint(p2.x, p2.y, p2.z, 0.0, 0.0, Math.atan2(p2.y - p1.y, p2.x - p1.x));
  722.                 beginWidth = sLane.getBeginWidth();
  723.             }
  724.             else
  725.             {
  726.                 OTSPoint3D p1 = sLane.getCenterLine().get(sLane.getCenterLine().size() - 2);
  727.                 OTSPoint3D p2 = sLane.getCenterLine().get(sLane.getCenterLine().size() - 1);
  728.                 sp = new DirectedPoint(p2.x, p2.y, p2.z, 0.0, 0.0, Math.atan2(p2.y - p1.y, p2.x - p1.x));
  729.                 beginWidth = sLane.getEndWidth();
  730.             }
  731.             if (eLink.getStartNode().equals(eNode))
  732.             {
  733.                 OTSPoint3D p1 = eLane.getCenterLine().get(1);
  734.                 OTSPoint3D p2 = eLane.getCenterLine().get(0);
  735.                 ep = new DirectedPoint(p2.x, p2.y, p2.z, 0.0, 0.0, Math.atan2(p1.y - p2.y, p1.x - p2.x));
  736.                 endWidth = eLane.getBeginWidth();
  737.             }
  738.             else
  739.             {
  740.                 OTSPoint3D p1 = eLane.getCenterLine().get(eLane.getCenterLine().size() - 2);
  741.                 OTSPoint3D p2 = eLane.getCenterLine().get(eLane.getCenterLine().size() - 1);
  742.                 ep = new DirectedPoint(p2.x, p2.y, p2.z, 0.0, 0.0, Math.atan2(p1.y - p2.y, p1.x - p2.x));
  743.                 endWidth = eLane.getEndWidth();
  744.             }
  745.             OTSLine3D designLine = Bezier.cubic(64, sp, ep);
  746.             CrossSectionLink newLink = new CrossSectionLink(linkId, sNode, eNode, linkType, designLine, laneKeepingPolicy);
  747.             newLink.addDirectionality(GTUType.ALL, LongitudinalDirectionality.DIR_PLUS);
  748.             Lane newLane =
  749.                     new Lane(newLink, laneId, Length.ZERO, Length.ZERO, beginWidth, endWidth, sLane.getLaneType(),
  750.                             LongitudinalDirectionality.DIR_PLUS, sLane.getSpeedLimit(GTUType.ALL),
  751.                             sLane.getOvertakingConditions());
  752.             network.addLink(newLink);
  753.             return newLane;
  754.         }

  755.         /** {@inheritDoc} */
  756.         @Override
  757.         public SimulatorInterface<DoubleScalar.Abs<TimeUnit>, DoubleScalar.Rel<TimeUnit>, OTSSimTimeDouble> getSimulator()

  758.         {
  759.             return this.simulator;
  760.         }

  761.         /**
  762.          * @return a GTUColorer
  763.          */
  764.         private GTUColorer makeSwitchableGTUColorer()
  765.         {
  766.             GTUColorer[] gtuColorers =
  767.                     new GTUColorer[] {
  768.                             new IDGTUColorer(),
  769.                             new VelocityGTUColorer(new Speed(100.0, SpeedUnit.KM_PER_HOUR)),
  770.                             new AccelerationGTUColorer(new Acceleration(1.0, AccelerationUnit.METER_PER_SECOND_2),
  771.                                     new Acceleration(1.0, AccelerationUnit.METER_PER_SECOND_2)) };
  772.             return new SwitchableGTUColorer(0, gtuColorers);
  773.         }

  774.         /** {@inheritDoc} */
  775.         @Override
  776.         public String toString()
  777.         {
  778.             return "TestOpenDriveModel [rtiCar.sizes=" + this.rtiCars.size() + ", carType=" + this.carType + ", lastId="
  779.                     + this.lastId + "]";
  780.         }
  781.     }

  782. }