From c305cbc82826edfbb4c8309d8f654efb5d5b0ef1 Mon Sep 17 00:00:00 2001 From: Hannes Janetzek Date: Sun, 21 Apr 2013 15:52:11 +0200 Subject: [PATCH] start of an API for GeometryBuffer - fix bug in ensure size --- src/org/oscim/core/GeometryBuffer.java | 131 ++++++++++++++++++++++++- 1 file changed, 126 insertions(+), 5 deletions(-) diff --git a/src/org/oscim/core/GeometryBuffer.java b/src/org/oscim/core/GeometryBuffer.java index 782183b4..48798fdb 100644 --- a/src/org/oscim/core/GeometryBuffer.java +++ b/src/org/oscim/core/GeometryBuffer.java @@ -1,42 +1,163 @@ package org.oscim.core; public class GeometryBuffer { + + // TODO + // -- check indexPos < Short.Max + final static boolean CHECK_STATE = true; + public float[] points; public short[] index; public int indexPos; 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.index = index; + mode = 0; + indexPos = 0; + pointPos = 0; } + public GeometryBuffer(int numPoints, int numIndices) { this.points = new float[numPoints * 2]; 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) return points; float[] tmp = new float[size * 2 + 1024]; if (copy) - System.arraycopy(tmp, 0, points, 0, points.length); + System.arraycopy(points, 0, tmp, 0, points.length); points = tmp; return points; } - public short[] ensureIndexSize(int size, boolean copy){ + public short[] ensureIndexSize(int size, boolean copy) { if (size < index.length) return index; short[] tmp = new short[size + 128]; if (copy) - System.arraycopy(tmp, 0, index, 0, index.length); + System.arraycopy(index, 0, tmp, 0, index.length); index = tmp; 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); + } + }