MapShape.hh
00001 /*
00002  * Copyright 2011 Nate Koenig & Andrew Howard
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of 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,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  *
00016 */
00017 /* Desc: Occupancy grid collision
00018  * Author: Nate Koenig
00019 */
00020 
00021 #ifndef MAPSHAPE_HH
00022 #define MAPSHAPE_HH
00023 
00024 #include <deque>
00025 
00026 #include "common/CommonTypes.hh"
00027 
00028 #include "physics/Collision.hh"
00029 #include "physics/Shape.hh"
00030 
00031 namespace gazebo
00032 {
00033     namespace physics
00034   {
00035     class SpaceTree;
00036     class QuadNode;
00037 
00040     
00042     class MapShape : public Shape
00043     {
00045       public: MapShape(CollisionPtr parent);
00046   
00048       public: virtual ~MapShape();
00049   
00051       public: void Update();
00052   
00054       public: virtual void Load( sdf::ElementPtr _sdf );
00055 
00057       public: virtual void Init();
00058   
00059       public: void FillShapeMsg(msgs::Geometry &_msg);
00060 
00061       public: virtual void ProcessMsg(const msgs::Geometry &_msg);
00062 
00063       public: std::string GetFilename() const;
00064       public: double GetScale() const;
00065       public: int GetThreshold() const;
00066       public: double GetHeight() const;
00067       public: int GetGranularity() const;
00068 
00070       private: void BuildTree(QuadNode *node);
00071   
00073       private: void GetPixelCount(unsigned int xStart, unsigned int yStart, 
00074                                   unsigned int width, unsigned int height, 
00075                                   unsigned int &freePixels, 
00076                                   unsigned int &occPixels  );
00077   
00079       private: void ReduceTree(QuadNode *node);
00080   
00082       private: void Merge(QuadNode *nodeA, QuadNode *nodeB);
00083   
00084       private: void CreateBox();
00085   
00087       private: void CreateBoxes(QuadNode *node);
00088   
00089       private: common::Image *mapImage;
00090   
00091       private: QuadNode *root;
00092   
00093       private: bool merged;
00094       private: static unsigned int collisionCounter;
00095     };
00096   
00097  
00099     class QuadNode
00100     {
00101       public: QuadNode( QuadNode *_parent ) 
00102               {
00103                 parent = _parent;
00104                 occupied = false;
00105                 leaf = true;
00106                 valid = true;
00107               }
00108   
00109       public: ~QuadNode() 
00110               { 
00111                 std::deque<QuadNode*>::iterator iter;
00112                 for (iter = children.begin(); iter != children.end(); iter++) 
00113                     delete (*iter); 
00114               }
00115   
00116       public: void Print(std::string space)
00117               {
00118                 std::deque<QuadNode*>::iterator iter;
00119   
00120                 printf("%sXY[%d %d] WH[%d %d] O[%d] L[%d] V[%d]\n",space.c_str(),x,y,width, height, occupied, leaf, valid);
00121                 space += "  ";
00122                 for (iter = children.begin(); iter != children.end(); iter++) 
00123                   if ((*iter)->occupied)
00124                     (*iter)->Print(space);
00125               }
00126   
00127       public: unsigned int x, y;
00128       public: unsigned int width, height;
00129   
00130       public: QuadNode *parent;
00131       public: std::deque<QuadNode*> children;
00132       public: bool occupied;
00133       public: bool leaf;
00134   
00135       public: bool valid;
00136     };
00138 
00140   }
00141 }
00142 #endif