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