File: SelectExtremePoint.java

package info (click to toggle)
gpsprune 26.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,824 kB
  • sloc: java: 52,154; sh: 25; makefile: 21; python: 15
file content (96 lines) | stat: -rw-r--r-- 2,292 bytes parent folder | download | duplicates (4)
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
package tim.prune.function;

import tim.prune.App;
import tim.prune.GenericFunction;
import tim.prune.data.DataPoint;
import tim.prune.data.UnitSetLibrary;
import tim.prune.gui.profile.SpeedData;

/**
 * Function to select an extreme point from the track
 */
public class SelectExtremePoint extends GenericFunction
{
	public enum Extreme {HIGHEST, LOWEST, FASTEST}

	private final Extreme _extreme;

	public SelectExtremePoint(App inApp, Extreme inExtreme) {
		super(inApp);
		_extreme = inExtreme;
	}

	@Override
	public void begin()
	{
		if (_extreme == Extreme.HIGHEST || _extreme == Extreme.LOWEST) {
			selectHighestOrLowest();
		}
		else if (_extreme == Extreme.FASTEST) {
			selectFastest();
		}
		else {
			throw new IllegalArgumentException("Unexpected extreme: " + _extreme);
		}
	}

	private void selectHighestOrLowest()
	{
		int bestPointIndex = -1;
		double bestValue = 0.0;
		for (int i=0; i<_app.getTrackInfo().getTrack().getNumPoints(); i++)
		{
			DataPoint point = _app.getTrackInfo().getTrack().getPoint(i);
			if (point == null || !point.hasAltitude()) {
				continue;
			}
			double currValue = point.getAltitude().getMetricValue();
			if (_extreme == Extreme.LOWEST) {
				currValue = -currValue;
			}
			if (bestPointIndex == -1 || currValue > bestValue)
			{
				bestValue = currValue;
				bestPointIndex = i;
			}
		}
		if (bestPointIndex >= 0) {
			_app.getTrackInfo().selectPoint(bestPointIndex);
		}
	}

	public void selectFastest()
	{
		SpeedData speeds = new SpeedData(_app.getTrackInfo().getTrack());
		speeds.init(UnitSetLibrary.getMetricUnitSet());
		int bestPointIndex = -1;
		double maxSpeed = 0.0;
		for (int i=0; i<_app.getTrackInfo().getTrack().getNumPoints(); i++)
		{
			final double speed;
			DataPoint point = _app.getTrackInfo().getTrack().getPoint(i);
			if (point.hasHSpeed()) {
				speed = point.getHSpeed().getValueInMetresPerSec();
			}
			else if (speeds.hasData(i)) {
				speed = speeds.getData(i);
			}
			else {
				continue;
			}
			if (bestPointIndex == -1 || speed > maxSpeed)
			{
				maxSpeed = speed;
				bestPointIndex = i;
			}
		}
		if (bestPointIndex > -1) {
			_app.getTrackInfo().selectPoint(bestPointIndex);
		}
	}

	@Override
	public String getNameKey() {
		return "menu.point.goto." + _extreme.toString().toLowerCase();
	}
}