X-Git-Url: https://gitweb.fperrin.net/?a=blobdiff_plain;f=tim%2Fprune%2Fdata%2FAltitude.java;h=06a32911197fc3e0560e81c45ac042bb5a080ed3;hb=140e9d165f85c3d4f0435a311e091209313faa2a;hp=54a3f68eb862416791840dcfcb84009a5d191499;hpb=5625a1abadb5f2ca5f017fe7dbda1d5141cb637b;p=GpsPrune.git diff --git a/tim/prune/data/Altitude.java b/tim/prune/data/Altitude.java index 54a3f68..06a3291 100644 --- a/tim/prune/data/Altitude.java +++ b/tim/prune/data/Altitude.java @@ -7,24 +7,39 @@ public class Altitude { private boolean _valid = false; private int _value = 0; - private int _format = -1; - public static final int FORMAT_NONE = -1; - public static final int FORMAT_METRES = 0; - public static final int FORMAT_FEET = 1; + private Format _format = Format.NO_FORMAT; + private String _stringValue = null; + + /** Altitude formats */ + public enum Format { + /** No format */ + NO_FORMAT, + /** Metres */ + METRES, + /** Feet */ + FEET + } + /** Constants for conversion */ private static final double CONVERT_FEET_TO_METRES = 0.3048; private static final double CONVERT_METRES_TO_FEET = 3.28084; + /** Constant for no altitude value */ + public static final Altitude NONE = new Altitude(null, Format.NO_FORMAT); + /** - * Constructor + * Constructor using String + * @param inString string to parse + * @param inFormat format of altitude, either metres or feet */ - public Altitude(String inString, int inFormat) + public Altitude(String inString, Format inFormat) { if (inString != null && !inString.equals("")) { try { + _stringValue = inString; _value = (int) Double.parseDouble(inString.trim()); _format = inFormat; _valid = true; @@ -35,15 +50,35 @@ public class Altitude /** - * Constructor + * Constructor with int value + * @param inValue int value of altitude + * @param inFormat format of altitude, either metres or feet */ - public Altitude(int inValue, int inFormat) + public Altitude(int inValue, Format inFormat) { _value = inValue; _format = inFormat; _valid = true; } + /** + * @return an exact copy of this Altitude object + */ + public Altitude clone() + { + return new Altitude(_stringValue, _format); + } + + /** + * Reset the altitude parameters to the same as the given object + * @param inClone clone object to copy + */ + public void reset(Altitude inClone) + { + _value = inClone._value; + _format = inClone._format; + _valid = inClone._valid; + } /** * @return true if the value could be parsed @@ -66,7 +101,7 @@ public class Altitude /** * @return format of number */ - public int getFormat() + public Format getFormat() { return _format; } @@ -77,18 +112,34 @@ public class Altitude * @param inFormat desired format, either FORMAT_METRES or FORMAT_FEET * @return value as an int */ - public int getValue(int inFormat) + public int getValue(Format inFormat) { - // TODO: Fix rounding errors here converting between units - return double? + // Note possible rounding errors here if converting to/from units if (inFormat == _format) return _value; - if (inFormat == FORMAT_METRES) + if (inFormat == Format.METRES) return (int) (_value * CONVERT_FEET_TO_METRES); - if (inFormat == FORMAT_FEET) + if (inFormat == Format.FEET) return (int) (_value * CONVERT_METRES_TO_FEET); return _value; } + /** + * Get a string version of the value + * @param inFormat specified format + * @return string value, if possible the original one + */ + public String getStringValue(Format inFormat) + { + if (!_valid) {return "";} + // Return string value if the same format or "no format" was requested + if ((inFormat == _format || inFormat == Format.NO_FORMAT) + && _stringValue != null && !_stringValue.equals("")) { + return _stringValue; + } + return "" + getValue(inFormat); + } + /** * Interpolate a new Altitude object between the given ones @@ -99,16 +150,55 @@ public class Altitude * @return Interpolated Altitude object */ public static Altitude interpolate(Altitude inStart, Altitude inEnd, int inIndex, int inNumSteps) + { + return interpolate(inStart, inEnd, 1.0 * (inIndex + 1) / (inNumSteps + 1)); + } + + + /** + * Interpolate a new Altitude object between the given ones + * @param inStart start altitude + * @param inEnd end altitude + * @param inFrac fraction of distance from first point + * @return Interpolated Altitude object + */ + public static Altitude interpolate(Altitude inStart, Altitude inEnd, double inFrac) { // Check if altitudes are valid if (inStart == null || inEnd == null || !inStart.isValid() || !inEnd.isValid()) - return new Altitude(null, FORMAT_NONE); + return Altitude.NONE; // Use altitude format of first point - int altFormat = inStart.getFormat(); + Format altFormat = inStart.getFormat(); int startValue = inStart.getValue(); int endValue = inEnd.getValue(altFormat); - int newValue = startValue - + (int) ((endValue - startValue) * 1.0 / (inNumSteps + 1) * (inIndex + 1)); + // interpolate between start and end + int newValue = startValue + (int) ((endValue - startValue) * inFrac); return new Altitude(newValue, altFormat); } + + /** + * Add the given offset to the current altitude + * @param inOffset offset as double + * @param inFormat format of offset, feet or metres + * @param inDecimals number of decimal places + */ + public void addOffset(double inOffset, Format inFormat, int inDecimals) + { + // Use the maximum number of decimal places from current value and offset + int numDecimals = NumberUtils.getDecimalPlaces(_stringValue); + if (numDecimals < inDecimals) {numDecimals = inDecimals;} + // Convert offset to correct units + double offset = inOffset; + if (inFormat != _format) + { + if (inFormat == Format.FEET) + offset = inOffset * CONVERT_FEET_TO_METRES; + else + offset = inOffset * CONVERT_METRES_TO_FEET; + } + // Add the offset + double newValue = Double.parseDouble(_stringValue.trim()) + offset; + _value = (int) newValue; + _stringValue = NumberUtils.formatNumber(newValue, numDecimals); + } }