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

ArMap.h

00001 /*
00002 MobileRobots Advanced Robotics Interface for Applications (ARIA)
00003 Copyright (C) 2004, 2005 ActivMedia Robotics LLC
00004 Copyright (C) 2006, 2007 MobileRobots Inc.
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 MobileRobots for information about a commercial version of ARIA at 
00022 robots@mobilerobots.com or 
00023 MobileRobots Inc, 19 Columbia Drive, Amherst, NH 03031; 800-639-9481
00024 */
00025 
00026 #ifndef ARMAP_H
00027 #define ARMAP_H
00028 
00029 #include "ariaTypedefs.h"
00030 #include "ariaUtil.h"
00031 #include "ArFunctor.h"
00032 #include "ArArgumentBuilder.h"
00033 #include "ArMutex.h"
00034 
00035 #include <vector>
00036 
00037 class ArFileParser;
00038 
00039 class ArMapObject;
00040 
00042 
00397 class ArMap
00398 {
00399   public:
00400 
00402   enum InfoType {
00403       // If any InfoType is added, then the ourInfoNames array must also be updated...
00404       //MAP_INFO,
00405     META_INFO,
00406       TASK_INFO,  
00407       ROUTE_INFO, 
00408     SCHED_TASK_INFO,
00409     SCHED_INFO,
00410       LAST_INFO = SCHED_INFO 
00411   };
00412 
00413   enum {
00414       INFO_COUNT = LAST_INFO + 1 
00415   };
00416 
00417 
00419   AREXPORT ArMap(const char *baseDirectory = "./",
00420          bool addToGlobalConfig = true, 
00421          const char *configSection = "Files",
00422          const char *configParam = "Map",
00423          const char *configDesc = 
00424          "Map of the environment we'll use to navigate",
00425          bool ignoreEmptyFileName = true);
00427   AREXPORT virtual ~ArMap(void);
00428 
00430   AREXPORT bool readFile(const char *fileName, 
00431              char *errorBuffer = NULL, size_t errorBufferLen = 0);
00432 
00434   AREXPORT bool writeFile(const char *fileName, bool internalCall = false);
00435 
00437   AREXPORT void addMapChangedCB(ArFunctor *functor, 
00438                 ArListPos::Pos position = ArListPos::LAST);
00440   AREXPORT void remMapChangedCB(ArFunctor *functor);
00441 
00443   AREXPORT const char *getBaseDirectory(void) const;
00445   AREXPORT const char *getFileName(void) const;
00447   void setIgnoreEmptyFileName(bool ignore) { myIgnoreEmptyFileName = ignore; }
00449   bool getIgnoreEmptyFileName(void) { return myIgnoreEmptyFileName; }
00451   AREXPORT void setBaseDirectory(const char *baseDirectory);
00453   std::list<ArMapObject *> *getMapObjects(void) { return &myMapObjects; }
00454 
00456   AREXPORT ArMapObject *findFirstMapObject(const char *name, const char *type);
00457 
00458 
00460   std::list<ArArgumentBuilder *> *getMapInfo(void) { return &myMapInfo; }
00461 
00463 
00469   AREXPORT std::list<ArArgumentBuilder *> *getInfo(int infoType);
00470 
00472   std::vector<ArPose> *getPoints(void) { return &myPoints; }
00474   std::vector<ArLineSegment> *getLines(void) { return &myLines; }
00476   ArPose getMinPose(void) { return myMin; }
00478   ArPose getMaxPose(void) { return myMax; }
00480   int getNumPoints(void) { return myNumPoints; }
00482   ArPose getLineMinPose(void) { return myLineMin; }
00484   ArPose getLineMaxPose(void) { return myLineMax; }
00486   int getNumLines(void) { return myNumLines; }
00488   int getResolution(void) { return myResolution; }
00490   AREXPORT ArMapObject *findMapObject(const char *name, 
00491                       const char *type = NULL);
00493   ArTime getMapObjectsChanged(void) { return myMapObjectsChanged; }
00495   ArTime getPointsChanged(void) { return myPointsChanged; }
00497   ArTime getLinesChanged(void) { return myLinesChanged; }
00498 
00500   ArTime getMapInfoChanged(void) { return myMapInfoChanged; }
00501 
00503 
00508   AREXPORT ArTime getInfoChanged(int infoType); 
00509 
00511   AREXPORT void setMapObjects(const std::list<ArMapObject *> *mapObjects); 
00513   AREXPORT void setPoints(const std::vector<ArPose> *points); 
00515   AREXPORT void setLines(const std::vector<ArLineSegment> *lines);
00516 
00518   AREXPORT void setMapInfo(const std::list<ArArgumentBuilder *> *mapInfo);
00519 
00521 
00529   AREXPORT bool setInfo(int infoType,
00530                         const std::list<ArArgumentBuilder *> *infoList); 
00531 
00532   AREXPORT void setResolution(int resolution);
00533       
00534   /***
00536   AREXPORT void setRouteInfo(const std::list<ArArgumentBuilder *> *routeInfo); 
00537   ****/
00538 
00540   AREXPORT void mapChanged(void);
00541 
00543   AREXPORT void writeToFunctor(ArFunctor1<const char *> *functor, 
00544                    const char *endOfLineChars);
00545 
00546 
00548   AREXPORT bool parseLine(char *line);
00550   AREXPORT void parsingComplete(void);
00551 
00553   AREXPORT int lock() {return(myMutex.lock());}
00555   AREXPORT int tryLock() {return(myMutex.tryLock());}
00557   AREXPORT int unlock() {return(myMutex.unlock());}
00558 
00559   
00560 
00561   // When loading a map, returns whether all header, objects, and lines have completed loading.
00566   AREXPORT bool isLoadingDataStarted() {return myLoadingDataStarted;}
00567 
00568   // When loading a map, returns whether all header and objects have completed loading.
00573   AREXPORT bool isLoadingLinesAndDataStarted() 
00574                {return myLoadingLinesAndDataStarted;}
00575 
00577 
00581   AREXPORT void writeObjectsToFunctor
00582                     (ArFunctor1<const char *> *functor, 
00583                      const char *endOfLineChars);
00584 
00586 
00591   AREXPORT void writePointsToFunctor
00592             (ArFunctor2<int, std::vector<ArPose> *> *functor);
00594 
00598   AREXPORT void writeLinesToFunctor
00599         (ArFunctor2<int, std::vector<ArLineSegment> *> *functor);
00600 
00602   AREXPORT bool readDataPoint( char *line);
00604   AREXPORT bool readLineSegment( char *line);
00606   AREXPORT void loadDataPoint(double x, double y);
00608   AREXPORT void loadLineSegment(double x1, double y1, double x2, double y2);
00610   AREXPORT void setMapChangedLogLevel(ArLog::LogLevel level) 
00611     {  myMapChangedLogLevel = level; }
00613   AREXPORT ArLog::LogLevel getMapChangedLogLevel(void)
00614     {  return myMapChangedLogLevel; }
00616   AREXPORT void addPreMapChangedCB(ArFunctor *functor,
00617                                   ArListPos::Pos position = ArListPos::LAST);
00619   AREXPORT void remPreMapChangedCB(ArFunctor *functor);
00620 
00621 protected:
00622 
00623 
00624   // Function for processing the config file
00625   bool processFile(char *errorBuffer, size_t errorBufferLen);
00626   // Function to read the 2D-Map
00627   bool handle2DMap(ArArgumentBuilder *arg);
00628   // Function to read the minimum pos
00629   bool handleMinPos(ArArgumentBuilder *arg);
00630   // Function to read the maximum pos
00631   bool handleMaxPos(ArArgumentBuilder *arg);
00632   // Function to read the number of points
00633   bool handleNumPoints(ArArgumentBuilder *arg);
00634   // Function to read the line minimum pos
00635   bool handleLineMinPos(ArArgumentBuilder *arg);
00636   // Function to read the line maximum pos
00637   bool handleLineMaxPos(ArArgumentBuilder *arg);
00638   // Function to read the number of lines
00639   bool handleNumLines(ArArgumentBuilder *arg);
00640   // Function to handle the resolution
00641   bool handleResolution(ArArgumentBuilder *arg);
00642   // Function to handle the cairns
00643   bool handleMapObject(ArArgumentBuilder *arg);
00644 
00645   bool handleInfo(ArArgumentBuilder *arg, int info);
00646 
00647   // Function to handle the information about the map
00648   bool handleMapInfo(ArArgumentBuilder *arg);
00649 
00650   // Function to catch the LINES line signifying data
00651   bool handleLines(ArArgumentBuilder *arg);
00652   // Function to catch the DATA line signifying data
00653   bool handleData(ArArgumentBuilder *arg);
00654   // Function to snag the map points (mainly for the getMap over the network)
00655   bool handlePoint(ArArgumentBuilder *arg);
00656   // Function to snag the line segments (mainly for the getMap over the network)
00657   bool handleLine(ArArgumentBuilder *arg);
00658 
00660 
00667   virtual const char *getInfoName(int infoType);
00668 
00670   static const char *ourInfoNames[INFO_COUNT];
00671 
00672   enum {
00673     MAX_MAP_NAME_LENGTH = 512
00674   };
00675 
00676   // lock for our data
00677   ArMutex myMutex;
00678   // resets the parser functions and variables (true if good, false otherwise)
00679   AREXPORT bool reset(void);
00680   std::string myBaseDirectory;
00681   std::string myFileName;
00682   struct stat myReadFileStat;
00683   ArFileParser *myLoadingParser;
00684 
00686   int myNumInfos;
00687 
00688   // vars for if we got some important info, the other important info
00689   // is taken care of by the adding of callbacks
00690   bool myLoadingGot2DMap;
00691   bool myLoadingGotMaxPos;
00692   bool myLoadingGotMinPos;
00693   bool myLoadingGotLineMaxPos;
00694   bool myLoadingGotLineMinPos;
00695   ArPose myLoadingMaxFromFile;
00696   ArPose myLoadingMinFromFile;
00697   int myLoadingPointsRead;
00698   ArPose myLoadingLineMaxFromFile;
00699   ArPose myLoadingLineMinFromFile;
00700   int myLoadingLinesRead;
00701 
00702   std::string myConfigParam;
00703   bool myIgnoreEmptyFileName;
00704   // data from the file
00705   std::list<ArMapObject *> myLoadingMapObjects;
00706   std::vector<ArPose> myLoadingPoints;
00707   std::vector<ArLineSegment> myLoadingLines;
00708 
00709   std::list<ArArgumentBuilder *> *myLoadingInfoArray;
00710   std::list<ArArgumentBuilder *> myLoadingMapInfo;
00711   int myLoadingNumPoints;
00712   int myLoadingNumLines;
00713   int myLoadingResolution;
00714   ArPose myLoadingMax;
00715   ArPose myLoadingMin;
00716   ArPose myLoadingLineMax;
00717   ArPose myLoadingLineMin;
00718 
00719   // our good data in memory (could be the same thing)
00720   std::list<ArMapObject *> myMapObjects;
00721   ArTime myMapObjectsChanged;
00722   std::vector<ArPose> myPoints;
00723   ArTime myPointsChanged;
00724 
00725   std::vector<ArLineSegment> myLines;
00726   ArTime myLinesChanged;
00727 
00728   std::list<ArArgumentBuilder *> *myInfoArray;
00729   ArTime *myInfoChangedArray;
00730 
00731   std::list<ArArgumentBuilder *> myMapInfo;
00732   ArTime myMapInfoChanged;
00733 
00734 
00735   int myNumPoints;
00736   int myNumLines;
00737   int myResolution;
00738   ArPose myMax;
00739   ArPose myMin;
00740   ArPose myLineMax;
00741   ArPose myLineMin;
00742 
00743 
00744   std::list<ArFunctor *> myMapChangedCBList;
00745   std::list<ArFunctor *> myPreMapChangedCBList;
00746   ArTime myMapChangedMapObjects;
00747   ArTime myMapChangedPoints;
00748 
00749   ArTime *myMapChangedInfoArray;
00750   ArTime myMapChangedMapInfo;
00751 
00752 
00753   // things for our config
00754   bool myConfigProcessedBefore;
00755   char myConfigMapName[MAX_MAP_NAME_LENGTH];
00756 
00757   bool myLoadingDataStarted;
00758   bool myLoadingLinesAndDataStarted;
00759   ArLog::LogLevel myMapChangedLogLevel;
00760   // callbacks
00761   ArRetFunctor1C<bool, ArMap, ArArgumentBuilder *> my2DMapCB;
00762   ArRetFunctor1C<bool, ArMap, ArArgumentBuilder *> myMinPosCB;
00763   ArRetFunctor1C<bool, ArMap, ArArgumentBuilder *> myMaxPosCB;
00764   ArRetFunctor1C<bool, ArMap, ArArgumentBuilder *> myNumPointsCB;
00765   ArRetFunctor1C<bool, ArMap, ArArgumentBuilder *> myLineMinPosCB;
00766   ArRetFunctor1C<bool, ArMap, ArArgumentBuilder *> myLineMaxPosCB;
00767   ArRetFunctor1C<bool, ArMap, ArArgumentBuilder *> myNumLinesCB;
00768   ArRetFunctor1C<bool, ArMap, ArArgumentBuilder *> myResolutionCB;
00769   ArRetFunctor1C<bool, ArMap, ArArgumentBuilder *> myMapObjectCB;
00770 
00771 
00772   ArRetFunctor1C<bool, ArMap, ArArgumentBuilder *> myMapInfoCB;
00773   ArRetFunctor2C<bool, ArMap, ArArgumentBuilder *, int> **myInfoCBArray;
00774 
00775   ArRetFunctor1C<bool, ArMap, ArArgumentBuilder *> myDataCB;
00776   ArRetFunctor1C<bool, ArMap, ArArgumentBuilder *> myLinesCB;
00777   ArRetFunctor1C<bool, ArMap, ArArgumentBuilder *> myPointCB;
00778   ArRetFunctor1C<bool, ArMap, ArArgumentBuilder *> myLineCB;
00779   ArRetFunctor2C<bool, ArMap, char *, size_t> myProcessFileCB;
00780 };
00781 
00783 class ArMapObject
00784 {
00785 public: 
00787   AREXPORT ArMapObject(const char *type, ArPose pose, const char *fileName,
00788                const char *iconName, const char *name,
00789                bool hasFromTo, ArPose fromPose, ArPose toPose)
00790     {
00791       if (type != NULL) myType = type; 
00792       if (name != NULL) myName = name; 
00793       myPose = pose; 
00794       if (iconName != NULL) myIconName = iconName;
00795       if (fileName != NULL) myFileName = fileName;
00796       myHasFromTo = hasFromTo; myFromPose = fromPose;  myToPose = toPose;
00797       if (myHasFromTo)
00798       {
00799        double angle = myPose.getTh();
00800        double sa = ArMath::sin(angle);
00801        double ca = ArMath::cos(angle);
00802        double fx = fromPose.getX();
00803        double fy = fromPose.getY();
00804        double tx = toPose.getX();
00805        double ty = toPose.getY();
00806        ArPose P0((fx*ca - fy*sa), (fx*sa + fy*ca));
00807        ArPose P1((tx*ca - fy*sa), (tx*sa + fy*ca));
00808        ArPose P2((tx*ca - ty*sa), (tx*sa + ty*ca));
00809        ArPose P3((fx*ca - ty*sa), (fx*sa + ty*ca));
00810        myFromToSegments.push_back(ArLineSegment(P0, P1));
00811        myFromToSegments.push_back(ArLineSegment(P1, P2));
00812        myFromToSegments.push_back(ArLineSegment(P2, P3));
00813        myFromToSegments.push_back(ArLineSegment(P3, P0));
00814       }
00815     }
00817   AREXPORT ArMapObject(const ArMapObject &mapObject) 
00818     {
00819       myType = mapObject.myType; myName = mapObject.myName; 
00820       myPose = mapObject.myPose; myIconName = mapObject.myIconName;
00821       myFileName = mapObject.myFileName;  myHasFromTo = mapObject.myHasFromTo;
00822       myFromPose = mapObject.myFromPose; myToPose = mapObject.myToPose;
00823       myFromToSegments = mapObject.myFromToSegments;
00824     }
00825 
00827   AREXPORT virtual ~ArMapObject() {}
00829   const char *getType(void) const { return myType.c_str(); }
00831   ArPose getPose(void) const { return myPose; }
00833   const char *getFileName(void) const { return myFileName.c_str(); }
00835   const char *getIconName(void) const { return myIconName.c_str(); }
00837   const char *getName(void) const { return myName.c_str(); }
00839   bool hasFromTo(void) const { return myHasFromTo; }
00841   ArPose getFromPose(void) const { return myFromPose; }
00843   ArPose getToPose(void) const { return myToPose; }
00844   void log(void) { 
00845     if (myHasFromTo)
00846       ArLog::log(ArLog::Terse, 
00847          "Cairn: %s %g %g %g \"%s\" %s \"%s\" %d %d %d %d",
00848          getType(), myPose.getX(), myPose.getY(), myPose.getTh(), 
00849          getFileName(), getIconName(), getName(), myFromPose.getX(),
00850          myFromPose.getY(), myToPose.getX(), myToPose.getY());
00851     else
00852       ArLog::log(ArLog::Terse, "Cairn: %s %g %g %g \"%s\" %s \"%s\"",
00853          getType(), myPose.getX(), myPose.getY(), myPose.getTh(), 
00854          getFileName(), getIconName(), getName());
00855   }
00857 
00868   std::list<ArLineSegment> getFromToSegments(void)
00869     {
00870       return myFromToSegments;
00871     }
00872 
00873   /*
00874   Don't use these, silently changing these values may screw up
00875   things that assume they don't change.
00876   void setPose(const ArPose& newpose) { myPose = newpose; }
00877   void setFromPose(const ArPose& newpose) { myFromPose = newpose; }
00878   void setToPose(const ArPose& newpose) { myToPose = newpose; }
00879   */
00880 protected:
00881   std::string myType;
00882   std::string myName;
00883   ArPose myPose;
00884   std::string myFileName;
00885   std::string myIconName;
00886   bool myHasFromTo;
00887   ArPose myFromPose;
00888   ArPose myToPose;
00889   std::list<ArLineSegment> myFromToSegments;
00890 };
00891 
00892 
00893 #endif // ARMAP_H

Generated on Tue Feb 20 10:51:40 2007 for Aria by  doxygen 1.4.0