DistancePoints.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2011 Google Inc.
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
00005  * use this file except in compliance with the License. You may obtain a copy of
00006  * the License at
00007  *
00008  * http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00012  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
00013  * License for the specific language governing permissions and limitations under
00014  * the License.
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   // Members for displaying the range vertices and polygons.
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   // Members for showing the robot shape.
00042   private FloatBuffer robotVertexBuffer;
00043   private int robotVertexCount;
00044   // Members for showing the reference markers.
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     // Clear the previous values.
00059     rangeVertices.clear();
00060     // The 90 degrees need to be added to offset the orientation differences
00061     // between the ROS coordinate system and the one used by OpenGL.
00062     minimumTheta += Math.toRadians(90.0);
00063     // Adding the center coordinate since it's needed for GL10.GL_TRIANGLE_FAN
00064     // to render the range polygons.
00065     rangeVertices.add(0.0f);
00066     rangeVertices.add(0.0f);
00067     rangeVertices.add(0.0f);
00068     // Calculate the coordinates for the range points. If the range is out of
00069     // bounds then do not display them.
00070     for (float rangeValue : range) {
00071       // Display the point if it's within the min and max valid range.
00072       if (rangeValue < maxRange && rangeValue > minRange) {
00073         // x
00074         rangeVertices.add((float) (rangeValue * Math.cos(minimumTheta)));
00075         // y
00076         rangeVertices.add((float) (rangeValue * Math.sin(minimumTheta)));
00077         // z
00078         rangeVertices.add(0.0f);
00079       }
00080       // Increment the theta for the next iteration.
00081       minimumTheta += thetaIncrement;
00082     }
00083     if (rangeVertexArray.length != rangeVertices.size()) {
00084       rangeVertexArray = new float[rangeVertices.size()];
00085     }
00086     // Move the contents of the List to the array.
00087     for (int i = 0; i < rangeVertices.size(); i++) {
00088       rangeVertexArray[i] = rangeVertices.get(i);
00089     }
00090     rangeVertexCount = rangeVertexArray.length / 3;
00091     // Update the buffers with the latest coordinates.
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       // Draw the vertices as triangle strip.
00122       gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, 0, rangeVertexCount);
00123       gl.glPointSize(3);
00124       gl.glColor4f(0.8f, 0.1f, 0.1f, 1f);
00125       // Draw the vertices as points.
00126       gl.glDrawArrays(GL10.GL_POINTS, 1, rangeVertexCount - 1);
00127       gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
00128     } catch (NullPointerException npe) {
00129       // Don't care.
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     // Hard coding the number of vertices since the count will not change
00145     // dynamically.
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     // TODO: The size of the robot is hard coded right now. Once the
00170     // transformations library is implemented, the shape of the robot should be
00171     // based on footprint of the robot.
00172     // Manually entering the coordinates for the robot. (The turtlebot is 1.1
00173     // square feet).
00174     // Top right
00175     robotVertices[0] = 0.1651f;
00176     robotVertices[1] = 0.1651f;
00177     robotVertices[2] = 0.0f;
00178     // Bottom right
00179     robotVertices[3] = 0.1651f;
00180     robotVertices[4] = -0.1651f;
00181     robotVertices[5] = 0.0f;
00182     // Bottom left
00183     robotVertices[6] = -0.1651f;
00184     robotVertices[7] = -0.1651f;
00185     robotVertices[8] = 0.0f;
00186     // Top left
00187     robotVertices[9] = -0.1651f;
00188     robotVertices[10] = 0.1651f;
00189     robotVertices[11] = 0.0f;
00190     robotVertexCount = robotVertices.length / 3;
00191     // Load the coordinates into the buffer.
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     // Horizontal line left point.
00204     referenceVertices[0] = -1.5f;
00205     referenceVertices[1] = yOffset;
00206     referenceVertices[2] = 0f;
00207     // Horizontal line right point.
00208     referenceVertices[3] = 1.5f;
00209     referenceVertices[4] = yOffset;
00210     referenceVertices[5] = 0f;
00211     // Vertical line (first from left) top.
00212     referenceVertices[6] = -1.5f;
00213     referenceVertices[7] = yOffset - yDelta;
00214     referenceVertices[8] = 0f;
00215     // Vertical line (first from left) bottom.
00216     referenceVertices[9] = -1.5f;
00217     referenceVertices[10] = yOffset + yDelta;
00218     referenceVertices[11] = 0f;
00219     // Vertical line (second from left) top.
00220     referenceVertices[12] = -0.5f;
00221     referenceVertices[13] = yOffset - yDelta;
00222     referenceVertices[14] = 0f;
00223     // Vertical line (second from left) bottom.
00224     referenceVertices[15] = -0.5f;
00225     referenceVertices[16] = yOffset + yDelta;
00226     referenceVertices[17] = 0f;
00227     // Vertical line (third from left) top.
00228     referenceVertices[18] = 0.5f;
00229     referenceVertices[19] = yOffset - yDelta;
00230     referenceVertices[20] = 0f;
00231     // Vertical line (third from left) bottom.
00232     referenceVertices[21] = 0.5f;
00233     referenceVertices[22] = yOffset + yDelta;
00234     referenceVertices[23] = 0f;
00235     // Vertical line (fourth from left) top.
00236     referenceVertices[24] = 1.5f;
00237     referenceVertices[25] = yOffset - yDelta;
00238     referenceVertices[26] = 0f;
00239     // Vertical line (fourth from left) bottom.
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 }


android_core
Author(s): Damon Kohler
autogenerated on Thu Jun 6 2019 21:20:07