X-Git-Url: https://gitweb.fperrin.net/?a=blobdiff_plain;f=tim%2Fprune%2Fdata%2FAltitude.java;h=7d9c21cb6c414a5f4d16503e1f6bce0dbf215a73;hb=7f5ed2be62905bd37717376dc22d09e5ea7edb4d;hp=73552ae62977528d97ca91784a9e3d1dec6cc5da;hpb=312fec956e43f5d0a38617da5d0add9c62563e2c;p=GpsPrune.git diff --git a/tim/prune/data/Altitude.java b/tim/prune/data/Altitude.java index 73552ae..7d9c21c 100644 --- a/tim/prune/data/Altitude.java +++ b/tim/prune/data/Altitude.java @@ -7,26 +7,26 @@ 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 static final double CONVERT_FEET_TO_METRES = 0.3048; - private static final double CONVERT_METRES_TO_FEET = 3.28084; + private Unit _unit = null; + private String _stringValue = null; + /** Constant to use for a lack of altitude */ + public static final Altitude NONE = new Altitude(null, null); /** - * Constructor + * Constructor using String + * @param inString string to parse + * @param inUnit of altitude, either metres or feet */ - public Altitude(String inString, int inFormat) + public Altitude(String inString, Unit inUnit) { + _unit = inUnit; if (inString != null && !inString.equals("")) { try { - _value = Integer.parseInt(inString.trim()); - _format = inFormat; + _stringValue = inString; + _value = (int) Double.parseDouble(inString.trim()); _valid = true; } catch (NumberFormatException nfe) {} @@ -35,15 +35,37 @@ public class Altitude /** - * Constructor + * Constructor with int value + * @param inValue int value of altitude + * @param inUnit unit of altitude, either metres or feet */ - public Altitude(int inValue, int inFormat) + public Altitude(int inValue, Unit inUnit) { _value = inValue; - _format = inFormat; + _unit = inUnit; _valid = true; + _stringValue = "" + inValue; } + /** + * @return an exact copy of this Altitude object + */ + public Altitude clone() + { + return new Altitude(_stringValue, _unit); + } + + /** + * Reset the altitude parameters to the same as the given object + * @param inClone clone object to copy + */ + public void reset(Altitude inClone) + { + _stringValue = inClone._stringValue; + _value = inClone._value; + _unit = inClone._unit; + _valid = inClone._valid; + } /** * @return true if the value could be parsed @@ -62,30 +84,51 @@ public class Altitude return _value; } - /** - * @return format of number + * @param inAltUnit altitude units to use + * @return rounded value in specified units */ - public int getFormat() + public int getValue(Unit inAltUnit) { - return _format; + if (inAltUnit == null) { + return getValue(); + } + return (int) (getMetricValue() * inAltUnit.getMultFactorFromStd()); } + /** + * @return unit of number + */ + public Unit getUnit() + { + return _unit; + } /** - * Get the altitude value in the specified format - * @param inFormat desired format, either FORMAT_METRES or FORMAT_FEET - * @return value as an int + * @return value of altitude in metres, used for calculations and charts */ - public int getValue(int inFormat) + public double getMetricValue() { - if (inFormat == _format) + if (_unit == UnitSetLibrary.UNITS_METRES || _unit == null) { return _value; - if (inFormat == FORMAT_METRES) - return (int) (_value * CONVERT_FEET_TO_METRES); - if (inFormat == FORMAT_FEET) - return (int) (_value * CONVERT_METRES_TO_FEET); - return _value; + } + return _value / _unit.getMultFactorFromStd(); + } + + /** + * Get a string version of the value + * @param inUnit specified unit + * @return string value, if possible the original one + */ + public String getStringValue(Unit inUnit) + { + if (!_valid) {return "";} + // Return string value if the same format or "no format" was requested + if ((inUnit == _unit || inUnit == null) + && _stringValue != null && !_stringValue.equals("")) { + return _stringValue; + } + return "" + getValue(inUnit); } @@ -98,16 +141,54 @@ 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(); + Unit altUnit = inStart.getUnit(); int startValue = inStart.getValue(); - int endValue = inEnd.getValue(altFormat); - int newValue = startValue - + (int) ((endValue - startValue) * 1.0 / (inNumSteps + 1) * (inIndex + 1)); - return new Altitude(newValue, altFormat); + int endValue = inEnd.getValue(altUnit); + // interpolate between start and end + int newValue = startValue + (int) ((endValue - startValue) * inFrac); + return new Altitude(newValue, altUnit); + } + + /** + * Add the given offset to the current altitude + * @param inOffset offset as double + * @param inUnit unit of offset, feet or metres + * @param inDecimals number of decimal places + */ + public void addOffset(double inOffset, Unit inUnit, 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 (inUnit != _unit && inUnit != null) + { + offset = inOffset / inUnit.getMultFactorFromStd() * _unit.getMultFactorFromStd(); + } + // FIXME: The following will fail if _stringValue is null - not sure how it can get in that state! + if (_stringValue == null) System.err.println("*** Altitude.addOffset - how did the string value get to be null?"); + // Add the offset + double newValue = Double.parseDouble(_stringValue.trim()) + offset; + _value = (int) newValue; + _stringValue = NumberUtils.formatNumberUk(newValue, numDecimals); } }