]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/data/PointScaler.java
a1338857e0c509acc6a3d6227f4044080e3f2914
[GpsPrune.git] / tim / prune / data / PointScaler.java
1 package tim.prune.data;
2
3 /**
4  * Class to manage the scaling of points, used by the ThreeDModel
5  */
6 public class PointScaler
7 {
8         // Original data
9         private Track _track = null;
10         // Scaled values
11         private double[] _xValues = null;
12         private double[] _yValues = null;
13         private double[] _altValues = null;
14         // Altitude range
15         private double _altitudeRange = 0.0;
16
17
18         /**
19          * Constructor
20          * @param inTrack Track object
21          */
22         public PointScaler(Track inTrack)
23         {
24                 _track = inTrack;
25         }
26
27
28         /**
29          * Scale the points
30          */
31         public void scale()
32         {
33                 // Work out extents
34                 TrackExtents extents = new TrackExtents(_track);
35                 extents.applySquareBorder();
36                 final double horizDistance = Math.max(extents.getHorizontalDistanceMetres(), 1.0);
37                 final int numPoints = _track.getNumPoints();
38
39                 // Find altitude range
40                 _altitudeRange = extents.getAltitudeRange().getRange() / horizDistance;
41                 final double minAltitude = extents.getAltitudeRange().getMinimum();
42
43                 // create new arrays for scaled values
44                 if (_xValues == null || _xValues.length != numPoints)
45                 {
46                         _xValues = new double[numPoints];
47                         _yValues = new double[numPoints];
48                         _altValues = new double[numPoints];
49                 }
50
51                 final double midXvalue = extents.getXRange().getMidValue();
52                 final double midYvalue = extents.getYRange().getMidValue();
53                 final double xyRange   = extents.getXRange().getRange();
54
55                 // Calculate scaled values
56                 for (int p=0; p<numPoints; p++)
57                 {
58                         DataPoint point = _track.getPoint(p);
59                         if (point != null)
60                         {
61                                 _xValues[p] = (_track.getX(p) - midXvalue) / xyRange;
62                                 _yValues[p] = (midYvalue - _track.getY(p)) / xyRange; // y values have to be inverted
63                                 _altValues[p] = (point.getAltitude().getMetricValue() - minAltitude) / horizDistance;
64                         }
65                 }
66         }
67
68
69
70         /**
71          * Get the horizontal value for the specified point
72          * @param inIndex index of point, starting at 0
73          * @return scaled horizontal value
74          */
75         public double getHorizValue(int inIndex)
76         {
77                 return _xValues[inIndex];
78         }
79
80         /**
81          * Get the vertical value for the specified point
82          * @param inIndex index of point, starting at 0
83          * @return scaled vertical value
84          */
85         public double getVertValue(int inIndex)
86         {
87                 return _yValues[inIndex];
88         }
89
90         /**
91          * Get the altitude value for the specified point
92          * @param inIndex index of point, starting at 0
93          * @return scaled altitude value
94          */
95         public double getAltValue(int inIndex)
96         {
97                 return _altValues[inIndex];
98         }
99
100         /**
101          * @return altitude range, in metres
102          */
103         public double getAltitudeRange()
104         {
105                 return _altitudeRange;
106         }
107 }