00001 /* 00002 * minioccupancygrid.hh 00003 * Mac Mason <mmason@willowgarage.com> 00004 * 00005 * A (miniature) occupancy grid implementation. Vitally, assumes that all 00006 * coordinates are positive. Watch out for that. 00007 * 00008 * get and set work in meters. 00009 */ 00010 00011 #pragma once 00012 #include <boost/noncopyable.hpp> 00013 #include <boost/thread.hpp> 00014 #include <utility> 00015 #include <vector> 00016 #include "nav_msgs/OccupancyGrid.h" 00017 00018 namespace semanticmodel 00019 { 00020 // In general, this shouldn't be noncopyable. Tough. 00021 class MiniatureOccupancyGrid : public boost::noncopyable 00022 { 00023 public: 00024 // Your choices. Like I said, miniature. Everything defaults to "empty". 00025 enum Occupancy { EMPTY, OCCUPIED }; 00026 00027 // Starts out empty. Gets bigger automatically. scale is expressed in 00028 // meters-per-cell. You probably want 0.01, or 0.05, or something like 00029 // that. 00030 MiniatureOccupancyGrid(double scale = 0.05); 00031 00032 // Locking for get and set; this returns a pointer which you _must 00033 // free_; I recommend usage like this: 00034 // 00035 // boost::scoped_ptr<boost::mutex::scoped_lock> lock(OG.get_lock()); 00036 boost::mutex::scoped_lock* get_lock(); 00037 00038 // Get the value at (x, y), measured in meters. Defaults to "empty", 00039 // should that not exist. 00040 Occupancy get(double x, double y) const; 00041 00042 // Set (x, y) (again in meters), to the given value. The underlying grid 00043 // will be expanded to make room for this. 00044 void set(double x, double y, Occupancy val); 00045 00046 // Get the sizes (it's ragged, so it's the largest size). 00047 std::pair<size_t, size_t> sizes() const; 00048 00049 // 2D ragged array. indexed as [x][y], after coordinate discretization. 00050 // Making these public makes me cry, but I'm in a hurry. 00051 std::vector < std::vector<Occupancy> > grid; 00052 double _scale; 00053 00054 // Convert from world coordinates ((x, y), in meters) to grid indices 00055 // ((a, b), in indices). a and b are output arguments. 00056 void round_coordinates(double x, double y, int& a, int& b) const; 00057 00058 // For publishing. 00059 void to_msg(nav_msgs::OccupancyGrid& og) const; 00060 00061 private: 00062 mutable boost::mutex data_mutex; 00063 }; 00064 }