1 package org.opentrafficsim.imb.demo;
2
3 import java.awt.geom.Point2D;
4 import java.awt.geom.Rectangle2D;
5 import java.awt.geom.Rectangle2D.Double;
6 import java.io.IOException;
7 import java.io.InputStream;
8 import java.io.Serializable;
9 import java.net.URL;
10 import java.util.ArrayList;
11 import java.util.List;
12
13 import javax.naming.NamingException;
14 import javax.swing.JPanel;
15 import javax.swing.SwingUtilities;
16 import javax.xml.parsers.ParserConfigurationException;
17
18 import nl.javel.gisbeans.io.esri.CoordinateTransform;
19 import nl.tudelft.simulation.dsol.SimRuntimeException;
20 import nl.tudelft.simulation.dsol.animation.D2.GisRenderable2D;
21 import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
22 import nl.tudelft.simulation.language.Throw;
23 import nl.tudelft.simulation.language.io.URLResource;
24
25 import org.djunits.unit.TimeUnit;
26 import org.djunits.value.vdouble.scalar.Duration;
27 import org.djunits.value.vdouble.scalar.Time;
28 import org.opentrafficsim.base.modelproperties.CompoundProperty;
29 import org.opentrafficsim.base.modelproperties.Property;
30 import org.opentrafficsim.base.modelproperties.PropertyException;
31 import org.opentrafficsim.core.dsol.OTSDEVSSimulatorInterface;
32 import org.opentrafficsim.core.dsol.OTSModelInterface;
33 import org.opentrafficsim.core.dsol.OTSSimTimeDouble;
34 import org.opentrafficsim.core.geometry.OTSGeometryException;
35 import org.opentrafficsim.core.gis.TransformWGS84DutchRDNew;
36 import org.opentrafficsim.core.gtu.GTUException;
37 import org.opentrafficsim.core.gtu.animation.GTUColorer;
38 import org.opentrafficsim.core.network.NetworkException;
39 import org.opentrafficsim.core.network.OTSNetwork;
40 import org.opentrafficsim.imb.IMBException;
41 import org.opentrafficsim.imb.connector.OTSIMBConnector;
42 import org.opentrafficsim.imb.transceiver.urbanstrategy.GTUTransceiver;
43 import org.opentrafficsim.imb.transceiver.urbanstrategy.LaneGTUTransceiver;
44 import org.opentrafficsim.imb.transceiver.urbanstrategy.LinkGTUTransceiver;
45 import org.opentrafficsim.imb.transceiver.urbanstrategy.NetworkTransceiver;
46 import org.opentrafficsim.imb.transceiver.urbanstrategy.NodeTransceiver;
47 import org.opentrafficsim.imb.transceiver.urbanstrategy.SensorGTUTransceiver;
48 import org.opentrafficsim.imb.transceiver.urbanstrategy.SimulatorTransceiver;
49 import org.opentrafficsim.road.network.factory.xml.XmlNetworkLaneParser;
50 import org.opentrafficsim.simulationengine.AbstractWrappableAnimation;
51 import org.opentrafficsim.simulationengine.OTSSimulationException;
52 import org.opentrafficsim.simulationengine.SimpleAnimator;
53 import org.opentrafficsim.simulationengine.SimpleSimulatorInterface;
54 import org.xml.sax.SAXException;
55
56
57
58
59
60
61
62
63
64
65 public class A58IMB extends AbstractWrappableAnimation
66 {
67
68 private static final long serialVersionUID = 20161007L;
69
70
71 private A58Model model;
72
73
74
75
76
77
78 public static void main(final String[] args) throws SimRuntimeException
79 {
80 SwingUtilities.invokeLater(new Runnable()
81 {
82 @Override
83 public void run()
84 {
85 try
86 {
87 A58IMB n201Model = new A58IMB();
88
89 n201Model.buildAnimator(new Time(0.0, TimeUnit.SECOND), new Duration(0.0, TimeUnit.SECOND),
90 new Duration(10.0, TimeUnit.HOUR), new ArrayList<Property<?>>(), null, true);
91 }
92 catch (SimRuntimeException | NamingException | OTSSimulationException | PropertyException exception)
93 {
94 exception.printStackTrace();
95 }
96 }
97 });
98 }
99
100
101 @Override
102 public final String shortName()
103 {
104 return "Model A58";
105 }
106
107
108 @Override
109 public final String description()
110 {
111 return "Model A58 - IMB";
112 }
113
114
115 @Override
116 public final void stopTimersThreads()
117 {
118 super.stopTimersThreads();
119 }
120
121
122 @Override
123 protected final JPanel makeCharts(final SimpleSimulatorInterface simulator)
124 {
125 return null;
126 }
127
128
129 @Override
130 protected final OTSModelInterface makeModel(final GTUColorer colorer)
131 {
132 System.out.println("A58IMB.makeModel called");
133 this.model = new A58Model(getSavedUserModifiedProperties(), colorer, new OTSNetwork("A58 network"));
134 return this.model;
135 }
136
137
138
139
140 private List<Property<?>> getSavedUserModifiedProperties()
141 {
142 return this.savedUserModifiedProperties;
143 }
144
145
146 @Override
147 protected final Double makeAnimationRectangle()
148 {
149 return new Rectangle2D.Double(150000, 385000, 5500, 5000);
150 }
151
152
153 @Override
154 public final String toString()
155 {
156 return "A58 network - IMB []";
157 }
158
159
160
161
162
163
164
165
166
167
168
169
170
171 class A58Model implements OTSModelInterface
172 {
173
174 private static final long serialVersionUID = 20141121L;
175
176
177 private OTSDEVSSimulatorInterface simulator;
178
179
180 private List<Property<?>> modelProperties = null;
181
182
183 private final OTSNetwork network;
184
185
186 OTSIMBConnector imbConnector;
187
188
189
190
191
192
193 A58Model(final List<Property<?>> modelProperties, final GTUColorer gtuColorer, final OTSNetwork network)
194 {
195 this.modelProperties = modelProperties;
196 this.network = network;
197 }
198
199
200 @Override
201 public final void constructModel(
202 final SimulatorInterface<Time, Duration, OTSSimTimeDouble> pSimulator)
203 throws SimRuntimeException
204 {
205 System.out.println("A58IMB: constructModel called; Connecting to IMB");
206 this.simulator = (OTSDEVSSimulatorInterface) pSimulator;
207 SimpleAnimator imbAnimator = (SimpleAnimator) pSimulator;
208 try
209 {
210 CompoundProperty imbSettings = null;
211 for (Property<?> property : this.modelProperties)
212 {
213 if (property.getKey().equals(OTSIMBConnector.PROPERTY_KEY))
214 {
215 imbSettings = (CompoundProperty) property;
216 }
217 }
218 System.out.println("link count " + this.network.getLinkMap().size());
219 Throw.whenNull(imbSettings, "IMB Settings not found in properties");
220 this.imbConnector = OTSIMBConnector.create(imbSettings, "OTS");
221 new NetworkTransceiver(this.imbConnector, imbAnimator, this.network);
222 new NodeTransceiver(this.imbConnector, imbAnimator, this.network);
223 new LinkGTUTransceiver(this.imbConnector, imbAnimator, this.network);
224 new LaneGTUTransceiver(this.imbConnector, imbAnimator, this.network);
225 new GTUTransceiver(this.imbConnector, imbAnimator, this.network);
226 new SensorGTUTransceiver(this.imbConnector, imbAnimator, this.network);
227 new SimulatorTransceiver(this.imbConnector, imbAnimator);
228 }
229 catch (IMBException exception)
230 {
231 throw new SimRuntimeException(exception);
232 }
233
234
235 InputStream stream = URLResource.getResourceAsStream("/A58v1.xml");
236 XmlNetworkLaneParser nlp = new XmlNetworkLaneParser(this.simulator);
237 try
238 {
239 nlp.build(stream, this.network);
240
241
242 }
243 catch (NetworkException | ParserConfigurationException | SAXException | IOException | NamingException | GTUException
244 | OTSGeometryException exception)
245 {
246 exception.printStackTrace();
247 }
248
249
250
251
252
253
254
255
256
257
258
259
260
261 URL gisURL = URLResource.getResource("/A58/map.xml");
262 System.err.println("GIS-map file: " + gisURL.toString());
263 CoordinateTransform rdto0 = new CoordinateTransformRD(0, 0);
264 new GisRenderable2D(this.simulator, gisURL, rdto0);
265 }
266
267
268 @Override
269 public SimulatorInterface<Time, Duration, OTSSimTimeDouble> getSimulator()
270
271 {
272 return this.simulator;
273 }
274
275
276 @Override
277 public final String toString()
278 {
279 return "A58Model [simulator=" + this.simulator + "]";
280 }
281
282 }
283
284
285
286
287 class CoordinateTransformRD implements CoordinateTransform, Serializable
288 {
289
290 private static final long serialVersionUID = 20141017L;
291
292
293 final double dx;
294
295
296 final double dy;
297
298
299
300
301
302 public CoordinateTransformRD(final double dx, final double dy)
303 {
304 this.dx = dx;
305 this.dy = dy;
306 }
307
308
309 @Override
310 public float[] floatTransform(double x, double y)
311 {
312 double[] d = doubleTransform(x, y);
313 return new float[] { (float) d[0], (float) d[1] };
314 }
315
316
317 @Override
318 public double[] doubleTransform(double x, double y)
319 {
320 try
321 {
322 Point2D c = TransformWGS84DutchRDNew.fromWGS84(x, y);
323 return new double[] { c.getX() - this.dx, c.getY() - this.dy };
324 }
325 catch (Exception exception)
326 {
327 System.err.println(exception.getMessage());
328 return new double[] { 0, 0 };
329 }
330 }
331
332
333 @Override
334 public final String toString()
335 {
336 return "CoordinateTransformRD [dx=" + this.dx + ", dy=" + this.dy + "]";
337 }
338 }
339
340 }