AbstractWaypointModel.java 9 KB
Newer Older
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*
 * Copyright (c) 2005-2010 KOM – Multimedia Communications Lab
 *
 * This file is part of PeerfactSim.KOM.
 * 
 * PeerfactSim.KOM is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 * 
 * PeerfactSim.KOM is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with PeerfactSim.KOM.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package de.tud.kom.p2psim.impl.topology.waypoints;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;

import org.jgrapht.alg.DijkstraShortestPath;

import com.google.common.collect.Sets;

import de.tud.kom.p2psim.api.scenario.ConfigurationException;
import de.tud.kom.p2psim.api.scenario.Configurator;
import de.tud.kom.p2psim.api.topology.obstacles.ObstacleModel;
import de.tud.kom.p2psim.api.topology.waypoints.WaypointModel;
import de.tud.kom.p2psim.api.topology.waypoints.WaypointModelListener;
import de.tud.kom.p2psim.api.util.geo.maps.Map;
import de.tud.kom.p2psim.impl.scenario.simcfg2.annotations.After;
import de.tud.kom.p2psim.impl.scenario.simcfg2.annotations.Configure;
import de.tud.kom.p2psim.impl.topology.PositionVector;
import de.tud.kom.p2psim.impl.topology.waypoints.graph.DefaultWeightedEdgeRetrievableGraph;
import de.tud.kom.p2psim.impl.topology.waypoints.graph.Path;
import de.tud.kom.p2psim.impl.topology.waypoints.graph.Waypoint;
import de.tud.kom.p2psim.impl.util.Tuple;
import de.tud.kom.p2psim.impl.util.geo.maps.MapChangeListener;
import de.tud.kom.p2psim.impl.util.geo.maps.MapLoader;
import de.tud.kom.p2psim.impl.util.structures.KdTree;
import de.tud.kom.p2psim.impl.util.structures.KdTree.Entry;
import de.tud.kom.p2psim.impl.util.structures.WaypointKdTree;
import de.tudarmstadt.maki.simonstrator.api.Monitor;
import de.tudarmstadt.maki.simonstrator.api.Monitor.Level;

/**
 * The abstract way point model manages a graph for the map data and holds
 * additional information for the fast retrieval of way points.
 * 
 * @author Fabio Zöllner
 * @version 1.0, 09.04.2012
 */
public abstract class AbstractWaypointModel implements WaypointModel {

	private PositionVector worldDimensions;

	private Map map;

	// private DefaultWeightedEdgeRetrievableGraph<Waypoint, Path> graph;
	private ArrayList<WaypointModelListener> listeners = new ArrayList<WaypointModelListener>();

	// k-d tree for fast retrieval of nearby waypoints
	private KdTree<Waypoint> kdTree = new WaypointKdTree(2);

	private ObstacleModel obstacleModel = null;
	
	// Determines if the loaded model should be scaled or truncated to fit the given world size
	private boolean scaleModel = true;

	private String mapName = "";

	public AbstractWaypointModel() {
		//
	}

	@Configure()
	@After(optional={ObstacleModel.class})
	public void _configure(Configurator configurator) {
		MapLoader mapLoader = (MapLoader)configurator.getConfigurable(MapLoader.class.getSimpleName());
		
		if (mapLoader == null) {
			throw new ConfigurationException("No MapLoader was configured. Unable to retrieve the map '" + mapName + "'.");
		}
		
		Map map = mapLoader.getMap(mapName);
		
		if (map == null) {
			throw new ConfigurationException("Couldn't retrieve the map '" + mapName + "' from the MapLoader. Make sure the map is configured.");
		}
		
		this.map = map;
		
		map.addMapChangeListener(new MapChangeListener() {
			@Override
			public void mapChanged(MapEvent event) {
				if (event instanceof PathEvent) {
					notifyAddedPath(((PathEvent)event).getPath());
				} else if (event instanceof WaypointEvent) {
					notifyAddedWaypoint(((WaypointEvent)event).getWaypoint());
				} else {
					notifyModifiedWaypoints();
				}
			}
		});

		Monitor.log(AbstractWaypointModel.class, Level.INFO,
				"The '" + map.getName() + "' " + map.getClass().getSimpleName()
						+ " has dimensions of " + map.getDimensions().getX()
						+ "x" + map.getDimensions().getY() + ".");

		PositionVector mapBorder = map.getDimensions().clone();
		mapBorder.divide(getWorldDimensions());
		if (mapBorder.getEntry(0) != 1 || mapBorder.getEntry(1) != 1) {
			Monitor.log(
					AbstractWaypointModel.class,
					Level.WARN,
					"You specified WORLD to be "
							+ getWorldDimensions().toString()
							+ " and used an OSM Map with "
							+ map.getDimensions()
							+ ", resulting in wrong scaling.");
		}
		
		init();
	}
	
	public abstract void init();

	public Map getMap() {
		return map;
	}

	public void setMap(String mapName) {
		this.mapName = mapName;
	}
	
	public void setScale(boolean shouldScale) {
		this.scaleModel = shouldScale;
	}
	
	public boolean isScaled() {
		return scaleModel;
	}

	@Override
	public double getScaleFactor() {
		if (!this.scaleModel) {
			return 1;
		}

        double mapX = getMap().getDimensions().getX();
        double mapY = getMap().getDimensions().getY();

        double worldX = getWorldDimensions().getX();
        double worldY = getWorldDimensions().getY();

        double factor = Math.abs((worldX + worldY) / (mapX + mapY));

		return factor;
	}

	@Override
	public void setObstacleModel(ObstacleModel model) {
		obstacleModel = model;
	}

	public PositionVector getWorldDimensions() {
		return worldDimensions;
	}

	@Override
	public void setWorldDimensions(PositionVector worldDimensions) {
		this.worldDimensions = worldDimensions;
	}

	@Override
	public Collection<Waypoint> getWaypoints(Class type) {
		return map.getWaypoints(type);
	}

	@Override
	public Waypoint getClosestWaypoint(PositionVector position) {
		return getClosestWaypoint(position, Waypoint.class);
	}

	@Override
	public Waypoint getClosestWaypoint(PositionVector position, Class type) {
		double distance = -1;
		Waypoint waypoint = null;

		for (Waypoint w : map.getGraph().vertexSet()) {
			if (!w.getClass().equals(type))
				continue;

			double d = position.distanceTo(w.getPosition());

			if (distance > d || distance == -1) {
				distance = d;
				waypoint = w;
			}
		}

		return waypoint;
	}

	public Waypoint getClosestWaypointKd(PositionVector position, Class type) {
		List<Entry<Waypoint>> waypoints = kdTree.nearestNeighbor(
				position.asDoubleArray(), 1, false);

		if (waypoints.isEmpty())
			return null;

		return waypoints.get(0).value;
	}

	@Override
	public List<Tuple<Waypoint, Path>> getConnectedWaypoints(Waypoint waypoint) {
		return getConnectedWaypoints(waypoint, Waypoint.class);
	}

	@Override
	public List<Tuple<Waypoint, Path>> getConnectedWaypoints(Waypoint waypoint,
			Class type) {
		Set<Path> paths = map.getGraph().edgesOf(waypoint);
		ArrayList<Tuple<Waypoint, Path>> waypointsAndPaths = new ArrayList<Tuple<Waypoint, Path>>();

		for (Path p : paths) {
			Waypoint destinationWaypoint = null;

			if (p.getSource().equals(waypoint))
				destinationWaypoint = p.getTarget();
			else if (p.getTarget().equals(waypoint))
				destinationWaypoint = p.getSource();

			if (destinationWaypoint.getClass().equals(type))
				waypointsAndPaths.add(new Tuple<Waypoint, Path>(
						destinationWaypoint, p));
		}

		return waypointsAndPaths;
	}

	@Override
	public int getNumberOfWaypoints(Class type) {
		Collection<Waypoint> wpList = map.getWaypoints(type);
		if (wpList == null)
			return -1;

		return wpList.size();
	}

	@Override
	public void addListener(WaypointModelListener listener) {
		this.listeners.add(listener);
	}

	@Override
	public void removeListener(WaypointModelListener listener) {
		this.listeners.remove(listener);
	}

	private void notifyAddedWaypoint(Waypoint waypoint) {
		for (WaypointModelListener l : listeners) {
			l.addedWaypoint(waypoint);
		}
	}
	
	private void notifyModifiedWaypoints() {
		for (WaypointModelListener l : listeners) {
			l.modifiedWaypoints();
		}
	}

	private void notifyAddedPath(Path path) {
		for (WaypointModelListener l : listeners) {
			l.addedPath(path);
		}
	}

	@Override
	public PositionVector getMetricDimensions() {
		return worldDimensions;
	}

	@Override
	public abstract void generateWaypoints();

	@Override
	public Set<Waypoint> getWaypoints() {
		if (map != null) {
			return map.getGraph().vertexSet();
		} else {
			return Sets.newHashSet();
		}
	}

	@Override
	public Set<Path> getPaths() {
		return map.getGraph().getAllEdges();
	}

	@Override
	public List<Path> getShortestPath(Waypoint start, Waypoint end) {
		DijkstraShortestPath<Waypoint, Path> dijkstrashortestpath = new DijkstraShortestPath<Waypoint, Path>(
				map.getGraph(), start, end);
		
		List<Path> paths = dijkstrashortestpath.getPathEdgeList();
		

		return dijkstrashortestpath.getPathEdgeList();
	}

	@Override
	public int getNumberOfWaypoints() {
		return map.getGraph().vertexSet().size();
	}

	@Override
	public DefaultWeightedEdgeRetrievableGraph<Waypoint, Path> getGraph() {
		if (map == null)
			return null;
		
		return map.getGraph();
	}

	public void addWaypoint(Waypoint wp) {
		map.addWaypoint(wp);
	}
}