File: AltitudeRange.java

package info (click to toggle)
gpsprune 17-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 3,984 kB
  • ctags: 5,218
  • sloc: java: 39,403; sh: 25; makefile: 17; python: 15
file content (232 lines) | stat: -rw-r--r-- 6,739 bytes parent folder | download | duplicates (3)
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package tim.prune.data;

import tim.prune.config.Config;

/**
 * Represents a range of altitudes, taking units into account.
 * Values assumed to be >= 0.
 */
public class AltitudeRange
{
	/** Range of altitudes in metres */
	private IntegerRange _range = new IntegerRange();
	/** Flag for whether previous value exists or not */
	private boolean _gotPreviousValue;
	/** Previous metric value */
	private int _previousValue;
	/** Total climb in metres */
	private int _climb;
	/** Total descent in metres */
	private int _descent;
	/** Flags for whether minimum or maximum has been found */
	private boolean _gotPreviousMinimum = false, _gotPreviousMaximum = false;
	/** Integer values of previous minimum and maximum, if any */
	private int     _previousExtreme = 0;


	/**
	 * Constructor
	 */
	public AltitudeRange() {
		clear();
	}

	/**
	 * Clear the altitude range
	 */
	public void clear()
	{
		_range.clear();
		_climb = _descent = 0;
		_gotPreviousValue = false;
		_previousValue = 0;
		_gotPreviousMinimum = _gotPreviousMaximum = false;
		_previousExtreme = 0;
	}


	/**
	 * Add a value to the range
	 * @param inAltitude value to add, only positive values considered
	 */
	public void addValue(Altitude inAltitude)
	{
		final int wiggleLimit = Config.getConfigInt(Config.KEY_ALTITUDE_TOLERANCE) / 100;

		if (inAltitude != null && inAltitude.isValid())
		{
			int altValue = (int) inAltitude.getMetricValue();
			_range.addValue(altValue);
			// Compare with previous value if any
			if (_gotPreviousValue)
			{
				if (altValue != _previousValue)
				{
					// Got an altitude value which is different from the previous one
					final boolean locallyUp = (altValue > _previousValue);
					final boolean overallUp = _gotPreviousMinimum && _previousValue > _previousExtreme;
					final boolean overallDn = _gotPreviousMaximum && _previousValue < _previousExtreme;
					final boolean moreThanWiggle = Math.abs(altValue - _previousValue) > wiggleLimit;
					// Do we know whether we're going up or down yet?
					if (!_gotPreviousMinimum && !_gotPreviousMaximum)
					{
						// we don't know whether we're going up or down yet - check limit
						if (moreThanWiggle)
						{
							if (locallyUp) {_gotPreviousMinimum = true;}
							else {_gotPreviousMaximum = true;}
							_previousExtreme = _previousValue;
							_previousValue = altValue;
							_gotPreviousValue = true;
						}
					}
					else if (overallUp)
					{
						if (locallyUp) {
							// we're still going up - do nothing
							_previousValue = altValue;
						}
						else if (moreThanWiggle)
						{
							// we're going up but have dropped over a maximum
							// Add the climb from _previousExtreme up to _previousValue
							_climb += (_previousValue - _previousExtreme);
							_previousExtreme = _previousValue;
							_gotPreviousMinimum = false; _gotPreviousMaximum = true;
							_previousValue = altValue;
							_gotPreviousValue = true;
						}
					}
					else if (overallDn)
					{
						if (locallyUp) {
							if (moreThanWiggle)
							{
								// we're going down but have climbed up from a minimum
								// Add the descent from _previousExtreme down to _previousValue
								_descent += (_previousExtreme - _previousValue);
								_previousExtreme = _previousValue;
								_gotPreviousMinimum = true; _gotPreviousMaximum = false;
								_previousValue = altValue;
								_gotPreviousValue = true;
							}
						}
						else {
							// we're still going down - do nothing
							_previousValue = altValue;
							_gotPreviousValue = true;
						}
					}
					// TODO: Behaviour when WIGGLE_LIMIT == 0 should be same as before, all differences cumulated
				}
			}
			else
			{
				// we haven't got a previous value at all, so it's the start of a new segment
				_previousValue = altValue;
				_gotPreviousValue = true;
			}

//			if (!_empty)
//			{
//				if (altValue > _previousValue)
//					_climb += (altValue - _previousValue);
//				else
//					_descent += (_previousValue - altValue);
//			}
//			_previousValue = altValue;
//			_empty = false;
		}
	}

	/**
	 * Reset the climb/descent calculations starting from the given value
	 * @param inAltitude altitude value
	 */
	public void ignoreValue(Altitude inAltitude)
	{
		// Process the previous value, if any, to update climb/descent as that's the end of the previous segment
		if (_gotPreviousValue && _gotPreviousMinimum && _previousValue > _previousExtreme) {
			_climb += (_previousValue - _previousExtreme);
		}
		else if (_gotPreviousValue && _gotPreviousMaximum && _previousValue < _previousExtreme) {
			_descent += (_previousExtreme - _previousValue);
		}
		// Eliminate the counting values to start the new segment
		_gotPreviousMinimum = _gotPreviousMaximum = false;
		_gotPreviousValue = false;
		// Now process this value if there is one
		if (inAltitude != null && inAltitude.isValid())
		{
			final int altValue = (int) inAltitude.getMetricValue();
			_range.addValue(altValue);
			_previousValue = altValue;
			_gotPreviousValue = true;
		}
	}

	/**
	 * @return true if altitude range found
	 */
	public boolean hasRange()
	{
		return _range.getMaximum() > _range.getMinimum();
	}


	/**
	 * @param inUnit altitude units to use
	 * @return minimum value, or -1 if none found
	 */
	public int getMinimum(Unit inUnit)
	{
		if (_range.getMinimum() <= 0) return _range.getMinimum();
		return (int) (_range.getMinimum() * inUnit.getMultFactorFromStd());
	}

	/**
	 * @param inUnit altitude units to use
	 * @return maximum value, or -1 if none found
	 */
	public int getMaximum(Unit inUnit)
	{
		if (_range.getMaximum() <= 0) return _range.getMaximum();
		return (int) (_range.getMaximum() * inUnit.getMultFactorFromStd());
	}

	/**
	 * @param inUnit altitude units to use
	 * @return total climb
	 */
	public int getClimb(Unit inUnit)
	{
		// May need to add climb from last segment
		int lastSegmentClimb = 0;
		if (_gotPreviousValue && _gotPreviousMinimum && _previousValue > _previousExtreme) {
			lastSegmentClimb = _previousValue - _previousExtreme;
		}
		return (int) ((_climb + lastSegmentClimb) * inUnit.getMultFactorFromStd());
	}

	/**
	 * @param inUnit altitude units to use
	 * @return total descent
	 */
	public int getDescent(Unit inUnit)
	{
		// May need to add descent from last segment
		int lastSegmentDescent = 0;
		if (_gotPreviousValue && _gotPreviousMaximum && _previousValue < _previousExtreme) {
			lastSegmentDescent = _previousExtreme - _previousValue;
		}
		return (int) ((_descent + lastSegmentDescent) * inUnit.getMultFactorFromStd());
	}

	/**
	 * @return overall height gain in metres
	 */
	public double getMetricHeightDiff()
	{
		return getClimb(UnitSetLibrary.UNITS_METRES) - getDescent(UnitSetLibrary.UNITS_METRES);
	}
}