1 package tim.prune.threedee;
3 import tim.prune.data.Altitude;
4 import tim.prune.data.DataPoint;
5 import tim.prune.data.PointScaler;
6 import tim.prune.data.Track;
9 * Class to hold a 3d model of the track data,
10 * including all points and scaling operations.
11 * Used by java3d and also Pov export functions
13 public class ThreeDModel
15 private Track _track = null;
16 private PointScaler _scaler = null;
17 private double _modelSize;
18 private int _altitudeCap = -1;
19 private double _scaleFactor = 1.0;
20 private double _altFactor = 1.0;
21 // TODO: How to store rods (lifts) in data?
22 private byte[] _pointTypes = null;
23 private byte[] _pointHeights = null;
25 private static final double DEFAULT_MODEL_SIZE = 10.0;
26 public static final int MINIMUM_ALTITUDE_CAP = 100;
28 // Constants for point types
29 public static final byte POINT_TYPE_WAYPOINT = 1;
30 public static final byte POINT_TYPE_NORMAL_POINT = 2;
31 public static final byte POINT_TYPE_SEGMENT_START = 3;
36 * @param inTrack Track object
38 public ThreeDModel(Track inTrack)
40 this(inTrack, DEFAULT_MODEL_SIZE);
46 * @param inTrack Track object
47 * @param inSize model size
49 public ThreeDModel(Track inTrack, double inSize)
53 if (_modelSize <= 0.0) _modelSize = DEFAULT_MODEL_SIZE;
58 * @return the number of points in the model
60 public int getNumPoints()
62 if (_track == null) return 0;
63 return _track.getNumPoints();
68 * Set the altitude cap
69 * @param inAltitudeCap altitude range to cap to (ignored if less than data range)
71 public void setAltitudeCap(int inAltitudeCap)
73 _altitudeCap = inAltitudeCap;
74 if (_altitudeCap < MINIMUM_ALTITUDE_CAP)
76 _altitudeCap = MINIMUM_ALTITUDE_CAP;
82 * Scale all points and calculate factors
86 // Use PointScaler to sort out x and y values
87 _scaler = new PointScaler(_track);
89 // Calculate scale factor to fit within box
91 if (_scaler.getMaximumHoriz() > 0.0 || _scaler.getMaximumVert() > 0.0)
93 if (_scaler.getMaximumHoriz() > _scaler.getMaximumVert())
95 // scale limited by longitude
96 _scaleFactor = _modelSize / _scaler.getMaximumHoriz();
100 // scale limited by latitude
101 _scaleFactor = _modelSize / _scaler.getMaximumVert();
104 // calculate altitude scale factor
106 if (_scaler.getMaximumAlt() >= 0)
108 // limit by altitude cap or by data range?
109 if (_scaler.getMaximumAlt() > _altitudeCap)
111 // data is bigger than cap
112 _altFactor = _modelSize / _scaler.getMaximumAlt();
117 _altFactor = _modelSize / _altitudeCap;
120 // calculate lat/long lines
121 _scaler.calculateLatLongLines();
123 // calculate point types and height codes
124 calculatePointTypes();
129 * Calculate the point types and height codes
131 private void calculatePointTypes()
133 int numPoints = getNumPoints();
134 _pointTypes = new byte[numPoints];
135 _pointHeights = new byte[numPoints];
136 // Loop over points in track
137 for (int i=0; i<numPoints; i++)
139 DataPoint point = _track.getPoint(i);
140 _pointTypes[i] = (point.isWaypoint()?POINT_TYPE_WAYPOINT:(point.getSegmentStart()?POINT_TYPE_SEGMENT_START:POINT_TYPE_NORMAL_POINT));
141 _pointHeights[i] = (byte) (point.getAltitude().getValue(Altitude.FORMAT_METRES) / 500);
147 * Get the scaled horizontal value for the specified point
148 * @param inIndex index of point
149 * @return scaled horizontal value
151 public double getScaledHorizValue(int inIndex)
153 return _scaler.getHorizValue(inIndex) * _scaleFactor;
157 * Get the scaled vertical value for the specified point
158 * @param inIndex index of point
159 * @return scaled vertical value
161 public double getScaledVertValue(int inIndex)
163 return _scaler.getVertValue(inIndex) * _scaleFactor;
167 * Get the scaled altitude value for the specified point
168 * @param inIndex index of point
169 * @return scaled altitude value
171 public double getScaledAltValue(int inIndex)
173 // if no altitude, just return 0
174 int altVal = _scaler.getAltValue(inIndex);
175 if (altVal < 0) return 0;
176 // scale according to altitude cap
177 return altVal * _altFactor;
182 * @return latitude lines
184 public double[] getLatitudeLines()
186 return _scaler.getLatitudeLines();
190 * @param inIndex index of line, starting at 0
191 * @return scaled position of latitude line
193 public double getScaledLatitudeLine(int inIndex)
195 return _scaler.getScaledLatitudeLines()[inIndex] * _scaleFactor;
199 * @return longitude lines
201 public double[] getLongitudeLines()
203 return _scaler.getLongitudeLines();
207 * @param inIndex index of line, starting at 0
208 * @return scaled position of longitude line
210 public double getScaledLongitudeLine(int inIndex)
212 return _scaler.getScaledLongitudeLines()[inIndex] * _scaleFactor;
216 * @param inIndex index of point, starting at 0
217 * @return point type, either POINT_TYPE_WAYPOINT or POINT_TYPE_NORMAL_POINT
219 public byte getPointType(int inIndex)
221 return _pointTypes[inIndex];
225 * @param inIndex index of point, starting at 0
226 * @return point height code
228 public byte getPointHeightCode(int inIndex)
230 return _pointHeights[inIndex];
234 * @return the current model size
236 public double getModelSize()