internal_utils.cpp
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <assert.h>
00004 #include "internal_utils.hpp"
00005 
00006 namespace fovis
00007 {
00008 
00009 bool
00010 optionsGetInt(const VisualOdometryOptions& options, std::string name,
00011     int* result)
00012 {
00013   VisualOdometryOptions::const_iterator iter = options.find(name);
00014   if(iter == options.end()) {
00015     fprintf(stderr, "Option [%s] not specified!\n", name.c_str());
00016     return false;
00017   }
00018   std::string val = iter->second;
00019   char* eptr = NULL;
00020   int v = strtol(val.c_str(), &eptr, 0);
00021   if(*eptr != '\0') {
00022     fprintf(stderr, "Illegal value of [%s] for integer option [%s]\n",
00023         val.c_str(), name.c_str());
00024     return false;
00025   }
00026   *result = v;
00027   return true;
00028 }
00029 
00030 bool
00031 optionsGetBool(const VisualOdometryOptions& options, std::string name,
00032     bool* result)
00033 {
00034   VisualOdometryOptions::const_iterator iter = options.find(name);
00035   if(iter == options.end()) {
00036     fprintf(stderr, "Option [%s] not specified!\n", name.c_str());
00037     return false;
00038   }
00039   std::string val = iter->second;
00040   std::string val_copy(val);
00041   // trim string and ignore case.
00042   val_copy.erase(std::remove_if(val_copy.begin(), val_copy.end(), ::isspace), val_copy.end());
00043   // ignore case.
00044   std::transform(val_copy.begin(), val_copy.end(), val_copy.begin(), ::tolower);
00045   if (val_copy == "true") {
00046     *result = true;
00047   } else if (val_copy == "false") {
00048     *result = false;
00049   } else {
00050     fprintf(stderr, "Illegal value of [%s] for boolean option [%s]\n",
00051         val.c_str(), name.c_str());
00052     return false;
00053   }
00054   return true;
00055 }
00056 
00057 int
00058 optionsGetIntOrFromDefault(const VisualOdometryOptions& options, std::string name,
00059     const VisualOdometryOptions& defaults)
00060 {
00061   int result;
00062   if(optionsGetInt(options, name, &result))
00063     return result;
00064   bool got_opt = optionsGetInt(defaults, name, &result);
00065   if(!got_opt) {
00066     assert(false);
00067   }
00068   fprintf(stderr, "Using default value of [%d] for option [%s]\n", result, name.c_str());
00069   return result;
00070 }
00071 
00072 bool
00073 optionsGetBoolOrFromDefault(const VisualOdometryOptions& options, std::string name,
00074     const VisualOdometryOptions& defaults)
00075 {
00076   bool result;
00077   if(optionsGetBool(options, name, &result))
00078     return result;
00079   bool got_opt = optionsGetBool(defaults, name, &result);
00080   if(!got_opt) {
00081     assert(false);
00082   }
00083   fprintf(stderr, "Using default value of [%s] for option [%s]\n", (result? "true" : "false"), name.c_str());
00084   return result;
00085 }
00086 
00087 bool
00088 optionsGetDouble(const VisualOdometryOptions& options, std::string name,
00089     double* result)
00090 {
00091   VisualOdometryOptions::const_iterator iter = options.find(name);
00092   if(iter == options.end()) {
00093     fprintf(stderr, "Option [%s] not specified!\n", name.c_str());
00094     return false;
00095   }
00096   std::string val = iter->second;
00097   char* eptr = NULL;
00098   double v = strtod(val.c_str(), &eptr);
00099   if(*eptr != '\0') {
00100     fprintf(stderr, "Illegal value of [%s] for float option [%s]\n",
00101         val.c_str(), name.c_str());
00102     return false;
00103   }
00104   *result = v;
00105   return true;
00106 }
00107 
00108 double
00109 optionsGetDoubleOrFromDefault(const VisualOdometryOptions& options, std::string name,
00110     const VisualOdometryOptions& defaults)
00111 {
00112   double result;
00113   if(optionsGetDouble(options, name, &result))
00114     return result;
00115   bool got_opt = optionsGetDouble(defaults, name, &result);
00116   if(!got_opt) {
00117     assert(false);
00118   }
00119   fprintf(stderr, "Using default value of [%f] for option [%s]\n", result, name.c_str());
00120   return result;
00121 }
00122 
00123 }


libfovis
Author(s): Albert Huang, Maurice Fallon
autogenerated on Thu Jun 6 2019 20:16:12