utils.cpp
Go to the documentation of this file.
00001 
00018 #include <stdio.h>
00019 #include <stdarg.h>
00020 #include <string.h>
00021 #include <vector>
00022 
00023 #include <sys/times.h>
00024 #include <sys/time.h>
00025 #include <sys/types.h>
00026 #include <dirent.h>
00027 #include <errno.h>
00028 #include <vector>
00029 #include <string>
00030 #include <iostream>
00031 
00032 #include <utils.h>
00033 
00034 namespace robotLibPbD {
00035 
00036 
00037   std::string waitForReturn(std::string msg)
00038   {
00039     std::cout << msg;
00040     char bChar[255];
00041     std::cin.getline(bChar, 255);
00042     std::string result = bChar;
00043     return result;
00044   }
00045 
00046   std::string combineStrings(std::vector<std::string> &input, std::string delimiter)
00047  {
00048    std::string tmp;
00049    for (unsigned int i=0; i<input.size(); i++)
00050      {
00051        tmp += input[i];
00052 
00053        if (i+1 < input.size())
00054          tmp += delimiter;
00055      }
00056 
00057    return tmp;
00058  }
00059 
00060 int getDirectoryFiles(std::string dir, std::vector<std::string> &files, bool appendDir)
00061 {
00062     DIR *dp;
00063     struct dirent *dirp;
00064     if((dp  = opendir(dir.c_str())) == NULL) 
00065       {
00066         std::cout << "Error(" << errno << ") opening " << dir << std::endl;
00067         return errno;
00068       }
00069 
00070     if (dir.length() > 0 && dir[dir.length()-1] != '/')
00071       dir = dir + "/";
00072     
00073     while ((dirp = readdir(dp)) != NULL) 
00074       {
00075         if (appendDir)
00076       files.push_back(dir + std::string(dirp->d_name));
00077         else
00078       files.push_back(std::string(dirp->d_name));
00079       }
00080     closedir(dp);
00081     return 0;
00082 }
00083 
00084 
00085 std::string boolToString(bool value){ if (value) return "true"; return "false"; }
00086 
00087 void strtokenize(const std::string& str,
00088                       std::vector<std::string>& tokens,
00089                       const std::string& delimiters)
00090 {
00091     // Skip delimiters at beginning.
00092     std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
00093     // Find first "non-delimiter".
00094     std::string::size_type pos     = str.find_first_of(delimiters, lastPos);
00095 
00096     while (std::string::npos != pos || std::string::npos != lastPos)
00097     {
00098         // Found a token, add it to the vector.
00099         tokens.push_back(str.substr(lastPos, pos - lastPos));
00100         // Skip delimiters.  Note the "not_of"
00101         lastPos = str.find_first_not_of(delimiters, pos);
00102         // Find next "non-delimiter"
00103         pos = str.find_first_of(delimiters, lastPos);
00104     }
00105 }
00106 
00107 //Ersetzt einen String
00108 //stringSearchString = string der mit stringReplaceString ersetzt wird
00109 //stringStringToReplace ist der string in dem gesucht und ersetzt wird
00110 std::string strreplace(const std::string &stringSearchString, const std::string &stringReplaceString, std::string stringStringToReplace)
00111 {
00112         std::string::size_type pos = stringStringToReplace.find(stringSearchString, 0);
00113         int intLengthSearch = stringSearchString.length();
00114         int intLengthReplacment = stringReplaceString.length();
00115 
00116         while(std::string::npos != pos)
00117         {
00118                 stringStringToReplace.replace(pos, intLengthSearch, stringReplaceString);
00119                 pos = stringStringToReplace.find(stringSearchString, pos + intLengthReplacment);
00120         }
00121 
00122         return stringStringToReplace;
00123 } 
00124 
00125 void strreplaceWithCondition(const std::string &stringSearchString, const std::string &stringReplaceString, std::string &stringStringToReplace, std::string condition)
00126 {
00127   if (condition.length() == 0)
00128     {
00129       stringStringToReplace = strreplace(stringSearchString, stringReplaceString, stringStringToReplace);
00130       return;
00131     }
00132 
00133   std::string::size_type pos = stringStringToReplace.find(stringSearchString, 0);
00134   int intLengthSearch = stringSearchString.length();
00135   int intLengthReplacment = stringReplaceString.length();
00136   
00137   while(std::string::npos != pos)
00138     {
00139       if (pos > 0 && stringStringToReplace[pos] == condition[0])
00140         {
00141           pos = stringStringToReplace.find(stringSearchString, pos + 1);
00142           continue;
00143         }
00144       stringStringToReplace.replace(pos, intLengthSearch, stringReplaceString);
00145       pos = stringStringToReplace.find(stringSearchString, pos + intLengthReplacment);
00146     }
00147 } 
00148 
00149 std::string printToString(const char* format, ...)
00150 {
00151   char buffer[65536];
00152   va_list argp;
00153   va_start(argp, format);
00154   vsprintf(buffer, format, argp);
00155   va_end(argp);
00156   
00157   return buffer;
00158 }
00159 
00160 
00161 void strToArray(std::string text, std::vector<double> &result, std::string delimiter)
00162 {
00163   std::vector<std::string> tokens;
00164   strtokenize(text, tokens, delimiter);
00165   result.clear();
00166   for (unsigned int i=0; i<tokens.size(); i++)
00167     if (tokens[i] != "")
00168       try
00169         {
00170           result.push_back(atof(tokens[i].c_str()));
00171         } catch(...)
00172         {
00173         }
00174 }
00175 
00176 void matrixToArray(const CMatrix &matrix, std::vector<double> &values)
00177 {
00178     values[0] = matrix.a[12];
00179     values[1] = matrix.a[13];
00180     values[2] = matrix.a[14];
00181                     
00182     values[3] = matrix.a[0];
00183     values[4] = matrix.a[1];
00184     values[5] = matrix.a[2];
00185                     
00186     values[6] = matrix.a[4];
00187     values[7] = matrix.a[5];
00188     values[8] = matrix.a[6];
00189                     
00190     values[9]  = matrix.a[8];
00191     values[10] = matrix.a[9];
00192     values[11] = matrix.a[10];
00193 }
00194 
00195 void arrayToMatrix(CMatrix &matrix, std::vector<double> &values)
00196 {
00197     matrix.a[12] = values[0];
00198     matrix.a[13] = values[1];
00199     matrix.a[14] = values[2];
00200     matrix.a[15] = 1.0;
00201                
00202     matrix.a[0] = values[3];
00203     matrix.a[1] = values[4];
00204     matrix.a[2] = values[5];
00205     matrix.a[3] = 0.0;
00206                
00207     matrix.a[4] = values[6];
00208     matrix.a[5] = values[7];
00209     matrix.a[6] = values[8];
00210     matrix.a[7] = 0.0;
00211                
00212     matrix.a[8] = values[9];
00213     matrix.a[9] = values[10];
00214     matrix.a[10] = values[11];
00215     matrix.a[11] = 0.0;
00216 }
00217 
00218 bool stringStartsWith(std::string text, std::string end, std::string sep)
00219 {
00220   std::vector<std::string> tokens;
00221   strtokenize(text, tokens, sep);
00222   if (tokens.size() == 0)
00223     return false;
00224   return strcasecmp(tokens[0].c_str(), end.c_str()) == 0;
00225 }
00226 
00227 
00228 bool stringEndsWith(std::string text, std::string end, std::string sep)
00229 {
00230   std::vector<std::string> tokens;
00231   strtokenize(text, tokens, sep);
00232   if (tokens.size() == 0)
00233     return false;
00234   return strcasecmp(tokens.back().c_str(), end.c_str()) == 0;
00235 }
00236 
00237 void vectorToMatrix6(CMatrix &matrix, const double* values)
00238 {
00239   CVec tmp;
00240   double angle;
00241   tmp.x = values[3];
00242   tmp.y = values[4];
00243   tmp.z = values[5];
00244   
00245   angle = tmp.length();
00246   if (angle > 0.0)
00247     tmp /= angle;
00248 
00249   CMathLib::getMatrixFromRotation(matrix, tmp, angle);
00250   matrix.a[12] = values[0];
00251   matrix.a[13] = values[1];
00252   matrix.a[14] = values[2];
00253 }
00254 
00255 void vectorToMatrix(CMatrix &matrix, const std::vector<double> &values)
00256 {
00257   CVec tmp;
00258   float angle;
00259   if (values.size() > 3)
00260     {
00261       tmp.x = values[3];
00262       if (values.size() > 4)
00263         {
00264           tmp.y = values[4];
00265           if (values.size() > 5)
00266             {
00267               tmp.z = values[5];
00268             }
00269         }
00270     }
00271   angle = tmp.length();
00272   if (angle > 0.0)
00273     tmp /= angle;
00274 
00275   CMathLib::getMatrixFromRotation(matrix, tmp, angle);
00276   if (values.size() > 0)
00277     {
00278       matrix.a[12] = values[0];
00279       if (values.size() > 1)
00280         {
00281           matrix.a[13] = values[1];
00282           if (values.size() > 2)
00283             {
00284               matrix.a[14] = values[2];
00285             } else matrix.a[14] = 0.0;
00286         } else matrix.a[13] = 0.0;
00287     } else matrix.a[12] = 0.0;
00288 }
00289 
00290 void matrixToVector(const CMatrix &matrix, std::vector<double> &values)
00291 {
00292   CVec tmp;
00293   double angle;
00294   CMathLib::getRotationFromMatrix(matrix, tmp, angle);
00295   values.resize(6);
00296   values[0] = matrix.a[12];
00297   values[1] = matrix.a[13];
00298   values[2] = matrix.a[14];
00299   values[3] = tmp.x * angle;
00300   values[4] = tmp.y * angle;
00301   values[5] = tmp.z * angle;
00302 }
00303 
00304 void matrixToVector6(const CMatrix &matrix, double *values)
00305 {
00306   CVec tmp;
00307   double angle;
00308   CMathLib::getRotationFromMatrix(matrix, tmp, angle);
00309   values[0] = matrix.a[12];
00310   values[1] = matrix.a[13];
00311   values[2] = matrix.a[14];
00312   values[3] = tmp.x * angle;
00313   values[4] = tmp.y * angle;
00314   values[5] = tmp.z * angle;
00315 }
00316 
00317 double strToDouble(const char* text, double value)
00318 {
00319   if (text == NULL)
00320     return value;
00321 
00322   try
00323     {
00324       return atof(text);
00325     } catch (...)
00326     {
00327       return value;
00328     }
00329 }
00330 
00331 std::string strtrim(std::string& s,const std::string& drop)
00332 {
00333  std::string r=s.erase(s.find_last_not_of(drop)+1);
00334  return r.erase(0,r.find_first_not_of(drop));
00335 }
00336 
00337 
00338 void strreplace(char *text, char what, char with)
00339 {
00340         char* ptr = text;
00341         
00342         while (*ptr != '\0')
00343         {
00344             if (*ptr == what)
00345                *ptr = with;
00346             
00347             ptr++;
00348         }
00349 }
00350 
00351 unsigned long getTickCount()
00352 {
00353      static struct timeval tstart;
00354      static struct timezone tz;
00355      gettimeofday(&tstart, &tz);
00356      return tstart.tv_sec*1000 + tstart.tv_usec / 1000;  
00357 }
00358 
00359 
00360 void getConvexHullXY(std::vector<CVec> &points, std::vector<CVec> &hull)
00361 {
00362   std::vector<std::pair<double, unsigned int> > angles;
00363   
00364 }
00365 
00366 }
00367 
00368 


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