start of an API for GeometryBuffer
- fix bug in ensure size
This commit is contained in:
parent
2c4eea3e59
commit
c305cbc828
@ -1,42 +1,163 @@
|
|||||||
package org.oscim.core;
|
package org.oscim.core;
|
||||||
|
|
||||||
public class GeometryBuffer {
|
public class GeometryBuffer {
|
||||||
|
|
||||||
|
// TODO
|
||||||
|
// -- check indexPos < Short.Max
|
||||||
|
final static boolean CHECK_STATE = true;
|
||||||
|
|
||||||
public float[] points;
|
public float[] points;
|
||||||
public short[] index;
|
public short[] index;
|
||||||
public int indexPos;
|
public int indexPos;
|
||||||
public int pointPos;
|
public int pointPos;
|
||||||
|
|
||||||
public GeometryBuffer(float[] points, short[] index){
|
public int mode;
|
||||||
|
|
||||||
|
public GeometryBuffer(float[] points, short[] index) {
|
||||||
|
if (points == null)
|
||||||
|
throw new IllegalArgumentException("GeometryBuffer points is null");
|
||||||
|
if (index == null)
|
||||||
|
throw new IllegalArgumentException("GeometryBuffer index is null");
|
||||||
|
|
||||||
this.points = points;
|
this.points = points;
|
||||||
this.index = index;
|
this.index = index;
|
||||||
|
mode = 0;
|
||||||
|
indexPos = 0;
|
||||||
|
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];
|
||||||
}
|
}
|
||||||
|
|
||||||
public float[] ensurePointSize(int size, boolean copy){
|
// ---- API ----
|
||||||
|
public void clear() {
|
||||||
|
index[0] = -1;
|
||||||
|
indexPos = 0;
|
||||||
|
pointPos = 0;
|
||||||
|
mode = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addPoint(float x, float y) {
|
||||||
|
if (pointPos >= points.length - 1)
|
||||||
|
ensurePointSize((pointPos >> 1) + 1, true);
|
||||||
|
|
||||||
|
points[pointPos++] = x;
|
||||||
|
points[pointPos++] = y;
|
||||||
|
|
||||||
|
index[indexPos] += 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPoint(int pos, float x, float y) {
|
||||||
|
points[(pos << 1) + 0] = x;
|
||||||
|
points[(pos << 1) + 1] = y;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void startPoints() {
|
||||||
|
if (CHECK_STATE)
|
||||||
|
setOrCheckMode(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void startLine() {
|
||||||
|
if (CHECK_STATE)
|
||||||
|
setOrCheckMode(2);
|
||||||
|
|
||||||
|
if (index[0] >= 0){
|
||||||
|
// start next
|
||||||
|
if (++indexPos >= index.length)
|
||||||
|
ensureIndexSize(indexPos, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
// initialize with zero points
|
||||||
|
index[indexPos] = 0;
|
||||||
|
// set new end marker
|
||||||
|
index[indexPos + 1] = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void startPolygon() {
|
||||||
|
if (CHECK_STATE)
|
||||||
|
setOrCheckMode(3);
|
||||||
|
|
||||||
|
if ((indexPos + 4) >= index.length)
|
||||||
|
ensureIndexSize(indexPos + 4, true);
|
||||||
|
|
||||||
|
if (indexPos > 0) {
|
||||||
|
|
||||||
|
// end ring
|
||||||
|
index[++indexPos] = 0;
|
||||||
|
|
||||||
|
// end polygon
|
||||||
|
index[++indexPos] = 0;
|
||||||
|
|
||||||
|
// next polygon start
|
||||||
|
indexPos++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// initialize with zero points
|
||||||
|
index[indexPos] = 0;
|
||||||
|
// set new end marker
|
||||||
|
index[indexPos + 1] = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void startHole() {
|
||||||
|
if (CHECK_STATE)
|
||||||
|
checkMode(3);
|
||||||
|
|
||||||
|
if ((indexPos + 3) >= index.length)
|
||||||
|
ensureIndexSize(indexPos, true);
|
||||||
|
|
||||||
|
// end ring
|
||||||
|
index[++indexPos] = 0;
|
||||||
|
|
||||||
|
// initialize with zero points
|
||||||
|
index[++indexPos] = 0;
|
||||||
|
|
||||||
|
// set new end marker
|
||||||
|
index[indexPos + 1] = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// ---- internals ----
|
||||||
|
public float[] ensurePointSize(int size, boolean copy) {
|
||||||
if (size * 2 < points.length)
|
if (size * 2 < points.length)
|
||||||
return points;
|
return points;
|
||||||
|
|
||||||
float[] tmp = new float[size * 2 + 1024];
|
float[] tmp = new float[size * 2 + 1024];
|
||||||
if (copy)
|
if (copy)
|
||||||
System.arraycopy(tmp, 0, points, 0, points.length);
|
System.arraycopy(points, 0, tmp, 0, points.length);
|
||||||
|
|
||||||
points = tmp;
|
points = tmp;
|
||||||
return points;
|
return points;
|
||||||
}
|
}
|
||||||
|
|
||||||
public short[] ensureIndexSize(int size, boolean copy){
|
public short[] ensureIndexSize(int size, boolean copy) {
|
||||||
if (size < index.length)
|
if (size < index.length)
|
||||||
return index;
|
return index;
|
||||||
|
|
||||||
short[] tmp = new short[size + 128];
|
short[] tmp = new short[size + 128];
|
||||||
if (copy)
|
if (copy)
|
||||||
System.arraycopy(tmp, 0, index, 0, index.length);
|
System.arraycopy(index, 0, tmp, 0, index.length);
|
||||||
|
|
||||||
index = tmp;
|
index = tmp;
|
||||||
|
|
||||||
return index;
|
return index;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void setOrCheckMode(int m) {
|
||||||
|
if (mode == m)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (mode != 0)
|
||||||
|
throw new IllegalArgumentException("GeometryBuffer not cleared " + m + "<>" + mode);
|
||||||
|
|
||||||
|
mode = m;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void checkMode(int m) {
|
||||||
|
if (mode != m)
|
||||||
|
throw new IllegalArgumentException("GeometryBuffer not cleared " + m + "<>" + mode);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user