use faster Math.abs utility, cleanups, comments

This commit is contained in:
Hannes Janetzek 2013-02-10 12:17:39 +01:00
parent 429b8a98d7
commit 32ba8bbae0
2 changed files with 44 additions and 25 deletions

View File

@ -17,6 +17,7 @@ package org.oscim.renderer.layer;
import org.oscim.core.Tile; import org.oscim.core.Tile;
import org.oscim.renderer.GLRenderer; import org.oscim.renderer.GLRenderer;
import org.oscim.theme.renderinstruction.Line; import org.oscim.theme.renderinstruction.Line;
import org.oscim.utils.FastMath;
import org.oscim.view.MapView; import org.oscim.view.MapView;
import android.graphics.Paint.Cap; import android.graphics.Paint.Cap;
@ -70,8 +71,8 @@ public final class LineLayer extends Layer {
float x, y, nextX, nextY; float x, y, nextX, nextY;
float a, ux, uy, vx, vy, wx, wy; float a, ux, uy, vx, vy, wx, wy;
int tmax = Tile.TILE_SIZE + 10; int tmax = Tile.TILE_SIZE + 4;
int tmin = -10; int tmin = -4;
boolean rounded = false; boolean rounded = false;
boolean squared = false; boolean squared = false;
@ -108,7 +109,6 @@ public final class LineLayer extends Layer {
roundCap = rounded; roundCap = rounded;
for (int i = 0, pos = 0, n = index.length; i < n; i++) { for (int i = 0, pos = 0, n = index.length; i < n; i++) {
int length = index[i]; int length = index[i];
// check end-marker in indices // check end-marker in indices
@ -139,11 +139,12 @@ public final class LineLayer extends Layer {
vx = nextX - x; vx = nextX - x;
vy = nextY - y; vy = nextY - y;
// Unit vector to next node
a = (float) Math.sqrt(vx * vx + vy * vy); a = (float) Math.sqrt(vx * vx + vy * vy);
vx /= a;
vy /= a;
vx = (vx / a); // perpendicular on the first segment
vy = (vy / a);
ux = -vy; ux = -vy;
uy = vx; uy = vx;
@ -153,12 +154,17 @@ public final class LineLayer extends Layer {
opos = 0; opos = 0;
} }
short ox, oy, dx, dy;
int ddx, ddy; int ddx, ddy;
ox = (short) (x * COORD_SCALE); // vertex point coordinate
oy = (short) (y * COORD_SCALE); short ox = (short) (x * COORD_SCALE);
short oy = (short) (y * COORD_SCALE);
// vertex extrusion vector, last two bit
// encode texture coord.
short dx, dy;
// when the endpoint is outside the tile region omit round caps.
boolean outside = (x < tmin || x > tmax || y < tmin || y > tmax); boolean outside = (x < tmin || x > tmax || y < tmin || y > tmax);
if (opos == VertexPoolItem.SIZE) { if (opos == VertexPoolItem.SIZE) {
@ -171,7 +177,6 @@ public final class LineLayer extends Layer {
// add first vertex twice // add first vertex twice
ddx = (int) ((ux - vx) * DIR_SCALE); ddx = (int) ((ux - vx) * DIR_SCALE);
ddy = (int) ((uy - vy) * DIR_SCALE); ddy = (int) ((uy - vy) * DIR_SCALE);
// last two bit encode texture coord (-1)
dx = (short) (0 | ddx & DIR_MASK); dx = (short) (0 | ddx & DIR_MASK);
dy = (short) (2 | ddy & DIR_MASK); dy = (short) (2 | ddy & DIR_MASK);
@ -310,8 +315,8 @@ public final class LineLayer extends Layer {
wx = nextX - x; wx = nextX - x;
wy = nextY - y; wy = nextY - y;
a = (float) Math.sqrt(wx * wx + wy * wy); a = (float) Math.sqrt(wx * wx + wy * wy);
wx = (wx / a); wx /= a;
wy = (wy / a); wy /= a;
// Sum of these two vectors points // Sum of these two vectors points
ux = vx + wx; ux = vx + wx;
@ -320,24 +325,23 @@ public final class LineLayer extends Layer {
// cross-product // cross-product
a = wx * uy - wy * ux; a = wx * uy - wy * ux;
// boolean split = false; if (FastMath.abs(a) < 0.01f) {
if (a < 0.01f && a > -0.01f) {
// Almost straight // Almost straight
ux = -wy; ux = -wy;
uy = wx; uy = wx;
} else { } else {
ux = (ux / a); ux /= a;
uy = (uy / a); uy /= a;
// avoid miter going to infinity. // avoid miter going to infinity.
// TODO add option for round joints // TODO add option for round joints
if (ux > 4.0f || ux < -4.0f || uy > 4.0f || uy < -4.0f) { if (FastMath.absMaxCmp(ux, uy, 4f)) {
ux = vx - wx; ux = vx - wx;
uy = vy - wy; uy = vy - wy;
a = -wy * ux + wx * uy; a = -wy * ux + wx * uy;
ux = (ux / a); ux /= a;
uy = (uy / a); uy /= a;
flip = !flip; flip = !flip;
} }
} }
@ -349,8 +353,8 @@ public final class LineLayer extends Layer {
ddy = (int) (uy * DIR_SCALE); ddy = (int) (uy * DIR_SCALE);
if (flip) { if (flip) {
ddx *= -1; ddx = -ddx;
ddy *= -1; ddy = -ddy;
} }
if (opos == VertexPoolItem.SIZE) { if (opos == VertexPoolItem.SIZE) {
si = si.next = VertexPool.get(); si = si.next = VertexPool.get();
@ -377,8 +381,9 @@ public final class LineLayer extends Layer {
x = nextX; x = nextX;
y = nextY; y = nextY;
vx = -1 * wx; // flip unit vector to point back
vy = -1 * wy; vx = -wx;
vy = -wy;
} }
ux = vy; ux = vy;
@ -401,8 +406,8 @@ public final class LineLayer extends Layer {
ddy = (int) (uy * DIR_SCALE); ddy = (int) (uy * DIR_SCALE);
if (flip) { if (flip) {
ddx *= -1; ddx = -ddx;
ddy *= -1; ddy = -ddy;
} }
v[opos++] = ox; v[opos++] = ox;

View File

@ -54,4 +54,18 @@ public class FastMath {
return (pow > 0 ? (1 << pow) : (1.0f / (1 << -pow))); return (pow > 0 ? (1 << pow) : (1.0f / (1 << -pow)));
} }
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;
}
public static boolean absMaxCmp(float value1, float value2, float cmp){
return value1 < -cmp || value1 > cmp || value2 < -cmp || value2 > cmp;
}
} }