-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLocationDataManager.java
More file actions
248 lines (213 loc) · 7.03 KB
/
LocationDataManager.java
File metadata and controls
248 lines (213 loc) · 7.03 KB
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
/* Copyright (C) 2014,2015 Maximilian Diedrich
*
* This program 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
* (at your option) any later version.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>
*/
package de.hu_berlin.informatik.spws2014.ImagePositionLocator;
import java.util.ArrayList;
import java.util.concurrent.Callable;
/**
* Application interface to the imagePositionLocator.
* Simplifies the usage and provides plugable data backends
* via ILDMIOHandler. By default also tries to filter too fast
* GPS movements.
*/
public class LocationDataManager {
// seconds until would be accepted
static final int MAX_POINT_DENIAL_TIME = 10;
// accepted movement speed in meters per second
static final int MAX_MOVEMENT_SPEED = 20;
// seconds untill the last GpsPoint cannot be used as a marker anymore
static final int MAX_VALID_GPS_TIME = 30;
// track data
Point2D imageSize;
long lastGPSFixTime;
boolean isSpeedFiltering = true;
// Session data
ILDMIOHandler iohandler;
ImagePositionLocator imagePositionAlg;
Callable<Void> hostAppCallback;
Point2D lastImagePoint;
GpsPoint lastGpsPoint;
/**
* Create new LocationDataManager and inits algorithm
* Gets initialized with data from IOHandler
*
* @param callback Function to be called, when a new ImagePosition is found.
* @param ldmio IOHandler
*/
public LocationDataManager(Callable<Void> callback, ILDMIOHandler ldmio, Point2D imageSize, ImagePositionLocator ipl) {
hostAppCallback = callback;
iohandler = ldmio;
this.imageSize = imageSize;
lastGPSFixTime = iohandler.getLastGpsPointTime();
ArrayList<GpsPoint> gpsPoints = iohandler.getAllGpsPoints();
int numberOfGpsPoints = gpsPoints.size();
if (numberOfGpsPoints != 0)
lastGpsPoint = gpsPoints.get(numberOfGpsPoints - 1);
imagePositionAlg = ipl;
}
/**
* Adds new GpsPoint to the algorithms knowledge. Input may be raw.
*
* @param input
*/
public void addPoint(GpsPoint input) {
if (input == null)
return;
boolean conforms = false;
double elapsedTime = 0;
double movementSpeed = 0;
if (lastGpsPoint == null || ((input.time - lastGpsPoint.time) / 1000) >= MAX_POINT_DENIAL_TIME) {
conforms = true;
} else {
elapsedTime = ((input.time - lastGpsPoint.time) / 1000);
movementSpeed = lastGpsPoint.getSphericalDistance(input) / elapsedTime / 1000;
if (movementSpeed < MAX_MOVEMENT_SPEED)
conforms = true;
}
if (conforms || !isSpeedFiltering) {
lastGpsPoint = input;
lastGPSFixTime = System.currentTimeMillis();
iohandler.setLastGpsPointTime(lastGPSFixTime);
iohandler.addGpsPoint(input);
reportNewImagePoint(imagePositionAlg.getPointPosition(input));
} else {
System.err.println("Point: " + input.toString() + " exceeds MAX_MOVEMENT_SPEED by " + movementSpeed);
}
}
/**
* Checks if setting a point now would be accepted
*/
public boolean isMarkerPlacingAllowed() {
if (lastGpsPoint == null)
return false;
if ((System.currentTimeMillis() - lastGPSFixTime) / 1000 > MAX_VALID_GPS_TIME)
return false;
return true;
}
/**
* Adds new Marker to the algorithms knowledge.
*
* @param imgpoint
* Pressed Point on original image. Is expected to be accurate.
* @param time
* Current time in milliseconds. Is expected to be consistent
* with the time of the GpsPoints.
*/
public Marker addMarker(Point2D imgpoint, long time)
throws NoGpsDataAvailableException, PointNotInImageBoundsException {
if (!isMarkerPlacingAllowed())
throw new NoGpsDataAvailableException("No GPS fix!");
if (imgpoint == null)
return null;
if (imgpoint.smallerThan(new Point2D(0, 0)))
throw new PointNotInImageBoundsException("Point has negative coordinates!");
if (imageSize.smallerThan(imgpoint))
throw new PointNotInImageBoundsException("Point greater than image size!");
Marker result = new Marker(imgpoint, time, lastGpsPoint);
if (iohandler.getMarker(result.realpoint) != null)
throw new NoGpsDataAvailableException("Point already known!");
iohandler.addMarker(result);
if (imagePositionAlg != null)
imagePositionAlg.newMarkerAdded(iohandler.getAllMarkers());
refreshLastPosition();
return result;
}
/**
* Refreshes all parameters and computes
* the last recieved GpsPoint anew.
*/
public void refreshLastPosition() {
// Send new estimated user position
if (lastGpsPoint != null) {
ArrayList<Marker> markers = iohandler.getAllMarkers();
if (markers.size() != 0) {
Point2D tmp;
if (markers.size() == 1) {
tmp = markers.get(0).imgpoint;
} else {
imagePositionAlg.newMarkerAdded(markers);
tmp = imagePositionAlg.getPointPosition(lastGpsPoint);
}
reportNewImagePoint(tmp);
}
}
}
/**
* Signals the host application that a new point was found.
*
* @param p New image position
*/
private void reportNewImagePoint(Point2D p) {
if (p != null || p != lastImagePoint) {
lastImagePoint = p;
if (hostAppCallback != null) {
try {
hostAppCallback.call();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
}
/**
* @return Last known image position.
*/
public Point2D getLastImagePoint() {
return lastImagePoint;
}
/**
* @return The number of remaining markers to be set
* by the user before the location algorithm starts.
*/
public int remainingUserMarkerInputs() {
int tmp = 3 - iohandler.getAllMarkers().size();
return (tmp < 0) ? 0 : tmp;
}
/**
* Checks or unchecks whether too fast movements should be registered
* @param isSpeedFiltering Desired state of isSpeedFiltering
* @return The previous state of isSpeedFiltering
*/
public boolean setSpeedFiltering(boolean isSpeedFiltering) {
boolean tmp = isSpeedFiltering;
this.isSpeedFiltering = isSpeedFiltering;
return tmp;
}
//-----------------------------------------------------
// IO-Handling
//-----------------------------------------------------
/**
* Saves all currently known data in the current LDMIO Handler
* and replaces it by newHandler.
*
* @param newHandler New data handler. If this is null,
* then a new LDMIOEmpty will be used.
*/
public void resetIOHandler(ILDMIOHandler newHandler) {
iohandler.save();
iohandler = (newHandler != null) ? newHandler : new LDMIOEmpty();
}
/**
* Adds all data in addHandler to the LDMs knowledge.
* Currently only transports marker data.
*/
public void addAllDataPointsFromIOHandler(ILDMIOHandler addHandler) {
ArrayList<Marker> tmpmarkers = addHandler.getAllMarkers();
for (Marker m : tmpmarkers) {
iohandler.addMarker(m);
}
}
}