AbstractObstacleModel.java 5.98 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
/*
 * 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.obstacles;

import java.awt.Color;
import java.util.ArrayList;
import java.util.List;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
Julian Zobel's avatar
Julian Zobel committed
29
30
31
import  org.locationtech.jts.geom.Coordinate;
import  org.locationtech.jts.geom.GeometryFactory;
import  org.locationtech.jts.geom.Point;
32
33
34
35
36
37
38
39
40
41

import de.tud.kom.p2psim.api.scenario.ConfigurationException;
import de.tud.kom.p2psim.api.scenario.Configurator;
import de.tud.kom.p2psim.api.topology.obstacles.Obstacle;
import de.tud.kom.p2psim.api.topology.obstacles.ObstacleModel;
import de.tud.kom.p2psim.api.topology.obstacles.ObstacleModelListener;
import de.tud.kom.p2psim.api.topology.waypoints.WaypointModel;
import de.tud.kom.p2psim.api.util.geo.maps.Map;
import de.tud.kom.p2psim.impl.scenario.simcfg2.annotations.Configure;
import de.tud.kom.p2psim.impl.simengine.Simulator;
42
import de.tud.kom.p2psim.impl.topology.util.PositionVector;
43
44
45
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;
46
47
import de.tudarmstadt.maki.simonstrator.api.Monitor;
import de.tudarmstadt.maki.simonstrator.api.Monitor.Level;
48
49
50
51
52
53
54
55
56

/**
 * This abstract obstacle model helps to created new 
 * models for generating obstacles.
 * 
 * @author Fabio Zöllner
 * @version 1.0, 09.04.2012
 */
public abstract class AbstractObstacleModel implements ObstacleModel {
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
	private PositionVector worldDimensions;

	private ArrayList<ObstacleModelListener> listeners = new ArrayList<ObstacleModelListener>();
	private GeometryFactory factory = new GeometryFactory();
	protected java.util.Map<Tuple<String, String>, Color> colors = Maps.newHashMap();
	
	protected WaypointModel waypointModel = null;
	
	private String mapName = "";
	private Map map = null;
    private boolean defaultConfiguratorSupportInit = false;
	
	public void setMap(String mapName) {
		this.mapName = mapName;
	}
	
	public void setInit(String dummy) {
        defaultConfiguratorSupportInit = true;
    }
	
	@Configure
	public void _configure(Configurator configurator) {
		MapLoader mapLoader = (MapLoader)configurator.getConfigurable("MapLoader");
		
		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 ObstacleEvent) {
					notifyAddedObstacle(((ObstacleEvent)event).getObstacle());
				}
			}
		});

		PositionVector mapBorder = map.getDimensions().clone();
		mapBorder.divide(getWorldDimensions());
		if (mapBorder.getEntry(0) != 1 || mapBorder.getEntry(1) != 1) {
106
107
			Monitor.log(AbstractObstacleModel.class, Level.WARN,
					"You specified WORLD to be "
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
					+ getWorldDimensions().toString()
					+ " and used an Map with " + map.getDimensions()
					+ ", resulting in wrong scaling.");
		}
	}
	
	protected PositionVector getWorldDimensions() {
		return worldDimensions;
	}

	@Override
	public void setWorldDimensions(PositionVector worldDimensions) {
		this.worldDimensions = worldDimensions;
		
		if (defaultConfiguratorSupportInit) {
            _configure(Simulator.getConfigurator());
        }
	}
	
	@Override
	public List<Obstacle> getObstacles() {
		if (map == null)
			return Lists.newArrayList();
		
		return map.getObstacles();
	}
	
	@Override
	public void addListener(ObstacleModelListener listener) {
		this.listeners.add(listener);
	}
	
	@Override
	public void removeListener(ObstacleModelListener listener) {
		this.listeners.remove(listener);
	}

	protected void notifyAddedObstacle(Obstacle obstacle) {
		for (ObstacleModelListener l : listeners) {
			l.addedObstacle(obstacle);
		}
	}
	
	public void setAddColor(String[] colorPattern) {
		if (colorPattern.length != 3) 
			return;
		
		int cint = Integer.parseInt(colorPattern[2], 16);
		
		Color color = new Color(cint);
		
		colors.put(Tuple.create(colorPattern[0], colorPattern[1]), color);
	}

	public java.util.Map<Tuple<String, String>, Color> getObstacleColors() {
		return this.colors;
	}
	
	@Override
	public void setWaypointModel(WaypointModel model) {
		this.waypointModel = model;
	}
	
	public Obstacle getEnclosing(PositionVector loc) {
        Coordinate coordinate = new Coordinate(loc.getX(), loc.getY());
        Point point = factory.createPoint(coordinate);

        for (Obstacle o : map.getObstacles()) {
            if (o.contains(point)) {
                return o;
            }
        }

        return null;

    }

	@Override
	public boolean contains(PositionVector loc) {
		Coordinate coordinate = new Coordinate(loc.getX(), loc.getY());
		Point point = factory.createPoint(coordinate);
		
		for (Obstacle o : map.getObstacles()) {
			if (o.contains(point)) {
				return true;
			}
		}
		
		return false;
	}
	
	public abstract void generateObstacles();
}