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