]> gitweb.fperrin.net Git - GpsPrune.git/blob - src/tim/prune/function/compress/ClosePointsAlgorithm.java
Version 19.2, December 2018
[GpsPrune.git] / src / tim / prune / function / compress / ClosePointsAlgorithm.java
1 package tim.prune.function.compress;
2
3 import java.awt.Component;
4 import java.awt.event.ActionListener;
5
6 import tim.prune.data.DataPoint;
7 import tim.prune.data.Track;
8
9 /**
10  * Algorithm for detecting close points to compress
11  * Only checks distance to previous point, not any earlier point
12  */
13 public class ClosePointsAlgorithm extends SingleParameterAlgorithm
14 {
15
16         /**
17          * Constructor
18          * @param inTrack track object
19          * @param inDetails track details object
20          * @param inListener listener to attach to activation control
21          */
22         public ClosePointsAlgorithm(Track inTrack, TrackDetails inDetails, ActionListener inListener)
23         {
24                 super(inTrack, inDetails, inListener);
25         }
26
27         /**
28          * Perform the compression and work out which points should be deleted
29          * @param inFlags deletion flags from previous algorithms
30          * @return number of points deleted
31          */
32         protected int compress(boolean[] inFlags)
33         {
34                 // Parse parameter
35                 double param = getParameter();
36                 // Use 1/x if x greater than 1
37                 if (param > 1.0) param = 1.0 / param;
38                 if (param <= 0.0 || param >= 1.0) {
39                         // Parameter isn't valid, don't delete any
40                         return 0;
41                 }
42                 double threshold = _trackDetails.getTrackSpan() * param;
43
44                 // Loop over all points checking distances to previous point
45                 // TODO: Maybe this should also check distance to _next_ point as well!
46                 int numPoints = _track.getNumPoints();
47                 int prevPointIndex = 0;
48                 int prevTrackPointIndex = 0;
49                 double pointDist = 0.0;
50                 int numDeleted = 0;
51                 for (int i=1; i<numPoints; i++)
52                 {
53                         // don't delete points already deleted
54                         if (!inFlags[i])
55                         {
56                                 DataPoint currPoint = _track.getPoint(i);
57                                 // Don't consider waypoints
58                                 if (!currPoint.isWaypoint())
59                                 {
60                                         // Don't delete any photo points or start/end of segments
61                                         if (!currPoint.hasMedia()
62                                                 && !_trackDetails.isSegmentStart(i) && !_trackDetails.isSegmentEnd(i))
63                                         {
64                                                 // Check current point against prevPoint
65                                                 pointDist = Math.abs(_track.getX(i) - _track.getX(prevPointIndex))
66                                                  + Math.abs(_track.getY(i) - _track.getY(prevPointIndex));
67                                                 if (pointDist < threshold) {
68                                                         inFlags[i] = true;
69                                                         numDeleted++;
70                                                 }
71                                                 else if (prevTrackPointIndex != prevPointIndex)
72                                                 {
73                                                         // Check current point against prevTrackPoint
74                                                         pointDist = Math.abs(_track.getX(i) - _track.getX(prevTrackPointIndex))
75                                                          + Math.abs(_track.getY(i) - _track.getY(prevTrackPointIndex));
76                                                         if (pointDist < threshold) {
77                                                                 inFlags[i] = true;
78                                                                 numDeleted++;
79                                                         }
80                                                 }
81                                         }
82                                         if (!inFlags[i]) {prevTrackPointIndex = i;}
83                                 }
84                                 if (!inFlags[i]) {prevPointIndex = i;}
85                         }
86                 }
87                 return numDeleted;
88         }
89
90
91         /**
92          * @return specific gui components for dialog
93          */
94         protected Component getSpecificGuiComponents()
95         {
96                 return getSpecificGuiComponents("dialog.compress.closepoints.paramdesc", "200");
97         }
98
99         /**
100          * @return title key for box
101          */
102         protected String getTitleTextKey()
103         {
104                 return "dialog.compress.closepoints.title";
105         }
106
107 }