]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/data/MidpointData.java
Version 14, October 2012
[GpsPrune.git] / tim / prune / data / MidpointData.java
1 package tim.prune.data;
2
3 /**
4  * Class to hold information about the mid-points between
5  * adjacent track points.  Used by the MapCanvas for creating
6  * points by dragging.
7  */
8 public class MidpointData
9 {
10         // track object
11         private Track _track = null;
12         // Scaled x, y values
13         private double[] _xValues = null;
14         private double[] _yValues = null;
15         // Validity flags
16         private boolean[] _valids = null;
17         // Flag to set data stale
18         private boolean _needRefresh = true;
19
20
21         /**
22          * Flag the data as needing to be updated
23          * @param inTrack track object from which to get the data
24          */
25         public void updateData(Track inTrack)
26         {
27                 _track = inTrack;
28                 _needRefresh = true;
29         }
30
31         /**
32          * Update the arrays of data from the track
33          */
34         private synchronized void updateData()
35         {
36                 _needRefresh = false;
37                 if (_track == null) return;
38                 // Make arrays the right size
39                 final int numPoints = _track.getNumPoints();
40                 if (_xValues == null || _xValues.length != numPoints)
41                 {
42                         _xValues = new double[numPoints];
43                         _yValues = new double[numPoints];
44                         _valids  = new boolean[numPoints];
45                 }
46                 if (numPoints <= 0) return;
47                 _valids[0] = false;
48
49                 // Loop over the points in the track
50                 for (int i=1; i<numPoints; i++)
51                 {
52                         boolean pointValid = false;
53                         DataPoint point = _track.getPoint(i);
54                         if (point != null && !point.getSegmentStart() && !point.isWaypoint())
55                         {
56                                 _xValues[i] = (_track.getX(i) + _track.getX(i-1)) / 2.0;
57                                 _yValues[i] = (_track.getY(i) + _track.getY(i-1)) / 2.0;
58                                 pointValid = true;
59                         }
60                         _valids[i] = pointValid;
61                 }
62         }
63
64         /**
65          * Find the nearest point to the specified x and y coordinates
66          * or -1 if no point is within the specified max distance
67          * @param inX x coordinate
68          * @param inY y coordinate
69          * @param inMaxDist maximum distance from selected coordinates
70          * @return index of nearest point or -1 if not found
71          */
72         public int getNearestPointIndex(double inX, double inY, double inMaxDist)
73         {
74                 if (_track == null) return -1;
75                 if (_needRefresh) updateData();
76                 final int numPoints = _track.getNumPoints();
77                 int nearestPoint = 0;
78                 double nearestDist = -1.0;
79                 double currDist;
80                 for (int i=1; i < numPoints; i++)
81                 {
82                         if (_valids[i])
83                         {
84                                 currDist = Math.abs(_xValues[i] - inX) + Math.abs(_yValues[i] - inY);
85                                 if (currDist < nearestDist || nearestDist < 0.0)
86                                 {
87                                         nearestPoint = i;
88                                         nearestDist = currDist;
89                                 }
90                         }
91                 }
92                 // Check whether it's within required distance
93                 if (nearestDist > inMaxDist && inMaxDist > 0.0) {
94                         return -1;
95                 }
96                 return nearestPoint;
97         }
98 }