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.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
00093
00094
00095 int i = 0;
00096 for (PoseStamped pose : path.getPoses()) {
00097
00098
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 }