Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

ArMap.h

00001 /*
00002 ActivMedia Robotics Interface for Applications (ARIA)
00003 Copyright (C) 2004,2005 ActivMedia Robotics, LLC
00004 
00005 
00006      This program is free software; you can redistribute it and/or modify
00007      it under the terms of the GNU General Public License as published by
00008      the Free Software Foundation; either version 2 of the License, or
00009      (at your option) any later version.
00010 
00011      This program is distributed in the hope that it will be useful,
00012      but WITHOUT ANY WARRANTY; without even the implied warranty of
00013      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014      GNU General Public License for more details.
00015 
00016      You should have received a copy of the GNU General Public License
00017      along with this program; if not, write to the Free Software
00018      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 
00020 If you wish to redistribute ARIA under different terms, contact 
00021 ActivMedia Robotics for information about a commercial version of ARIA at 
00022 robots@activmedia.com or 
00023 ActivMedia Robotics, 19 Columbia Drive, Amherst, NH 03031; 800-639-9481
00024 
00025 */
00026 
00027 #ifndef ARMAP_H
00028 #define ARMAP_H
00029 
00030 #include "ariaTypedefs.h"
00031 #include "ariaUtil.h"
00032 #include "ArFunctor.h"
00033 #include "ArArgumentBuilder.h"
00034 #include "ArMutex.h"
00035 
00036 #include <vector>
00037 
00038 class ArFileParser;
00039 
00040 class ArMapObject;
00041 
00043 
00280 class ArMap
00281 {
00282   public:
00283 
00285   enum InfoType {
00286       // If any InfoType is added, then the ourInfoNames array must also be updated...
00287           //MAP_INFO,
00288           TASK_INFO,  
00289           ROUTE_INFO, 
00290           LAST_INFO = ROUTE_INFO 
00291   };
00292 
00293   enum {
00294           INFO_COUNT = LAST_INFO + 1 
00295   };
00296 
00297 
00299   ArMap(const char *baseDirectory = "./",
00300                  bool addToGlobalConfig = true, 
00301                  const char *configSection = "Files",
00302                  const char *configParam = "Map",
00303                  const char *configDesc = 
00304                  "Map of the environment we'll use to navigate",
00305                  bool ignoreEmptyFileName = true);
00307   virtual ~ArMap(void);
00308 
00310   bool readFile(const char *fileName, 
00311                          char *errorBuffer = NULL, size_t errorBufferLen = 0);
00312 
00314   bool writeFile(const char *fileName);
00315 
00317   void addMapChangedCB(ArFunctor *functor, 
00318                                 ArListPos::Pos position = ArListPos::LAST);
00320   void remMapChangedCB(ArFunctor *functor);
00321 
00323   const char *getBaseDirectory(void) const;
00325   const char *getFileName(void) const;
00327   void setIgnoreEmptyFileName(bool ignore) { myIgnoreEmptyFileName = ignore; }
00329   bool getIgnoreEmptyFileName(void) { return myIgnoreEmptyFileName; }
00331   void setBaseDirectory(const char *baseDirectory);
00333   std::list<ArMapObject *> *getMapObjects(void) { return &myMapObjects; }
00334 
00335 
00337   std::list<ArArgumentBuilder *> *getMapInfo(void) { return &myMapInfo; }
00338 
00340 
00346   std::list<ArArgumentBuilder *> *getInfo(int infoType);
00347 
00349   std::vector<ArPose> *getPoints(void) { return &myPoints; }
00351   std::vector<ArLineSegment> *getLines(void) { return &myLines; }
00353   ArPose getMinPose(void) { return myMin; }
00355   ArPose getMaxPose(void) { return myMax; }
00357   int getNumPoints(void) { return myNumPoints; }
00359   ArPose getLineMinPose(void) { return myLineMin; }
00361   ArPose getLineMaxPose(void) { return myLineMax; }
00363   int getNumLines(void) { return myNumLines; }
00365   int getResolution(void) { return myResolution; }
00367   ArMapObject *findMapObject(const char *name, 
00368                                       const char *type = NULL);
00370   ArTime getMapObjectsChanged(void) { return myMapObjectsChanged; }
00372   ArTime getPointsChanged(void) { return myPointsChanged; }
00374   ArTime getLinesChanged(void) { return myLinesChanged; }
00375 
00377   ArTime getMapInfoChanged(void) { return myMapInfoChanged; }
00378 
00380 
00385   ArTime getInfoChanged(int infoType); 
00386 
00388   void setMapObjects(const std::list<ArMapObject *> *mapObjects); 
00390   void setPoints(const std::vector<ArPose> *points); 
00392   void setLines(const std::vector<ArLineSegment> *lines);
00393 
00395   void setMapInfo(const std::list<ArArgumentBuilder *> *mapInfo);
00396 
00398 
00406   bool setInfo(int infoType,
00407                                                 const std::list<ArArgumentBuilder *> *infoList); 
00408 
00409   void setResolution(int resolution);
00410           
00411   /***
00413   void setRouteInfo(const std::list<ArArgumentBuilder *> *routeInfo); 
00414   ****/
00415 
00417   void mapChanged(void);
00418 
00420   void writeToFunctor(ArFunctor1<const char *> *functor, 
00421                                const char *endOfLineChars);
00422 
00423 
00425   bool parseLine(char *line);
00427   void parsingComplete(void);
00428 
00430   int lock() {return(myMutex.lock());}
00432   int tryLock() {return(myMutex.tryLock());}
00434   int unlock() {return(myMutex.unlock());}
00435 
00436   
00437 
00438   // When loading a map, returns whether all header, objects, and lines have completed loading.
00443   bool isLoadingDataStarted() {return myLoadingDataStarted;}
00444 
00445   // When loading a map, returns whether all header and objects have completed loading.
00450   bool isLoadingLinesAndDataStarted() 
00451                {return myLoadingLinesAndDataStarted;}
00452 
00454 
00458   void writeObjectsToFunctor
00459                                         (ArFunctor1<const char *> *functor, 
00460                                  const char *endOfLineChars);
00461 
00463 
00468   void writePointsToFunctor
00469                         (ArFunctor2<int, std::vector<ArPose> *> *functor);
00471 
00475   void writeLinesToFunctor
00476                 (ArFunctor2<int, std::vector<ArLineSegment> *> *functor);
00477 
00479   bool readDataPoint( char *line);
00481   bool readLineSegment( char *line);
00483   void loadDataPoint(double x, double y);
00485   void loadLineSegment(double x1, double y1, double x2, double y2);
00487   void setMapChangedLogLevel(ArLog::LogLevel level) 
00488     {  myMapChangedLogLevel = level; }
00490   ArLog::LogLevel getMapChangedLogLevel(void)
00491     {  return myMapChangedLogLevel; }
00492 protected:
00493 
00494 
00495   // Function for processing the config file
00496   bool processFile(char *errorBuffer, size_t errorBufferLen);
00497   // Function to read the 2D-Map
00498   bool handle2DMap(ArArgumentBuilder *arg);
00499   // Function to read the minimum pos
00500   bool handleMinPos(ArArgumentBuilder *arg);
00501   // Function to read the maximum pos
00502   bool handleMaxPos(ArArgumentBuilder *arg);
00503   // Function to read the number of points
00504   bool handleNumPoints(ArArgumentBuilder *arg);
00505   // Function to read the line minimum pos
00506   bool handleLineMinPos(ArArgumentBuilder *arg);
00507   // Function to read the line maximum pos
00508   bool handleLineMaxPos(ArArgumentBuilder *arg);
00509   // Function to read the number of lines
00510   bool handleNumLines(ArArgumentBuilder *arg);
00511   // Function to handle the resolution
00512   bool handleResolution(ArArgumentBuilder *arg);
00513   // Function to handle the cairns
00514   bool handleMapObject(ArArgumentBuilder *arg);
00515 
00516   bool handleInfo(ArArgumentBuilder *arg, int info);
00517 
00518   // Function to handle the information about the map
00519   bool handleMapInfo(ArArgumentBuilder *arg);
00520 
00521   // Function to catch the LINES line signifying data
00522   bool handleLines(ArArgumentBuilder *arg);
00523   // Function to catch the DATA line signifying data
00524   bool handleData(ArArgumentBuilder *arg);
00525   // Function to snag the map points (mainly for the getMap over the network)
00526   bool handlePoint(ArArgumentBuilder *arg);
00527   // Function to snag the line segments (mainly for the getMap over the network)
00528   bool handleLine(ArArgumentBuilder *arg);
00529 
00531 
00538   virtual const char *getInfoName(int infoType);
00539 
00541   static const char *ourInfoNames[INFO_COUNT];
00542 
00543   // lock for our data
00544   ArMutex myMutex;
00545   // resets the parser functions and variables (true if good, false otherwise)
00546   bool reset(void);
00547   std::string myBaseDirectory;
00548   std::string myFileName;
00549   struct stat myReadFileStat;
00550   ArFileParser *myLoadingParser;
00551 
00553   int myNumInfos;
00554 
00555   // vars for if we got some important info, the other important info
00556   // is taken care of by the adding of callbacks
00557   bool myLoadingGot2DMap;
00558   bool myLoadingGotMaxPos;
00559   bool myLoadingGotMinPos;
00560   bool myLoadingGotLineMaxPos;
00561   bool myLoadingGotLineMinPos;
00562   ArPose myLoadingMaxFromFile;
00563   ArPose myLoadingMinFromFile;
00564   int myLoadingPointsRead;
00565   ArPose myLoadingLineMaxFromFile;
00566   ArPose myLoadingLineMinFromFile;
00567   int myLoadingLinesRead;
00568 
00569   std::string myConfigParam;
00570   bool myIgnoreEmptyFileName;
00571   // data from the file
00572   std::list<ArMapObject *> myLoadingMapObjects;
00573   std::vector<ArPose> myLoadingPoints;
00574   std::vector<ArLineSegment> myLoadingLines;
00575 
00576   std::list<ArArgumentBuilder *> *myLoadingInfoArray;
00577   std::list<ArArgumentBuilder *> myLoadingMapInfo;
00578   int myLoadingNumPoints;
00579   int myLoadingNumLines;
00580   int myLoadingResolution;
00581   ArPose myLoadingMax;
00582   ArPose myLoadingMin;
00583   ArPose myLoadingLineMax;
00584   ArPose myLoadingLineMin;
00585 
00586   // our good data in memory (could be the same thing)
00587   std::list<ArMapObject *> myMapObjects;
00588   ArTime myMapObjectsChanged;
00589   std::vector<ArPose> myPoints;
00590   ArTime myPointsChanged;
00591 
00592   std::vector<ArLineSegment> myLines;
00593   ArTime myLinesChanged;
00594 
00595   std::list<ArArgumentBuilder *> *myInfoArray;
00596   ArTime *myInfoChangedArray;
00597 
00598   std::list<ArArgumentBuilder *> myMapInfo;
00599   ArTime myMapInfoChanged;
00600 
00601 
00602   int myNumPoints;
00603   int myNumLines;
00604   int myResolution;
00605   ArPose myMax;
00606   ArPose myMin;
00607   ArPose myLineMax;
00608   ArPose myLineMin;
00609 
00610 
00611   std::list<ArFunctor *> myMapChangedCBList;
00612   ArTime myMapChangedMapObjects;
00613   ArTime myMapChangedPoints;
00614 
00615   ArTime *myMapChangedInfoArray;
00616   ArTime myMapChangedMapInfo;
00617 
00618 
00619   // things for our config
00620   bool myConfigProcessedBefore;
00621   char myConfigMapName[512];
00622 
00623   bool myLoadingDataStarted;
00624   bool myLoadingLinesAndDataStarted;
00625   ArLog::LogLevel myMapChangedLogLevel;
00626   // callbacks
00627   ArRetFunctor1C<bool, ArMap, ArArgumentBuilder *> my2DMapCB;
00628   ArRetFunctor1C<bool, ArMap, ArArgumentBuilder *> myMinPosCB;
00629   ArRetFunctor1C<bool, ArMap, ArArgumentBuilder *> myMaxPosCB;
00630   ArRetFunctor1C<bool, ArMap, ArArgumentBuilder *> myNumPointsCB;
00631   ArRetFunctor1C<bool, ArMap, ArArgumentBuilder *> myLineMinPosCB;
00632   ArRetFunctor1C<bool, ArMap, ArArgumentBuilder *> myLineMaxPosCB;
00633   ArRetFunctor1C<bool, ArMap, ArArgumentBuilder *> myNumLinesCB;
00634   ArRetFunctor1C<bool, ArMap, ArArgumentBuilder *> myResolutionCB;
00635   ArRetFunctor1C<bool, ArMap, ArArgumentBuilder *> myMapObjectCB;
00636 
00637 
00638   ArRetFunctor1C<bool, ArMap, ArArgumentBuilder *> myMapInfoCB;
00639   ArRetFunctor2C<bool, ArMap, ArArgumentBuilder *, int> **myInfoCBArray;
00640 
00641   ArRetFunctor1C<bool, ArMap, ArArgumentBuilder *> myDataCB;
00642   ArRetFunctor1C<bool, ArMap, ArArgumentBuilder *> myLinesCB;
00643   ArRetFunctor1C<bool, ArMap, ArArgumentBuilder *> myPointCB;
00644   ArRetFunctor1C<bool, ArMap, ArArgumentBuilder *> myLineCB;
00645   ArRetFunctor2C<bool, ArMap, char *, size_t> myProcessFileCB;
00646 };
00647 
00649 class ArMapObject
00650 {
00651 public: 
00653   ArMapObject(const char *type, ArPose pose, const char *fileName,
00654                        const char *iconName, const char *name,
00655                        bool hasFromTo, ArPose fromPose, ArPose toPose)
00656     {
00657       if (type != NULL) myType = type; 
00658       if (name != NULL) myName = name; 
00659       myPose = pose; 
00660       if (iconName != NULL) myIconName = iconName;
00661       if (fileName != NULL) myFileName = fileName;
00662       myHasFromTo = hasFromTo; myFromPose = fromPose;  myToPose = toPose;
00663     }
00665   ArMapObject(const ArMapObject &mapObject) 
00666     {
00667       myType = mapObject.myType; myName = mapObject.myName; 
00668       myPose = mapObject.myPose; myIconName = mapObject.myIconName;
00669       myFileName = mapObject.myFileName;  myHasFromTo = mapObject.myHasFromTo;
00670       myFromPose = mapObject.myFromPose; myToPose = mapObject.myToPose;
00671     }
00672 
00674   virtual ~ArMapObject() {}
00676   const char *getType(void) const { return myType.c_str(); }
00678   ArPose getPose(void) const { return myPose; }
00680   const char *getFileName(void) const { return myFileName.c_str(); }
00682   const char *getIconName(void) const { return myIconName.c_str(); }
00684   const char *getName(void) const { return myName.c_str(); }
00686   bool hasFromTo(void) const { return myHasFromTo; }
00688   ArPose getFromPose(void) const { return myFromPose; }
00690   ArPose getToPose(void) const { return myToPose; }
00691   void log(void) { 
00692     if (myHasFromTo)
00693       ArLog::log(ArLog::Terse, 
00694                  "Cairn: %s %g %g %g \"%s\" %s \"%s\" %d %d %d %d",
00695                  getType(), myPose.getX(), myPose.getY(), myPose.getTh(), 
00696                  getFileName(), getIconName(), getName(), myFromPose.getX(),
00697                  myFromPose.getY(), myToPose.getX(), myToPose.getY());
00698     else
00699       ArLog::log(ArLog::Terse, "Cairn: %s %g %g %g \"%s\" %s \"%s\"",
00700                  getType(), myPose.getX(), myPose.getY(), myPose.getTh(), 
00701                  getFileName(), getIconName(), getName());
00702   }
00703 protected:
00704   std::string myType;
00705   std::string myName;
00706   ArPose myPose;
00707   std::string myFileName;
00708   std::string myIconName;
00709   bool myHasFromTo;
00710   ArPose myFromPose;
00711   ArPose myToPose;
00712 };
00713 
00714 
00715 #endif // ARMAP_H

Generated on Wed Oct 19 12:56:36 2005 for Aria by  doxygen 1.4.0