RouteImpl.java 5.68 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
/*
 * 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.movement.local;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import com.graphhopper.util.PointList;
import com.graphhopper.util.shapes.GHPoint3D;

import de.tud.kom.p2psim.api.topology.movement.SimLocationActuator;
import de.tud.kom.p2psim.impl.topology.PositionVector;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.Location;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.route.Route;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.route.RouteSensor.RouteSegmentListener;

/**
 * This class contains one trajectory from a start to a destination. It saves
 * the respective points (coordinates) along the path with a given movement
 * speed and also (if enabled) the assigned IDs of the roads and walkways that
 * are traversed.
 * 
 * TODO add segment handling
 * 
 * Segment handling is only done if there is a segment listener registered for
 * the respective client.
 * 
 * @author Bjoern Richerzhagen
 * @version 1.0, Aug 9, 2017
 */
public class RouteImpl implements Route {

	private PositionVector start;

	private PositionVector destination;

	private PointList pointList;

	private final List<PositionVector> positionsList;

	private final List<RouteSegmentImpl> routeSegments;

	private final Set<RouteSegmentListener> segmentListeners;

	private int currentIndex;

	private int currentSegmentIndex;

	private int routeLength;

	private int routeSegmentsLength;

	/**
	 * 
	 * @param start
	 * @param destination
	 * @param pointList
	 * @param segmentListeners
	 * @param routeSegments
	 */
	public RouteImpl(PositionVector start,
			PositionVector destination, PointList pointList,
			Set<RouteSegmentListener> segmentListeners,
			List<RouteSegmentImpl> routeSegments) {
		this.start = start;
		this.destination = destination;
		this.pointList = pointList;
		this.positionsList = new LinkedList<>();
		// Create Position vectors ONCE
		for (GHPoint3D temp : pointList) {
			positionsList
					.add(RealWorldStreetsMovement.transformGPSWindowToOwnWorld(
							temp.getLat(), temp.getLon()));
		}
		routeLength = positionsList.size();
		this.segmentListeners = segmentListeners;
		this.routeSegments = routeSegments;
		this.routeSegmentsLength = routeSegments.size();
	}

	/**
	 * 
	 * @param start
	 * @param destination
	 * @param pointList
	 */
	public RouteImpl(PositionVector start,
			PositionVector destination, PointList pointList) {
		this(start, destination, pointList, Collections.emptySet(),
				Collections.emptyList());
	}

	public PositionVector getStart() {
		return start;
	}

	public PositionVector getDestination() {
		return destination;
	}

	@Override
	public Location getTargetLocation() {
		return destination;
	}

	@Override
	public boolean hasSegmentInformation() {
		return routeSegmentsLength != 0;
	}

	@Override
	public RouteSegment getCurrentSegment() {
		if (!hasSegmentInformation()) {
			throw new UnsupportedOperationException(
					"To access segments within a Route, you need to enable the respective functionality in the local movement strategy (usually: calculateRouteSegments=true)");
		}
		return routeSegments.get(currentSegmentIndex);
	}

	@Override
	public List<RouteSegment> getSegmentsAhead() {
		if (!hasSegmentInformation()) {
			throw new UnsupportedOperationException(
					"To access segments within a Route, you need to enable the respective functionality in the local movement strategy (usually: calculateRouteSegments=true)");
		}
		return Collections.unmodifiableList(routeSegments
				.subList(currentSegmentIndex, routeSegmentsLength));
	}

	@Override
	public List<RouteSegment> getSegmentsBehind() {
		if (!hasSegmentInformation()) {
			throw new UnsupportedOperationException(
					"To access segments within a Route, you need to enable the respective functionality in the local movement strategy (usually: calculateRouteSegments=true)");
		}
		return Collections.unmodifiableList(routeSegments
				.subList(0, currentSegmentIndex));
	}

	/**
	 * Update the internal location with the given movement speed
	 * 
	 * @param speed
	 * @param realPosition
	 * @return
	 */
	public PositionVector updateCurrentLocation(SimLocationActuator comp,
			double speed, double tolerance) {
		List<PositionVector> sublist = positionsList.subList(currentIndex,
				routeLength);
		PositionVector newPosition = null;
		for (PositionVector candidate : sublist) {
			newPosition = comp.getRealPosition().moveStep(candidate, speed);
			if (candidate.distanceTo(comp.getRealPosition()) < tolerance) {
				currentIndex++;
			} else {
				break;
			}
		}

		/*
		 * TODO Segment handling (also inform listeners!)
		 */

		return newPosition;
	}

	@Override
	public String toString() {
		return "\n\tfrom " + start.toString() + " to " + destination.toString()
				+ "\n\tstep: " + currentIndex + " length: " + routeLength;
	}

	public PointList getPointList() {
		return pointList;
	}

	public int getCurrentIndex() {
		return this.currentIndex;
	}

}