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 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
|
package tim.prune.data;
import java.util.List;
import tim.prune.UpdateMessageBroker;
import tim.prune.gui.map.MapUtils;
/**
* Class to hold all track information,
* including track points and waypoints
*/
public class Track
{
// Data points
private DataPoint[] _dataPoints;
// Scaled x, y values
private double[] _xValues = null;
private double[] _yValues = null;
private boolean _scaled = false;
private int _numPoints;
private boolean _hasTrackpoint = false;
private boolean _hasWaypoint = false;
private FieldList _masterFieldList;
// variable ranges
private DoubleRange _latRange = null, _longRange = null;
private DoubleRange _xRange = null, _yRange = null;
/**
* Constructor for empty track
*/
public Track()
{
// create field list
_masterFieldList = new FieldList();
// make empty DataPoint array
_dataPoints = new DataPoint[0];
_numPoints = 0;
}
/**
* Constructor using fields and points from another Track
* @param inFieldList Field list from another Track object
* @param inPoints (edited) point array
*/
public Track(FieldList inFieldList, DataPoint[] inPoints)
{
_masterFieldList = inFieldList;
_dataPoints = inPoints;
if (_dataPoints == null) {
_dataPoints = new DataPoint[0];
}
_numPoints = _dataPoints.length;
}
/**
* Request that a rescale be done to recalculate derived values
*/
public void requestRescale() {
_scaled = false;
}
/**
* Extend the track's field list with the given additional fields
* @param inFieldList list of fields to be added
*/
private void extendFieldList(FieldList inFieldList) {
_masterFieldList = _masterFieldList.merge(inFieldList);
}
////////////////// Modification methods //////////////////////
/**
* Crop the track to the given size - subsequent points are not (yet) deleted
* @param inNewSize new number of points in track
*/
public void cropTo(int inNewSize)
{
if (inNewSize >= 0 && inNewSize < getNumPoints())
{
_numPoints = inNewSize;
// needs to be scaled again
_scaled = false;
UpdateMessageBroker.informSubscribers();
}
}
/**
* Delete the specified point
* @param inIndex point index
* @return true if successful
*/
public boolean deletePoint(int inIndex)
{
DataPoint point = getPoint(inIndex);
if (point == null) {
return false;
}
if (point.getSegmentStart())
{
DataPoint nextTrackPoint = getNextTrackPoint(inIndex+1);
if (nextTrackPoint != null) {
nextTrackPoint.setSegmentStart(true);
}
}
DataPoint[] newPointArray = new DataPoint[_numPoints - 1];
// Copy points before the selected point
if (inIndex > 0) {
System.arraycopy(_dataPoints, 0, newPointArray, 0, inIndex);
}
// Copy points after
int numAfter = _numPoints - inIndex - 1;
if (numAfter > 0) {
System.arraycopy(_dataPoints, inIndex + 1, newPointArray, inIndex, numAfter);
}
// Copy points over original array
_dataPoints = newPointArray;
_numPoints --;
// needs to be scaled again
_scaled = false;
return true;
}
/**
* Rearrange all the points in the track according to the given list
* @param inIndexes point indexes for new ordering
*/
public boolean rearrangePoints(List<Integer> inIndexes)
{
if (inIndexes == null || inIndexes.size() != _numPoints) {
return false;
}
DataPoint[] newPointArray = new DataPoint[_numPoints];
// Move points around
for (int i=0; i<_numPoints; i++) {
newPointArray[i] = getPoint(inIndexes.get(i));
}
// Copy array references
_dataPoints = newPointArray;
_scaled = false;
return true;
}
//////// information methods /////////////
/**
* Get the point at the given index
* @param inPointNum index number, starting at 0
* @return DataPoint object, or null if out of range
*/
public DataPoint getPoint(int inPointNum)
{
if (inPointNum > -1 && inPointNum < getNumPoints()) {
return _dataPoints[inPointNum];
}
return null;
}
/**
* @return the number of (valid) points in the track
*/
public int getNumPoints() {
return _numPoints;
}
/**
* @return The range of x values as a DoubleRange object
*/
public DoubleRange getXRange()
{
if (!_scaled) {scalePoints();}
return _xRange;
}
/**
* @return The range of y values as a DoubleRange object
*/
public DoubleRange getYRange()
{
if (!_scaled) {scalePoints();}
return _yRange;
}
/**
* @return The range of lat values as a DoubleRange object
*/
public DoubleRange getLatRange()
{
if (!_scaled) {scalePoints();}
return _latRange;
}
/**
* @return The range of lon values as a DoubleRange object
*/
public DoubleRange getLonRange()
{
if (!_scaled) {scalePoints();}
return _longRange;
}
/**
* @param inPointNum point index, starting at 0
* @return scaled x value of specified point
*/
public double getX(int inPointNum)
{
if (!_scaled) {scalePoints();}
return _xValues[inPointNum];
}
/**
* @param inPointNum point index, starting at 0
* @return scaled y value of specified point
*/
public double getY(int inPointNum)
{
if (!_scaled) {scalePoints();}
return _yValues[inPointNum];
}
/**
* @return the master field list
*/
public FieldList getFieldList() {
return _masterFieldList;
}
/**
* Checks if any data exists for the specified field
* @param inField Field to examine
* @return true if data exists for this field
*/
public boolean hasData(Field inField) {
return hasData(inField, 0, _numPoints-1);
}
/**
* Checks if any data exists for the specified field in the specified range
* @param inField Field to examine
* @param inStart start of range to check
* @param inEnd end of range to check (inclusive)
* @return true if data exists for this field
*/
public boolean hasData(Field inField, int inStart, int inEnd)
{
// Loop over selected point range
for (int i=inStart; i<=inEnd; i++)
{
DataPoint point = getPoint(i);
if (point == null) {
continue;
}
final boolean hasValue;
if (inField == Field.ALTITUDE) {
hasValue = point.hasAltitude();
}
else if (inField == Field.TIMESTAMP) {
hasValue = point.hasTimestamp();
}
else if (inField == Field.PHOTO) {
hasValue = point.getPhoto() != null;
}
else if (inField == Field.AUDIO) {
hasValue = point.getAudio() != null;
}
else {
hasValue = point.getFieldValue(inField) != null;
}
if (hasValue) {
return true;
}
}
return false;
}
/**
* @return true if track has altitude data
*/
public boolean hasAltitudeData() {
return hasData(Field.ALTITUDE);
}
/**
* @return true if track contains at least one trackpoint
*/
public boolean hasTrackPoints()
{
if (!_scaled) {scalePoints();}
return _hasTrackpoint;
}
/**
* @return true if track contains waypoints
*/
public boolean hasWaypoints()
{
if (!_scaled) {scalePoints();}
return _hasWaypoint;
}
/**
* Collect all the waypoints into the given List
* @param inList List to fill with waypoints
*/
public void getWaypoints(List<DataPoint> inList)
{
// clear list
inList.clear();
// loop over points and copy all waypoints into list
for (int i=0; i<=_numPoints-1; i++)
{
if (_dataPoints[i] != null && _dataPoints[i].isWaypoint())
{
inList.add(_dataPoints[i]);
}
}
}
/**
* Search for the given Point in the track and return the index
* @param inPoint Point to look for
* @return index of Point, if any or -1 if not found
*/
public int getPointIndex(DataPoint inPoint)
{
if (inPoint != null)
{
// Loop over points in track
for (int i=0; i<=_numPoints-1; i++)
{
if (_dataPoints[i] == inPoint) {
return i;
}
}
}
// not found
return -1;
}
/**
* @return true if all the points in the track have the same source file
*/
public boolean hasSingleSourceFile()
{
SourceInfo prevInfo = null;
for (int p=0; p < getNumPoints(); p++)
{
DataPoint point = getPoint(p);
SourceInfo info = (point == null ? null : point.getSourceInfo());
if (info == null || (prevInfo != null && info != prevInfo)) {
return false;
}
if (info.getFile() == null) {
return false;
}
prevInfo = info;
}
return true;
}
///////// Internal processing methods ////////////////
/**
* Scale all the points in the track to gain x and y values
* ready for plotting
*/
private synchronized void scalePoints()
{
// Loop through all points in track, to see limits of lat, long
_longRange = new DoubleRange();
_latRange = new DoubleRange();
_hasWaypoint = false; _hasTrackpoint = false;
for (int p=0; p < getNumPoints(); p++)
{
DataPoint point = getPoint(p);
if (point != null && point.isValid())
{
_longRange.addValue(point.getLongitude().getDouble());
_latRange.addValue(point.getLatitude().getDouble());
if (point.isWaypoint()) {
_hasWaypoint = true;
}
else {
_hasTrackpoint = true;
}
}
}
// Loop over points and calculate scales
_xValues = new double[getNumPoints()];
_yValues = new double[getNumPoints()];
_xRange = new DoubleRange();
_yRange = new DoubleRange();
for (int p=0; p < getNumPoints(); p++)
{
DataPoint point = getPoint(p);
if (point != null)
{
_xValues[p] = MapUtils.getXFromLongitude(point.getLongitude().getDouble());
_xRange.addValue(_xValues[p]);
_yValues[p] = MapUtils.getYFromLatitude(point.getLatitude().getDouble());
_yRange.addValue(_yValues[p]);
}
}
_scaled = true;
}
/**
* Find the nearest track point to the specified point
* @param inPointIndex index of point within track
* @return point index of nearest track point
*/
public int getNearestTrackPointIndex(int inPointIndex) {
return getNearestPointIndex(_xValues[inPointIndex], _yValues[inPointIndex], -1.0, true);
}
/**
* Find the nearest point to the specified x and y coordinates
* or -1 if no point is within the specified max distance
* @param inX x coordinate
* @param inY y coordinate
* @param inMaxDist maximum distance from selected coordinates
* @param inJustTrackPoints true if waypoints should be ignored
* @return index of nearest point or -1 if not found
*/
public int getNearestPointIndex(double inX, double inY, double inMaxDist, boolean inJustTrackPoints)
{
int nearestPoint = 0;
double nearestDist = -1.0;
double mDist, yDist;
try {
for (int i=0; i < getNumPoints(); i++)
{
if (!inJustTrackPoints || !_dataPoints[i].isWaypoint())
{
yDist = Math.abs(_yValues[i] - inY);
if (yDist < nearestDist || nearestDist < 0.0)
{
// y dist is within range, so check x too
mDist = yDist + getMinXDist(_xValues[i] - inX);
if (mDist < nearestDist || nearestDist < 0.0)
{
nearestPoint = i;
nearestDist = mDist;
}
}
}
}
} catch (ArrayIndexOutOfBoundsException obe) {
return -1; // probably moving the mouse while data is changing
}
// Check whether it's within required distance
if (nearestDist > inMaxDist && inMaxDist > 0.0) {
return -1;
}
return nearestPoint;
}
/**
* @param inX x value of point
* @return minimum wrapped value
*/
private static double getMinXDist(double inX) {
return Math.min(Math.min(Math.abs(inX), Math.abs(inX-1.0)), Math.abs(inX+1.0));
}
/**
* Get the next track point starting from the given index
* @param inStartIndex index to start looking from
* @return next track point, or null if end of data reached
*/
public DataPoint getNextTrackPoint(int inStartIndex) {
return getNextTrackPoint(inStartIndex, _numPoints, true);
}
/**
* Get the next track point starting from the given index
* @param inStartIndex index to start looking from
* @param inEndIndex index to stop looking (inclusive)
* @return next track point, or null if end of data reached
*/
public DataPoint getFirstTrackPointBetween(int inStartIndex, int inEndIndex) {
return getNextTrackPoint(inStartIndex, inEndIndex, true);
}
/**
* Get the previous track point starting from the given index
* @param inStartIndex index to start looking from
* @return next track point, or null if end of data reached
*/
public DataPoint getPreviousTrackPoint(int inStartIndex)
{
// end index is given as _numPoints but actually it just counts down to -1
return getNextTrackPoint(inStartIndex, _numPoints, false);
}
/**
* Get the next track point starting from the given index
* @param inStartIndex index to start looking from
* @param inEndIndex index to stop looking (inclusive)
* @param inCountUp true for next, false for previous
* @return next track point, or null if end of data reached
*/
private DataPoint getNextTrackPoint(int inStartIndex, int inEndIndex, boolean inCountUp)
{
int increment = inCountUp ? 1 : -1;
for (int i=inStartIndex; i<=inEndIndex; i+=increment)
{
DataPoint point = getPoint(i);
// Exit if end of data reached - there wasn't a track point
if (point == null) {
return null;
}
if (point.isValid() && !point.isWaypoint()) {
// next track point found
return point;
}
}
return null;
}
/**
* @param inStartIndex start index of range
* @param inEndIndex end index of range
* @return true if there are any track points in this range
*/
public boolean isTrackPointWithin(int inStartIndex, int inEndIndex)
{
for (int i = inStartIndex; i <= inEndIndex; i++)
{
if (!getPoint(i).isWaypoint()) {
return true;
}
}
return false;
}
/**
* @param inStartIndex start index of range
* @param inEndIndex end index of range
* @return true if there are any segment breaks in this range
*/
public boolean isSegmentBreakWithin(int inStartIndex, int inEndIndex)
{
for (int i = inStartIndex; i <= inEndIndex; i++)
{
DataPoint point = getPoint(i);
if (!point.isWaypoint() && point.getSegmentStart()) {
return true;
}
}
return false;
}
/**
* Append the given point to the end of the track
* @param inPoint point to append
* @return true if successful
*/
public boolean appendPoint(DataPoint inPoint) {
return insertPoint(inPoint, _numPoints);
}
/**
* Re-insert the specified point at the given index
* @param inPoint point to insert
* @param inIndex index at which to insert the point
* @return true if it worked, false otherwise
*/
public boolean insertPoint(DataPoint inPoint, int inIndex)
{
if (inIndex > _numPoints || inPoint == null) {
return false;
}
// Make new array to copy points over to
DataPoint[] newPointArray = new DataPoint[_numPoints + 1];
if (inIndex > 0) {
System.arraycopy(_dataPoints, 0, newPointArray, 0, inIndex);
}
newPointArray[inIndex] = inPoint;
if (inIndex < _numPoints) {
System.arraycopy(_dataPoints, inIndex, newPointArray, inIndex+1, _numPoints - inIndex);
}
// Change over to new array
_dataPoints = newPointArray;
_numPoints++;
extendFieldList(inPoint.getFieldList());
// needs to be scaled again
_scaled = false;
UpdateMessageBroker.informSubscribers();
return true;
}
/**
* Append the specified point range to the end of the track
* @param inPoints list of points to append
* @return true if it worked, false otherwise
*/
public boolean appendRange(List<DataPoint> inPoints)
{
if (inPoints == null || inPoints.isEmpty()) {
return false;
}
// Make new array to copy points over to
DataPoint[] newPointArray = new DataPoint[_numPoints + inPoints.size()];
System.arraycopy(_dataPoints, 0, newPointArray, 0, _numPoints);
int index = _numPoints;
for (DataPoint point : inPoints)
{
newPointArray[index] = point;
extendFieldList(point.getFieldList());
index++;
}
// Change over to new array
_dataPoints = newPointArray;
_numPoints = _dataPoints.length;
_scaled = false;
return true;
}
}
|