00001
00002 #ifndef _MapBuilder_h_
00003 #define _MapBuilder_h_
00004
00005 #include <queue>
00006
00007 #include "Behaviors/BehaviorBase.h"
00008 #include "Shared/newmat/newmat.h"
00009
00010 #include "BlobData.h"
00011 #include "LineData.h"
00012 #include "Point.h"
00013 #include "VRmixin.h"
00014 #include "LookoutRequests.h"
00015 #include "MapBuilderRequests.h"
00016 #include "SketchTypes.h"
00017 #include "ShapeSpace.h"
00018
00019 namespace DualCoding {
00020
00021 class SketchSpace;
00022 class ScanRequest;
00023
00024 class MapBuilder : public BehaviorBase {
00025 protected:
00026 SketchSpace &camSkS;
00027 ShapeSpace &camShS, &groundShS;
00028
00029 struct maps {
00030 enum Space { local, world } space;
00031 SketchSpace &SkS;
00032 ShapeSpace &ShS;
00033 vector<Point> gazePts;
00034 vector<NEWMAT::Matrix> baseToCamMats;
00035 maps(Space _space, SketchSpace& _SkS) :
00036 space(_space), SkS(_SkS), ShS(_SkS.getDualSpace()), gazePts(), baseToCamMats() {}
00037 } local, world, *cur;
00038
00039 public:
00040 const int xres, yres;
00041 NEWMAT::ColumnVector ground_plane;
00042
00043 protected:
00044 Shape<AgentData> &theAgent;
00045
00046 NEWMAT::Matrix localToWorldMatrix, worldToLocalTranslateMatrix, worldToLocalRotateMatrix;
00047
00048 vector<Point> badGazePoints;
00049 bool agentAtOrigin;
00050
00051 queue<MapBuilderRequest*> requests;
00052 MapBuilderRequest *curReq;
00053 unsigned int idCounter;
00054
00055 unsigned int maxDistSq;
00056 unsigned int pointAtID, scanID;
00057 Point nextGazePoint;
00058
00059
00060 void requestComplete();
00061
00062 void executeRequest();
00063
00064 bool requestExitTest();
00065
00066 public:
00067 MapBuilder();
00068 virtual ~MapBuilder() {}
00069 virtual void DoStart();
00070 virtual void DoStop();
00071 virtual void processEvent(const EventBase&);
00072 virtual std::string getDescription() const { return "MapBuilder"; }
00073 void printShS(ShapeSpace&) const;
00074 unsigned int executeRequest(const MapBuilderRequest& req);
00075
00076 void processImage(const Sketch<uchar>&, const NEWMAT::Matrix& camToBase, const NEWMAT::Matrix& baseToCam);
00077
00078
00079 static bool isPointVisible(const Point &pt, const NEWMAT::Matrix& baseToCam, float maxDistanceSq) ;
00080 static bool isLineVisible(const LineData& ln, const NEWMAT::Matrix& baseToCam);
00081 static bool isShapeVisible(const ShapeRoot &ground_shape, const NEWMAT::Matrix& baseToCam, float maxDistanceSq);
00082
00083
00084
00085 const Shape<AgentData>& getAgent() const { return theAgent; }
00086
00087
00088 void setAgent(const Point &location, const AngTwoPi heading);
00089
00090
00091 void moveAgent(coordinate_t const local_dx, coordinate_t const local_dy, AngTwoPi dtheta);
00092
00093 vector<ShapeRoot> getShapes(const ShapeSpace& ShS, int minConf=2) const {
00094 const vector<ShapeRoot> allShapes = ShS.allShapes();
00095 if (&ShS == &camShS || &ShS == &groundShS || minConf <= 0)
00096 return allShapes;
00097 vector<ShapeRoot> nonNoiseShapes;
00098 for (vector<ShapeRoot>::const_iterator it = allShapes.begin();
00099 it != allShapes.end(); it++)
00100 if ((*it)->getConfidence() >= minConf)
00101 nonNoiseShapes.push_back(*it);
00102 return nonNoiseShapes;
00103 }
00104
00105 const vector<Point>& getGazePts() const { return cur->gazePts; }
00106 static bool returnTrue() { return true; }
00107 static bool returnFalse() { return false; }
00108
00109
00110
00111 vector<Shape<LineData> >
00112 getCamLines(const Sketch<uchar>&, const set<int>& objectColors,
00113 const set<int>& occluderColors) const ;
00114 vector<Shape<PolygonData> >
00115 getCamPolygons(const Sketch<uchar>&, const set<int>& objectColors,
00116 const set<int>& occluderColors) const ;
00117 vector<Shape<EllipseData> >
00118 getCamEllipses(const Sketch<uchar>&, const set<int>& objectColors,
00119 const set<int>& occluderColors) const ;
00120 vector<Shape<SphereData> >
00121 getCamSpheres(const Sketch<uchar>&, const set<int>& objectColors,
00122 const set<int>& occluderColors) const ;
00123 vector<Shape<LineData> >
00124 getCamWalls(const Sketch<uchar>&, unsigned int) const ;
00125 vector<Shape<BlobData> >
00126 getCamBlobs(const Sketch<uchar>& sketch, const set<int>& objectColors) const {
00127 return BlobData::extractBlobs(sketch, objectColors);
00128 }
00129
00130
00131 static Shape<BlobData> formBoundingBox(coordinate_t left, coordinate_t right,
00132 coordinate_t front, coordinate_t rear) {
00133 return Shape<BlobData>
00134 (new BlobData(VRmixin::groundShS, Point(front,left), Point(front,right),
00135 Point(rear,right), Point(rear,left), fabs((left-right)*(front-rear)),
00136 vector<BlobData::run>(), BlobData::groundplane, rgb()));
00137 }
00138
00139 void importLocalToWorld();
00140 void importWorldToLocal(const ShapeRoot &shape);
00141
00142 static void matchSrcToDst(ShapeSpace &src, ShapeSpace &dst, set<int> polygonEdgeColors=set<int>(),
00143 bool mergeSrc=true, bool mergeDst=true);
00144
00145 protected:
00146
00147 void scan(ScanRequest& req);
00148 void storeImageAt(const Point& pt);
00149 void storeImage();
00150
00151
00152 void defineGazePts(vector<Point>&, const ShapeRoot& area, bool doScan);
00153 void defineGazePts(vector<Point>& gazePts, const vector<Point>& corners, float meshSize);
00154
00155 void getCameraShapes(const Sketch<uchar>& camFrame);
00156 void getCamBlobs();
00157
00158 void extendLocal(const NEWMAT::Matrix& baseToCam);
00159 void extendWorld(const NEWMAT::Matrix& baseToCam);
00160
00161
00162 void removeNoise(ShapeSpace&, const NEWMAT::Matrix& baseToCam);
00163
00164 void removeGazePts(vector<Point>&, const NEWMAT::Matrix& baseToCam);
00165
00166
00167 bool determineNextGazePoint();
00168
00169 bool determineNextGazePoint(const vector<ShapeRoot>&);
00170
00171 bool determineNextGazePoint(vector<Point> &gazePts);
00172
00173 void moveToNextGazePoint(const bool manualOverride=false);
00174
00175
00176 bool isBadGazePoint(const Point&) const ;
00177 void projectToGround(const NEWMAT::Matrix& camToBase);
00178 void filterGroundShapes(const NEWMAT::Matrix& baseToCam);
00179
00180
00181 void calculateGroundPlane(const MapBuilderRequest::GroundPlaneAssumption_t& gpa);
00182
00183 private:
00184 MapBuilder(const MapBuilder&);
00185 MapBuilder& operator=(const MapBuilder&);
00186 };
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296 }
00297
00298 #endif
00299