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


android_core
Author(s): Damon Kohler
autogenerated on Thu Aug 27 2015 12:11:33