]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/data/GradientCalculator.java
Version 19.1, August 2018
[GpsPrune.git] / tim / prune / data / GradientCalculator.java
1 package tim.prune.data;
2
3 /**
4  * Abstract class to hold static calculation functions
5  * for gradient (like glide slope)
6  */
7 public abstract class GradientCalculator
8 {
9         /**
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
14          */
15         public static void calculateGradient(Track inTrack, int inIndex, SpeedValue inValue)
16         {
17                 if (inValue != null)
18                 {
19                         inValue.setInvalid();
20                 }
21                 if (inTrack == null || inIndex < 0 || inValue == null)
22                 {
23                         System.err.println("Cannot calculate gradient for index " + inIndex);
24                         return;
25                 }
26
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()) {
30                         return;
31                 }
32
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());
36                 }
37                 else if (!point.getSegmentStart())
38                 {
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())
44                         {
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);
52                         }
53                 }
54                 // otherwise, just leave value as invalid
55         }
56 }