]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/data/GradientCalculator.java
Version 19, May 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                 inValue.setInvalid();
18                 if (inTrack == null || inIndex < 0 || inValue == null)
19                 {
20                         System.err.println("Cannot calculate gradient for index " + inIndex);
21                         return;
22                 }
23
24                 // If no altitude or it's a waypoint then no gradient either
25                 DataPoint point = inTrack.getPoint(inIndex);
26                 if (point == null || !point.hasAltitude() || point.isWaypoint()) {
27                         return;
28                 }
29
30                 // If the point has horizontal and vertical speeds already then just use those
31                 if (point.hasHSpeed() && point.hasVSpeed()) {
32                         inValue.setValue(point.getVSpeed().getValueInMetresPerSec() / point.getHSpeed().getValueInMetresPerSec());
33                 }
34                 else if (!point.getSegmentStart())
35                 {
36                         // Use the previous track point and the next track point
37                         DataPoint p = inTrack.getPreviousTrackPoint(inIndex-1);
38                         DataPoint q = inTrack.getNextTrackPoint(inIndex+1);
39                         if (p != null && q != null && !q.getSegmentStart()
40                                 && p.hasAltitude() && q.hasAltitude())
41                         {
42                                 final double horizRads = DataPoint.calculateRadiansBetween(p, point) +
43                                         DataPoint.calculateRadiansBetween(point, q);
44                                 final double horizDist = Distance.convertRadiansToDistance(horizRads, UnitSetLibrary.UNITS_METRES);
45                                 final double heightDiff = q.getAltitude().getMetricValue() - p.getAltitude().getMetricValue();
46                                 // Get gradient in radians
47                                 final double gradient = Math.atan2(heightDiff, horizDist);
48                                 inValue.setValue(gradient);
49                         }
50                 }
51                 // otherwise, just leave value as invalid
52         }
53 }