1 package tim.prune.data;
4 * Class to manage the scaling of points, used by the ThreeDModel
6 public class PointScaler
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;
21 private double _altitudeRange = 0.0;
22 private double _minAltitudeMetres = 0.0;
23 // Horizontal distance
24 private double _horizDistanceMetres = 0.0;
29 * @param inTrack Track object
31 public PointScaler(Track inTrack)
37 * @param inTrack terrain track to add
39 public void addTerrain(Track inTrack)
41 _terrainTrack = inTrack;
50 TrackExtents extents = new TrackExtents(_track);
51 extents.applySquareBorder();
52 _horizDistanceMetres = Math.max(extents.getHorizontalDistanceMetres(), 1.0);
53 final int numPoints = _track.getNumPoints();
55 // Find altitude range (including terrain)
56 DoubleRange altRangeMetres = extents.getAltitudeRange();
57 if (_terrainTrack != null) {
58 altRangeMetres.combine(new TrackExtents(_terrainTrack).getAltitudeRange());
60 _altitudeRange = altRangeMetres.getRange() / _horizDistanceMetres;
61 _minAltitudeMetres = altRangeMetres.getMinimum();
63 // create new arrays for scaled values
64 if (_xValues == null || _xValues.length != numPoints)
66 _xValues = new double[numPoints];
67 _yValues = new double[numPoints];
68 _altValues = new double[numPoints];
69 if (_terrainTrack != null)
71 _terrainxValues = new double[_terrainTrack.getNumPoints()];
72 _terrainyValues = new double[_terrainTrack.getNumPoints()];
73 _terrainAltValues = new double[_terrainTrack.getNumPoints()];
77 final double midXvalue = extents.getXRange().getMidValue();
78 final double midYvalue = extents.getYRange().getMidValue();
79 final double xyRange = extents.getXRange().getRange();
81 // Calculate scaled values
82 for (int p=0; p<numPoints; p++)
84 DataPoint point = _track.getPoint(p);
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;
92 if (_terrainTrack != null)
94 for (int p=0; p<_terrainTrack.getNumPoints(); p++)
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;
106 * Get the horizontal value for the specified point
107 * @param inIndex index of point, starting at 0
108 * @return scaled horizontal value
110 public double getHorizValue(int inIndex)
112 return _xValues[inIndex];
116 * Get the vertical value for the specified point
117 * @param inIndex index of point, starting at 0
118 * @return scaled vertical value
120 public double getVertValue(int inIndex)
122 return _yValues[inIndex];
126 * Get the altitude value for the specified point
127 * @param inIndex index of point, starting at 0
128 * @return scaled altitude value
130 public double getAltValue(int inIndex)
132 return _altValues[inIndex];
136 * @return altitude range as fraction of horizontal range
138 public double getAltitudeRange()
140 return _altitudeRange;
144 * Get the horizontal value for the specified point
145 * @param inIndex index of point, starting at 0
146 * @return scaled horizontal value
148 public double getTerrainHorizValue(int inIndex)
150 return _terrainxValues[inIndex];
154 * Get the vertical value for the specified point
155 * @param inIndex index of point, starting at 0
156 * @return scaled vertical value
158 public double getTerrainVertValue(int inIndex)
160 return _terrainyValues[inIndex];
164 * @param inIndex index of point in terrain track
165 * @return scaled altitude value for the specified terrain point
167 public double getTerrainAltValue(int inIndex)
169 if (_terrainAltValues != null) {
170 return _terrainAltValues[inIndex];