diff options
Diffstat (limited to 'Domains/crossingScenario')
-rw-r--r-- | Domains/crossingScenario/src/crossingScenario/run/DrawScenario.java | 210 |
1 files changed, 210 insertions, 0 deletions
diff --git a/Domains/crossingScenario/src/crossingScenario/run/DrawScenario.java b/Domains/crossingScenario/src/crossingScenario/run/DrawScenario.java new file mode 100644 index 00000000..02abbd28 --- /dev/null +++ b/Domains/crossingScenario/src/crossingScenario/run/DrawScenario.java | |||
@@ -0,0 +1,210 @@ | |||
1 | package crossingScenario.run; | ||
2 | |||
3 | import java.awt.BasicStroke; | ||
4 | import java.awt.Color; | ||
5 | import java.awt.Desktop; | ||
6 | import java.awt.Graphics2D; | ||
7 | import java.awt.image.BufferedImage; | ||
8 | import java.io.File; | ||
9 | import java.io.IOException; | ||
10 | import java.io.PrintWriter; | ||
11 | import java.nio.file.Path; | ||
12 | import java.util.ArrayList; | ||
13 | import java.util.List; | ||
14 | import java.util.regex.Matcher; | ||
15 | import java.util.regex.Pattern; | ||
16 | |||
17 | import javax.imageio.ImageIO; | ||
18 | |||
19 | import org.eclipse.emf.common.util.URI; | ||
20 | import org.eclipse.emf.ecore.EPackage; | ||
21 | import org.eclipse.emf.ecore.resource.Resource; | ||
22 | import org.eclipse.emf.ecore.resource.ResourceSet; | ||
23 | import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; | ||
24 | import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl; | ||
25 | |||
26 | import crossingScenario.Actor; | ||
27 | import crossingScenario.CrossingScenario; | ||
28 | import crossingScenario.CrossingScenarioPackage; | ||
29 | import crossingScenario.Lane; | ||
30 | import crossingScenario.Lane_Horizontal; | ||
31 | import crossingScenario.Lane_Vertical; | ||
32 | |||
33 | public class DrawScenario { | ||
34 | public static final int SIZE = 1000; | ||
35 | |||
36 | public static void main(String[] args) throws IOException { | ||
37 | drawScenario("outputs/models/1.xmi"); | ||
38 | } | ||
39 | |||
40 | public static File drawScenario(String pathToXmi) throws IOException { | ||
41 | |||
42 | Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("*", new XMIResourceFactoryImpl()); | ||
43 | EPackage.Registry.INSTANCE.put(CrossingScenarioPackage.eNS_URI, CrossingScenarioPackage.eINSTANCE); | ||
44 | ResourceSet rs = new ResourceSetImpl(); | ||
45 | Resource res = rs.getResource(URI.createFileURI(pathToXmi), true); | ||
46 | |||
47 | // Initialise board | ||
48 | CrossingScenario cs = ((CrossingScenario) res.getContents().get(0)); | ||
49 | |||
50 | double multiplier = SIZE / cs.getXSize() / 2.0; | ||
51 | |||
52 | double added = SIZE / 10; | ||
53 | int xSize = SIZE; | ||
54 | int xAdded = (int) (xSize + added*2); | ||
55 | int ySize = (int) (cs.getYSize() * 2 * multiplier); | ||
56 | int yAdded = (int) (ySize + added*2); | ||
57 | |||
58 | // initialize + basic shapes | ||
59 | final BufferedImage image = new BufferedImage(xAdded, yAdded, BufferedImage.TYPE_INT_ARGB); | ||
60 | final Graphics2D g = image.createGraphics();// Untested, uncompiled code | ||
61 | |||
62 | // flip y axis | ||
63 | int m = SIZE / 2; | ||
64 | g.translate(xAdded/2, yAdded/2); | ||
65 | g.scale(1, -1); | ||
66 | |||
67 | //background | ||
68 | g.setStroke(new BasicStroke(3)); | ||
69 | g.setPaint(Color.LIGHT_GRAY); | ||
70 | g.fillRect(-xSize/2, -ySize/2, xSize, ySize); | ||
71 | |||
72 | //lanes | ||
73 | g.setPaint(Color.RED); | ||
74 | g.drawRect(-5, -5, 10, 10); | ||
75 | for (Lane l : cs.getLanes()) { | ||
76 | int ref = (int) (l.getReferenceCoord() * multiplier); | ||
77 | if (l instanceof Lane_Horizontal) { | ||
78 | g.drawLine(-xSize/2, ref, xSize/2, ref); | ||
79 | } | ||
80 | if (l instanceof Lane_Vertical) { | ||
81 | g.drawLine(ref, -ySize/2, ref, ySize/2); | ||
82 | } | ||
83 | } | ||
84 | |||
85 | g.setPaint(Color.GREEN); | ||
86 | for (Actor a : cs.getActors()) { | ||
87 | //Draw actor | ||
88 | int left = (int) ((a.getXPos()-a.getWidth()/2) * multiplier); | ||
89 | int bot = (int) ((a.getYPos()-a.getLength()/2) * multiplier); | ||
90 | int width = (int) (a.getWidth() * multiplier); | ||
91 | int length = (int) (a.getLength() * multiplier); | ||
92 | // System.out.println(left); | ||
93 | // System.out.println(bot); | ||
94 | // System.out.println(width); | ||
95 | // System.out.println(length); | ||
96 | // System.out.println(); | ||
97 | g.drawRect(left, bot, width, length); | ||
98 | |||
99 | //Draw Speed? | ||
100 | |||
101 | //Draw location at collision? | ||
102 | } | ||
103 | |||
104 | // int numVars = vals.size(); | ||
105 | // final int numActors = (numVars - 1) / 4; | ||
106 | // | ||
107 | // double time = 0; | ||
108 | // if (!is3) | ||
109 | // time = vals.get(numVars - 1); | ||
110 | // else | ||
111 | // time = vals.get(numVars / 2); | ||
112 | // | ||
113 | // for (int i = 0; i < numActors; i++) { | ||
114 | // | ||
115 | // int x = 0, y = 0, vx = 0, vy = 0; | ||
116 | // if (!is3) { | ||
117 | // int first = i * 4; | ||
118 | // x = (int) (vals.get(first) * 100); | ||
119 | // y = (int) (vals.get(first + 1) * 100); | ||
120 | // vx = (int) (vals.get(first + 2) * 100); | ||
121 | // vy = (int) (vals.get(first + 3) * 100); | ||
122 | // } else { | ||
123 | // int first = i * 2; | ||
124 | // x = (int) (vals.get(first) * 100); | ||
125 | // y = (int) (vals.get(first + 1) * 100); | ||
126 | // vx = (int) (vals.get(first + 1 + numVars / 2) * 100); | ||
127 | // vy = (int) (vals.get(first + 2 + numVars / 2) * 100); | ||
128 | // } | ||
129 | // g.setStroke(new BasicStroke(10)); | ||
130 | // g.setPaint(Color.BLACK); | ||
131 | // g.drawOval(x - 2, y - 2, 4, 4); | ||
132 | // boolean isEGO = (!is3 && i == 0) || (is3 && i == numActors - 2); | ||
133 | // boolean isPED = (i == numActors - 1); | ||
134 | // if (isEGO) { | ||
135 | // // EGO | ||
136 | // g.drawRect(x - 50, y - 150, 100, 300); | ||
137 | // g.setPaint(Color.PINK); | ||
138 | // if (acceleration) | ||
139 | // g.drawRect((int) (x + (vx * time * time) - 50), (int) (y + (vy * time * time) - 150), 100, 300); | ||
140 | // else | ||
141 | // g.drawRect((int) (x + (vx * time) - 50), (int) (y + (vy * time) - 150), 100, 300); | ||
142 | // } else if (isPED) { | ||
143 | // // PED | ||
144 | // g.drawRect(x - 50, y - 50, 100, 100); | ||
145 | // g.setPaint(Color.PINK); | ||
146 | // if (acceleration) | ||
147 | // g.drawRect((int) (x + (vx * time * time) - 50), (int) (y + (vy * time * time) - 50), 100, 100); | ||
148 | // else | ||
149 | // g.drawRect((int) (x + (vx * time) - 50), (int) (y + (vy * time) - 50), 100, 100); | ||
150 | // } else { | ||
151 | // // CAR | ||
152 | // g.drawRect(x - 50, y - 150, 100, 300); | ||
153 | // } | ||
154 | // | ||
155 | // g.setStroke(new BasicStroke(4)); | ||
156 | // g.setPaint(Color.GREEN); | ||
157 | // g.drawLine(x, y, x + vx, y + vy); | ||
158 | // | ||
159 | // } | ||
160 | g.dispose(); | ||
161 | |||
162 | File f = new File("outputs/drawnModel.png"); | ||
163 | try { | ||
164 | ImageIO.write(image, "png", f); | ||
165 | } catch (IOException e) { | ||
166 | e.printStackTrace(); | ||
167 | } | ||
168 | |||
169 | // Desktop.getDesktop().open(f); | ||
170 | System.out.println("finished!"); | ||
171 | return f; | ||
172 | |||
173 | // CrossingScenario cs = ((CrossingScenario) res.getContents().get(0)); | ||
174 | // | ||
175 | // for (Actor o : cs.getActors()) { | ||
176 | // String nodeName = "A(" + rndbl(o.getXPos(), 1) + "," + rndbl(o.getYPos(), 1) + ")"; | ||
177 | // printer.println(o.hashCode() + " " + nodeName); | ||
178 | // } | ||
179 | // | ||
180 | // for (Lane o : cs.getLanes()) { | ||
181 | // String prefix = ""; | ||
182 | // if (cs.getHorizontal_head().equals(o) || cs.getVertical_head().equals(o)) { | ||
183 | // prefix = "HEAD"; | ||
184 | // } | ||
185 | // String nodeName = prefix + "L(" + rndbl(o.getReferenceCoord(), 3) + ")" | ||
186 | // + o.eClass().getName().substring(5, 9); | ||
187 | // printer.println(o.hashCode() + " " + nodeName); | ||
188 | // } | ||
189 | // printer.println("#"); | ||
190 | // for (Lane o : cs.getLanes()) { | ||
191 | // if (o.getPrevLane() != null) { | ||
192 | // int curName = o.hashCode(); | ||
193 | // int curPrev = o.getPrevLane().hashCode(); | ||
194 | // double edgeLabel = rndbl(o.getPrevLane().getNumWidth(), 1); | ||
195 | // printer.println(curName + " " + curPrev + " " + edgeLabel); | ||
196 | // } | ||
197 | // } | ||
198 | // | ||
199 | // for (Actor o : cs.getActors()) { | ||
200 | // int actName = o.hashCode(); | ||
201 | // int lanName = o.getPlacedOn().hashCode(); | ||
202 | // printer.println(actName + " " + lanName); | ||
203 | // } | ||
204 | // | ||
205 | // printer.flush(); | ||
206 | // printer.close(); | ||
207 | // System.out.println("TGF CREATED"); | ||
208 | |||
209 | } | ||
210 | } | ||