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.geotools.referencing.crs.DefaultEngineeringCRS;
12  import org.opengis.referencing.crs.CoordinateReferenceSystem;
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.network.LongitudinalDirectionality;
17  import org.opentrafficsim.core.network.NetworkException;
18  import org.opentrafficsim.core.network.factory.LaneAnimation;
19  import org.opentrafficsim.core.network.factory.Link;
20  import org.opentrafficsim.core.network.factory.Node;
21  import org.opentrafficsim.core.network.factory.ShoulderAnimation;
22  import org.opentrafficsim.core.network.geotools.LinearGeometry;
23  import org.opentrafficsim.core.network.lane.Lane;
24  import org.opentrafficsim.core.network.lane.Shoulder;
25  import org.opentrafficsim.core.unit.FrequencyUnit;
26  import org.opentrafficsim.core.unit.LengthUnit;
27  import org.opentrafficsim.core.unit.TimeUnit;
28  import org.opentrafficsim.core.value.vdouble.scalar.DoubleScalar;
29  import org.opentrafficsim.core.value.vdouble.scalar.DoubleScalar.Abs;
30  import org.opentrafficsim.core.value.vdouble.scalar.DoubleScalar.Rel;
31  
32  import com.vividsolutions.jts.geom.Coordinate;
33  import com.vividsolutions.jts.geom.GeometryFactory;
34  import com.vividsolutions.jts.geom.LineString;
35  
36  /**
37   * <p>
38   * Copyright (c) 2013-2014 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/node/13">OpenTrafficSim License</a>.
40   * <p>
41   * @version Oct 17, 2014 <br>
42   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
43   */
44  /** */
45  public class TestModel implements OTSModelInterface
46  {
47      /** */
48      private static final long serialVersionUID = 1L;
49  
50      /** simulator. */
51      private OTSSimulatorInterface simulator;
52  
53      /** {@inheritDoc} */
54      @Override
55      public final void constructModel(final SimulatorInterface<Abs<TimeUnit>, Rel<TimeUnit>, OTSSimTimeDouble> pSimulator)
56              throws SimRuntimeException, RemoteException
57      {
58          this.simulator = (OTSSimulatorInterface) pSimulator;
59  
60          // create a small graph and a road around it.
61          GeometryFactory factory = new GeometryFactory();
62          CoordinateReferenceSystem crs = DefaultEngineeringCRS.CARTESIAN_2D;
63  
64          Node n0 = new Node("N0", new Coordinate(-25.0, 0.0));
65          Node n1 = new Node("N1", new Coordinate(0.0, 0.0));
66          Link l01 = new Link("L01", n0, n1, new DoubleScalar.Rel<LengthUnit>(25.0, LengthUnit.METER));
67          LineString ls01 =
68                  factory.createLineString(new Coordinate[]{new Coordinate(-25.0, 0.0), new Coordinate(0.0, 0.0)});
69  
70          Node n2 = new Node("N2", new Coordinate(25.0, 20.0));
71          Link l12 =
72                  new Link("L12", n1, n2,
73                          new DoubleScalar.Rel<LengthUnit>(Math.sqrt(25 * 25 + 20 * 20), LengthUnit.METER));
74          LineString ls12 =
75                  factory.createLineString(new Coordinate[]{new Coordinate(0.0, 0.0), new Coordinate(25.0, 20.0)});
76  
77          Node n3 = new Node("N3", new Coordinate(50.0, 0.0));
78          Link l23 =
79                  new Link("L23", n2, n3,
80                          new DoubleScalar.Rel<LengthUnit>(Math.sqrt(25 * 25 + 20 * 20), LengthUnit.METER));
81          LineString ls23 =
82                  factory.createLineString(new Coordinate[]{new Coordinate(25.0, 20.0), new Coordinate(50.0, 0.0)});
83  
84          Node n4 = new Node("N4", new Coordinate(75.0, -20.0));
85          Link l34 =
86                  new Link("L34", n3, n4,
87                          new DoubleScalar.Rel<LengthUnit>(Math.sqrt(25 * 25 + 20 * 20), LengthUnit.METER));
88          LineString ls34 =
89                  factory.createLineString(new Coordinate[]{new Coordinate(50.0, 0.0), new Coordinate(75.0, -20.0)});
90  
91          Node n5 = new Node("N5", new Coordinate(100.0, 0.0));
92          Link l45 =
93                  new Link("L45", n4, n5,
94                          new DoubleScalar.Rel<LengthUnit>(Math.sqrt(25 * 25 + 20 * 20), LengthUnit.METER));
95          LineString ls45 =
96                  factory.createLineString(new Coordinate[]{new Coordinate(75.0, -20.0), new Coordinate(100.0, 0.0)});
97  
98          Node n6 = new Node("N6", new Coordinate(125.0, 0.0));
99          Link l56 = new Link("L56", n5, n6, new DoubleScalar.Rel<LengthUnit>(25.0, LengthUnit.METER));
100         LineString ls56 =
101                 factory.createLineString(new Coordinate[]{new Coordinate(100.0, 0.0), new Coordinate(125.0, 0.0)});
102 
103         Node n7 = new Node("N7", new Coordinate(300.0, 0.0));
104         Link l67 =
105                 new Link("L67", n6, n7, new DoubleScalar.Rel<LengthUnit>(75.0 + 4.0 * Math.sqrt(25 * 25 + 20 * 20),
106                         LengthUnit.METER));
107         LineString ls67 =
108                 factory.createLineString(new Coordinate[]{new Coordinate(125.0, 0.0), new Coordinate(150.0, 0.0),
109                         new Coordinate(175.0, 20.0), new Coordinate(200.0, 0.0), new Coordinate(225.0, -20.0),
110                         new Coordinate(250.0, 0.0), new Coordinate(300.0, 0.0)});
111 
112         try
113         {
114             new LinearGeometry(l01, ls01, crs);
115             new LinearGeometry(l12, ls12, crs);
116             new LinearGeometry(l23, ls23, crs);
117             new LinearGeometry(l34, ls34, crs);
118             new LinearGeometry(l45, ls45, crs);
119             new LinearGeometry(l56, ls56, crs);
120             new LinearGeometry(l67, ls67, crs);
121         }
122         catch (NetworkException ne)
123         {
124             throw new SimRuntimeException(ne);
125         }
126 
127         try
128         {
129             add2x2Lanes(l01);
130             add2x2Lanes(l12);
131             add2x2Lanes(l23);
132             add2x2Lanes(l34);
133             add2x2Lanes(l45);
134             add2x2Lanes(l56);
135             add2x2Lanes(l67);
136         }
137         catch (NetworkException exception)
138         {
139             exception.printStackTrace();
140         }
141     }
142 
143     /**
144      * The road is as follows; The design line goes from left to right.
145      * 
146      * <pre>
147      * ----------------------- +9.50
148      * SSSSS Shoulder SL SSSSS +9.00 (width = 1.0)
149      * ----------------------- +8.50
150      * EEEEE Emergency ELL EEE +8.25 (width = 0.5)
151      * ----------------------- +8.00
152      * LLLLL Lane L1 LLLLLLLLL +6.25 (width = 3.5)
153      * ----------------------- +4.50
154      * LLLLL Lane L2 LLLLLLLLL +2.75 (width = 3.5)
155      * ----------------------- +1.00
156      * EEEEE Emergency ELM EEE +0.75 (width = 0.5)
157      * ----------------------- +0.50
158      * SSSSS Shoulder SM SSSSS +0.00 (width = 1.0)
159      * ----------------------- -0.50
160      * EEEEE Emergency ERM EEE -0.75 (width = 0.5)
161      * ----------------------- -1.00
162      * LLLLL Lane R2 LLLLLLLLL -2.75 (width = 3.5)
163      * ----------------------- -4.50
164      * LLLLL Lane R1 LLLLLLLLL -6.25 (width = 3.5)
165      * ----------------------- -8.00
166      * EEEEE Emergency ERR EEE -8.25 (width = 0.5)
167      * ----------------------- -8.50
168      * SSSSS Shoulder SR SSSSS -9.00 (width = 1.0)
169      * ----------------------- -9.50
170      * </pre>
171      *
172      * <br>
173      * @param link link.
174      * @throws NetworkException
175      */
176     private void add2x2Lanes(final Link link) throws NetworkException
177     {
178         // four lanes, grass underneath, lines between lane1-2 and lane 2-3, barrier between lane 2-3
179         // lane is 3.5 meters wide. gap between 3-4 is one meter. outside 0.5 meters on both sides
180         DoubleScalar.Rel<LengthUnit> m05 = new DoubleScalar.Rel<LengthUnit>(0.5, LengthUnit.METER);
181         DoubleScalar.Rel<LengthUnit> m10 = new DoubleScalar.Rel<LengthUnit>(1.0, LengthUnit.METER);
182         DoubleScalar.Rel<LengthUnit> m35 = new DoubleScalar.Rel<LengthUnit>(3.5, LengthUnit.METER);
183         DoubleScalar.Abs<FrequencyUnit> f0 = new DoubleScalar.Abs<FrequencyUnit>(0.0, FrequencyUnit.PER_HOUR);
184         DoubleScalar.Abs<FrequencyUnit> f200 = new DoubleScalar.Abs<FrequencyUnit>(200.0, FrequencyUnit.PER_HOUR);
185 
186         Shoulder sL = new Shoulder(link, new DoubleScalar.Rel<LengthUnit>(9.0, LengthUnit.METER), m10, m10);
187 
188         Lane laneELL =
189                 new Lane(link, new DoubleScalar.Rel<LengthUnit>(8.25, LengthUnit.METER),
190                         new DoubleScalar.Rel<LengthUnit>(8.25, LengthUnit.METER), m05, m05, null,
191                         LongitudinalDirectionality.NONE, f0);
192         Lane laneL1 =
193                 new Lane(link, new DoubleScalar.Rel<LengthUnit>(6.25, LengthUnit.METER),
194                         new DoubleScalar.Rel<LengthUnit>(6.25, LengthUnit.METER), m35, m35, null,
195                         LongitudinalDirectionality.BACKWARD, f200);
196         Lane laneL2 =
197                 new Lane(link, new DoubleScalar.Rel<LengthUnit>(2.75, LengthUnit.METER),
198                         new DoubleScalar.Rel<LengthUnit>(2.75, LengthUnit.METER), m35, m35, null,
199                         LongitudinalDirectionality.BACKWARD, f200);
200         Lane laneELM =
201                 new Lane(link, new DoubleScalar.Rel<LengthUnit>(0.75, LengthUnit.METER),
202                         new DoubleScalar.Rel<LengthUnit>(0.75, LengthUnit.METER), m05, m05, null,
203                         LongitudinalDirectionality.NONE, f0);
204 
205         Shoulder sM = new Shoulder(link, new DoubleScalar.Rel<LengthUnit>(0.0, LengthUnit.METER), m10, m10);
206 
207         Lane laneERM =
208                 new Lane(link, new DoubleScalar.Rel<LengthUnit>(-0.75, LengthUnit.METER),
209                         new DoubleScalar.Rel<LengthUnit>(-0.75, LengthUnit.METER), m05, m05, null,
210                         LongitudinalDirectionality.NONE, f0);
211         Lane laneR2 =
212                 new Lane(link, new DoubleScalar.Rel<LengthUnit>(-2.75, LengthUnit.METER),
213                         new DoubleScalar.Rel<LengthUnit>(-2.75, LengthUnit.METER), m35, m35, null,
214                         LongitudinalDirectionality.FORWARD, f200);
215         Lane laneR1 =
216                 new Lane(link, new DoubleScalar.Rel<LengthUnit>(-6.25, LengthUnit.METER),
217                         new DoubleScalar.Rel<LengthUnit>(-6.25, LengthUnit.METER), m35, m35, null,
218                         LongitudinalDirectionality.FORWARD, f200);
219         Lane laneERR =
220                 new Lane(link, new DoubleScalar.Rel<LengthUnit>(-8.25, LengthUnit.METER),
221                         new DoubleScalar.Rel<LengthUnit>(-8.25, LengthUnit.METER), m05, m05, null,
222                         LongitudinalDirectionality.NONE, f0);
223 
224         Shoulder sR = new Shoulder(link, new DoubleScalar.Rel<LengthUnit>(-9.0, LengthUnit.METER), m10, m10);
225 
226         try
227         {
228             new LaneAnimation(laneELL, this.simulator, Color.GRAY);
229             new LaneAnimation(laneL1, this.simulator, Color.GRAY);
230             new LaneAnimation(laneL2, this.simulator, Color.GRAY);
231             new LaneAnimation(laneELM, this.simulator, Color.GRAY);
232             new LaneAnimation(laneERM, this.simulator, Color.GRAY);
233             new LaneAnimation(laneR2, this.simulator, Color.GRAY);
234             new LaneAnimation(laneR1, this.simulator, Color.GRAY);
235             new LaneAnimation(laneERR, this.simulator, Color.GRAY);
236 
237             new ShoulderAnimation(sL, this.simulator);
238             new ShoulderAnimation(sM, this.simulator);
239             new ShoulderAnimation(sR, this.simulator);
240         }
241         catch (NamingException | RemoteException ne)
242         {
243             //
244         }
245     }
246 
247     /** {@inheritDoc} */
248     @Override
249     public final SimulatorInterface<Abs<TimeUnit>, Rel<TimeUnit>, OTSSimTimeDouble> getSimulator()
250             throws RemoteException
251     {
252         return this.simulator;
253     }
254 }