Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

MapBuilder.h

Go to the documentation of this file.
00001 //-*-c++-*-
00002 #ifndef _MapBuilder_h_
00003 #define _MapBuilder_h_
00004 
00005 #include <queue>
00006 
00007 #include "Crew/MapBuilderRequest.h"
00008 #include "Shared/fmatSpatial.h"
00009 
00010 #include "DualCoding/Point.h"
00011 
00012 #include "DualCoding/BlobData.h"
00013 #include "DualCoding/EllipseData.h"
00014 #include "DualCoding/LineData.h"
00015 #include "DualCoding/SphereData.h"
00016 #include "DualCoding/TargetData.h"
00017 #include "DualCoding/MarkerData.h"
00018 
00019 #include "DualCoding/VRmixin.h"
00020 #include "DualCoding/SketchTypes.h"
00021 #include "DualCoding/ShapeSpace.h"
00022 #include "DualCoding/PolygonData.h"
00023 
00024 // Note: these are NOT in the DualCoding namespace
00025 class LookoutSketchEvent;
00026 class SiftTekkotsu;
00027 
00028 namespace DualCoding {
00029 
00030 class SketchSpace;
00031 
00032 class MapBuilder : public BehaviorBase {
00033 protected:
00034   SketchSpace &camSkS;
00035   ShapeSpace &camShS, &groundShS;
00036   SketchSpace &localSkS;
00037   ShapeSpace &localShS;
00038   SketchSpace &worldSkS;
00039   ShapeSpace &worldShS;
00040 
00041   const int xres, yres; //!< width and height of camera frame
00042 
00043   PlaneEquation ground_plane; //!< ground plane to which shapes are projected
00044 
00045   static bool retain; //!< if true, VRmixin::stopCrew will not clear MapBuilder structures
00046 
00047 public:
00048   //! Control whether static structures (mapbuilder, sketchGUI sockets, etc.) are retained
00049   static void setRetain(bool r) { retain = r; }
00050   static bool isRetained() { return retain; }
00051 
00052   typedef unsigned int MapBuilderVerbosity_t;
00053   static const MapBuilderVerbosity_t MBVstart = 1<<0;
00054   static const MapBuilderVerbosity_t MBVevents = 1<<1;
00055   static const MapBuilderVerbosity_t MBVexecute = 1<<2;
00056   static const MapBuilderVerbosity_t MBVcomplete = 1<<3;
00057   static const MapBuilderVerbosity_t MBVdefineGazePoints = 1<<4;
00058   static const MapBuilderVerbosity_t MBVnextGazePoint = 1<<5;
00059   static const MapBuilderVerbosity_t MBVshapeSearch = 1<<6;
00060   static const MapBuilderVerbosity_t MBVshapesFound = 1<<7;
00061   static const MapBuilderVerbosity_t MBVgroundPlane = 1<<8;
00062   static const MapBuilderVerbosity_t MBVprojectionFailed = 1<<9;
00063   static const MapBuilderVerbosity_t MBVimportShapes = 1<<10;
00064   static const MapBuilderVerbosity_t MBVnotAdmissible = 1<<11;
00065   static const MapBuilderVerbosity_t MBVshapeMatch = 1<<12;
00066   static const MapBuilderVerbosity_t MBVshapesMerge = 1<<13;
00067   static const MapBuilderVerbosity_t MBVshouldSee = 1<<14;
00068   static const MapBuilderVerbosity_t MBVdeleteShape = 1<<15;
00069   static const MapBuilderVerbosity_t MBVsetAgent = 1<<16;
00070   static const MapBuilderVerbosity_t MBVbadGazePoint = 1<<17;
00071 
00072 private:
00073   static MapBuilderVerbosity_t verbosity;
00074 public:
00075   static void setVerbosity(MapBuilderVerbosity_t v) { verbosity = v; }
00076 
00077 protected:
00078   friend class Lookout;
00079   friend class BiColorMarkerData;   // needs to call getCamBlobs()
00080   friend class Pilot;
00081 
00082   Shape<AgentData> &theAgent; //!< Agent in the world frame
00083    //!@name Transformation matrices between local and world frames
00084   //@{
00085  public:
00086   fmat::Transform localToWorldMatrix, worldToLocalTranslateMatrix, worldToLocalRotateMatrix;
00087   //@}
00088 
00089 protected:
00090   std::vector<Point> badGazePoints; //!<  gaze points for which HeadPointerMC.lookAtPoint() returned false
00091 
00092   std::queue<MapBuilderRequest*> requests;
00093   MapBuilderRequest *curReq;
00094   unsigned int idCounter;
00095   float maxDistSq; //!< square of current request's max distance parameter
00096   std::map<string,SiftTekkotsu*> siftMatchers;
00097 
00098   unsigned int pointAtID, scanID; //!< ID's for lookout requests
00099   Point nextGazePoint;
00100 
00101   //! Triggers action to execute the request at the front of the queue
00102   void executeRequest();
00103   //! calls exitTest of current request if there is one and returns the result
00104   bool requestExitTest();
00105   //! posts completion event and deletes current request, executes next request if there is one
00106   void requestComplete(); 
00107 
00108 #ifdef TGT_IS_AIBO
00109 public:
00110 #endif
00111   //! Sets agent location and heading, and recomputes local-to-world transformation matrices.  Called by the Pilot.
00112   void setAgent(const Point &worldLocation, const AngTwoPi worldHeading, bool quiet=false);
00113 
00114 public:
00115   MapBuilder(); //!< Constructor
00116   virtual ~MapBuilder() {}   //!< Destructor
00117   virtual void preStart();
00118   virtual void stop(); 
00119   virtual std::string getDescription() const { return "MapBuilder"; }
00120   void printShS(ShapeSpace&) const;
00121   unsigned int executeRequest(const MapBuilderRequest&, unsigned int *req_id=NULL); //!< Execute a MapBuilder request, and optionally store the request id in a variable; the id will be returned in any case
00122   void executeRequest(BehaviorBase* requestingBehavior, const MapBuilderRequest &req); //!< Execture a MapBuilder request and store the address of the requesting behavior to use as the source ID in a mapbuilder status event
00123 
00124   virtual void doEvent();
00125   void processImage(const LookoutSketchEvent&);
00126 
00127   // Returns true if a ground shape should be seen in the current camera frame
00128   static bool isPointVisible(const Point &pt, const fmat::Transform& baseToCam, float maxDistanceSq) ;
00129   static bool isLineVisible(const LineData& ln, const fmat::Transform& baseToCam);
00130   static bool isShapeVisible(const ShapeRoot &ground_shape, const fmat::Transform& baseToCam, float maxDistanceSq);
00131   
00132   
00133   //! utility functions which may be used by MapBuilderRequest's exit condition and others
00134   //@{
00135   const Shape<AgentData>& getAgent() const { return theAgent; }
00136   
00137   //! updates the agent location and heading after a relative move
00138   void moveAgent(coordinate_t const local_dx, coordinate_t const local_dy, coordinate_t const local_dz, AngSignPi dtheta);
00139   //@}
00140   
00141   std::vector<ShapeRoot> getShapes(const ShapeSpace& ShS, int minConf=2) const {
00142     const std::vector<ShapeRoot> allShapes = ShS.allShapes();
00143     if (&ShS == &camShS || &ShS == &groundShS || minConf <= 0) 
00144       return allShapes;
00145     std::vector<ShapeRoot> nonNoiseShapes;
00146     for (std::vector<ShapeRoot>::const_iterator it = allShapes.begin();
00147    it != allShapes.end(); it++)
00148       if ((*it)->getConfidence() >= minConf)
00149   nonNoiseShapes.push_back(*it);
00150     return nonNoiseShapes;
00151   }
00152 
00153   void importLocalToWorld();
00154 
00155   ShapeRoot importLocalShapeToWorld(const ShapeRoot &localShape);
00156   ShapeRoot importWorldToLocal(const ShapeRoot &worldShape);
00157   template<class T> Shape<T> importWorldToLocal(const Shape<T> &worldShape);
00158 
00159 protected:
00160   //!@name Shape extraction functions
00161   //@{
00162   void getCameraShapes(const Sketch<uchar>& camFrame);
00163 
00164   std::vector<Shape<LineData> > 
00165   getCamLines(const Sketch<uchar>&, const std::set<color_index>& objectColors, 
00166         const std::set<color_index>& occluderColors) const;
00167 
00168   std::vector<Shape<EllipseData> > 
00169   getCamEllipses(const Sketch<uchar>&, const std::set<color_index>& objectColors, 
00170      const std::set<color_index>& occluderColors) const;
00171 
00172   void getCamPolygons(const Sketch<uchar>&, const std::set<color_index>& objectColors, 
00173           const std::set<color_index>& occluderColors) const;
00174 
00175   std::vector<Shape<LineData> >  
00176   getCamWalls(const Sketch<uchar>&, unsigned int) const;
00177 
00178   void getCamSpheres(const Sketch<uchar>&, const std::set<color_index>& objectColors, 
00179          const std::set<color_index>& occluderColors) const;
00180 
00181   void getCamBlobs(const Sketch<uchar>& camFrame,
00182        const std::set<color_index>& colors,
00183        const std::map<color_index,int>& minBlobAreas,
00184        const std::map<color_index, BlobData::BlobOrientation_t>& blobOrientations,
00185        const std::map<color_index,coordinate_t>& assumedBlobHeights);
00186   void getCamBlobs(const set<color_index>& colors, int defMinBlobArea=0);
00187   
00188   void getCamTargets(const Sketch<uchar> &camFrame, const std::set<color_index>& objectColors,
00189          const std::set<color_index>& occluderColors) const;
00190   
00191   std::vector<Shape<MarkerData> > 
00192   getCamMarkers(const Sketch<uchar> &camFrame, const std::set<color_index>& objectColors,
00193     const std::set<color_index>& occluderColors, const std::set<MarkerType_t>& markerTypes) const;
00194 
00195   void getCamSiftObjects(const Sketch<uchar> &rawY, const std::string &siftDatabasePath, 
00196        const std::set<std::string> &siftObjectNames);
00197 
00198   void getCamAprilTags(const Sketch<uchar> &rawY);
00199   //@}
00200 
00201 public:
00202   void newSiftMatcher(const std::string &siftDatabasePath);
00203 
00204   void saveSiftDatabase(const std::string &siftDatabasePath);
00205 
00206   void trainSiftObject(const std::string &siftDatabasePath,
00207            const std::string &objectName, const std::string &modelName="model1");
00208 
00209   void trainSiftObject(const std::string &siftDatabasePath, const Sketch<uchar> &sketch,
00210            const std::string &objectName, const std::string &modelName="model1");
00211 
00212 public: // ** debug **
00213   // matching shapes between two spaces.
00214   void matchSrcToDst(ShapeSpace &src, ShapeSpace &dst, std::set<color_index> polygonEdgeColors=std::set<color_index>(),
00215           bool mergeSrc=true, bool mergeDst=true);
00216 
00217   //!@name Functions to make requests to the Lookout
00218   //@{
00219   void storeImage(bool useNextGazePoint);
00220   void grabCameraImageAndGo();
00221   void scanForGazePts();
00222   //@}
00223 
00224   //! define gazePts either virtually or by scan
00225   void defineGazePts();
00226   
00227   void extendLocal(const fmat::Transform& baseToCam);
00228   void extendWorld(const fmat::Transform& baseToCam);
00229 
00230   //! decrement confidence of shapes which should have been seen according to the baseToCam matrix
00231   void removeNoise(ShapeSpace&, const fmat::Transform& baseToCam);
00232   //! erase gaze points which should have been seen according to the baseToCam matrix
00233   void removeGazePts(std::vector<Point>&, const fmat::Transform& baseToCam);
00234   
00235   //! Returns true if it has set up a valid next gaze point in nextGazePoint
00236   bool determineNextGazePoint();
00237   //! Returns true if there is a shape which needs be looked at again and is reachable; sets it up as nextGazePoint
00238   bool determineNextGazePoint(const std::vector<ShapeRoot>&);
00239   // Returns true if an element of gazePts can be looked at; sets it up as nextGazePoint
00240   bool determineNextGazePoint(std::vector<Point> &gazePts);
00241   //! Starts robot moving to the next gaze point
00242   void moveToNextGazePoint(const bool manualOverride=false);
00243   void doNextSearch();
00244   void doNextSearch2();
00245 
00246   // operations in ground shape space 
00247   bool isBadGazePoint(const Point&) const ;
00248   void projectToGround(const fmat::Transform& camToBase);
00249   ShapeRoot projectToLocal(ShapeRoot &shape);
00250   void filterGroundShapes(const fmat::Transform& baseToCam);
00251 
00252   // calculates ground plane based on ground plane assumption type
00253   void calculateGroundPlane();
00254 
00255 private:
00256   MapBuilder(const MapBuilder&); //!< never call this
00257   MapBuilder& operator=(const MapBuilder&);  //!< never call this
00258 };
00259 
00260 template<class T> Shape<T> MapBuilder::importWorldToLocal(const Shape<T> &worldShape) {
00261   ShapeRoot temp(localShS.importShape(worldShape));
00262   Shape<T> localShape(ShapeRootType(temp,T));
00263   localShape->applyTransform(worldToLocalTranslateMatrix);
00264   localShape->applyTransform(worldToLocalRotateMatrix);
00265   return localShape;
00266 }
00267 
00268 //! Utility function for deleting queues of pointers to Lookout or MapBuilder requests
00269 template<typename T> void deleteAll(std::queue<T*> &q) {
00270   while ( ! q.empty() ) {
00271     delete q.front();
00272     q.pop();
00273   }
00274 }
00275 
00276 } // namespace
00277 
00278 #endif
00279 

DualCoding 5.1CVS
Generated Fri Mar 16 05:23:46 2012 by Doxygen 1.6.3