GridCellsLayer.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 android.os.Handler;
00020 import org.ros.android.view.visualization.Camera;
00021 import org.ros.android.view.visualization.Color;
00022 import org.ros.android.view.visualization.Vertices;
00023 import org.ros.message.MessageListener;
00024 import org.ros.namespace.GraphName;
00025 import org.ros.node.ConnectedNode;
00026 import org.ros.rosjava_geometry.FrameName;
00027 import org.ros.rosjava_geometry.FrameTransformTree;
00028 
00029 import java.util.concurrent.locks.Lock;
00030 import java.util.concurrent.locks.ReentrantLock;
00031 
00032 import javax.microedition.khronos.opengles.GL10;
00033 
00037 public class GridCellsLayer extends SubscriberLayer<nav_msgs.GridCells> implements TfLayer {
00038 
00039   private final Color color;
00040   private final Lock lock;
00041 
00042   private FrameName frame;
00043   private Camera camera;
00044   private boolean ready;
00045   private nav_msgs.GridCells message;
00046 
00047   public GridCellsLayer(String topicName, Color color) {
00048     this(GraphName.of(topicName), color);
00049   }
00050 
00051   public GridCellsLayer(GraphName topicName, Color color) {
00052     super(topicName, "nav_msgs/GridCells");
00053     this.color = color;
00054     frame = null;
00055     lock = new ReentrantLock();
00056     ready = false;
00057   }
00058 
00059   @Override
00060   public void draw(GL10 gl) {
00061     if (!ready) {
00062       return;
00063     }
00064     super.draw(gl);
00065     lock.lock();
00066     float pointSize =
00067         (float) (Math.max(message.getCellWidth(), message.getCellHeight()) * camera.getZoom());
00068     float[] vertices = new float[3 * message.getCells().size()];
00069     int i = 0;
00070     for (geometry_msgs.Point cell : message.getCells()) {
00071       vertices[i] = (float) cell.getX();
00072       vertices[i + 1] = (float) cell.getY();
00073       vertices[i + 2] = 0.0f;
00074       i += 3;
00075     }
00076     gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
00077     gl.glVertexPointer(3, GL10.GL_FLOAT, 0, Vertices.toFloatBuffer(vertices));
00078     color.apply(gl);
00079     gl.glPointSize(pointSize);
00080     gl.glDrawArrays(GL10.GL_POINTS, 0, message.getCells().size());
00081     gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
00082     lock.unlock();
00083   }
00084 
00085   @Override
00086   public void onStart(ConnectedNode connectedNode, Handler handler,
00087       final FrameTransformTree frameTransformTree, Camera camera) {
00088     super.onStart(connectedNode, handler, frameTransformTree, camera);
00089     this.camera = camera;
00090     getSubscriber().addMessageListener(new MessageListener<nav_msgs.GridCells>() {
00091       @Override
00092       public void onNewMessage(nav_msgs.GridCells data) {
00093         frame = FrameName.of(data.getHeader().getFrameId());
00094         if (frameTransformTree.lookUp(frame) != null) {
00095           if (lock.tryLock()) {
00096             message = data;
00097             ready = true;
00098             lock.unlock();
00099           }
00100         }
00101       }
00102     });
00103   }
00104 
00105   @Override
00106   public FrameName getFrame() {
00107     return frame;
00108   }
00109 }


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