testcases_main.cpp
Go to the documentation of this file.
1 
18 #include <iostream>
19 
20 #include <stdlib.h> /* srand, rand */
21 #include <time.h> /* time */
22 #include <vector>
24 #include <iostream> //inp
25 #include <unistd.h>
26 #include <QObject>
27 #include <math.h>
31 #ifndef Q_MOC_RUN
32 #include <Eigen/Dense>
33 #include <ros/ros.h>
34 #include <ros/init.h>
35 #include <boost/shared_ptr.hpp>
36 #include <boost/algorithm/string.hpp>
37 #endif
39 
40 /*class MarkerDetectionHelper : public QObject
41 {
42 
43  public slots:
44  void markerFound(int markerNumber, Eigen::Matrix4d transformation)
45  {
46 
47  }
48 };*/
49 
50 
51 using namespace std;
53 
54 bool test_averageframe(string filename)
55 {
56  std::ifstream input(filename);
57  std::string line;
58  unsigned int lineNumber = 1;
59  std::vector<Eigen::Quaterniond> rotationVector;
60  std::vector<Eigen::Vector3d> translationVector;
61  while(std::getline( input, line ))
62  {
63  std::vector<std::string> strs;
64  boost::split(strs, line, boost::is_any_of(";"));
65  if (strs.size() == 7)
66  {
67  std::vector<double> numericValues;
68  bool success = true;
69  for (unsigned int i = 0; i < strs.size(); i++)
70  {
71  try
72  {
73  std::string temp = strs.at(i);
74  boost::algorithm::trim(temp);
75  numericValues.push_back(boost::lexical_cast<double>(temp));
76  }
77  catch (boost::bad_lexical_cast const&)
78  {
79  ROS_ERROR_STREAM("Line " << lineNumber << ": " <<strs.at(i) << " is not a number.");
80  success = false;
81  break;
82  }
83  }
84  if (success)
85  {
86  Eigen::Quaterniond rotation(numericValues.at(3),numericValues.at(0),numericValues.at(1),numericValues.at(2));
87  Eigen::Vector3d translation(numericValues.at(4),numericValues.at(5),numericValues.at(6));
88  rotationVector.push_back(rotation);
89  translationVector.push_back(translation);
90  }
91  }
92  else
93  {
94  ROS_ERROR_STREAM("Line " << lineNumber << ": Expected 7 items, got " << strs.size());
95  }
96  lineNumber ++;
97  }
98  if (rotationVector.size() > 0)
99  {
100  Eigen::Vector3d averageTranslation = translationVector.at(0);
101  Eigen::Quaterniond averageRotation = rotationVector.at(0);
102 
103  for (unsigned int i = 1; i < rotationVector.size(); i++)
104  {
105  Eigen::Quaterniond tempRotation = rotationVector.at(i);
106  Eigen::Vector3d tempTranslation = translationVector.at(i);
107  averageTranslation = averageTranslation + tempTranslation;
108  averageRotation = averageRotation.slerp((double)i/(i+1.0), tempRotation);
109  }
110  averageTranslation = averageTranslation * 1.0/(double)rotationVector.size();
111  ROS_INFO_STREAM("Position X=" << averageTranslation(0) << " Y=" << averageTranslation(1) << " Z=" << averageTranslation(2));
112  ROS_INFO_STREAM("Rotation w=" << averageRotation.w() << " x=" << averageRotation.x() << " y=" << averageRotation.y() << " z=" << averageRotation.z());
113  }
114  return true;
115 }
116 
118 {
119  bool success = false;
120  Marker_Manager markerManager(4.0, 3.0);
121  //MarkerDetectionHelper helper;
122  //connect(markerManager, SIGNAL(markerFound(int,Eigen::Matrix4d )),helper, SLOT(markerFound(int, Eigen::Matrix4d )));
123 
124  return success;
125 }
126 
128 {
129  double pan, tilt;
130  bool success = false;
131  unsigned int testCount = 3;
132  Eigen::Matrix4d m1_array[testCount];
133  double pan_array[testCount];
134  double tilt_array[testCount];
135  for (unsigned int k = 0; k < testCount; k++)
136  {
137  Eigen::Matrix4d m1, m2;
138  pan = rand() % 1000;
139  tilt = rand() & 1000;
140  pan_array[k] = pan;
141  tilt_array[k] = tilt;
142  double roll = fmod((double)rand(),2.0*M_PI);
143  double pitch = fmod((double)rand(),2.0*M_PI);
144  double yaw = fmod((double)rand(),2.0*M_PI);
145  ROS_INFO_STREAM("Roll: " << roll << " pitch: " << pitch << " yaw: " << yaw);
146  Eigen::Affine3d tr1 = Eigen::AngleAxisd(roll, Eigen::Vector3d::UnitZ()) * Eigen::AngleAxisd(pitch, Eigen::Vector3d::UnitX()) * Eigen::AngleAxisd(yaw, Eigen::Vector3d::UnitZ()) * Eigen::Translation3d(rand() % 1000, rand()% 1000, rand()% 1000);
147  roll = fmod((double)rand(),2.0*M_PI);
148  pitch = fmod((double)rand(),2.0*M_PI);
149  yaw = fmod((double)rand(),2.0*M_PI);
150  ROS_INFO_STREAM("Roll: " << roll << " pitch: " << pitch << " yaw: " << yaw);
151  Eigen::Affine3d tr2 = Eigen::AngleAxisd(roll, Eigen::Vector3d::UnitZ()) * Eigen::AngleAxisd(pitch, Eigen::Vector3d::UnitX()) * Eigen::AngleAxisd(yaw, Eigen::Vector3d::UnitZ()) * Eigen::Translation3d(rand()% 1000, rand()% 1000, rand()% 1000);
152  m1 = tr1.matrix();
153  m2 = tr2.matrix();
154  m1_array[k] = m1;
155  manager->writeToFile(m1, m2, pan, tilt);
156  }
157 
158  std::vector<Transformation_Data> dataCollection = manager->readFromFile();
159  if (dataCollection.size() >= testCount)
160  {
161  success = true;
162  for (unsigned int k = 0; k < testCount; k++)
163  {
164  Eigen::Matrix4d m1;
165  m1 = m1_array[k];
166  pan = pan_array[k];
167  tilt = tilt_array[k];
168  Transformation_Data data = dataCollection[k];
169  success &= (fabs(pan - data.pan) < 0.00001);
170  if (success) {cout << "Pan correct" << endl;}
171  else {ROS_ERROR_STREAM("Pan incorrect. Expected " << pan << ", got " << data.pan << " difference " << fabs(pan - data.pan));return success;}
172  success &= (fabs(tilt - data.tilt) < 0.00001);
173  if (success) {cout << "Tilt correct" << endl;}
174  else {ROS_ERROR_STREAM("Tilt incorrect. Expected " << tilt << ", got " << data.tilt << " difference " << fabs(tilt - data.tilt));return success;}
175  for (unsigned int i = 0; i < 4; i++)
176  {
177  for (unsigned int j = 0; j < 4; j++)
178  {
179  success &= (fabs(m1(i,j) - data.PTU_Frame(i,j)) < 0.001);
180  if (!success) {ROS_ERROR_STREAM("PTU frame incorrect. Expected " << m1(i,j) << ", got " << data.PTU_Frame(i,j) << " difference " << fabs(m1(i,j) - data.PTU_Frame(i,j)));return success;}
181  }
182  }
183  if (success) {cout << "PTU frame correct" << endl;}
184  }
185  }
186  else
187  {
188  ROS_ERROR_STREAM("Not enough dataNodes found. Expected " << testCount << ", got " << dataCollection.size());
189  }
190  return success;
191 }
192 
194 {
195  Eigen::Vector3d top, temp;
196  Eigen::Vector2d base1, base2, base3;
197  double angle12, angle23, angle31;
198  double side1, side2, side3, sideBase;
199 
200  bool success;
201  /* initialize random seed: */
202 
203  top[0] = (rand() % 1000 + 1) /100.0;
204  top[1] = (rand() % 1000 + 1) /100.0;
205  top[2] = (rand() % 1000 + 1) /500.0;
206 
207  base1[0] = (rand() % 1000 + 1) /100.0;
208  base1[1] = (rand() % 1000 + 1) /100.0;
209  base2[0] = (rand() % 1000 + 1) /100.0;
210  base2[1] = (rand() % 1000 + 1) /100.0;
211  base3[0] = (rand() % 1000 + 1) /100.0;
212  // Nur rechten Winkel zulassen
213  base3[1] = (base1[0]*base3[0]-base2[0]*base3[0]-base1[0]*base2[0]+base2[0]*base2[0]-base1[1]*base2[1]+base2[1]*base2[1])/(base2[1]-base1[1]);
214  // Alle Winkel zulassen
215  //base3[1] = (rand() % 1000 + 1) /100.0;
216 
217  temp = Eigen::Vector3d(base1.x(), base1.y(), 0.0f);
218  side1 = (top - temp).norm();
219  temp = Eigen::Vector3d(base2.x(), base2.y(), 0.0f);
220  side2 = (top - temp).norm();
221  temp = Eigen::Vector3d(base3.x(), base3.y(), 0.0f);
222  side3 = (top - temp).norm();
223  sideBase = (base1 - base2).norm();
224  angle12 = acos((-sideBase*sideBase + side1*side1 + side2*side2)/(2*side1*side2));
225 
226  sideBase = (base2 - base3).norm();
227  angle23 = acos((-sideBase*sideBase + side3*side3 + side2*side2)/(2*side3*side2));
228 
229  sideBase = (base3 - base1).norm();
230  angle31 = acos((-sideBase*sideBase + side1*side1 + side3*side3)/(2*side1*side3));
231 
232  cout << "Testing vectors [" << base1.x() << ", " << base1.y() << "] ";
233  cout << "[" << base2.x() << ", " << base2.y() << "] ";
234  cout << "[" << base3.x() << ", " << base3.y() << "]" << endl;
235  cout << "with top-point [" << top.x() << ", " << top.y() << ", " << top.z() << "]" << endl;
236  cout << "Distances " << side1 << ", " << side2 << ", " << side3 << "" << endl;
237  cout << "Angles " << angle12 << ", " << angle23 << ", " << angle31 << "" << endl;
238 
239  //Call Testfunction
241  calibrationObject->top_angle_ab = angle12;
242  calibrationObject->top_angle_bc = angle23;
243  calibrationObject->top_angle_ca = angle31;
244  calibrationObject->side_a = (base1 - base2).norm();
245  calibrationObject->side_b = (base2 - base3).norm();
246  calibrationObject->side_c = (base1 - base3).norm();
247 
248  boost::shared_ptr<FeasibilityChecker> feasibilityChecker(new FeasibilityChecker(calibrationObject, Eigen::Vector3d(0.0,0.0,1.0), 0.15));
249  ResectionSolver solver(calibrationObject, feasibilityChecker);
250  solver.solve(base1, base2, base3);
251  cout << "Results:" << endl;
252  success = false;
253  if (solver.solutions_a.size() == 0) {cout << "No solutions found." << endl;}
254  for (unsigned int i = 0; i < solver.solutions_a.size(); i++)
255  {
256  if (abs(solver.solutions_b[i] - side1) <= 0.0001f && abs(solver.solutions_c[i] - side2) <= 0.0001f && abs(solver.solutions_a[i] - side3) <= 0.0001f)
257  {
258  //system("Color 02");
259  cout << "[" << solver.solutions_b[i] << ", " << solver.solutions_c[i] << ", " << solver.solutions_a[i]<< "]" << endl;
260  success = true;
261  }
262  else
263  {
264  cout << "[" << solver.solutions_b[i] << ", " << solver.solutions_c[i] << ", " << solver.solutions_a[i] << "]" << endl;
265  //system("Color 04");
266  }
267  }
268  if (success) {
269  cout << "Distance test successful" << endl;
270  success = false;
271  for (unsigned int i = 0; i < solver.solutions.size(); i++)
272  {
273  if (abs(solver.solutions[i].x() - top.x()) <= 0.0001f && abs(solver.solutions[i].y() - top.y()) <= 0.0001f && abs(solver.solutions[i].z() - top.z()) <= 0.0001f)
274  {
275  //system("Color 02");
276  cout << "[" << solver.solutions[i][0] << ", " <<solver.solutions[i][1] << ", " << solver.solutions[i][2]<< "]" << endl;
277  success = true;
278  }
279  else
280  {
281  cout << "[" << solver.solutions[i][0] << ", " <<solver.solutions[i][1] << ", " << solver.solutions[i][2]<< "]" << endl;
282  //system("Color 04");
283  }
284  }
285  if (success) {cout << "Top-point test successful!" << endl;}
286  }
287 
288  return success;
289 }
290 
291 bool is_numeric(string string_)
292 {
293  int sizeOfString = string_.size();
294  int iteration = 0;
295  bool isNumeric = true;
296 
297  while(iteration < sizeOfString)
298  {
299  if(!isdigit(string_[iteration]))
300  {
301  isNumeric = false;
302  break;
303  }
304 
305  iteration++;
306  }
307  return isNumeric;
308 }
309 
310 bool start_test(string args)
311 {
312  bool testFound = false;
313  if (args.substr(0,9) == "resection")
314  {
315  string numerArg = args.substr(9);
316  numerArg.erase(std::remove(numerArg.begin(), numerArg.end(), ' '), numerArg.end());
317  srand (time(NULL));
318  if (is_numeric(numerArg) && numerArg.size() > 0) {
319  unsigned int numbers = atoi(numerArg.c_str());
320  unsigned int success = 0;
321  for (unsigned int i = 0; i < numbers; i++)
322  {
323  if (test_resectionsolver()) {success++;}
324  clock_t time_end;
325  time_end = clock() + 20 * CLOCKS_PER_SEC/1000;
326  while (clock() < time_end)
327  {
328  }
329  cout << endl;
330  }
331  cout << success << "/" << numbers << " were successful." << endl;
332  }
333  else
334  {
335  int inp;
336  do
337  {
339  inp = cin.get();
340  }
341  while (inp != 32);
342  }
343  testFound = true;
344  }
345  else if (args.substr(0,13) == "serialization")
346  {
347  string numerArg = args.substr(13);
348  numerArg.erase(std::remove(numerArg.begin(), numerArg.end(), ' '), numerArg.end());
349  srand (time(NULL));
350  if (numerArg.size() > 0) {
351  std::string writer = numerArg.c_str();
353  if (writer == "XML")
354  {
355  manager = new TransformationFile_Manager_XML(workspacePath + "/test.xml");
356  }
357  else
358  {
359  manager = new TransformationFile_Manager_Data(workspacePath + "/test.data");
360  }
361  if (test_serialisation(manager))
362  {
363  ROS_INFO("Test was successful.");
364  }
365  else
366  {
367  ROS_INFO("Test failed.");
368  }
369  }
370  testFound = true;
371  }
372  else if (args.substr(0,15) == "markerdetection")
373  {
375  testFound = true;
376  }
377  else if (args.substr(0,12) == "averageframe")
378  {
379  string filename = args.substr(13);
380  filename.erase(std::remove(filename.begin(), filename.end(), ' '), filename.end());
381  if (filename.size() > 0) test_averageframe(filename);
382  testFound = true;
383  }
384  else
385  {
386  cout << "Testcase '" << args << "'not declared!" << endl;
387 
388  }
389  return testFound;
390 }
391 
392 int main(int argc, char *argv[])
393 {
394  string inp = "";
395  char *arg[0];
396  int x = 0;
397  ros::init(x, arg, "calibrationTool_tests");
399  string myWorkspace;
400  nh.getParam("test_workspace", myWorkspace);
401  ROS_INFO_STREAM("Using workspace " << myWorkspace);
402  workspacePath = myWorkspace;
403  std::cout << "Please choose a test to run: " << std::endl;
404 
405  std::getline(std::cin, inp);
406  string args_ = inp;
407  while (args_ != "exit")
408  {
409  if (!start_test(args_))
410  {
411  std::cout << "Available tests are: " << std::endl;
412  std::cout << " resection <count>" << std::endl;
413  std::cout << " serialization XML|DATA" << std::endl;
414  std::cout << " markerdetection" << std::endl;
415  std::cout << " averageframe <filename>" << std::endl;
416  }
417  std::cout << "Please choose a test to run: " << std::endl;
418  std::getline(std::cin, inp);
419  args_ = inp;
420  }
421 
422  return 0;
423 }
bool start_test(string args)
f
std::vector< Eigen::Vector3d, Eigen::aligned_allocator< Eigen::Vector3d > > solutions
unsigned int solve(const Eigen::Vector2d &base_A, const Eigen::Vector2d &base_B, const Eigen::Vector2d &base_C)
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
ROSCPP_DECL const std::string & getName()
virtual std::vector< Transformation_Data > readFromFile(string filePath)=0
bool test_serialisation(Abstract_TransformationFile_Manager *manager)
#define ROS_INFO(...)
std::vector< double > solutions_a
std::vector< double > solutions_b
int main(int argc, char *argv[])
Eigen::Matrix4d PTU_Frame
#define ROS_INFO_STREAM(args)
bool test_averageframe(string filename)
bool test_resectionsolver()
bool getParam(const std::string &key, std::string &s) const
bool is_numeric(string string_)
#define ROS_ERROR_STREAM(args)
bool test_markerDetection()
string workspacePath
virtual bool writeToFile(Transformation_Data data)=0
std::vector< double > solutions_c


asr_mild_calibration_tool
Author(s): Aumann Florian, Heller Florian, Meißner Pascal
autogenerated on Mon Dec 2 2019 03:11:43