]> gitweb.fperrin.net Git - GpsPrune.git/blob - src/tim/prune/function/compress/WackyPointAlgorithm.java
66a4a486e58b44e3df6e24d9d9a9992cb6104394
[GpsPrune.git] / src / tim / prune / function / compress / WackyPointAlgorithm.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 wacky points to compress
11  */
12 public class WackyPointAlgorithm extends SingleParameterAlgorithm
13 {
14         /**
15          * Constructor
16          * @param inTrack track object
17          * @param inDetails track details object
18          * @param inListener listener to attach to activation control
19          */
20         public WackyPointAlgorithm(Track inTrack, TrackDetails inDetails, ActionListener inListener)
21         {
22                 super(inTrack, inDetails, inListener);
23         }
24
25         /**
26          * Perform the compression and work out which points should be deleted
27          * @param inFlags deletion flags from previous algorithms
28          * @return number of points deleted
29          */
30         protected int compress(boolean[] inFlags)
31         {
32                 double param = getParameter();
33                 if (param <= 0.0) return 0;
34                 int numPoints = _track.getNumPoints();
35                 int numDeleted = 0;
36                 double threshold = param * _trackDetails.getMeanRadians();
37                 DataPoint currPoint = null, prevPoint = null;
38                 // Loop over all points looking for points far away from neighbours
39                 for (int i=0; i<numPoints; i++)
40                 {
41                         currPoint = _track.getPoint(i);
42                         // Don't delete points which are already marked as deleted
43                         if (!inFlags[i])
44                         {
45                                 // Don't delete any waypoints or photo points, or start/end of segments
46                                 if (!currPoint.isWaypoint() && !currPoint.hasMedia()
47                                         && !_trackDetails.isSegmentStart(i) && !_trackDetails.isSegmentEnd(i))
48                                 {
49                                         // Measure distance from previous track point
50                                         if (DataPoint.calculateRadiansBetween(prevPoint, currPoint) > threshold)
51                                         {
52                                                 // Now need to find next track point, and measure distances
53                                                 DataPoint nextPoint = _track.getNextTrackPoint(i+1);
54                                                 if (nextPoint != null && DataPoint.calculateRadiansBetween(currPoint, nextPoint) > threshold
55                                                         && DataPoint.calculateRadiansBetween(prevPoint, nextPoint) < threshold)
56                                                 {
57                                                         // Found a point to delete (hope that next point hasn't been deleted already)
58                                                         inFlags[i] = true;
59                                                         numDeleted++;
60                                                 }
61                                         }
62                                 }
63                                 // Remember last (not-deleted) track point
64                                 if (!currPoint.isWaypoint()) {prevPoint = currPoint;}
65                         }
66                 }
67                 return numDeleted;
68         }
69
70         /**
71          * @return specific gui components for dialog
72          */
73         protected Component getSpecificGuiComponents()
74         {
75                 return getSpecificGuiComponents("dialog.compress.wackypoints.paramdesc", "2");
76         }
77
78         /**
79          * @return title key for box
80          */
81         protected String getTitleTextKey()
82         {
83                 return "dialog.compress.wackypoints.title";
84         }
85
86 }