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;
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
// -- check indexPos < Short.Max
@@ -11,7 +25,9 @@ public class GeometryBuffer {
public int indexPos;
public int pointPos;
public int mode;
//public int type;
// GEOM_*
public GeometryType type;
public GeometryBuffer(float[] points, short[] index) {
if (points == null)
@@ -21,14 +37,17 @@ public class GeometryBuffer {
this.points = points;
this.index = index;
mode = 0;
indexPos = 0;
pointPos = 0;
this.type = GeometryType.NONE;
this.indexPos = 0;
this.pointPos = 0;
}
public GeometryBuffer(int numPoints, int numIndices) {
this.points = new float[numPoints * 2];
this.index = new short[numIndices];
this.type = GeometryType.NONE;
this.indexPos = 0;
this.pointPos = 0;
}
// ---- API ----
@@ -36,7 +55,7 @@ public class GeometryBuffer {
index[0] = -1;
indexPos = 0;
pointPos = 0;
mode = 0;
type = GeometryType.NONE;
}
public void addPoint(float x, float y) {
@@ -48,6 +67,15 @@ public class GeometryBuffer {
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) {
points[(pos << 1) + 0] = x;
@@ -56,12 +84,12 @@ public class GeometryBuffer {
public void startPoints() {
if (CHECK_STATE)
setOrCheckMode(1);
setOrCheckMode(GeometryType.POINT);
}
public void startLine() {
if (CHECK_STATE)
setOrCheckMode(2);
setOrCheckMode(GeometryType.LINE);
// start next
if ((index[0] >= 0) && (++indexPos >= index.length))
@@ -75,7 +103,7 @@ public class GeometryBuffer {
public void startPolygon() {
if (CHECK_STATE)
setOrCheckMode(3);
setOrCheckMode(GeometryType.POLY);
if ((indexPos + 4) >= index.length)
ensureIndexSize(indexPos + 4, true);
@@ -100,7 +128,7 @@ public class GeometryBuffer {
public void startHole() {
if (CHECK_STATE)
checkMode(3);
checkMode(GeometryType.POLY);
if ((indexPos + 3) >= index.length)
ensureIndexSize(indexPos, true);
@@ -115,8 +143,6 @@ public class GeometryBuffer {
index[indexPos + 1] = -1;
}
// ---- internals ----
public float[] ensurePointSize(int size, boolean copy) {
if (size * 2 < points.length)
@@ -143,19 +169,19 @@ public class GeometryBuffer {
return index;
}
private void setOrCheckMode(int m) {
if (mode == m)
private void setOrCheckMode(GeometryType m) {
if (type == m)
return;
if (mode != 0)
throw new IllegalArgumentException("GeometryBuffer not cleared " + m + "<>" + mode);
if (type != GeometryType.NONE)
throw new IllegalArgumentException("GeometryBuffer not cleared " + m + "<>" + type);
mode = m;
type = m;
}
private void checkMode(int m) {
if (mode != m)
throw new IllegalArgumentException("GeometryBuffer not cleared " + m + "<>" + mode);
private void checkMode(GeometryType m) {
if (type != m)
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.
*/
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.
public int layer;
// osm tags of the way.
public Tag[] tags;
// GEOM_*
public int geometryType;
// ---- random stuff, to be removed ----
// building tags
@@ -50,9 +46,8 @@ public class MapElement extends GeometryBuffer {
super(points, indices);
}
public void set(Tag[] tags, int layer, int type) {
public void set(Tag[] tags, int layer) {
this.layer = layer;
this.tags = tags;
this.geometryType = type;
}
}