PathLayer.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.visualization.layer;
00018 
00019 import org.ros.android.view.visualization.VisualizationView;
00020 import org.ros.android.view.visualization.Color;
00021 import geometry_msgs.PoseStamped;
00022 import org.ros.message.MessageListener;
00023 import org.ros.namespace.GraphName;
00024 import org.ros.node.ConnectedNode;
00025 
00026 import java.nio.ByteBuffer;
00027 import java.nio.ByteOrder;
00028 import java.nio.FloatBuffer;
00029 
00030 import javax.microedition.khronos.opengles.GL10;
00031 
00038 public class PathLayer extends SubscriberLayer<nav_msgs.Path> implements TfLayer {
00039 
00040   private static final Color COLOR = Color.fromHexAndAlpha("03dfc9", 0.3f);
00041   private static final float POINT_SIZE = 5.0f;
00042 
00043   private FloatBuffer vertexBuffer;
00044   private boolean ready;
00045   private GraphName frame;
00046 
00047   public PathLayer(String topic) {
00048     this(GraphName.of(topic));
00049   }
00050 
00051   public PathLayer(GraphName topic) {
00052     super(topic, "nav_msgs/Path");
00053     ready = false;
00054   }
00055 
00056   @Override
00057   public void draw(VisualizationView view, GL10 gl) {
00058     if (ready) {
00059       gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
00060       gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
00061       COLOR.apply(gl);
00062       gl.glPointSize(POINT_SIZE);
00063       gl.glDrawArrays(GL10.GL_POINTS, 0, vertexBuffer.limit() / 3);
00064       gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
00065     }
00066   }
00067 
00068   @Override
00069   public void onStart(VisualizationView view, ConnectedNode connectedNode) {
00070     super.onStart(view, connectedNode);
00071     getSubscriber().addMessageListener(new MessageListener<nav_msgs.Path>() {
00072       @Override
00073       public void onNewMessage(nav_msgs.Path path) {
00074         updateVertexBuffer(path);
00075         ready = true;
00076       }
00077     });
00078   }
00079 
00080   private void updateVertexBuffer(nav_msgs.Path path) {
00081     ByteBuffer goalVertexByteBuffer =
00082         ByteBuffer.allocateDirect(path.getPoses().size() * 3 * Float.SIZE / 8);
00083     goalVertexByteBuffer.order(ByteOrder.nativeOrder());
00084     vertexBuffer = goalVertexByteBuffer.asFloatBuffer();
00085     if (path.getPoses().size() > 0) {
00086       frame = GraphName.of(path.getPoses().get(0).getHeader().getFrameId());
00087       // Path poses are densely packed and will make the path look like a solid
00088       // line even if it is drawn as points. Skipping poses provides the visual
00089       // point separation were looking for.
00090       int i = 0;
00091       for (PoseStamped pose : path.getPoses()) {
00092         // TODO(damonkohler): Choose the separation between points as a pixel
00093         // value. This will require inspecting the zoom level from the camera.
00094         if (i % 15 == 0) {
00095           vertexBuffer.put((float) pose.getPose().getPosition().getX());
00096           vertexBuffer.put((float) pose.getPose().getPosition().getY());
00097           vertexBuffer.put((float) pose.getPose().getPosition().getZ());
00098         }
00099         i++;
00100       }
00101     }
00102     vertexBuffer.position(0);
00103   }
00104 
00105   @Override
00106   public GraphName getFrame() {
00107     return frame;
00108   }
00109 }


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