diff --git a/vtm-app/src/org/oscim/app/location/Compass.java b/vtm-app/src/org/oscim/app/location/Compass.java index 5b33451b..9ccb7718 100644 --- a/vtm-app/src/org/oscim/app/location/Compass.java +++ b/vtm-app/src/org/oscim/app/location/Compass.java @@ -32,6 +32,7 @@ import org.oscim.event.Event; import org.oscim.layers.Layer; import org.oscim.map.Map; import org.oscim.renderer.LocationRenderer; +import org.oscim.utils.FastMath; @SuppressWarnings("deprecation") public class Compass extends Layer implements SensorEventListener, Map.UpdateListener, @@ -230,20 +231,13 @@ public class Compass extends Layer implements SensorEventListener, Map.UpdateLis // float rotation = (float) Math.toDegrees(mAzimuthRadians); float change = rotation - mCurRotation; - if (change > 180) - change -= 360; - else if (change < -180) - change += 360; + change = (float) FastMath.clampDegree(change); // low-pass change *= 0.05; rotation = mCurRotation + change; - - if (rotation > 180) - rotation -= 360; - else if (rotation < -180) - rotation += 360; + rotation = (float) FastMath.clampDegree(rotation); // float tilt = (float) Math.toDegrees(mRotationV[1]); // float tilt = (float) Math.toDegrees(mPitchAxisRadians); diff --git a/vtm-json/src/org/oscim/tiling/source/geojson/MapzenGeojsonTileSource.java b/vtm-json/src/org/oscim/tiling/source/geojson/MapzenGeojsonTileSource.java index d50a8f79..d03d5c41 100644 --- a/vtm-json/src/org/oscim/tiling/source/geojson/MapzenGeojsonTileSource.java +++ b/vtm-json/src/org/oscim/tiling/source/geojson/MapzenGeojsonTileSource.java @@ -18,7 +18,7 @@ package org.oscim.tiling.source.geojson; import org.oscim.core.MapElement; import org.oscim.core.Tag; import org.oscim.tiling.source.UrlTileSource; -import org.oscim.utils.math.MathUtils; +import org.oscim.utils.FastMath; import java.util.LinkedHashMap; import java.util.Map; @@ -116,7 +116,7 @@ public class MapzenGeojsonTileSource extends GeojsonTileSource { Object area = properties.get(Tag.KEY_AREA); String areaStr = (area instanceof String) ? (String) area : String.valueOf(area); float height = Float.parseFloat(volumeStr) / Float.parseFloat(areaStr); - String heightStr = String.valueOf(MathUtils.round2(height)); + String heightStr = String.valueOf(FastMath.round2(height)); mapElement.tags.add(new Tag(Tag.KEY_HEIGHT, heightStr, false)); } } diff --git a/vtm-json/src/org/oscim/tiling/source/geojson/NextzenGeojsonTileSource.java b/vtm-json/src/org/oscim/tiling/source/geojson/NextzenGeojsonTileSource.java index cb6b2935..7ac3c92f 100644 --- a/vtm-json/src/org/oscim/tiling/source/geojson/NextzenGeojsonTileSource.java +++ b/vtm-json/src/org/oscim/tiling/source/geojson/NextzenGeojsonTileSource.java @@ -17,7 +17,7 @@ package org.oscim.tiling.source.geojson; import org.oscim.core.MapElement; import org.oscim.core.Tag; import org.oscim.tiling.source.UrlTileSource; -import org.oscim.utils.math.MathUtils; +import org.oscim.utils.FastMath; import java.util.LinkedHashMap; import java.util.Map; @@ -115,7 +115,7 @@ public class NextzenGeojsonTileSource extends GeojsonTileSource { Object area = properties.get(Tag.KEY_AREA); String areaStr = (area instanceof String) ? (String) area : String.valueOf(area); float height = Float.parseFloat(volumeStr) / Float.parseFloat(areaStr); - String heightStr = String.valueOf(MathUtils.round2(height)); + String heightStr = String.valueOf(FastMath.round2(height)); mapElement.tags.add(new Tag(Tag.KEY_HEIGHT, heightStr, false)); } } diff --git a/vtm/src/org/oscim/core/MapPosition.java b/vtm/src/org/oscim/core/MapPosition.java index 57b5c4a2..b088587f 100644 --- a/vtm/src/org/oscim/core/MapPosition.java +++ b/vtm/src/org/oscim/core/MapPosition.java @@ -98,7 +98,7 @@ public class MapPosition { } public MapPosition setBearing(float bearing) { - this.bearing = clampBearing(bearing); + this.bearing = (float) FastMath.clampDegree(bearing); return this; } @@ -107,7 +107,7 @@ public class MapPosition { } public MapPosition setRoll(float roll) { - this.roll = clampRoll(roll); + this.roll = (float) FastMath.clampDegree(roll); return this; } @@ -181,26 +181,14 @@ public class MapPosition { this.y = y; this.scale = scale; - this.bearing = clampBearing(bearing); + this.bearing = (float) FastMath.clampDegree(bearing); this.tilt = tilt; this.zoomLevel = FastMath.log2((int) scale); } public void set(double x, double y, double scale, float bearing, float tilt, float roll) { set(x, y, scale, bearing, tilt); - this.roll = clampRoll(roll); - } - - private static float clampBearing(float bearing) { - while (bearing > 180) - bearing -= 360; - while (bearing < -180) - bearing += 360; - return bearing; - } - - private static float clampRoll(float roll) { - return clampBearing(roll); // Uses the same logic + this.roll = (float) FastMath.clampDegree(roll); } /** diff --git a/vtm/src/org/oscim/map/ViewController.java b/vtm/src/org/oscim/map/ViewController.java index 44f352e2..ead1ab35 100644 --- a/vtm/src/org/oscim/map/ViewController.java +++ b/vtm/src/org/oscim/map/ViewController.java @@ -199,10 +199,7 @@ public class ViewController extends Viewport { public void setRotation(double degree) { ThreadUtils.assertMainThread(); - while (degree > 180) - degree -= 360; - while (degree < -180) - degree += 360; + degree = FastMath.clampDegree(degree); mPos.bearing = (float) degree; @@ -216,10 +213,7 @@ public class ViewController extends Viewport { public void setRoll(double degree) { ThreadUtils.assertMainThread(); - while (degree > 180) - degree -= 360; - while (degree < -180) - degree += 360; + degree = FastMath.clampDegree(degree); mPos.roll = (float) degree; diff --git a/vtm/src/org/oscim/tiling/source/mvt/TileDecoder.java b/vtm/src/org/oscim/tiling/source/mvt/TileDecoder.java index 356fb9fc..acdaf198 100644 --- a/vtm/src/org/oscim/tiling/source/mvt/TileDecoder.java +++ b/vtm/src/org/oscim/tiling/source/mvt/TileDecoder.java @@ -24,7 +24,7 @@ import org.oscim.core.Tag; import org.oscim.core.Tile; import org.oscim.tiling.ITileDataSink; import org.oscim.tiling.source.PbfDecoder; -import org.oscim.utils.math.MathUtils; +import org.oscim.utils.FastMath; import org.oscim.utils.pool.Inlist; import org.oscim.utils.pool.Pool; import org.slf4j.Logger; @@ -258,7 +258,7 @@ public class TileDecoder extends PbfDecoder { && f.elem.tags.containsKey(Tag.KEY_AREA)) { float volume = Float.parseFloat(f.elem.tags.getValue(Tag.KEY_VOLUME)); float area = Float.parseFloat(f.elem.tags.getValue(Tag.KEY_AREA)); - String heightStr = String.valueOf(MathUtils.round2(volume / area)); + String heightStr = String.valueOf(FastMath.round2(volume / area)); f.elem.tags.add(new Tag(Tag.KEY_HEIGHT, heightStr, false)); } } diff --git a/vtm/src/org/oscim/tiling/source/oscimap4/TileDecoder.java b/vtm/src/org/oscim/tiling/source/oscimap4/TileDecoder.java index 4f3e7780..9c3e0565 100644 --- a/vtm/src/org/oscim/tiling/source/oscimap4/TileDecoder.java +++ b/vtm/src/org/oscim/tiling/source/oscimap4/TileDecoder.java @@ -25,7 +25,7 @@ import org.oscim.core.TagSet; import org.oscim.core.Tile; import org.oscim.tiling.ITileDataSink; import org.oscim.tiling.source.PbfDecoder; -import org.oscim.utils.math.MathUtils; +import org.oscim.utils.FastMath; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -229,7 +229,7 @@ public class TileDecoder extends PbfDecoder { else if (Tag.KEY_HEIGHT.equals(key) || Tag.KEY_MIN_HEIGHT.equals(key)) { // Reformat values to established meters in OSM - tag = new Tag(key, String.valueOf(MathUtils.round2(Float.valueOf(val) / 100)), false); + tag = new Tag(key, String.valueOf(FastMath.round2(Float.valueOf(val) / 100)), false); } else tag = new Tag(key, val, false, true); diff --git a/vtm/src/org/oscim/utils/FastMath.java b/vtm/src/org/oscim/utils/FastMath.java index 649699f0..7005db84 100644 --- a/vtm/src/org/oscim/utils/FastMath.java +++ b/vtm/src/org/oscim/utils/FastMath.java @@ -17,6 +17,73 @@ package org.oscim.utils; public class FastMath { + + public static float abs(float value) { + return value < 0 ? -value : value; + } + + public static float absMax(float value1, float value2) { + float a1 = value1 < 0 ? -value1 : value1; + float a2 = value2 < 0 ? -value2 : value2; + return a2 < a1 ? a1 : a2; + } + + /** + * test if any absolute value is greater than 'cmp' + */ + public static boolean absMaxCmp(float value1, float value2, float cmp) { + return value1 < -cmp || value1 > cmp || value2 < -cmp || value2 > cmp; + } + + /** + * test if any absolute value is greater than 'cmp' + */ + public static boolean absMaxCmp(int value1, int value2, int cmp) { + return value1 < -cmp || value1 > cmp || value2 < -cmp || value2 > cmp; + } + + public static int clamp(int value, int min, int max) { + return (value < min ? min : (value > max ? max : value)); + } + + public static float clamp(float value, float min, float max) { + return (value < min ? min : (value > max ? max : value)); + } + + public static double clamp(double value, double min, double max) { + return (value < min ? min : (value > max ? max : value)); + } + + /** + * Returns normalized degree in range of -180° to +180° + */ + public static double clampDegree(double degree) { + while (degree > 180) + degree -= 360; + while (degree < -180) + degree += 360; + return degree; + } + + public static float clampN(float value) { + return (value < 0f ? 0f : (value > 1f ? 1f : value)); + } + + /** + * Returns normalized radian in range of -PI to +PI + */ + public static double clampRadian(double radian) { + while (radian > Math.PI) + radian -= 2 * Math.PI; + while (radian < -Math.PI) + radian += 2 * Math.PI; + return radian; + } + + public static byte clampToByte(int value) { + return (byte) (value < 0 ? 0 : (value > 255 ? 255 : value)); + } + /** * Integer version of log2(x) *
@@ -58,59 +125,8 @@ public class FastMath { return (x > 0 ? (1 << x) : (1.0f / (1 << -x))); } - public static int clamp(int value, int min, int max) { - return (value < min ? min : (value > max ? max : value)); - } - - public static float clamp(float value, float min, float max) { - return (value < min ? min : (value > max ? max : value)); - } - - public static double clamp(double value, double min, double max) { - return (value < min ? min : (value > max ? max : value)); - } - - public static float clampN(float value) { - return (value < 0f ? 0f : (value > 1f ? 1f : value)); - } - - /** - * Returns normalized radian in range of -PI to +PI - */ - public static double clampRadian(double radian) { - while (radian > Math.PI) - radian -= 2 * Math.PI; - while (radian < -Math.PI) - radian += 2 * Math.PI; - return radian; - } - - public static byte clampToByte(int value) { - return (byte) (value < 0 ? 0 : (value > 255 ? 255 : value)); - } - - public static float abs(float value) { - return value < 0 ? -value : value; - } - - public static float absMax(float value1, float value2) { - float a1 = value1 < 0 ? -value1 : value1; - float a2 = value2 < 0 ? -value2 : value2; - return a2 < a1 ? a1 : a2; - } - - /** - * test if any absolute value is greater than 'cmp' - */ - public static boolean absMaxCmp(float value1, float value2, float cmp) { - return value1 < -cmp || value1 > cmp || value2 < -cmp || value2 > cmp; - } - - /** - * test if any absolute value is greater than 'cmp' - */ - public static boolean absMaxCmp(int value1, int value2, int cmp) { - return value1 < -cmp || value1 > cmp || value2 < -cmp || value2 > cmp; + public static float round2(float value) { + return Math.round(value * 100) / 100f; } public static boolean withinSquaredDist(int dx, int dy, int distance) { diff --git a/vtm/src/org/oscim/utils/math/MathUtils.java b/vtm/src/org/oscim/utils/math/MathUtils.java index a7f4576f..c50b450d 100644 --- a/vtm/src/org/oscim/utils/math/MathUtils.java +++ b/vtm/src/org/oscim/utils/math/MathUtils.java @@ -295,10 +295,6 @@ public class MathUtils { return (int) (x + BIG_ENOUGH_ROUND) - BIG_ENOUGH_INT; } - public static float round2(float value) { - return Math.round(value * 100) / 100f; - } - /** * Returns the closest integer to the specified float. This method will only * properly round floats that are positive.