datapairs.cpp
Go to the documentation of this file.
00001 
00018 #include "datapairs.h"
00019 //#include "manipulation/mathexpr.h"
00020 #include "muParser.h"
00021 
00022 #include "log.h"
00023 #include "utils.h"
00024 #include "vecmath.h"
00025 
00026 
00027 bool DataPairs::getItem(unsigned int index, std::vector<std::string> &item)
00028 {
00029     if (index >= values.size())
00030        return false;
00031     
00032     item = values[index];
00033     
00034     return true;
00035 }
00036       
00037 
00038 void DataPairs::copyFrom(DataPairs &other)
00039 {
00040      clear();
00041      for (unsigned int i=0; i<other.getLength(); i++)
00042      {
00043          std::vector<std::string> item ;
00044          if (other.getItem(i, item) && item.size() > 1)
00045           add(item[0], item[1]);
00046      }
00047 }
00048 
00049 void DataPairs::print()
00050 {
00051      for (unsigned int i=0; i<values.size(); i++)
00052      {
00053        LOG_VERBOSE("%s: ", values[i][0].c_str());
00054          for (unsigned int j=1; j<values[i].size(); j++)
00055            LOG_VERBOSE("%s ",  values[i][j].c_str());
00056          LOG_VERBOSE("\n");
00057      }
00058 }
00059 
00060 // sets max array size
00061 void DataPairs::setMaxSize(unsigned int size)
00062 {
00063     values.reserve(size);
00064 }
00065 
00066 // returns first index of item with name "str"
00067 // name[10] = "hund" value[10] = "dobermann"
00068 // name[12] = "hund" value[12] = "dackel"
00069 // -> getFirstIndex("hund") = 10
00070 int DataPairs::getFirstIndex(std::string str)
00071 {
00072     for (unsigned int i=0; i<values.size(); i++)
00073       if (values[i].size() > 0 && strcasecmp(values[i][0].c_str(), str.c_str()) == 0)
00074            return i;
00075            
00076     return -1;
00077 }
00078 
00079 // returns array with indize of all items with name "str"
00080 int DataPairs::getIndex(std::string str, std::vector<unsigned int> &ids)
00081 {
00082     ids.clear();
00083     
00084     for (unsigned int i=0; i<values.size(); i++)
00085         if (strcasecmp(values[i][0].c_str(), str.c_str())  == 0)
00086            ids.push_back(i);
00087           
00088     if (ids.size() == 0)
00089         return -1;
00090          
00091     return ids[0];
00092 }
00093 
00094 // below: get item with name "str" and return its value as type "Int", "Bool", etc 
00095 int DataPairs::getInt(std::string str, int def)
00096 {
00097     int id = getFirstIndex(str);
00098     
00099     if (id >= 0)
00100        return atoi(values[id][1].c_str());
00101        
00102     return def;     
00103 }
00104 
00105 float DataPairs::getFloat(std::string str, float def)
00106 {
00107     int id = getFirstIndex(str);
00108     
00109     if (id >= 0)
00110        return atof(values[id][1].c_str());
00111        
00112     return def; 
00113 }
00114 
00115 
00116 std::string DataPairs::getString(std::string str, std::string def)
00117 {
00118     int id = getFirstIndex(str);
00119     
00120     if (id >= 0)
00121        return values[id][1];
00122        
00123     return def; 
00124 }
00125 
00126 void DataPairs::clear()
00127 {
00128     values.clear();
00129 }
00130 
00131 unsigned int DataPairs::getLength()
00132 {
00133     return values.size();
00134 }
00135 
00136 // get all strings
00137 void DataPairs::getStrings(std::string str, std::vector<std::string> &result)
00138 {
00139     result.clear();
00140     std::vector<unsigned int> ids;
00141     getIndex(str, ids);
00142     
00143     for (unsigned int i=0;i<ids.size();i++)
00144         result.push_back(values[ids[i]][1]);
00145        
00146     return; 
00147 }
00148 
00149 
00150 bool DataPairs::getBool(std::string str, bool def)
00151 {
00152     int id = getFirstIndex(str);
00153     
00154     if (id >= 0)
00155        return strcasecmp(values[id][1].c_str(), "true") == 0;
00156        
00157     return def; 
00158 }
00159 
00160 void DataPairs::add(std::string n, std::string v, bool replace)
00161 {
00162   int id = getFirstIndex(n);
00163 
00164   if (id >= 0 && replace)
00165     {
00166       values[id].clear();
00167       values[id].push_back(n);
00168       values[id].push_back(v);
00169       return;
00170     }
00171   
00172   std::vector<std::string> newItem;
00173   newItem.push_back(n);
00174   newItem.push_back(v);
00175   
00176   values.push_back(newItem);
00177 }
00178 
00179 
00180 bool DataPairs::getIntValue(std::string str, int &result)
00181 {
00182   int id = getFirstIndex(str);
00183   if (id < 0)
00184     return false;
00185 
00186   try
00187     {
00188       result = (int) atof(values[id][1].c_str());
00189     }
00190   catch (...)
00191     {
00192       return false;
00193     }
00194   return true;
00195 }
00196 
00197 bool DataPairs::getFloatValue(std::string str, float &result)
00198 {
00199   int id = getFirstIndex(str);
00200   if (id < 0)
00201     return false;
00202 
00203   try
00204     {
00205       result = atof(values[id][1].c_str());
00206     }
00207   catch (...)
00208     {
00209       return false;
00210     }
00211   return true;
00212 }
00213 
00214 bool DataPairs::getStringValue(std::string str, std::string &result)
00215 {
00216   int id = getFirstIndex(str);
00217   if (id < 0)
00218     return false;
00219 
00220   result = values[id][1];
00221   return true;
00222 }
00223 
00224 double parseMathExprRandomDouble01()
00225 {
00226   return robotLibPbD::getUniform();
00227 }
00228 
00229 double parseMathExprRandomInteger(double first, double second)
00230 {
00231   return (double)robotLibPbD::getUniform((int) first, (int) second);
00232 }
00233 
00234 std::string parseMathExpr(std::string output)
00235 {
00236   using namespace mu;
00237 
00238   std::string result;
00239 
00240   std::vector<std::string> tokens;
00241   robotLibPbD::strtokenize(output, tokens, " ");
00242   
00243   Parser p;
00244   //p.DefineFun("rand", parseMathExprRandomInteger, false);
00245   //p.DefineFun("uniform", parseMathExprRandomDouble01, false);
00246 
00247   result = "";
00248   for (unsigned int i=0; i<tokens.size(); i++)
00249     {
00250       //printf("%s ->", tokens[i].c_str());
00251 
00252       try
00253         {
00254           p.SetExpr(tokens[i]);
00255           double value = p.Eval();
00256           //printf("Math: %s = %g\n", tokens[i].c_str(), value);
00257 
00258           tokens[i] = robotLibPbD::printToString("%g", value);
00259         }
00260       catch (Parser::exception_type &e)
00261         {
00262         }
00263       
00264       if (i+1 < tokens.size())
00265         result += tokens[i] + " ";
00266       else
00267         result += tokens[i];
00268     }
00269 
00270   return result;
00271 }
00272 
00273 bool DataPairs::getBoolValue(std::string str, bool &result)
00274 {
00275   int id = getFirstIndex(str);
00276   if (id < 0)
00277     return false;
00278 
00279   result = strcasecmp(values[id][1].c_str(), "true") == 0;
00280   return true;
00281 }
00282 
00283 
00284 void DataPairs::resolveString(std::string input, std::string &output)
00285 {
00286   char buffer[1024], buffer2[1024], buffer3[1024];
00287   sprintf(buffer, "%s", input.c_str());
00288   char *start, *end;
00289 
00290  
00291 
00292   start = &buffer[0];
00293   while (start != NULL && *start != '\0')
00294     {
00295       if (*start == '[')
00296         {
00297           bool replaced = false;
00298           start++;
00299           end = start;
00300           while (end != NULL && *end != '\0')
00301             {
00302               if (*end == ']')
00303                 {
00304                   *end = '\0';
00305                   sprintf(buffer3, "%s", start);
00306 
00307                   if (getFirstIndex(buffer3) >= 0)
00308                     sprintf(buffer3, "%s", getString(buffer3).c_str());
00309                   else
00310                     sprintf(buffer3, "%s", buffer3);
00311 
00312                   end++;
00313                   sprintf(buffer2, "%s", end);
00314 
00315                   start--;
00316                   sprintf(start, "%s%s", buffer3, buffer2);
00317                   
00318                   replaced = true;
00319                   break;
00320                 }
00321               end++;
00322             }
00323 
00324           if (replaced)
00325             {
00326               start = &buffer[0];
00327               continue;
00328             }
00329         }
00330 
00331       start++;
00332     }
00333 
00334   output = buffer;
00335   
00336   if (true)
00337     {
00338       output = parseMathExpr(output);
00339     }
00340 }
00341 
00342 
00343 std::string DataPairs::resolveString(std::string input)
00344 {
00345   std::string output;
00346   resolveString(input, output);
00347   return output;
00348 }
00349 
00350 void  DataPairs::resolve()
00351 {
00352   for (unsigned int i=0; i<values.size(); i++)
00353     for (unsigned int j=1; j<values[i].size(); j++)
00354       values[i][j] = resolveString(values[i][j]);
00355 }
00356 
00357 
00358 
00359 void DataPairs::loadFromXml(CConfiguration &config, TiXmlElement* node, DataPairs* replace)
00360 {
00361   std::vector<TiXmlElement*> result;
00362   config.findNodes("Value", result, node);
00363   for (unsigned int i=0; i<result.size(); i++)
00364     {
00365       std::string name, value;
00366       name = config.getAttributeString(result[i], "name", "");
00367       value = config.getAttributeString(result[i], "value", "");
00368 
00369       if (replace != NULL)
00370         {
00371           name = replace->resolveString(name);
00372           value = replace->resolveString(value);
00373         }
00374 
00375       if (name != "")
00376         add(name, value, true);
00377     }
00378 }


asr_kinematic_chain_optimizer
Author(s): Aumann Florian, Heller Florian, Jäkel Rainer, Wittenbeck Valerij
autogenerated on Sat Jun 8 2019 19:42:49