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 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
|
package tim.prune.data;
import tim.prune.DataSubscriber;
import tim.prune.UpdateMessageBroker;
/**
* Class to represent a selected portion of a Track
* and its properties
*/
public class Selection
{
private Track _track = null;
private int _currentPoint = -1;
private boolean _valid = false;
private int _startIndex = -1, _endIndex = -1;
private int _currentPhotoIndex = -1;
private IntegerRange _altitudeRange = null;
private int _climb = -1, _descent = -1;
private Altitude.Format _altitudeFormat = Altitude.Format.NO_FORMAT;
private long _totalSeconds = 0L, _movingSeconds = 0L;
private double _angDistance = -1.0, _angMovingDistance = -1.0;
private int _numSegments = 0;
/**
* Constructor
* @param inTrack track object
*/
public Selection(Track inTrack)
{
_track = inTrack;
}
/**
* Mark selection invalid so it will be recalculated
*/
public void markInvalid()
{
_valid = false;
}
/**
* @return the current point index
*/
public int getCurrentPointIndex()
{
return _currentPoint;
}
/**
* @return true if range is selected
*/
public boolean hasRangeSelected()
{
return _startIndex >= 0 && _endIndex > _startIndex;
}
/**
* Recalculate all selection details
*/
private void recalculate()
{
_altitudeFormat = Altitude.Format.NO_FORMAT;
_numSegments = 0;
if (_track.getNumPoints() > 0 && hasRangeSelected())
{
_altitudeRange = new IntegerRange();
_climb = 0;
_descent = 0;
Altitude altitude = null;
Timestamp time = null, startTime = null, endTime = null;
Timestamp previousTime = null;
DataPoint lastPoint = null, currPoint = null;
_angDistance = 0.0; _angMovingDistance = 0.0;
_totalSeconds = 0L; _movingSeconds = 0L;
int altValue = 0;
int lastAltValue = 0;
boolean foundAlt = false;
for (int i=_startIndex; i<=_endIndex; i++)
{
currPoint = _track.getPoint(i);
altitude = currPoint.getAltitude();
// Ignore waypoints in altitude calculations
if (!currPoint.isWaypoint() && altitude.isValid())
{
altValue = altitude.getValue(_altitudeFormat);
if (_altitudeFormat == Altitude.Format.NO_FORMAT)
_altitudeFormat = altitude.getFormat();
_altitudeRange.addValue(altValue);
if (foundAlt)
{
if (altValue > lastAltValue)
_climb += (altValue - lastAltValue);
else
_descent += (lastAltValue - altValue);
}
lastAltValue = altValue;
foundAlt = true;
}
// Store the first and last timestamp in the range
time = currPoint.getTimestamp();
if (time.isValid())
{
if (startTime == null || startTime.isAfter(time)) startTime = time;
if (endTime == null || time.isAfter(endTime)) endTime = time;
// add moving time
if (!currPoint.getSegmentStart() && previousTime != null && time.isAfter(previousTime)) {
_movingSeconds += time.getSecondsSince(previousTime);
}
previousTime = time;
}
// Calculate distances, again ignoring waypoints
if (!currPoint.isWaypoint())
{
if (lastPoint != null)
{
double radians = DataPoint.calculateRadiansBetween(lastPoint, currPoint);
_angDistance += radians;
if (currPoint.getSegmentStart()) {
_numSegments++;
}
else {
_angMovingDistance += radians;
}
}
lastPoint = currPoint;
// If it's a track point then there must be at least one segment
if (_numSegments == 0) {_numSegments = 1;}
}
}
if (endTime != null) {
_totalSeconds = endTime.getSecondsSince(startTime);
}
}
_valid = true;
}
/**
* @return start index
*/
public int getStart()
{
if (!_valid) recalculate();
return _startIndex;
}
/**
* @return end index
*/
public int getEnd()
{
if (!_valid) recalculate();
return _endIndex;
}
/**
* @return the altitude format, ie feet or metres
*/
public Altitude.Format getAltitudeFormat()
{
return _altitudeFormat;
}
/**
* @return altitude range
*/
public IntegerRange getAltitudeRange()
{
if (!_valid) recalculate();
return _altitudeRange;
}
/**
* @return climb
*/
public int getClimb()
{
if (!_valid) recalculate();
return _climb;
}
/**
* @return descent
*/
public int getDescent()
{
if (!_valid) recalculate();
return _descent;
}
/**
* @return number of seconds spanned by selection
*/
public long getNumSeconds()
{
if (!_valid) recalculate();
return _totalSeconds;
}
/**
* @return number of seconds spanned by segments within selection
*/
public long getMovingSeconds()
{
if (!_valid) recalculate();
return _movingSeconds;
}
/**
* @param inUnits distance units to use, from class Distance
* @return distance of Selection in specified units
*/
public double getDistance(Distance.Units inUnits)
{
return Distance.convertRadiansToDistance(_angDistance, inUnits);
}
/**
* @param inUnits distance units to use, from class Distance
* @return moving distance of Selection in specified units
*/
public double getMovingDistance(Distance.Units inUnits)
{
return Distance.convertRadiansToDistance(_angMovingDistance, inUnits);
}
/**
* @return number of segments in selection
*/
public int getNumSegments()
{
return _numSegments;
}
/**
* Clear selected point, range and photo
*/
public void clearAll()
{
_currentPoint = -1;
selectRange(-1, -1);
_currentPhotoIndex = -1;
check();
}
/**
* Select range from start to end
* @param inStartIndex index of start of range
* @param inEndIndex index of end of range
*/
public void selectRange(int inStartIndex, int inEndIndex)
{
_startIndex = inStartIndex;
_endIndex = inEndIndex;
markInvalid();
check();
}
/**
* Select the range from the current point
*/
public void selectRangeStart()
{
selectRangeStart(_currentPoint);
}
/**
* Set the index for the start of the range selection
* @param inStartIndex start index
*/
private void selectRangeStart(int inStartIndex)
{
if (inStartIndex < 0)
{
_startIndex = _endIndex = -1;
}
else
{
_startIndex = inStartIndex;
// Move end of selection to max if necessary
if (_endIndex <= _startIndex)
{
_endIndex = _track.getNumPoints() - 1;
}
}
markInvalid();
UpdateMessageBroker.informSubscribers();
}
/**
* Select the range up to the current point
*/
public void selectRangeEnd()
{
selectRangeEnd(_currentPoint);
}
/**
* Set the index for the end of the range selection
* @param inEndIndex end index
*/
public void selectRangeEnd(int inEndIndex)
{
if (inEndIndex < 0)
{
_startIndex = _endIndex = -1;
}
else
{
_endIndex = inEndIndex;
// Move start of selection to min if necessary
if (_startIndex > _endIndex || _startIndex < 0) {
_startIndex = 0;
}
}
markInvalid();
UpdateMessageBroker.informSubscribers();
}
/**
* Modify the selection given that the selected range has been deleted
*/
public void modifyRangeDeleted()
{
// Modify current point, if any
if (_currentPoint > _endIndex)
{
_currentPoint -= (_endIndex - _startIndex);
}
else if (_currentPoint > _startIndex)
{
_currentPoint = _startIndex;
}
// Clear selected range
_startIndex = _endIndex = -1;
// Check for consistency and fire update
check();
}
/**
* Modify the selection when a point is deleted
*/
public void modifyPointDeleted()
{
// current point index doesn't change, just gets checked
// range needs to get altered if deleted point is inside or before
if (hasRangeSelected() && _currentPoint <= _endIndex)
{
_endIndex--;
if (_currentPoint < _startIndex)
_startIndex--;
markInvalid();
}
check();
}
/**
* Select the specified photo and point
* @param inPhotoIndex index of selected photo in PhotoList
* @param inPointIndex index of selected point
*/
public void selectPhotoAndPoint(int inPhotoIndex, int inPointIndex)
{
_currentPhotoIndex = inPhotoIndex;
_currentPoint = inPointIndex;
check();
}
/**
* @return currently selected photo index
*/
public int getCurrentPhotoIndex()
{
return _currentPhotoIndex;
}
/**
* Check that the selection still makes sense
* and fire update message to listeners
*/
private void check()
{
if (_track != null)
{
if (_track.getNumPoints() > 0)
{
int maxIndex = _track.getNumPoints() - 1;
if (_currentPoint > maxIndex)
{
_currentPoint = maxIndex;
}
if (_endIndex > maxIndex)
{
_endIndex = maxIndex;
}
if (_startIndex > maxIndex)
{
_startIndex = maxIndex;
}
}
else
{
// track is empty, clear selections
_currentPoint = _startIndex = _endIndex = -1;
}
}
UpdateMessageBroker.informSubscribers(DataSubscriber.SELECTION_CHANGED);
}
}
|