utils.cpp
Go to the documentation of this file.
1 
18 #include <stdio.h>
19 #include <stdarg.h>
20 #include <string.h>
21 #include <vector>
22 
23 #include <sys/times.h>
24 #include <sys/time.h>
25 #include <sys/types.h>
26 #include <dirent.h>
27 #include <errno.h>
28 #include <vector>
29 #include <string>
30 #include <iostream>
31 
32 #include <utils.h>
33 
34 namespace robotLibPbD {
35 
36 
37  std::string waitForReturn(std::string msg)
38  {
39  std::cout << msg;
40  char bChar[255];
41  std::cin.getline(bChar, 255);
42  std::string result = bChar;
43  return result;
44  }
45 
46  std::string combineStrings(std::vector<std::string> &input, std::string delimiter)
47  {
48  std::string tmp;
49  for (unsigned int i=0; i<input.size(); i++)
50  {
51  tmp += input[i];
52 
53  if (i+1 < input.size())
54  tmp += delimiter;
55  }
56 
57  return tmp;
58  }
59 
60 int getDirectoryFiles(std::string dir, std::vector<std::string> &files, bool appendDir)
61 {
62  DIR *dp;
63  struct dirent *dirp;
64  if((dp = opendir(dir.c_str())) == NULL)
65  {
66  std::cout << "Error(" << errno << ") opening " << dir << std::endl;
67  return errno;
68  }
69 
70  if (dir.length() > 0 && dir[dir.length()-1] != '/')
71  dir = dir + "/";
72 
73  while ((dirp = readdir(dp)) != NULL)
74  {
75  if (appendDir)
76  files.push_back(dir + std::string(dirp->d_name));
77  else
78  files.push_back(std::string(dirp->d_name));
79  }
80  closedir(dp);
81  return 0;
82 }
83 
84 
85 std::string boolToString(bool value){ if (value) return "true"; return "false"; }
86 
87 void strtokenize(const std::string& str,
88  std::vector<std::string>& tokens,
89  const std::string& delimiters)
90 {
91  // Skip delimiters at beginning.
92  std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
93  // Find first "non-delimiter".
94  std::string::size_type pos = str.find_first_of(delimiters, lastPos);
95 
96  while (std::string::npos != pos || std::string::npos != lastPos)
97  {
98  // Found a token, add it to the vector.
99  tokens.push_back(str.substr(lastPos, pos - lastPos));
100  // Skip delimiters. Note the "not_of"
101  lastPos = str.find_first_not_of(delimiters, pos);
102  // Find next "non-delimiter"
103  pos = str.find_first_of(delimiters, lastPos);
104  }
105 }
106 
107 //Ersetzt einen String
108 //stringSearchString = string der mit stringReplaceString ersetzt wird
109 //stringStringToReplace ist der string in dem gesucht und ersetzt wird
110 std::string strreplace(const std::string &stringSearchString, const std::string &stringReplaceString, std::string stringStringToReplace)
111 {
112  std::string::size_type pos = stringStringToReplace.find(stringSearchString, 0);
113  int intLengthSearch = stringSearchString.length();
114  int intLengthReplacment = stringReplaceString.length();
115 
116  while(std::string::npos != pos)
117  {
118  stringStringToReplace.replace(pos, intLengthSearch, stringReplaceString);
119  pos = stringStringToReplace.find(stringSearchString, pos + intLengthReplacment);
120  }
121 
122  return stringStringToReplace;
123 }
124 
125 void strreplaceWithCondition(const std::string &stringSearchString, const std::string &stringReplaceString, std::string &stringStringToReplace, std::string condition)
126 {
127  if (condition.length() == 0)
128  {
129  stringStringToReplace = strreplace(stringSearchString, stringReplaceString, stringStringToReplace);
130  return;
131  }
132 
133  std::string::size_type pos = stringStringToReplace.find(stringSearchString, 0);
134  int intLengthSearch = stringSearchString.length();
135  int intLengthReplacment = stringReplaceString.length();
136 
137  while(std::string::npos != pos)
138  {
139  if (pos > 0 && stringStringToReplace[pos] == condition[0])
140  {
141  pos = stringStringToReplace.find(stringSearchString, pos + 1);
142  continue;
143  }
144  stringStringToReplace.replace(pos, intLengthSearch, stringReplaceString);
145  pos = stringStringToReplace.find(stringSearchString, pos + intLengthReplacment);
146  }
147 }
148 
149 std::string printToString(const char* format, ...)
150 {
151  char buffer[65536];
152  va_list argp;
153  va_start(argp, format);
154  vsprintf(buffer, format, argp);
155  va_end(argp);
156 
157  return buffer;
158 }
159 
160 
161 void strToArray(std::string text, std::vector<double> &result, std::string delimiter)
162 {
163  std::vector<std::string> tokens;
164  strtokenize(text, tokens, delimiter);
165  result.clear();
166  for (unsigned int i=0; i<tokens.size(); i++)
167  if (tokens[i] != "")
168  try
169  {
170  result.push_back(atof(tokens[i].c_str()));
171  } catch(...)
172  {
173  }
174 }
175 
176 void matrixToArray(const CMatrix &matrix, std::vector<double> &values)
177 {
178  values[0] = matrix.a[12];
179  values[1] = matrix.a[13];
180  values[2] = matrix.a[14];
181 
182  values[3] = matrix.a[0];
183  values[4] = matrix.a[1];
184  values[5] = matrix.a[2];
185 
186  values[6] = matrix.a[4];
187  values[7] = matrix.a[5];
188  values[8] = matrix.a[6];
189 
190  values[9] = matrix.a[8];
191  values[10] = matrix.a[9];
192  values[11] = matrix.a[10];
193 }
194 
195 void arrayToMatrix(CMatrix &matrix, std::vector<double> &values)
196 {
197  matrix.a[12] = values[0];
198  matrix.a[13] = values[1];
199  matrix.a[14] = values[2];
200  matrix.a[15] = 1.0;
201 
202  matrix.a[0] = values[3];
203  matrix.a[1] = values[4];
204  matrix.a[2] = values[5];
205  matrix.a[3] = 0.0;
206 
207  matrix.a[4] = values[6];
208  matrix.a[5] = values[7];
209  matrix.a[6] = values[8];
210  matrix.a[7] = 0.0;
211 
212  matrix.a[8] = values[9];
213  matrix.a[9] = values[10];
214  matrix.a[10] = values[11];
215  matrix.a[11] = 0.0;
216 }
217 
218 bool stringStartsWith(std::string text, std::string end, std::string sep)
219 {
220  std::vector<std::string> tokens;
221  strtokenize(text, tokens, sep);
222  if (tokens.size() == 0)
223  return false;
224  return strcasecmp(tokens[0].c_str(), end.c_str()) == 0;
225 }
226 
227 
228 bool stringEndsWith(std::string text, std::string end, std::string sep)
229 {
230  std::vector<std::string> tokens;
231  strtokenize(text, tokens, sep);
232  if (tokens.size() == 0)
233  return false;
234  return strcasecmp(tokens.back().c_str(), end.c_str()) == 0;
235 }
236 
237 void vectorToMatrix6(CMatrix &matrix, const double* values)
238 {
239  CVec tmp;
240  double angle;
241  tmp.x = values[3];
242  tmp.y = values[4];
243  tmp.z = values[5];
244 
245  angle = tmp.length();
246  if (angle > 0.0)
247  tmp /= angle;
248 
249  CMathLib::getMatrixFromRotation(matrix, tmp, angle);
250  matrix.a[12] = values[0];
251  matrix.a[13] = values[1];
252  matrix.a[14] = values[2];
253 }
254 
255 void vectorToMatrix(CMatrix &matrix, const std::vector<double> &values)
256 {
257  CVec tmp;
258  float angle;
259  if (values.size() > 3)
260  {
261  tmp.x = values[3];
262  if (values.size() > 4)
263  {
264  tmp.y = values[4];
265  if (values.size() > 5)
266  {
267  tmp.z = values[5];
268  }
269  }
270  }
271  angle = tmp.length();
272  if (angle > 0.0)
273  tmp /= angle;
274 
275  CMathLib::getMatrixFromRotation(matrix, tmp, angle);
276  if (values.size() > 0)
277  {
278  matrix.a[12] = values[0];
279  if (values.size() > 1)
280  {
281  matrix.a[13] = values[1];
282  if (values.size() > 2)
283  {
284  matrix.a[14] = values[2];
285  } else matrix.a[14] = 0.0;
286  } else matrix.a[13] = 0.0;
287  } else matrix.a[12] = 0.0;
288 }
289 
290 void matrixToVector(const CMatrix &matrix, std::vector<double> &values)
291 {
292  CVec tmp;
293  double angle;
294  CMathLib::getRotationFromMatrix(matrix, tmp, angle);
295  values.resize(6);
296  values[0] = matrix.a[12];
297  values[1] = matrix.a[13];
298  values[2] = matrix.a[14];
299  values[3] = tmp.x * angle;
300  values[4] = tmp.y * angle;
301  values[5] = tmp.z * angle;
302 }
303 
304 void matrixToVector6(const CMatrix &matrix, double *values)
305 {
306  CVec tmp;
307  double angle;
308  CMathLib::getRotationFromMatrix(matrix, tmp, angle);
309  values[0] = matrix.a[12];
310  values[1] = matrix.a[13];
311  values[2] = matrix.a[14];
312  values[3] = tmp.x * angle;
313  values[4] = tmp.y * angle;
314  values[5] = tmp.z * angle;
315 }
316 
317 double strToDouble(const char* text, double value)
318 {
319  if (text == NULL)
320  return value;
321 
322  try
323  {
324  return atof(text);
325  } catch (...)
326  {
327  return value;
328  }
329 }
330 
331 std::string strtrim(std::string& s,const std::string& drop)
332 {
333  std::string r=s.erase(s.find_last_not_of(drop)+1);
334  return r.erase(0,r.find_first_not_of(drop));
335 }
336 
337 
338 void strreplace(char *text, char what, char with)
339 {
340  char* ptr = text;
341 
342  while (*ptr != '\0')
343  {
344  if (*ptr == what)
345  *ptr = with;
346 
347  ptr++;
348  }
349 }
350 
351 unsigned long getTickCount()
352 {
353  static struct timeval tstart;
354  static struct timezone tz;
355  gettimeofday(&tstart, &tz);
356  return tstart.tv_sec*1000 + tstart.tv_usec / 1000;
357 }
358 
359 
360 void getConvexHullXY(std::vector<CVec> &points, std::vector<CVec> &hull)
361 {
362  std::vector<std::pair<double, unsigned int> > angles;
363 
364 }
365 
366 }
367 
368 
void strtokenize(const std::string &str, std::vector< std::string > &tokens, const std::string &delimiters)
Definition: utils.cpp:87
double strToDouble(const char *text, double value=0.0)
Definition: utils.cpp:317
PRECISION z
Definition: vecmath.h:60
std::string printToString(const char *format,...)
Definition: utils.cpp:149
PRECISION length() const
Definition: vecmath.h:168
std::string waitForReturn(std::string msg="Press any key.\n")
Definition: utils.cpp:37
std::string combineStrings(std::vector< std::string > &input, std::string delimiter=" ")
Definition: utils.cpp:46
std::string strtrim(std::string &s, const std::string &drop=" ")
Definition: utils.cpp:331
void vectorToMatrix6(CMatrix &matrix, const double *values)
Definition: utils.cpp:237
static void getMatrixFromRotation(CMatrix &mat, const CVec &axis, double angle)
Transforms axis-angle representation into homogenous matrix representation.
Definition: vecmath.cpp:409
PRECISION a[16]
Definition: vecmath.h:190
std::string strreplace(const std::string &stringSearchString, const std::string &stringReplaceString, std::string stringStringToReplace)
Definition: utils.cpp:110
static void getRotationFromMatrix(const CMatrix &mat, CVec &axis, double &angle)
Transforms homogenous matrix into axis-angle representation.
Definition: vecmath.cpp:463
int getDirectoryFiles(std::string dir, std::vector< std::string > &files, bool appendDir=false)
Definition: utils.cpp:60
bool stringEndsWith(std::string text, std::string end, std::string sep="_")
Definition: utils.cpp:228
std::string boolToString(bool value)
Definition: utils.cpp:85
void matrixToVector6(const CMatrix &matrix, double *values)
Definition: utils.cpp:304
Homogenous vector.
Definition: vecmath.h:57
void vectorToMatrix(CMatrix &matrix, const std::vector< double > &values)
Definition: utils.cpp:255
Homogenous matrix.
Definition: vecmath.h:187
void strToArray(std::string text, std::vector< double > &result, std::string delimiter=" ")
Definition: utils.cpp:161
bool stringStartsWith(std::string text, std::string end, std::string sep="_")
Definition: utils.cpp:218
void getConvexHullXY(std::vector< CVec > &points, std::vector< CVec > &hull)
Definition: utils.cpp:360
void matrixToArray(const CMatrix &matrix, std::vector< double > &values)
Definition: utils.cpp:176
PRECISION y
Definition: vecmath.h:60
void matrixToVector(const CMatrix &matrix, std::vector< double > &values)
Definition: utils.cpp:290
PRECISION x
Definition: vecmath.h:60
void arrayToMatrix(CMatrix &matrix, std::vector< double > &values)
Definition: utils.cpp:195
unsigned long getTickCount()
Definition: utils.cpp:351
void strreplaceWithCondition(const std::string &stringSearchString, const std::string &stringReplaceString, std::string &stringStringToReplace, std::string condition="")
Definition: utils.cpp:125


asr_kinematic_chain_optimizer
Author(s): Aumann Florian, Heller Florian, Jäkel Rainer, Wittenbeck Valerij
autogenerated on Mon Jun 10 2019 12:35:36