1 package org.opentrafficsim.draw.road;
2
3 import java.awt.Color;
4 import java.awt.Graphics2D;
5 import java.awt.image.ImageObserver;
6 import java.io.Serializable;
7 import java.rmi.RemoteException;
8
9 import javax.naming.NamingException;
10
11 import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
12 import org.opentrafficsim.core.geometry.Bounds;
13 import org.opentrafficsim.core.geometry.DirectedPoint;
14 import org.opentrafficsim.core.geometry.OTSLine3D;
15 import org.opentrafficsim.draw.core.ClonableRenderable2DInterface;
16 import org.opentrafficsim.draw.core.PaintLine;
17 import org.opentrafficsim.draw.core.PaintPolygons;
18 import org.opentrafficsim.draw.core.TextAlignment;
19 import org.opentrafficsim.draw.core.TextAnimation;
20 import org.opentrafficsim.road.network.lane.Lane;
21
22 import nl.tudelft.simulation.dsol.animation.Locatable;
23 import nl.tudelft.simulation.dsol.animation.D2.Renderable2D;
24 import nl.tudelft.simulation.language.d2.Angle;
25 import nl.tudelft.simulation.naming.context.Contextualized;
26
27
28
29
30
31
32
33
34
35
36 public class LaneAnimation extends Renderable2D<Lane> implements ClonableRenderable2DInterface<Lane>, Serializable
37 {
38
39 private static final long serialVersionUID = 20141017L;
40
41
42 private final Color color;
43
44
45 private final Text text;
46
47
48
49
50
51
52
53
54
55 public LaneAnimation(final Lane lane, final OTSSimulatorInterface simulator, final Color color)
56 throws NamingException, RemoteException
57 {
58 super(lane, simulator);
59 this.color = color;
60 this.text = new Text(lane, lane.getParentLink().getId() + "." + lane.getId(), 0.0f, 0.0f, TextAlignment.CENTER,
61 Color.BLACK, simulator);
62 new CenterLineAnimation(new CenterLine(lane.getCenterLine()), simulator);
63 }
64
65
66
67
68 public final Text getText()
69 {
70 return this.text;
71 }
72
73
74 @Override
75 public final void paint(final Graphics2D graphics, final ImageObserver observer)
76 {
77 Lane lane = getSource();
78 if (this.color != null)
79 {
80 PaintPolygons.paintMultiPolygon(graphics, this.color, lane.getLocation(), lane.getContour(), true);
81 }
82 }
83
84
85 @Override
86 public void destroy(final Contextualized contextProvider)
87 {
88 super.destroy(contextProvider);
89 this.text.destroy(contextProvider);
90 }
91
92
93 @Override
94 @SuppressWarnings("checkstyle:designforextension")
95 public ClonableRenderable2DInterface<Lane> clone(final Lane newSource, final OTSSimulatorInterface newSimulator)
96 throws NamingException, RemoteException
97 {
98
99 return new LaneAnimation(newSource, newSimulator, this.color);
100 }
101
102
103 @Override
104 public final String toString()
105 {
106 return "LaneAnimation [lane = " + getSource().toString() + ", color=" + this.color + "]";
107 }
108
109
110
111
112 public static class CenterLine implements Locatable
113 {
114
115 private final OTSLine3D centerLine;
116
117
118
119
120
121 CenterLine(final OTSLine3D centerLine)
122 {
123 this.centerLine = centerLine;
124 }
125
126 @Override
127 public final DirectedPoint getLocation()
128 {
129 DirectedPoint dp = this.centerLine.getLocation();
130 return new DirectedPoint(dp.x, dp.y, dp.z + 0.1);
131 }
132
133 @Override
134 public final Bounds getBounds() throws RemoteException
135 {
136 return this.centerLine.getBounds();
137 }
138
139
140
141
142
143 public OTSLine3D getCenterLine()
144 {
145 return this.centerLine;
146 }
147
148 }
149
150
151
152
153 public static class CenterLineAnimation extends Renderable2D<CenterLine>
154 implements ClonableRenderable2DInterface<CenterLine>, Serializable
155 {
156
157 private static final Color COLOR = Color.MAGENTA.darker().darker();
158
159
160 private static final long serialVersionUID = 20180426L;
161
162
163
164
165
166
167
168
169 public CenterLineAnimation(final CenterLine centerLine, final OTSSimulatorInterface simulator)
170 throws NamingException, RemoteException
171 {
172 super(centerLine, simulator);
173 }
174
175 @Override
176 public final ClonableRenderable2DInterface<CenterLine> clone(final CenterLine newSource,
177 final OTSSimulatorInterface newSimulator) throws NamingException, RemoteException
178 {
179
180 return null;
181 }
182
183 @Override
184 public final void paint(final Graphics2D graphics, final ImageObserver observer)
185 {
186 PaintLine.paintLine(graphics, COLOR, 0.1, getSource().getLocation(), getSource().getCenterLine());
187 }
188
189 }
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204 public class Text extends TextAnimation
205 {
206
207 private static final long serialVersionUID = 20161211L;
208
209
210
211
212
213
214
215
216
217
218
219
220 public Text(final Locatable source, final String text, final float dx, final float dy,
221 final TextAlignment textPlacement, final Color color, final OTSSimulatorInterface simulator)
222 throws RemoteException, NamingException
223 {
224 super(source, text, dx, dy, textPlacement, color, simulator, TextAnimation.RENDERALWAYS);
225 }
226
227
228 @Override
229 @SuppressWarnings("checkstyle:designforextension")
230 public DirectedPoint getLocation()
231 {
232
233 DirectedPoint p = ((Lane) getSource()).getCenterLine().getLocationFractionExtended(0.5);
234 double a = Angle.normalizePi(p.getRotZ());
235 if (a > Math.PI / 2.0 || a < -0.99 * Math.PI / 2.0)
236 {
237 a += Math.PI;
238 }
239 return new DirectedPoint(p.x, p.y, Double.MAX_VALUE, 0.0, 0.0, a);
240 }
241
242
243 @Override
244 @SuppressWarnings("checkstyle:designforextension")
245 public TextAnimation clone(final Locatable newSource, final OTSSimulatorInterface newSimulator)
246 throws RemoteException, NamingException
247 {
248 return new Text(newSource, getText(), getDx(), getDy(), getTextAlignment(), getColor(), newSimulator);
249 }
250
251
252 @Override
253 public final String toString()
254 {
255 return "Text []";
256 }
257
258 }
259
260 }