1 package tim.prune.data;
4 * Abstract class to hold static calculation functions
5 * for gradient (like glide slope)
7 public abstract class GradientCalculator
10 * Calculate the gradient value of the track at the specified index
11 * @param inTrack track object
12 * @param inIndex index of point to calculate gradient for
13 * @param inValue object in which to place result of calculation
15 public static void calculateGradient(Track inTrack, int inIndex, SpeedValue inValue)
21 if (inTrack == null || inIndex < 0 || inValue == null)
23 System.err.println("Cannot calculate gradient for index " + inIndex);
27 // If no altitude or it's a waypoint then no gradient either
28 DataPoint point = inTrack.getPoint(inIndex);
29 if (point == null || !point.hasAltitude() || point.isWaypoint()) {
33 // If the point has horizontal and vertical speeds already then just use those
34 if (point.hasHSpeed() && point.hasVSpeed()) {
35 inValue.setValue(point.getVSpeed().getValueInMetresPerSec() / point.getHSpeed().getValueInMetresPerSec());
37 else if (!point.getSegmentStart())
39 // Use the previous track point and the next track point
40 DataPoint p = inTrack.getPreviousTrackPoint(inIndex-1);
41 DataPoint q = inTrack.getNextTrackPoint(inIndex+1);
42 if (p != null && q != null && !q.getSegmentStart()
43 && p.hasAltitude() && q.hasAltitude())
45 final double horizRads = DataPoint.calculateRadiansBetween(p, point) +
46 DataPoint.calculateRadiansBetween(point, q);
47 final double horizDist = Distance.convertRadiansToDistance(horizRads, UnitSetLibrary.UNITS_METRES);
48 final double heightDiff = q.getAltitude().getMetricValue() - p.getAltitude().getMetricValue();
49 // Get gradient in radians
50 final double gradient = Math.atan2(heightDiff, horizDist);
51 inValue.setValue(gradient);
54 // otherwise, just leave value as invalid