]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/data/PointScaler.java
Version 16, February 2014
[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         /** Secondary data for terrain grid */
11         private Track _terrainTrack = null;
12         // Scaled values for data track
13         private double[] _xValues = null;
14         private double[] _yValues = null;
15         private double[] _altValues = null;
16         // Scaled values for terrain track, if any
17         private double[] _terrainxValues = null;
18         private double[] _terrainyValues = null;
19         private double[] _terrainAltValues = null;
20         // Altitude range
21         private double _altitudeRange = 0.0;
22         private double _minAltitudeMetres = 0.0;
23         // Horizontal distance
24         private double _horizDistanceMetres = 0.0;
25
26
27         /**
28          * Constructor
29          * @param inTrack Track object
30          */
31         public PointScaler(Track inTrack)
32         {
33                 _track = inTrack;
34         }
35
36         /**
37          * @param inTrack terrain track to add
38          */
39         public void addTerrain(Track inTrack)
40         {
41                 _terrainTrack = inTrack;
42         }
43
44         /**
45          * Scale the points
46          */
47         public void scale()
48         {
49                 // Work out extents
50                 TrackExtents extents = new TrackExtents(_track);
51                 extents.applySquareBorder();
52                 _horizDistanceMetres = Math.max(extents.getHorizontalDistanceMetres(), 1.0);
53                 final int numPoints = _track.getNumPoints();
54
55                 // Find altitude range (including terrain)
56                 DoubleRange altRangeMetres = extents.getAltitudeRange();
57                 if (_terrainTrack != null) {
58                         altRangeMetres.combine(new TrackExtents(_terrainTrack).getAltitudeRange());
59                 }
60                 _altitudeRange = altRangeMetres.getRange() / _horizDistanceMetres;
61                 _minAltitudeMetres = altRangeMetres.getMinimum();
62
63                 // create new arrays for scaled values
64                 if (_xValues == null || _xValues.length != numPoints)
65                 {
66                         _xValues = new double[numPoints];
67                         _yValues = new double[numPoints];
68                         _altValues = new double[numPoints];
69                         if (_terrainTrack != null)
70                         {
71                                 _terrainxValues = new double[_terrainTrack.getNumPoints()];
72                                 _terrainyValues = new double[_terrainTrack.getNumPoints()];
73                                 _terrainAltValues = new double[_terrainTrack.getNumPoints()];
74                         }
75                 }
76
77                 final double midXvalue = extents.getXRange().getMidValue();
78                 final double midYvalue = extents.getYRange().getMidValue();
79                 final double xyRange   = extents.getXRange().getRange();
80
81                 // Calculate scaled values
82                 for (int p=0; p<numPoints; p++)
83                 {
84                         DataPoint point = _track.getPoint(p);
85                         if (point != null)
86                         {
87                                 _xValues[p] = (_track.getX(p) - midXvalue) / xyRange;
88                                 _yValues[p] = (midYvalue - _track.getY(p)) / xyRange; // y values have to be inverted
89                                 _altValues[p] = (point.getAltitude().getMetricValue() - _minAltitudeMetres) / _horizDistanceMetres;
90                         }
91                 }
92                 if (_terrainTrack != null)
93                 {
94                         for (int p=0; p<_terrainTrack.getNumPoints(); p++)
95                         {
96                                 _terrainxValues[p] = (_terrainTrack.getX(p) - midXvalue) / xyRange;
97                                 _terrainyValues[p] = (midYvalue - _terrainTrack.getY(p)) / xyRange; // y values have to be inverted
98                                 _terrainAltValues[p] = (_terrainTrack.getPoint(p).getAltitude().getMetricValue() - _minAltitudeMetres) / _horizDistanceMetres;
99                         }
100                 }
101         }
102
103
104
105         /**
106          * Get the horizontal value for the specified point
107          * @param inIndex index of point, starting at 0
108          * @return scaled horizontal value
109          */
110         public double getHorizValue(int inIndex)
111         {
112                 return _xValues[inIndex];
113         }
114
115         /**
116          * Get the vertical value for the specified point
117          * @param inIndex index of point, starting at 0
118          * @return scaled vertical value
119          */
120         public double getVertValue(int inIndex)
121         {
122                 return _yValues[inIndex];
123         }
124
125         /**
126          * Get the altitude value for the specified point
127          * @param inIndex index of point, starting at 0
128          * @return scaled altitude value
129          */
130         public double getAltValue(int inIndex)
131         {
132                 return _altValues[inIndex];
133         }
134
135         /**
136          * @return altitude range as fraction of horizontal range
137          */
138         public double getAltitudeRange()
139         {
140                 return _altitudeRange;
141         }
142
143         /**
144          * Get the horizontal value for the specified point
145          * @param inIndex index of point, starting at 0
146          * @return scaled horizontal value
147          */
148         public double getTerrainHorizValue(int inIndex)
149         {
150                 return _terrainxValues[inIndex];
151         }
152
153         /**
154          * Get the vertical value for the specified point
155          * @param inIndex index of point, starting at 0
156          * @return scaled vertical value
157          */
158         public double getTerrainVertValue(int inIndex)
159         {
160                 return _terrainyValues[inIndex];
161         }
162
163         /**
164          * @param inIndex index of point in terrain track
165          * @return scaled altitude value for the specified terrain point
166          */
167         public double getTerrainAltValue(int inIndex)
168         {
169                 if (_terrainAltValues != null) {
170                         return _terrainAltValues[inIndex];
171                 }
172                 return 0.0;
173         }
174 }