FastMath reorganization

This commit is contained in:
Emux
2018-02-11 11:19:54 +02:00
parent bd9e7d7456
commit 67dde0af7d
9 changed files with 86 additions and 98 deletions

View File

@@ -32,6 +32,7 @@ import org.oscim.event.Event;
import org.oscim.layers.Layer; import org.oscim.layers.Layer;
import org.oscim.map.Map; import org.oscim.map.Map;
import org.oscim.renderer.LocationRenderer; import org.oscim.renderer.LocationRenderer;
import org.oscim.utils.FastMath;
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public class Compass extends Layer implements SensorEventListener, Map.UpdateListener, 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 rotation = (float) Math.toDegrees(mAzimuthRadians);
float change = rotation - mCurRotation; float change = rotation - mCurRotation;
if (change > 180) change = (float) FastMath.clampDegree(change);
change -= 360;
else if (change < -180)
change += 360;
// low-pass // low-pass
change *= 0.05; change *= 0.05;
rotation = mCurRotation + change; rotation = mCurRotation + change;
rotation = (float) FastMath.clampDegree(rotation);
if (rotation > 180)
rotation -= 360;
else if (rotation < -180)
rotation += 360;
// float tilt = (float) Math.toDegrees(mRotationV[1]); // float tilt = (float) Math.toDegrees(mRotationV[1]);
// float tilt = (float) Math.toDegrees(mPitchAxisRadians); // float tilt = (float) Math.toDegrees(mPitchAxisRadians);

View File

@@ -18,7 +18,7 @@ package org.oscim.tiling.source.geojson;
import org.oscim.core.MapElement; import org.oscim.core.MapElement;
import org.oscim.core.Tag; import org.oscim.core.Tag;
import org.oscim.tiling.source.UrlTileSource; import org.oscim.tiling.source.UrlTileSource;
import org.oscim.utils.math.MathUtils; import org.oscim.utils.FastMath;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
@@ -116,7 +116,7 @@ public class MapzenGeojsonTileSource extends GeojsonTileSource {
Object area = properties.get(Tag.KEY_AREA); Object area = properties.get(Tag.KEY_AREA);
String areaStr = (area instanceof String) ? (String) area : String.valueOf(area); String areaStr = (area instanceof String) ? (String) area : String.valueOf(area);
float height = Float.parseFloat(volumeStr) / Float.parseFloat(areaStr); 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)); mapElement.tags.add(new Tag(Tag.KEY_HEIGHT, heightStr, false));
} }
} }

View File

@@ -17,7 +17,7 @@ package org.oscim.tiling.source.geojson;
import org.oscim.core.MapElement; import org.oscim.core.MapElement;
import org.oscim.core.Tag; import org.oscim.core.Tag;
import org.oscim.tiling.source.UrlTileSource; import org.oscim.tiling.source.UrlTileSource;
import org.oscim.utils.math.MathUtils; import org.oscim.utils.FastMath;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
@@ -115,7 +115,7 @@ public class NextzenGeojsonTileSource extends GeojsonTileSource {
Object area = properties.get(Tag.KEY_AREA); Object area = properties.get(Tag.KEY_AREA);
String areaStr = (area instanceof String) ? (String) area : String.valueOf(area); String areaStr = (area instanceof String) ? (String) area : String.valueOf(area);
float height = Float.parseFloat(volumeStr) / Float.parseFloat(areaStr); 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)); mapElement.tags.add(new Tag(Tag.KEY_HEIGHT, heightStr, false));
} }
} }

View File

@@ -98,7 +98,7 @@ public class MapPosition {
} }
public MapPosition setBearing(float bearing) { public MapPosition setBearing(float bearing) {
this.bearing = clampBearing(bearing); this.bearing = (float) FastMath.clampDegree(bearing);
return this; return this;
} }
@@ -107,7 +107,7 @@ public class MapPosition {
} }
public MapPosition setRoll(float roll) { public MapPosition setRoll(float roll) {
this.roll = clampRoll(roll); this.roll = (float) FastMath.clampDegree(roll);
return this; return this;
} }
@@ -181,26 +181,14 @@ public class MapPosition {
this.y = y; this.y = y;
this.scale = scale; this.scale = scale;
this.bearing = clampBearing(bearing); this.bearing = (float) FastMath.clampDegree(bearing);
this.tilt = tilt; this.tilt = tilt;
this.zoomLevel = FastMath.log2((int) scale); this.zoomLevel = FastMath.log2((int) scale);
} }
public void set(double x, double y, double scale, float bearing, float tilt, float roll) { public void set(double x, double y, double scale, float bearing, float tilt, float roll) {
set(x, y, scale, bearing, tilt); set(x, y, scale, bearing, tilt);
this.roll = clampRoll(roll); this.roll = (float) FastMath.clampDegree(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
} }
/** /**

View File

@@ -199,10 +199,7 @@ public class ViewController extends Viewport {
public void setRotation(double degree) { public void setRotation(double degree) {
ThreadUtils.assertMainThread(); ThreadUtils.assertMainThread();
while (degree > 180) degree = FastMath.clampDegree(degree);
degree -= 360;
while (degree < -180)
degree += 360;
mPos.bearing = (float) degree; mPos.bearing = (float) degree;
@@ -216,10 +213,7 @@ public class ViewController extends Viewport {
public void setRoll(double degree) { public void setRoll(double degree) {
ThreadUtils.assertMainThread(); ThreadUtils.assertMainThread();
while (degree > 180) degree = FastMath.clampDegree(degree);
degree -= 360;
while (degree < -180)
degree += 360;
mPos.roll = (float) degree; mPos.roll = (float) degree;

View File

@@ -24,7 +24,7 @@ import org.oscim.core.Tag;
import org.oscim.core.Tile; import org.oscim.core.Tile;
import org.oscim.tiling.ITileDataSink; import org.oscim.tiling.ITileDataSink;
import org.oscim.tiling.source.PbfDecoder; 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.Inlist;
import org.oscim.utils.pool.Pool; import org.oscim.utils.pool.Pool;
import org.slf4j.Logger; import org.slf4j.Logger;
@@ -258,7 +258,7 @@ public class TileDecoder extends PbfDecoder {
&& f.elem.tags.containsKey(Tag.KEY_AREA)) { && f.elem.tags.containsKey(Tag.KEY_AREA)) {
float volume = Float.parseFloat(f.elem.tags.getValue(Tag.KEY_VOLUME)); float volume = Float.parseFloat(f.elem.tags.getValue(Tag.KEY_VOLUME));
float area = Float.parseFloat(f.elem.tags.getValue(Tag.KEY_AREA)); 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)); f.elem.tags.add(new Tag(Tag.KEY_HEIGHT, heightStr, false));
} }
} }

View File

@@ -25,7 +25,7 @@ import org.oscim.core.TagSet;
import org.oscim.core.Tile; import org.oscim.core.Tile;
import org.oscim.tiling.ITileDataSink; import org.oscim.tiling.ITileDataSink;
import org.oscim.tiling.source.PbfDecoder; import org.oscim.tiling.source.PbfDecoder;
import org.oscim.utils.math.MathUtils; import org.oscim.utils.FastMath;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@@ -229,7 +229,7 @@ public class TileDecoder extends PbfDecoder {
else if (Tag.KEY_HEIGHT.equals(key) else if (Tag.KEY_HEIGHT.equals(key)
|| Tag.KEY_MIN_HEIGHT.equals(key)) { || Tag.KEY_MIN_HEIGHT.equals(key)) {
// Reformat values to established meters in OSM // 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 } else
tag = new Tag(key, val, false, true); tag = new Tag(key, val, false, true);

View File

@@ -17,6 +17,73 @@
package org.oscim.utils; package org.oscim.utils;
public class FastMath { 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) * Integer version of log2(x)
* <p/> * <p/>
@@ -58,59 +125,8 @@ public class FastMath {
return (x > 0 ? (1 << x) : (1.0f / (1 << -x))); return (x > 0 ? (1 << x) : (1.0f / (1 << -x)));
} }
public static int clamp(int value, int min, int max) { public static float round2(float value) {
return (value < min ? min : (value > max ? max : value)); return Math.round(value * 100) / 100f;
}
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 boolean withinSquaredDist(int dx, int dy, int distance) { public static boolean withinSquaredDist(int dx, int dy, int distance) {

View File

@@ -295,10 +295,6 @@ public class MathUtils {
return (int) (x + BIG_ENOUGH_ROUND) - BIG_ENOUGH_INT; 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 * Returns the closest integer to the specified float. This method will only
* properly round floats that are positive. * properly round floats that are positive.