Extract2dMap.cpp
Go to the documentation of this file.
00001 
00049 #include <stdexcept>
00050 #include <getopt.h>
00051 #include "MapExtractor.h"
00052 
00053 using namespace std;
00054 using namespace roboearth;
00055 
00056 #define USAGE "\nUsage: \n" \
00057                 "This program takes a 3d slice out of an Octomap and performs\n" \
00058                 "a 2d projection (if parameters min_z and max_z are given) or\n" \
00059                 "extracts a 2d map at height min_z (if max_z is missing)\n" \
00060                 "and saves the result as an image. Related meta data \n" \
00061                 "get stored in a seperate yaml file.\n" \
00062                 "\nOptions:\n" \
00063                 "-h, --help \tshow this description\n" \
00064                 "-o, --octomap \t3d octomap to read and work on\n" \
00065                 "-z, --min_z \tminimum height to consider for 2d projection\n" \
00066                 "\t\t/ height to extract 2d map at\n" \
00067                 "-Z, --max_z \tmaximum height to consider for 2d projection\n" \
00068                 "-n, --name \tfilename without extension used for storing \n" \
00069                 "\t\tresulting 2d image (.pgm) and meta data (.yaml)\n" \
00070                 "\nExamples:\n" \
00071                 "extract2dMap -z 0.303 -o hospital_room.bt -n hr_303\n" \
00072                 "extract2dMap -z 0.2 -Z 1.5 -o hospital_room.bt -n hr_2to15\n" \
00073                 "extract2dMap --min_z 0.2 --max_z 1.5 --octomap hospital_room.bt " \
00074                 "--name hr_2to15\n"
00075 
00076 int main(int argc, char** argv) {
00077 
00078         cout << endl;
00079 
00080         string inOctomap("");
00081         string outMapname("");
00082 
00083         double minZ = -std::numeric_limits<double>::max();
00084         double maxZ = std::numeric_limits<double>::max();
00085 
00086         char* ePtr;
00087         bool usage = false;
00088 
00089         static const struct option long_options[] = {
00090                         { "help", no_argument, 0, 'h' },
00091                         { "min_z", required_argument, 0, 'z' },
00092                         { "max_z", required_argument, 0, 'Z' },
00093                         { "octomap", required_argument, 0, 'o' },
00094                         { "name", required_argument, 0, 'n' },
00095                         { 0,0,0,0 }
00096         };
00097 
00098         bool minZset = false;
00099         bool maxZset = false;
00100         
00101         /* parse options */
00102         while (optind < argc) {
00103 
00104                 int index = -1;
00105                 struct option * opt = 0;
00106 
00107                 int result = getopt_long(argc, argv, "ho:z:Z:n:", long_options, &index);
00108                 if (result == -1) {
00109                         break;
00110                 }
00111 
00112                 switch (result) {
00113                 case 'h':
00114                         usage = true;
00115                         break;
00116                 case 'o':
00117                         inOctomap = optarg;
00118                         break;
00119                 case 'z':
00120                         minZ = strtod(optarg, &ePtr);
00121                         minZset = true;
00122                         if (*ePtr != '\0') {
00123                                 cout << "ERROR: value for 'min_z' could not be parsed: " << optarg << endl;
00124                                 usage = true;
00125                         }
00126                         break;
00127                 case 'Z':
00128                         maxZ = strtod(optarg, &ePtr);
00129                         maxZset = true;
00130                         if (*ePtr != '\0') {
00131                                 cout << "ERROR: value for 'max_z' could not be parsed: " << optarg << endl;
00132                                 usage = true;
00133                         }
00134                         break;
00135                 case 'n':
00136                         outMapname = optarg;
00137                         break;
00138                 default:
00139                         return 1;
00140                 }
00141         }
00142         /* print all other parameters */
00143         while (optind < argc) {
00144                 cout << "INFO: ignoring other parameter: " << argv[optind++] << endl;
00145         }
00146 
00147         if (inOctomap.empty()) {
00148                 cout << "ERROR: no 3d octomap specified (-o / --octomap)" << endl;
00149                 usage = true;
00150         }
00151         if (outMapname.empty()) {
00152                 cout << "ERROR: name for output map not specified (-n / --name)" << endl;
00153                 usage = true;
00154         }
00155         if (!minZset) {
00156                 cout << "ERROR: min_z has to be specified (-z / --min_z)" << endl;
00157                 usage = true;
00158         }
00159         if (minZ > maxZ) {
00160                 cout << "ERROR: min_z is larger than max_z (min_z:" << minZ << ", max_z:" << maxZ << ")" << endl;
00161                 usage = true;
00162         }
00163 
00164         if (usage) {
00165 
00166                 cout << USAGE << endl;
00167                 return 1;
00168 
00169         } else {
00170 
00171                 try {
00172 
00173                         MapExtractor mapEx(inOctomap); // load 3d octomap
00174 
00175                         cout << endl << "Exporting 2d map '" << outMapname.c_str() << "'";
00176                         cout << endl;
00177 
00178                         bool success = false;
00179                         if (!maxZset) {
00180                           success = mapEx.save2dMap(outMapname, minZ, true); // save 2dmap
00181                         } else {
00182                           success = mapEx.save2dMap(outMapname, minZ, maxZ, true); // save projected 2dmap
00183                         }
00184                                                 
00185                         if (success) {
00186                                 cout << "done." << endl;
00187                         } else {
00188                                 cout << "failed." << endl;
00189                         }
00190 
00191                 } catch (runtime_error& e) {
00192                         cout << e.what() << endl << endl;
00193                         return 2;
00194                 }
00195 
00196         }
00197 
00198         cout << endl;
00199 
00200 }


re_2dmap_extractor
Author(s): Alexander Perzylo
autogenerated on Sun Jan 5 2014 11:28:08