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