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.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
00088
00089
00090 int i = 0;
00091 for (PoseStamped pose : path.getPoses()) {
00092
00093
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 }