View Javadoc
1   package org.opentrafficsim.demo.geometry;
2   
3   import java.awt.Color;
4   import java.rmi.RemoteException;
5   
6   import javax.naming.NamingException;
7   
8   import nl.tudelft.simulation.dsol.SimRuntimeException;
9   import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
10  
11  import org.djunits.unit.TimeUnit;
12  import org.djunits.value.vdouble.scalar.DoubleScalar.Abs;
13  import org.djunits.value.vdouble.scalar.DoubleScalar.Rel;
14  import org.opentrafficsim.core.OTS_SCALAR;
15  import org.opentrafficsim.core.dsol.OTSModelInterface;
16  import org.opentrafficsim.core.dsol.OTSSimTimeDouble;
17  import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
18  import org.opentrafficsim.core.geometry.OTSGeometryException;
19  import org.opentrafficsim.core.geometry.OTSLine3D;
20  import org.opentrafficsim.core.geometry.OTSPoint3D;
21  import org.opentrafficsim.core.network.LongitudinalDirectionality;
22  import org.opentrafficsim.core.network.NetworkException;
23  import org.opentrafficsim.core.network.OTSNode;
24  import org.opentrafficsim.road.network.animation.LaneAnimation;
25  import org.opentrafficsim.road.network.animation.ShoulderAnimation;
26  import org.opentrafficsim.road.network.lane.CrossSectionLink;
27  import org.opentrafficsim.road.network.lane.Lane;
28  import org.opentrafficsim.road.network.lane.NoTrafficLane;
29  import org.opentrafficsim.road.network.lane.Shoulder;
30  import org.opentrafficsim.road.network.lane.changing.LaneKeepingPolicy;
31  import org.opentrafficsim.road.network.lane.changing.OvertakingConditions;
32  
33  /**
34   * <p>
35   * Copyright (c) 2013-2015 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
36   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
37   * <p>
38   * $LastChangedDate: 2015-09-24 19:14:49 +0200 (Thu, 24 Sep 2015) $, @version $Revision: 1430 $, by $Author: averbraeck $,
39   * initial version ct 17, 2014 <br>
40   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
41   */
42  /** */
43  public class TestModel implements OTSModelInterface, OTS_SCALAR
44  {
45      /** */
46      private static final long serialVersionUID = 1L;
47  
48      /** simulator. */
49      private OTSSimulatorInterface simulator;
50  
51      /** {@inheritDoc} */
52      @Override
53      public final void
54          constructModel(final SimulatorInterface<Abs<TimeUnit>, Rel<TimeUnit>, OTSSimTimeDouble> pSimulator)
55              throws SimRuntimeException, RemoteException
56      {
57          this.simulator = (OTSSimulatorInterface) pSimulator;
58  
59          // create a small graph and a road around it.
60  
61          OTSNode n0 = new OTSNode("N0", new OTSPoint3D(-25.0, 0.0));
62          OTSNode n1 = new OTSNode("N1", new OTSPoint3D(0.0, 0.0));
63          CrossSectionLink l01 =
64              new CrossSectionLink("L01", n0, n1, new OTSLine3D(new OTSPoint3D[]{new OTSPoint3D(-25.0, 0.0),
65                  new OTSPoint3D(0.0, 0.0)}), LaneKeepingPolicy.KEEP_RIGHT);
66  
67          OTSNode n2 = new OTSNode("N2", new OTSPoint3D(25.0, 20.0));
68          CrossSectionLink l12 =
69              new CrossSectionLink("L12", n1, n2, new OTSLine3D(new OTSPoint3D[]{new OTSPoint3D(0.0, 0.0),
70                  new OTSPoint3D(25.0, 20.0)}), LaneKeepingPolicy.KEEP_RIGHT);
71  
72          OTSNode n3 = new OTSNode("N3", new OTSPoint3D(50.0, 0.0));
73          CrossSectionLink l23 =
74              new CrossSectionLink("L23", n2, n3, new OTSLine3D(new OTSPoint3D[]{new OTSPoint3D(25.0, 20.0),
75                  new OTSPoint3D(50.0, 0.0)}), LaneKeepingPolicy.KEEP_RIGHT);
76  
77          OTSNode n4 = new OTSNode("N4", new OTSPoint3D(75.0, -20.0));
78          CrossSectionLink l34 =
79              new CrossSectionLink("L34", n3, n4, new OTSLine3D(new OTSPoint3D[]{new OTSPoint3D(50.0, 0.0),
80                  new OTSPoint3D(75.0, -20.0)}), LaneKeepingPolicy.KEEP_RIGHT);
81  
82          OTSNode n5 = new OTSNode("N5", new OTSPoint3D(100.0, 0.0));
83          CrossSectionLink l45 =
84              new CrossSectionLink("L45", n4, n5, new OTSLine3D(new OTSPoint3D[]{new OTSPoint3D(75.0, -20.0),
85                  new OTSPoint3D(100.0, 0.0)}), LaneKeepingPolicy.KEEP_RIGHT);
86  
87          OTSNode n6 = new OTSNode("N6", new OTSPoint3D(125.0, 0.0));
88          CrossSectionLink l56 =
89              new CrossSectionLink("L56", n5, n6, new OTSLine3D(new OTSPoint3D[]{new OTSPoint3D(100.0, 0.0),
90                  new OTSPoint3D(125.0, 0.0)}), LaneKeepingPolicy.KEEP_RIGHT);
91  
92          OTSNode n7 = new OTSNode("N7", new OTSPoint3D(300.0, 0.0));
93          CrossSectionLink l67 =
94              new CrossSectionLink("L67", n6, n7, new OTSLine3D(new OTSPoint3D[]{new OTSPoint3D(125.0, 0.0),
95                  new OTSPoint3D(150.0, 0.0), new OTSPoint3D(175.0, 20.0), new OTSPoint3D(200.0, 0.0),
96                  new OTSPoint3D(225.0, -20.0), new OTSPoint3D(250.0, 0.0), new OTSPoint3D(300.0, 0.0)}),
97                  LaneKeepingPolicy.KEEP_RIGHT);
98  
99          try
100         {
101             add2x2Lanes(l01);
102             add2x2Lanes(l12);
103             add2x2Lanes(l23);
104             add2x2Lanes(l34);
105             add2x2Lanes(l45);
106             add2x2Lanes(l56);
107             add2x2Lanes(l67);
108         }
109         catch (NetworkException | OTSGeometryException exception)
110         {
111             exception.printStackTrace();
112         }
113     }
114 
115     /**
116      * The road is as follows; The design line goes from left to right.
117      * 
118      * <pre>
119      * ----------------------- +9.50
120      * SSSSS Shoulder SL SSSSS +9.00 (width = 1.0)
121      * ----------------------- +8.50
122      * EEEEE Emergency ELL EEE +8.25 (width = 0.5)
123      * ----------------------- +8.00
124      * LLLLL Lane L1 LLLLLLLLL +6.25 (width = 3.5)
125      * ----------------------- +4.50
126      * LLLLL Lane L2 LLLLLLLLL +2.75 (width = 3.5)
127      * ----------------------- +1.00
128      * EEEEE Emergency ELM EEE +0.75 (width = 0.5)
129      * ----------------------- +0.50
130      * SSSSS Shoulder SM SSSSS +0.00 (width = 1.0)
131      * ----------------------- -0.50
132      * EEEEE Emergency ERM EEE -0.75 (width = 0.5)
133      * ----------------------- -1.00
134      * LLLLL Lane R2 LLLLLLLLL -2.75 (width = 3.5)
135      * ----------------------- -4.50
136      * LLLLL Lane R1 LLLLLLLLL -6.25 (width = 3.5)
137      * ----------------------- -8.00
138      * EEEEE Emergency ERR EEE -8.25 (width = 0.5)
139      * ----------------------- -8.50
140      * SSSSS Shoulder SR SSSSS -9.00 (width = 1.0)
141      * ----------------------- -9.50
142      * </pre>
143      *
144      * <br>
145      * @param link link.
146      * @throws NetworkException on network inconsistency
147      * @throws OTSGeometryException on network inconsistency
148      */
149     private void add2x2Lanes(final CrossSectionLink link) throws NetworkException, OTSGeometryException
150     {
151         // four lanes, grass underneath, lines between lane1-2 and lane 2-3, barrier between lane 2-3
152         // lane is 3.5 meters wide. gap between 3-4 is one meter. outside 0.5 meters on both sides
153         Length.Rel m05 = new Length.Rel(0.5, METER);
154         Length.Rel m10 = new Length.Rel(1.0, METER);
155         Length.Rel m35 = new Length.Rel(3.5, METER);
156         Speed.Abs speedLimit = new Speed.Abs(100, KM_PER_HOUR);
157 
158         Shoulder sL = new Shoulder(link, "sL", new Length.Rel(9.0, METER), m10, m10);
159 
160         Lane laneELL =
161             new NoTrafficLane(link, "ELL", new Length.Rel(8.25, METER), new Length.Rel(8.25, METER), m05, m05);
162         Lane laneL1 =
163             new Lane(link, "L1", new Length.Rel(6.25, METER), new Length.Rel(6.25, METER), m35, m35, null,
164                 LongitudinalDirectionality.BACKWARD, speedLimit, new OvertakingConditions.LeftAndRight());
165         Lane laneL2 =
166             new Lane(link, "L2", new Length.Rel(2.75, METER), new Length.Rel(2.75, METER), m35, m35, null,
167                 LongitudinalDirectionality.BACKWARD, speedLimit, new OvertakingConditions.LeftAndRight());
168         Lane laneELM =
169             new NoTrafficLane(link, "ELM", new Length.Rel(0.75, METER), new Length.Rel(0.75, METER), m05, m05);
170 
171         Shoulder sM = new Shoulder(link, "sM", new Length.Rel(0.0, METER), m10, m10);
172 
173         Lane laneERM =
174             new NoTrafficLane(link, "ERM", new Length.Rel(-0.75, METER), new Length.Rel(-0.75, METER), m05, m05);
175         Lane laneR2 =
176             new Lane(link, "R2", new Length.Rel(-2.75, METER), new Length.Rel(-2.75, METER), m35, m35, null,
177                 LongitudinalDirectionality.FORWARD, speedLimit, new OvertakingConditions.LeftAndRight());
178         Lane laneR1 =
179             new Lane(link, "R1", new Length.Rel(-6.25, METER), new Length.Rel(-6.25, METER), m35, m35, null,
180                 LongitudinalDirectionality.FORWARD, speedLimit, new OvertakingConditions.LeftAndRight());
181         Lane laneERR =
182             new NoTrafficLane(link, "ERR", new Length.Rel(-8.25, METER), new Length.Rel(-8.25, METER), m05, m05);
183 
184         Shoulder sR = new Shoulder(link, "sR", new Length.Rel(-9.0, METER), m10, m10);
185 
186         try
187         {
188             new LaneAnimation(laneELL, this.simulator, Color.GRAY);
189             new LaneAnimation(laneL1, this.simulator, Color.GRAY);
190             new LaneAnimation(laneL2, this.simulator, Color.GRAY);
191             new LaneAnimation(laneELM, this.simulator, Color.GRAY);
192             new LaneAnimation(laneERM, this.simulator, Color.GRAY);
193             new LaneAnimation(laneR2, this.simulator, Color.GRAY);
194             new LaneAnimation(laneR1, this.simulator, Color.GRAY);
195             new LaneAnimation(laneERR, this.simulator, Color.GRAY);
196 
197             new ShoulderAnimation(sL, this.simulator, Color.GREEN);
198             new ShoulderAnimation(sM, this.simulator, Color.GREEN);
199             new ShoulderAnimation(sR, this.simulator, Color.GREEN);
200         }
201         catch (NamingException | RemoteException ne)
202         {
203             //
204         }
205     }
206 
207     /** {@inheritDoc} */
208     @Override
209     public final SimulatorInterface<Abs<TimeUnit>, Rel<TimeUnit>, OTSSimTimeDouble> getSimulator()
210         throws RemoteException
211     {
212         return this.simulator;
213     }
214 }