Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 package org.ros.android.view;
00018
00019 import java.nio.ByteBuffer;
00020 import java.nio.ByteOrder;
00021 import java.nio.FloatBuffer;
00022 import java.util.ArrayList;
00023 import java.util.List;
00024
00025 import javax.microedition.khronos.opengles.GL10;
00026
00033 class DistancePoints {
00034
00035
00036 private FloatBuffer rangeVertexBuffer;
00037 private ByteBuffer rangeVertexByteBuffer;
00038 private List<Float> rangeVertices = new ArrayList<Float>();
00039 private float[] rangeVertexArray = new float[0];
00040 private int rangeVertexCount;
00041
00042 private FloatBuffer robotVertexBuffer;
00043 private int robotVertexCount;
00044
00045 private FloatBuffer referenceVertexBuffer;
00046
00047 public DistancePoints() {
00048 initRobot();
00049 initReferenceMarker();
00050 }
00051
00056 public void updateRange(List<Float> range, float maxRange, float minRange, float minimumTheta,
00057 float thetaIncrement) {
00058
00059 rangeVertices.clear();
00060
00061
00062 minimumTheta += Math.toRadians(90.0);
00063
00064
00065 rangeVertices.add(0.0f);
00066 rangeVertices.add(0.0f);
00067 rangeVertices.add(0.0f);
00068
00069
00070 for (float rangeValue : range) {
00071
00072 if (rangeValue < maxRange && rangeValue > minRange) {
00073
00074 rangeVertices.add((float) (rangeValue * Math.cos(minimumTheta)));
00075
00076 rangeVertices.add((float) (rangeValue * Math.sin(minimumTheta)));
00077
00078 rangeVertices.add(0.0f);
00079 }
00080
00081 minimumTheta += thetaIncrement;
00082 }
00083 if (rangeVertexArray.length != rangeVertices.size()) {
00084 rangeVertexArray = new float[rangeVertices.size()];
00085 }
00086
00087 for (int i = 0; i < rangeVertices.size(); i++) {
00088 rangeVertexArray[i] = rangeVertices.get(i);
00089 }
00090 rangeVertexCount = rangeVertexArray.length / 3;
00091
00092 initRangeVertexBuffer();
00093 rangeVertexBuffer.put(rangeVertexArray);
00094 rangeVertexBuffer.position(0);
00095 }
00096
00097 private void initRangeVertexBuffer() {
00098 int requiredVertexByteBufferCapacity = rangeVertices.size() * Float.SIZE / 8;
00099 if (rangeVertexByteBuffer == null
00100 || requiredVertexByteBufferCapacity > rangeVertexByteBuffer.capacity()) {
00101 rangeVertexByteBuffer = ByteBuffer.allocateDirect(requiredVertexByteBufferCapacity);
00102 rangeVertexByteBuffer.order(ByteOrder.nativeOrder());
00103 }
00104 rangeVertexBuffer = rangeVertexByteBuffer.asFloatBuffer();
00105 }
00106
00114 public void drawRange(GL10 gl) {
00115 try {
00116 gl.glDisable(GL10.GL_CULL_FACE);
00117 gl.glFrontFace(GL10.GL_CW);
00118 gl.glVertexPointer(3, GL10.GL_FLOAT, 0, rangeVertexBuffer);
00119 gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
00120 gl.glColor4f(0.35f, 0.35f, 0.35f, 0.7f);
00121
00122 gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, 0, rangeVertexCount);
00123 gl.glPointSize(3);
00124 gl.glColor4f(0.8f, 0.1f, 0.1f, 1f);
00125
00126 gl.glDrawArrays(GL10.GL_POINTS, 1, rangeVertexCount - 1);
00127 gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
00128 } catch (NullPointerException npe) {
00129
00130 }
00131 }
00132
00139 public void drawReferenceMarker(GL10 gl) {
00140 gl.glEnable(GL10.GL_LINE_SMOOTH);
00141 gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
00142 gl.glVertexPointer(3, GL10.GL_FLOAT, 0, referenceVertexBuffer);
00143 gl.glColor4f(0.7f, 0.7f, 0.7f, 1f);
00144
00145
00146 gl.glDrawArrays(GL10.GL_LINES, 0, 10);
00147 gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
00148 gl.glDisable(GL10.GL_LINE_SMOOTH);
00149 }
00150
00157 public void drawRobot(GL10 gl) {
00158 gl.glEnable(GL10.GL_LINE_SMOOTH);
00159 gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
00160 gl.glVertexPointer(3, GL10.GL_FLOAT, 0, robotVertexBuffer);
00161 gl.glColor4f(0.6f, 0, 0, 1f);
00162 gl.glDrawArrays(GL10.GL_LINE_LOOP, 0, robotVertexCount);
00163 gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
00164 gl.glDisable(GL10.GL_LINE_SMOOTH);
00165 }
00166
00167 private void initRobot() {
00168 float[] robotVertices = new float[4 * 3];
00169
00170
00171
00172
00173
00174
00175 robotVertices[0] = 0.1651f;
00176 robotVertices[1] = 0.1651f;
00177 robotVertices[2] = 0.0f;
00178
00179 robotVertices[3] = 0.1651f;
00180 robotVertices[4] = -0.1651f;
00181 robotVertices[5] = 0.0f;
00182
00183 robotVertices[6] = -0.1651f;
00184 robotVertices[7] = -0.1651f;
00185 robotVertices[8] = 0.0f;
00186
00187 robotVertices[9] = -0.1651f;
00188 robotVertices[10] = 0.1651f;
00189 robotVertices[11] = 0.0f;
00190 robotVertexCount = robotVertices.length / 3;
00191
00192 ByteBuffer vertexByteBuffer = ByteBuffer.allocateDirect(robotVertices.length * Float.SIZE / 8);
00193 vertexByteBuffer.order(ByteOrder.nativeOrder());
00194 robotVertexBuffer = vertexByteBuffer.asFloatBuffer();
00195 robotVertexBuffer.put(robotVertices);
00196 robotVertexBuffer.position(0);
00197 }
00198
00199 private void initReferenceMarker() {
00200 float[] referenceVertices = new float[10 * 3];
00201 float yOffset = -2f;
00202 float yDelta = 0.25f;
00203
00204 referenceVertices[0] = -1.5f;
00205 referenceVertices[1] = yOffset;
00206 referenceVertices[2] = 0f;
00207
00208 referenceVertices[3] = 1.5f;
00209 referenceVertices[4] = yOffset;
00210 referenceVertices[5] = 0f;
00211
00212 referenceVertices[6] = -1.5f;
00213 referenceVertices[7] = yOffset - yDelta;
00214 referenceVertices[8] = 0f;
00215
00216 referenceVertices[9] = -1.5f;
00217 referenceVertices[10] = yOffset + yDelta;
00218 referenceVertices[11] = 0f;
00219
00220 referenceVertices[12] = -0.5f;
00221 referenceVertices[13] = yOffset - yDelta;
00222 referenceVertices[14] = 0f;
00223
00224 referenceVertices[15] = -0.5f;
00225 referenceVertices[16] = yOffset + yDelta;
00226 referenceVertices[17] = 0f;
00227
00228 referenceVertices[18] = 0.5f;
00229 referenceVertices[19] = yOffset - yDelta;
00230 referenceVertices[20] = 0f;
00231
00232 referenceVertices[21] = 0.5f;
00233 referenceVertices[22] = yOffset + yDelta;
00234 referenceVertices[23] = 0f;
00235
00236 referenceVertices[24] = 1.5f;
00237 referenceVertices[25] = yOffset - yDelta;
00238 referenceVertices[26] = 0f;
00239
00240 referenceVertices[27] = 1.5f;
00241 referenceVertices[28] = yOffset + yDelta;
00242 referenceVertices[29] = 0f;
00243 ByteBuffer referenceVertexByteBuffer =
00244 ByteBuffer.allocateDirect(referenceVertices.length * Float.SIZE / 8);
00245 referenceVertexByteBuffer.order(ByteOrder.nativeOrder());
00246 referenceVertexBuffer = referenceVertexByteBuffer.asFloatBuffer();
00247 referenceVertexBuffer.put(referenceVertices);
00248 referenceVertexBuffer.position(0);
00249 }
00250 }