CompressedOccupancyGridLayer.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 com.google.common.base.Preconditions;
00020 
00021 import android.graphics.Bitmap;
00022 import android.graphics.BitmapFactory;
00023 import android.os.Handler;
00024 import org.jboss.netty.buffer.ChannelBuffer;
00025 import org.ros.android.view.visualization.Camera;
00026 import org.ros.android.view.visualization.TextureBitmap;
00027 import org.ros.message.MessageListener;
00028 import org.ros.namespace.GraphName;
00029 import org.ros.node.ConnectedNode;
00030 import org.ros.rosjava_geometry.FrameName;
00031 import org.ros.rosjava_geometry.FrameTransformTree;
00032 import org.ros.rosjava_geometry.Transform;
00033 
00034 import javax.microedition.khronos.opengles.GL10;
00035 
00040 public class CompressedOccupancyGridLayer extends SubscriberLayer<nav_msgs.OccupancyGrid> implements
00041     TfLayer {
00042 
00046   private static final int COLOR_OCCUPIED = 0xdfffffff;
00047 
00051   private static final int COLOR_FREE = 0xff8d8d8d;
00052 
00056   private static final int COLOR_UNKNOWN = 0xff000000;
00057 
00058   private final TextureBitmap textureBitmap;
00059 
00060   private boolean ready;
00061   private FrameName frame;
00062 
00063   public CompressedOccupancyGridLayer(String topic) {
00064     this(GraphName.of(topic));
00065   }
00066 
00067   public CompressedOccupancyGridLayer(GraphName topic) {
00068     super(topic, nav_msgs.OccupancyGrid._TYPE);
00069     textureBitmap = new TextureBitmap();
00070     ready = false;
00071   }
00072 
00073   @Override
00074   public void draw(GL10 gl) {
00075     if (ready) {
00076       textureBitmap.draw(gl);
00077     }
00078   }
00079 
00080   @Override
00081   public FrameName getFrame() {
00082     return frame;
00083   }
00084 
00085   @Override
00086   public void onStart(ConnectedNode connectedNode, Handler handler,
00087       FrameTransformTree frameTransformTree, Camera camera) {
00088     super.onStart(connectedNode, handler, frameTransformTree, camera);
00089     getSubscriber().addMessageListener(new MessageListener<nav_msgs.OccupancyGrid>() {
00090       @Override
00091       public void onNewMessage(nav_msgs.OccupancyGrid message) {
00092         update(message);
00093       }
00094     });
00095   }
00096 
00097   void update(nav_msgs.OccupancyGrid message) {
00098     ChannelBuffer buffer = message.getData();
00099     Bitmap bitmap =
00100         BitmapFactory.decodeByteArray(buffer.array(), buffer.arrayOffset(), buffer.readableBytes());
00101     int stride = bitmap.getWidth();
00102     int height = bitmap.getHeight();
00103     Preconditions.checkArgument(stride <= 1024);
00104     Preconditions.checkArgument(height <= 1024);
00105     int[] pixels = new int[stride * height];
00106     bitmap.getPixels(pixels, 0, stride, 0, 0, stride, height);
00107     for (int i = 0; i < pixels.length; i++) {
00108       // Pixels are ARGB packed ints.
00109       if (pixels[i] == 0xffffffff) {
00110         pixels[i] = COLOR_UNKNOWN;
00111       } else if (pixels[i] == 0xff000000) {
00112         pixels[i] = COLOR_FREE;
00113       } else {
00114         pixels[i] = COLOR_OCCUPIED;
00115       }
00116     }
00117     float resolution = message.getInfo().getResolution();
00118     Transform origin = Transform.fromPoseMessage(message.getInfo().getOrigin());
00119     textureBitmap.updateFromPixelArray(pixels, stride, resolution, origin, COLOR_UNKNOWN);
00120     frame = FrameName.of(message.getHeader().getFrameId());
00121     ready = true;
00122   }
00123 }


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