aboutsummaryrefslogtreecommitdiffstats
path: root/Domains/crossingScenario/src/crossingScenario/run/DrawScenario.java
blob: 7f90beb22769e1fef0037fd2c6dfabb8234fa633 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package crossingScenario.run;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.stream.Collectors;

import javax.imageio.ImageIO;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;

import crossingScenario.Actor;
import crossingScenario.CollisionExists;
import crossingScenario.CrossingScenario;
import crossingScenario.CrossingScenarioPackage;
import crossingScenario.Lane;
import crossingScenario.Lane_Horizontal;
import crossingScenario.Lane_Vertical;
import crossingScenario.Relation;
import crossingScenario.VisionBlocked;

public class DrawScenario {
	public static final int SIZE = 1000;
	
	public static void main(String[] args) throws IOException {
		drawScenario("outputs/models/4.xmi");
	}

	public static File drawScenario(String pathToXmi) throws IOException {

		Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("*", new XMIResourceFactoryImpl());
		EPackage.Registry.INSTANCE.put(CrossingScenarioPackage.eNS_URI, CrossingScenarioPackage.eINSTANCE);
		ResourceSet rs = new ResourceSetImpl();
		Resource res = rs.getResource(URI.createFileURI(pathToXmi), true);

		// Initialise board
		CrossingScenario cs = ((CrossingScenario) res.getContents().get(0));
		
		double multiplier = SIZE / cs.getXSize() / 2.0;
		
		double added = SIZE / 10;
		int xSize =  SIZE;
		int xAdded = (int) (xSize  + added*2);
		int ySize =  (int) (cs.getYSize() * 2 * multiplier);
		int yAdded = (int) (ySize  + added*2);

		// initialize + basic shapes
		final BufferedImage image = new BufferedImage(xAdded, yAdded, BufferedImage.TYPE_INT_ARGB);
		final Graphics2D g = image.createGraphics();// Untested, uncompiled code

		// flip y axis
		int m = SIZE / 2;
		g.translate(xAdded/2, yAdded/2);
		g.scale(1, -1);
		
		//background
		g.setStroke(new BasicStroke(3));
		g.setPaint(Color.LIGHT_GRAY);
		g.fillRect(-xSize/2, -ySize/2, xSize, ySize);

		//lanes
		g.setPaint(Color.RED);
		//origin
		g.drawRect(-5, -5, 10, 10);
		//Axes
		g.drawLine(-xSize/2, 0, xSize/2, 0);
		g.drawLine(0, -ySize/2, 0, ySize/2);
		for (Lane l : cs.getLanes()) {
			int ref = (int) ((l.getReferenceCoord() +5)* multiplier);
			if (l instanceof Lane_Horizontal) {
				g.drawLine(-xSize/2, ref, xSize/2, ref);
			}
			if (l instanceof Lane_Vertical) {
				g.drawLine(ref, -ySize/2, ref, ySize/2);				
			}
		}
		
		for (Actor a : cs.getActors()) {
			//Draw actor
			int left = (int) ((a.getXPos()-a.getWidth()/2) * multiplier);
			int bot = (int) ((a.getYPos()-a.getLength()/2) * multiplier);
			int width = (int) (a.getWidth() * multiplier);
			int length = (int) (a.getLength() * multiplier);
//			System.out.println(left);
//			System.out.println(bot);
//			System.out.println(width);
//			System.out.println(length);
//			System.out.println();
			if (!cs.getRelations().stream().filter(r -> r instanceof VisionBlocked)
					.filter(r -> ((VisionBlocked) r).getBlockedBy().equals(a)).
					collect(Collectors.toList()).isEmpty())
				g.setPaint(Color.BLACK);
			else
				g.setPaint(Color.GREEN);

			g.drawRect(left, bot, width, length);
		}

		g.setPaint(Color.BLUE);
		for (Relation ce : cs.getRelations().stream().
				filter(r -> r instanceof CollisionExists).collect(Collectors.toList())) {
			Actor a1 = ce.getSource();
			Actor a2 = ce.getTarget();
			Double t = ((CollisionExists) ce).getCollisionTime();
			
			for (Actor a : new Actor[] {a1, a2}) {
				//Draw final pos
				g.setStroke(new BasicStroke(3));
				int left = (int) (((a.getXPos() + (t * a.getXSpeed()))-a.getWidth()/2) * multiplier);
				int bot = (int) (((a.getYPos() + (t * a.getYSpeed()))-a.getLength()/2) * multiplier);
				int width = (int) (a.getWidth() * multiplier);
				int length = (int) (a.getLength() * multiplier);

				
			
				//Draw line
				g.drawRect(left, bot, width, length);
				g.setStroke(new BasicStroke(1));
				int x1 = (int) (a.getXPos() * multiplier);
				int y1 = (int) (a.getYPos() * multiplier);
				int x2 = (int) ((a.getXPos() + (t * a.getXSpeed())) * multiplier);
				int y2 = (int) ((a.getYPos() + (t * a.getYSpeed())) * multiplier);
				g.drawLine(x1, y1, x2, y2);
			
			}			
		}
		g.dispose();
		
		File f = new File("outputs/drawnModel.png");
		try {
			ImageIO.write(image, "png", f);
		} catch (IOException e) {
			e.printStackTrace();
		}

//		Desktop.getDesktop().open(f);
		System.out.println("finished!");
		return f;
	}
}