File: ClosePointsAlgorithm.java

package info (click to toggle)
gpsprune 10-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 2,220 kB
  • ctags: 3,013
  • sloc: java: 22,662; sh: 23; makefile: 16; python: 15
file content (107 lines) | stat: -rw-r--r-- 2,978 bytes parent folder | download
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
package tim.prune.function.compress;

import java.awt.Component;
import java.awt.event.ActionListener;

import tim.prune.data.DataPoint;
import tim.prune.data.Track;

/**
 * Algorithm for detecting close points to compress
 * Only checks distance to previous point, not any earlier point
 */
public class ClosePointsAlgorithm extends SingleParameterAlgorithm
{

	/**
	 * Constructor
	 * @param inTrack track object
	 * @param inDetails track details object
	 * @param inListener listener to attach to activation control
	 */
	public ClosePointsAlgorithm(Track inTrack, TrackDetails inDetails, ActionListener inListener)
	{
		super(inTrack, inDetails, inListener);
	}

	/**
	 * Perform the compression and work out which points should be deleted
	 * @param inFlags deletion flags from previous algorithms
	 * @return number of points deleted
	 */
	protected int compress(boolean[] inFlags)
	{
		// Parse parameter
		double param = getParameter();
		// Use 1/x if x greater than 1
		if (param > 1.0) param = 1.0 / param;
		if (param <= 0.0 || param >= 1.0) {
			// Parameter isn't valid, don't delete any
			return 0;
		}
		double threshold = _trackDetails.getTrackSpan() * param;

		// Loop over all points checking distances to previous point
		// TODO: Maybe this should also check distance to _next_ point as well!
		int numPoints = _track.getNumPoints();
		int prevPointIndex = 0;
		int prevTrackPointIndex = 0;
		double pointDist = 0.0;
		int numDeleted = 0;
		for (int i=1; i<numPoints; i++)
		{
			// don't delete points already deleted
			if (!inFlags[i])
			{
				DataPoint currPoint = _track.getPoint(i);
				// Don't consider waypoints
				if (!currPoint.isWaypoint())
				{
					// Don't delete any photo points or start/end of segments
					if (currPoint.getPhoto() == null
						&& !_trackDetails.isSegmentStart(i) && !_trackDetails.isSegmentEnd(i))
					{
						// Check current point against prevPoint
						pointDist = Math.abs(_track.getX(i) - _track.getX(prevPointIndex))
						 + Math.abs(_track.getY(i) - _track.getY(prevPointIndex));
						if (pointDist < threshold) {
							inFlags[i] = true;
							numDeleted++;
						}
						else if (prevTrackPointIndex != prevPointIndex)
						{
							// Check current point against prevTrackPoint
							pointDist = Math.abs(_track.getX(i) - _track.getX(prevTrackPointIndex))
							 + Math.abs(_track.getY(i) - _track.getY(prevTrackPointIndex));
							if (pointDist < threshold) {
								inFlags[i] = true;
								numDeleted++;
							}
						}
					}
					if (!inFlags[i]) {prevTrackPointIndex = i;}
				}
				if (!inFlags[i]) {prevPointIndex = i;}
			}
		}
		return numDeleted;
	}


	/**
	 * @return specific gui components for dialog
	 */
	protected Component getSpecificGuiComponents()
	{
		return getSpecificGuiComponents("dialog.compress.closepoints.paramdesc", "200");
	}

	/**
	 * @return title key for box
	 */
	protected String getTitleTextKey()
	{
		return "dialog.compress.closepoints.title";
	}

}