]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/function/compress/SingletonAlgorithm.java
Version 7, February 2009
[GpsPrune.git] / tim / prune / function / compress / SingletonAlgorithm.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 singleton points to compress
11  */
12 public class SingletonAlgorithm 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 SingletonAlgorithm(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                 // System.out.println("Singleton algorithm compressing : " + param + ", " + _trackDetails.getMeanRadians());
35                 int numPoints = _track.getNumPoints();
36                 int numDeleted = 0;
37                 double threshold = param * _trackDetails.getMeanRadians();
38                 DataPoint currPoint = null, prevPoint = null;
39                 // Loop over all points looking for points far away from neighbours
40                 for (int i=0; i<numPoints; i++)
41                 {
42                         currPoint = _track.getPoint(i);
43                         // Don't delete points which are already marked as deleted
44                         if (!inFlags[i])
45                         {
46                                 // Don't delete any waypoints or photo points
47                                 // Only interested in start and end of segments
48                                 if (!currPoint.isWaypoint() && currPoint.getPhoto() == null
49                                         && _trackDetails.isSegmentStart(i) && _trackDetails.isSegmentEnd(i))
50                                 {
51                                         // Measure distance from previous track point
52                                         if (DataPoint.calculateRadiansBetween(prevPoint, currPoint) > threshold)
53                                         {
54                                                 // Now need to find next track point, and measure distances
55                                                 DataPoint nextPoint = _track.getNextTrackPoint(i+1);
56                                                 if (nextPoint != null && DataPoint.calculateRadiansBetween(currPoint, nextPoint) > threshold)
57                                                 {
58                                                         // Found a point to delete (hope that next point hasn't been deleted already)
59                                                         inFlags[i] = true;
60                                                         numDeleted++;
61                                                 }
62                                         }
63                                 }
64                                 // Remember last (not-deleted) track point
65                                 if (!currPoint.isWaypoint()) {prevPoint = currPoint;}
66                         }
67                 }
68                 return numDeleted;
69         }
70
71         /**
72          * @return specific gui components for dialog
73          */
74         protected Component getSpecificGuiComponents()
75         {
76                 return getSpecificGuiComponents("dialog.compress.singletons.paramdesc", "2");
77         }
78
79         /**
80          * @return title key for box
81          */
82         protected String getTitleTextKey()
83         {
84                 return "dialog.compress.singletons.title";
85         }
86 }