improve GeometryBuffer API

This commit is contained in:
Hannes Janetzek 2013-04-24 15:15:33 +02:00
parent ae993eccce
commit 923019a34a
9 changed files with 80 additions and 64 deletions

View File

@ -1,6 +1,20 @@
package org.oscim.core; package org.oscim.core;
public class GeometryBuffer { public class GeometryBuffer {
public enum GeometryType {
NONE(0),
POINT(1),
LINE(2),
POLY(3);
private GeometryType(int type) {
nativeInt = type;
}
public final int nativeInt;
}
// TODO // TODO
// -- check indexPos < Short.Max // -- check indexPos < Short.Max
@ -11,7 +25,9 @@ public class GeometryBuffer {
public int indexPos; public int indexPos;
public int pointPos; public int pointPos;
public int mode; //public int type;
// GEOM_*
public GeometryType type;
public GeometryBuffer(float[] points, short[] index) { public GeometryBuffer(float[] points, short[] index) {
if (points == null) if (points == null)
@ -21,14 +37,17 @@ public class GeometryBuffer {
this.points = points; this.points = points;
this.index = index; this.index = index;
mode = 0; this.type = GeometryType.NONE;
indexPos = 0; this.indexPos = 0;
pointPos = 0; this.pointPos = 0;
} }
public GeometryBuffer(int numPoints, int numIndices) { public GeometryBuffer(int numPoints, int numIndices) {
this.points = new float[numPoints * 2]; this.points = new float[numPoints * 2];
this.index = new short[numIndices]; this.index = new short[numIndices];
this.type = GeometryType.NONE;
this.indexPos = 0;
this.pointPos = 0;
} }
// ---- API ---- // ---- API ----
@ -36,7 +55,7 @@ public class GeometryBuffer {
index[0] = -1; index[0] = -1;
indexPos = 0; indexPos = 0;
pointPos = 0; pointPos = 0;
mode = 0; type = GeometryType.NONE;
} }
public void addPoint(float x, float y) { public void addPoint(float x, float y) {
@ -48,6 +67,15 @@ public class GeometryBuffer {
index[indexPos] += 2; index[indexPos] += 2;
} }
public boolean isPoly(){
return type == GeometryType.POLY;
}
public boolean isLine(){
return type == GeometryType.LINE;
}
public boolean isPoint(){
return type == GeometryType.POINT;
}
public void setPoint(int pos, float x, float y) { public void setPoint(int pos, float x, float y) {
points[(pos << 1) + 0] = x; points[(pos << 1) + 0] = x;
@ -56,12 +84,12 @@ public class GeometryBuffer {
public void startPoints() { public void startPoints() {
if (CHECK_STATE) if (CHECK_STATE)
setOrCheckMode(1); setOrCheckMode(GeometryType.POINT);
} }
public void startLine() { public void startLine() {
if (CHECK_STATE) if (CHECK_STATE)
setOrCheckMode(2); setOrCheckMode(GeometryType.LINE);
// start next // start next
if ((index[0] >= 0) && (++indexPos >= index.length)) if ((index[0] >= 0) && (++indexPos >= index.length))
@ -75,7 +103,7 @@ public class GeometryBuffer {
public void startPolygon() { public void startPolygon() {
if (CHECK_STATE) if (CHECK_STATE)
setOrCheckMode(3); setOrCheckMode(GeometryType.POLY);
if ((indexPos + 4) >= index.length) if ((indexPos + 4) >= index.length)
ensureIndexSize(indexPos + 4, true); ensureIndexSize(indexPos + 4, true);
@ -100,7 +128,7 @@ public class GeometryBuffer {
public void startHole() { public void startHole() {
if (CHECK_STATE) if (CHECK_STATE)
checkMode(3); checkMode(GeometryType.POLY);
if ((indexPos + 3) >= index.length) if ((indexPos + 3) >= index.length)
ensureIndexSize(indexPos, true); ensureIndexSize(indexPos, true);
@ -115,8 +143,6 @@ public class GeometryBuffer {
index[indexPos + 1] = -1; index[indexPos + 1] = -1;
} }
// ---- internals ---- // ---- internals ----
public float[] ensurePointSize(int size, boolean copy) { public float[] ensurePointSize(int size, boolean copy) {
if (size * 2 < points.length) if (size * 2 < points.length)
@ -143,19 +169,19 @@ public class GeometryBuffer {
return index; return index;
} }
private void setOrCheckMode(int m) { private void setOrCheckMode(GeometryType m) {
if (mode == m) if (type == m)
return; return;
if (mode != 0) if (type != GeometryType.NONE)
throw new IllegalArgumentException("GeometryBuffer not cleared " + m + "<>" + mode); throw new IllegalArgumentException("GeometryBuffer not cleared " + m + "<>" + type);
mode = m; type = m;
} }
private void checkMode(int m) { private void checkMode(GeometryType m) {
if (mode != m) if (type != m)
throw new IllegalArgumentException("GeometryBuffer not cleared " + m + "<>" + mode); throw new IllegalArgumentException("GeometryBuffer not cleared " + m + "<>" + type);
} }
} }

View File

@ -23,17 +23,13 @@ package org.oscim.core;
* dont keep a reference to it when passed as parameter. * dont keep a reference to it when passed as parameter.
*/ */
public class MapElement extends GeometryBuffer { public class MapElement extends GeometryBuffer {
public static final int GEOM_NONE = 0;
public static final int GEOM_POINT = 1;
public static final int GEOM_LINE = 2;
public static final int GEOM_POLY = 3;
// osm layer of the way. // osm layer of the way.
public int layer; public int layer;
// osm tags of the way. // osm tags of the way.
public Tag[] tags; public Tag[] tags;
// GEOM_*
public int geometryType;
// ---- random stuff, to be removed ---- // ---- random stuff, to be removed ----
// building tags // building tags
@ -50,9 +46,8 @@ public class MapElement extends GeometryBuffer {
super(points, indices); super(points, indices);
} }
public void set(Tag[] tags, int layer, int type) { public void set(Tag[] tags, int layer) {
this.layer = layer; this.layer = layer;
this.tags = tags; this.tags = tags;
this.geometryType = type;
} }
} }

View File

@ -18,6 +18,7 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.RandomAccessFile; import java.io.RandomAccessFile;
import org.oscim.core.GeometryBuffer.GeometryType;
import org.oscim.core.MapElement; import org.oscim.core.MapElement;
import org.oscim.core.Tag; import org.oscim.core.Tag;
import org.oscim.core.Tile; import org.oscim.core.Tile;
@ -670,8 +671,8 @@ public class MapDatabase implements IMapDatabase {
mElem.startPoints(); mElem.startPoints();
mElem.addPoint(longitude, latitude); mElem.addPoint(longitude, latitude);
mElem.type = GeometryType.POINT;
mElem.set(curTags, layer, MapElement.GEOM_POINT); mElem.set(curTags, layer);
mapDatabaseCallback.renderElement(mElem); mapDatabaseCallback.renderElement(mElem);
// mGeom.points[0] = longitude; // mGeom.points[0] = longitude;
@ -988,8 +989,8 @@ public class MapDatabase implements IMapDatabase {
projectToTile(mElem.points, mElem.index); projectToTile(mElem.points, mElem.index);
mElem.layer = layer; mElem.layer = layer;
mElem.geometryType = closed ? MapElement.GEOM_POLY : MapElement.GEOM_LINE; mElem.type = closed ? GeometryType.POLY : GeometryType.LINE;
mElem.tags = curTags; mElem.set(tags, layer);
mapDatabaseCallback.renderElement(mElem); mapDatabaseCallback.renderElement(mElem);
} }

View File

@ -23,6 +23,7 @@ import java.util.Arrays;
import org.oscim.core.BoundingBox; import org.oscim.core.BoundingBox;
import org.oscim.core.GeoPoint; import org.oscim.core.GeoPoint;
import org.oscim.core.GeometryBuffer.GeometryType;
import org.oscim.core.MapElement; import org.oscim.core.MapElement;
import org.oscim.core.Tag; import org.oscim.core.Tag;
import org.oscim.core.Tile; import org.oscim.core.Tile;
@ -418,16 +419,15 @@ public class MapDatabase implements IMapDatabase {
} }
mElem.tags = tags; mElem.tags = tags;
switch (type) { switch (type) {
case TAG_TILE_LINE: case TAG_TILE_LINE:
mElem.geometryType= MapElement.GEOM_LINE; mElem.type= GeometryType.LINE;
break; break;
case TAG_TILE_POLY: case TAG_TILE_POLY:
mElem.geometryType= MapElement.GEOM_POLY; mElem.type= GeometryType.POLY;
break; break;
case TAG_TILE_POINT: case TAG_TILE_POINT:
mElem.geometryType= MapElement.GEOM_POINT; mElem.type= GeometryType.POINT;
break; break;
} }

View File

@ -36,6 +36,7 @@ import java.util.Map;
import org.oscim.core.BoundingBox; import org.oscim.core.BoundingBox;
import org.oscim.core.GeoPoint; import org.oscim.core.GeoPoint;
import org.oscim.core.GeometryBuffer.GeometryType;
import org.oscim.core.MapElement; import org.oscim.core.MapElement;
import org.oscim.core.Tag; import org.oscim.core.Tag;
import org.oscim.core.Tile; import org.oscim.core.Tile;
@ -448,8 +449,8 @@ public class MapDatabase implements IMapDatabase {
// FIXME, remove all tiles from cache then remove this below // FIXME, remove all tiles from cache then remove this below
//if (layer == 0) //if (layer == 0)
// layer = 5; // layer = 5;
mElement.type = polygon ? GeometryType.POLY : GeometryType.LINE;
mElement.set(tags, layer, polygon ? MapElement.GEOM_POLY : MapElement.GEOM_LINE); mElement.set(tags, layer);
mMapGenerator.renderElement(mElement); mMapGenerator.renderElement(mElement);
return true; return true;
} }
@ -528,7 +529,8 @@ public class MapDatabase implements IMapDatabase {
mElement.index[0] = (short)numNodes; mElement.index[0] = (short)numNodes;
mElement.set(tags, layer, MapElement.GEOM_POINT); mElement.type = GeometryType.POINT;
mElement.set(tags, layer);
mMapGenerator.renderElement(mElement); mMapGenerator.renderElement(mElement);
return cnt; return cnt;

View File

@ -14,10 +14,6 @@
*/ */
package org.oscim.database.test; package org.oscim.database.test;
import static org.oscim.core.MapElement.GEOM_LINE;
import static org.oscim.core.MapElement.GEOM_POINT;
import static org.oscim.core.MapElement.GEOM_POLY;
import org.oscim.core.BoundingBox; import org.oscim.core.BoundingBox;
import org.oscim.core.MapElement; import org.oscim.core.MapElement;
import org.oscim.core.Tag; import org.oscim.core.Tag;
@ -99,7 +95,7 @@ public class MapDatabase implements IMapDatabase {
e.addPoint(x2, y2); e.addPoint(x2, y2);
e.addPoint(x1, y2); e.addPoint(x1, y2);
e.set(mTags, 0, GEOM_POLY); e.set(mTags, 0);
mapDatabaseCallback.renderElement(e); mapDatabaseCallback.renderElement(e);
if (renderWays) { if (renderWays) {
@ -120,7 +116,7 @@ public class MapDatabase implements IMapDatabase {
e.addPoint(size / 2, size / 2); e.addPoint(size / 2, size / 2);
e.addPoint(size / 2, size / 2 + size); e.addPoint(size / 2, size / 2 + size);
e.set(mTagsWay, 0, GEOM_LINE); e.set(mTagsWay, 0);
mapDatabaseCallback.renderElement(e); mapDatabaseCallback.renderElement(e);
e.clear(); e.clear();
@ -137,7 +133,7 @@ public class MapDatabase implements IMapDatabase {
e.addPoint(10, 0); e.addPoint(10, 0);
e.addPoint(10, size); e.addPoint(10, size);
e.set(mTagsWay, 1, GEOM_LINE); e.set(mTagsWay, 1);
mapDatabaseCallback.renderElement(e); mapDatabaseCallback.renderElement(e);
} }
@ -152,7 +148,7 @@ public class MapDatabase implements IMapDatabase {
r + (float) Math.sin(d) * (r - 40)); r + (float) Math.sin(d) * (r - 40));
} }
e.set(mTagsBoundary, 1, GEOM_LINE); e.set(mTagsBoundary, 1);
mapDatabaseCallback.renderElement(e); mapDatabaseCallback.renderElement(e);
} }
@ -162,7 +158,7 @@ public class MapDatabase implements IMapDatabase {
e.addPoint(size / 2, size / 2); e.addPoint(size / 2, size / 2);
mTagsPlace[1] = new Tag("name", tile.toString()); mTagsPlace[1] = new Tag("name", tile.toString());
e.set(mTagsPlace, 0, GEOM_POINT); e.set(mTagsPlace, 0);
mapDatabaseCallback.renderElement(e); mapDatabaseCallback.renderElement(e);
} }
return QueryResult.SUCCESS; return QueryResult.SUCCESS;

View File

@ -14,13 +14,11 @@
*/ */
package org.oscim.layers.tile; package org.oscim.layers.tile;
import static org.oscim.core.MapElement.GEOM_LINE;
import static org.oscim.core.MapElement.GEOM_POINT;
import static org.oscim.core.MapElement.GEOM_POLY;
import static org.oscim.layers.tile.MapTile.STATE_NONE; import static org.oscim.layers.tile.MapTile.STATE_NONE;
import java.util.Arrays; import java.util.Arrays;
import org.oscim.core.GeometryBuffer.GeometryType;
import org.oscim.core.MapElement; import org.oscim.core.MapElement;
import org.oscim.core.MercatorProjection; import org.oscim.core.MercatorProjection;
import org.oscim.core.Tag; import org.oscim.core.Tag;
@ -131,12 +129,12 @@ public class MapTileLoader extends TileLoader implements IRenderCallback, IMapDa
m.addPoint(s, 0); m.addPoint(s, 0);
m.addPoint(0, 0); m.addPoint(0, 0);
m.tags = new Tag[] { new Tag("debug", "box") }; m.tags = new Tag[] { new Tag("debug", "box") };
m.geometryType = GEOM_LINE; m.type = GeometryType.LINE;
m = mDebugPoint = new MapElement(); m = mDebugPoint = new MapElement();
m.startPoints(); m.startPoints();
m.addPoint(s >> 1, 10); m.addPoint(s >> 1, 10);
m.geometryType = GEOM_POINT; m.type = GeometryType.POINT;
} }
/* (non-Javadoc) /* (non-Javadoc)
@ -287,7 +285,7 @@ public class MapTileLoader extends TileLoader implements IRenderCallback, IMapDa
mElement = element; mElement = element;
if (element.geometryType == GEOM_POINT) { if (element.type == GeometryType.POINT) {
// remove tags that should not be cached in Rendertheme // remove tags that should not be cached in Rendertheme
filterTags(element.tags); filterTags(element.tags);
@ -304,7 +302,7 @@ public class MapTileLoader extends TileLoader implements IRenderCallback, IMapDa
if (!filterTags(element.tags)) if (!filterTags(element.tags))
return; return;
boolean closed = element.geometryType == GEOM_POLY; boolean closed = element.type == GeometryType.POLY;
mDrawingLayer = getValidLayer(element.layer) * renderLevels; mDrawingLayer = getValidLayer(element.layer) * renderLevels;
@ -398,7 +396,7 @@ public class MapTileLoader extends TileLoader implements IRenderCallback, IMapDa
} }
lineLayer.addLine(mElement.points, mElement.index, lineLayer.addLine(mElement.points, mElement.index,
mElement.geometryType == GEOM_POLY); mElement.type == GeometryType.POLY);
// keep reference for outline layer // keep reference for outline layer
mCurLineLayer = lineLayer; mCurLineLayer = lineLayer;

View File

@ -74,9 +74,9 @@ public final class LineLayer extends Layer {
} }
public void addLine(GeometryBuffer geom) { public void addLine(GeometryBuffer geom) {
if (geom.mode == 3) if (geom.isPoly())
addLine(geom.points, geom.index, -1, true); addLine(geom.points, geom.index, -1, true);
else if (geom.mode == 2) else if (geom.isLine())
addLine(geom.points, geom.index, -1, false); addLine(geom.points, geom.index, -1, false);
} }

View File

@ -14,12 +14,10 @@
*/ */
package org.oscim.theme; package org.oscim.theme;
import static org.oscim.core.MapElement.GEOM_LINE;
import static org.oscim.core.MapElement.GEOM_POLY;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.oscim.core.GeometryBuffer.GeometryType;
import org.oscim.core.MapElement; import org.oscim.core.MapElement;
import org.oscim.theme.renderinstruction.RenderInstruction; import org.oscim.theme.renderinstruction.RenderInstruction;
import org.oscim.theme.rule.Closed; import org.oscim.theme.rule.Closed;
@ -175,9 +173,9 @@ public class RenderTheme implements IRenderTheme {
// the item matching tags and zoomlevel // the item matching tags and zoomlevel
RenderInstructionItem ri = null; RenderInstructionItem ri = null;
int type = element.geometryType; int type = element.type.nativeInt;
if (type < 1 || type > 3) { if (type < 1 || type > 3) {
Log.d(TAG, "invalid geometry type for RenderTheme " + type); Log.d(TAG, "invalid geometry type for RenderTheme " + element.type.name());
return null; return null;
} }
@ -215,11 +213,11 @@ public class RenderTheme implements IRenderTheme {
List<RenderInstruction> matches = cache.instructionList; List<RenderInstruction> matches = cache.instructionList;
matches.clear(); matches.clear();
if (type == GEOM_LINE) { if (element.type == GeometryType.LINE) {
for (int i = 0, n = mRules.length; i < n; i++) for (int i = 0, n = mRules.length; i < n; i++)
mRules[i].matchWay(element.tags, mRules[i].matchWay(element.tags,
(byte) zoomLevel, Closed.NO, matches); (byte) zoomLevel, Closed.NO, matches);
} else if (type == GEOM_POLY) { } else if (element.type == GeometryType.POLY) {
for (int i = 0, n = mRules.length; i < n; i++) for (int i = 0, n = mRules.length; i < n; i++)
mRules[i].matchWay(element.tags, mRules[i].matchWay(element.tags,
(byte) zoomLevel, Closed.YES, matches); (byte) zoomLevel, Closed.YES, matches);