optimizer_iv.cpp
Go to the documentation of this file.
1 
18 #include <cstdio>
19 #include <iostream>
20 #include <cstring>
21 #include <fstream>
22 
23 #include <optimizer_iv.h>
24 
25 using namespace robotLibPbD;
26 
27 std::string COptimizerIv::addLine(CVec &first, CVec &second, double width)
28 {
29  std::vector<double> end, start;
30  start.push_back(first.x);
31  start.push_back(first.y);
32  start.push_back(first.z);
33  end.push_back(second.x);
34  end.push_back(second.y);
35  end.push_back(second.z);
36 
37  CVec diff(end.at(0) - start.at(0), end.at(1)-start.at(1), end.at(2) - start.at(2));
38  CVec linIndep;
39 
40  if(diff.x == 0)
41  {
42  linIndep.set(1,0,0);
43  }
44  else
45  {
46  linIndep.set(0,1,0);
47  }
48 
49  if (diff.x != 0.0)
50  {
51  linIndep.y = 1.0;
52  linIndep.z = 1.0;
53  linIndep.x = (-linIndep.y*diff.y - linIndep.z*diff.z) / diff.x;
54  }
55  else if (diff.y != 0.0)
56  {
57  linIndep.x = 1.0;
58  linIndep.z = 1.0;
59  linIndep.y = (-linIndep.x*diff.x - linIndep.z*diff.z) / diff.y;
60  }
61  else if (diff.z != 0.0)
62  {
63  linIndep.x = 1.0;
64  linIndep.y = 1.0;
65  linIndep.z = (-linIndep.x*diff.x - linIndep.y*diff.y) / diff.z;
66  }
67 
68 
69  linIndep.normalize();
70  diff.normalize();
71 
72 
73  CVec orth(diff.y * linIndep.z - diff.z * linIndep.y, diff.z * linIndep.x - diff.x - linIndep.z, diff.x * linIndep.y - diff.y * linIndep.w);
74 
75  orth = linIndep ^ diff;
76 
77 
78  orth.normalize();
79 
80  CMatrix pose;
81  //pose.set(linIndep.x, linIndep.y, linIndep.z, 0, diff.x, diff.y, diff.z, 0, orth.x, orth.y, orth.z, 0, 0,0,0,1);
82  pose.set(linIndep.x, diff.x, orth.x, 0, linIndep.y, diff.y, orth.y, 0, linIndep.z, diff.z, orth.z, 0, 0, 0, 0, 1);
83 
84 
85  CVec axis;
86  double angle;
87 
88  CMathLib::getRotationFromMatrix(pose, axis, angle);
89  axis.normalize();
90 
91  std::string file;
92 
93 
94  file += "Separator {\n";
95 
96  file += printToString("Transform { translation %f %f %f\n rotation %f %f %f %f\n }\n",
97  start.at(0) + (end.at(0) - start.at(0)) / 2, start.at(1) + (end.at(1) - start.at(1)) / 2, start.at(2) + (end.at(2) - start.at(2)) / 2,
98  axis.x, axis.y, axis.z, angle);
99 
100  file += "Material { ambientColor 0.6 0.6 0.6 diffuseColor 0.6 0.6 0.6 }\n";
101 
102  file += printToString("Cylinder { radius %f height %f }\n", width, sqrt(pow(end.at(0) - start.at(0),2) + pow(end.at(1) - start.at(1),2) + pow(end.at(2) - start.at(2),2)));
103 
104  file += "}\n";
105 
106 
107  return file;
108 }
109 
110 std::string COptimizerIv::getInventorCoordinateSystem(double scaleFactor, std::string ivCoordFilename)
111 {
112  std::string file;
113  file += "Separator {\n";
114 
115  file += printToString("Transform { scaleFactor %f %f %f }\n", scaleFactor, scaleFactor, scaleFactor);
116 
117  file += printToString("File { name \"%s\" }\n", ivCoordFilename.c_str());
118 
119  file += "}\n";
120  return file;
121 }
122 
123 bool COptimizerIv::generateInventor(std::vector<double> values, std::string filename, std::string ivCoordFilename)
124 {
125  double *st = (double*)calloc(dofs,sizeof(double));
126  for (unsigned int i=0; i<dofs && i<values.size(); i++)
127  st[i] = values[i];
128 
129  std::string text;
130 
131  CFrame* tcpFrame;
132  if (!frames.getFrameByName("TCP", tcpFrame))
133  tcpFrame = NULL;
134 
135  CFrame* cameraFrame;
136  if (!frames.getFrameByName("Camera", cameraFrame))
137  cameraFrame = NULL;
138 
139  CVec axis;
140  double angle;
141 
142  std::string file;
143  file += "#Inventor V2.1 ascii\n";
144  file += "Separator {\n";
145 
146 
147  file += "Separator {\n";
148 
149  file += getInventorCoordinateSystem(ivCoordScale, ivCoordFilename);
150  file += "Sphere { radius 10.0 }\n";
151  file += "}\n";
152 
153  CMatrix pose;
154 
155  if (cameraFrame != NULL)
156  {
157  pose = cameraFrame->getRelativeToBase();
158  CMathLib::getRotationFromMatrix(pose, axis, angle);
159 
160  file += "Separator {\n";
161 
162  file += printToString("Transform { translation %f %f %f\n rotation %f %f %f %f\n }\n",
163  pose.a[12], pose.a[13], pose.a[14],
164  axis.x, axis.y, axis.z, angle);
165 
166  file += getInventorCoordinateSystem(ivCoordScale, ivCoordFilename);
167 
168  file += "Material { ambientColor 0.5 0.5 0.5 diffuseColor 0.5 0.5 0.5 }\n";
169 
170  file += printToString("Sphere { radius %f }\n", ivSphereRadius);
171 
172  file += "}\n";
173  }
174 
175  for (unsigned int i=0; i<examples.size(); i++)
176  {
177  setData(examples[i]);
178  setValue(st);
179 
180  if (false && tcpFrame != NULL)
181  {
182  pose = tcpFrame->getRelativeToBase();
183  CMathLib::getRotationFromMatrix(pose, axis, angle);
184 
185  file += "Separator {\n";
186 
187  file += printToString("Transform { translation %f %f %f\n rotation %f %f %f %f\n }\n",
188  pose.a[12], pose.a[13], pose.a[14],
189  axis.x, axis.y, axis.z, angle);
190 
191  file += getInventorCoordinateSystem(ivCoordScale, ivCoordFilename);
192  file += "}\n";
193  }
194  // create inventor file
195 
196  for (unsigned int j=0; j<globalFunctions.size(); j++)
197  {
198  CMatrix pose = globalFunctions[j]->first->getRelativeToBase();
199  CMathLib::getRotationFromMatrix(pose, axis, angle);
200 
201  file += "Separator {\n";
202 
203  file += printToString("Transform { translation %f %f %f\n rotation %f %f %f %f\n }\n",
204  pose.a[12], pose.a[13], pose.a[14],
205  axis.x, axis.y, axis.z, angle);
206 
207  file += getInventorCoordinateSystem(ivCoordScale, ivCoordFilename);
208 
209  file += "Material { ambientColor 0.0 0.0 1.0 diffuseColor 0.0 0.0 1.0 }\n";
210 
211  file += printToString("Sphere { radius %f }\n", ivSphereRadius);
212 
213 
214 
215  file += "}\n";
216  }
217 
218  for (unsigned int j=0; j<functions.size(); j++)
219  {
220  CMatrix pose = functions[j]->first->getRelativeToBase();
221  CMathLib::getRotationFromMatrix(pose, axis, angle);
222 
223  file += "Separator {\n";
224 
225 
226  file += addLine(functions[j]->first->getRelativeToBase()[3], functions[j]->second->getRelativeToBase()[3], ivLineWidth);
227 
228  file += printToString("Transform { translation %f %f %f\n rotation %f %f %f %f\n }\n",
229  pose.a[12], pose.a[13], pose.a[14],
230  axis.x, axis.y, axis.z, angle);
231 
232  file += getInventorCoordinateSystem(ivCoordScale, ivCoordFilename);
233 
234  file += "Material { ambientColor 0.0 0.0 1.0 diffuseColor 0.0 0.0 1.0 }\n";
235 
236  file += printToString("Sphere { radius %f }\n", ivSphereRadius);
237 
238 
239 
240  file += "}\n";
241 
242  pose = functions[j]->second->getRelativeToBase();
243  CMathLib::getRotationFromMatrix(pose, axis, angle);
244 
245  file += "Separator {\n";
246 
247 
248  file += printToString("Transform { translation %f %f %f\n rotation %f %f %f %f\n }\n",
249  pose.a[12], pose.a[13], pose.a[14],
250  axis.x, axis.y, axis.z, angle);
251 
252  file += getInventorCoordinateSystem(ivCoordScale, ivCoordFilename);
253 
254  file += "Material { ambientColor 0.0 1.0 0.0 diffuseColor 0.0 1.0 0.0 }\n";
255 
256  file += printToString("Sphere { radius %f }\n", ivSphereRadius);
257 
258 
259 
260  file += "}\n";
261  }
262 
263 
264  for (unsigned int j=0; j<functions.size(); j++)
265  {
266  CVec fpos, spos;
267  CMatrix pose = functions[j]->first->getRelativeToBase();
268  pose.invert();
269 
270  fpos = pose * functions[j]->first->getRelativeToBase()[3];
271  spos = pose * functions[j]->second->getRelativeToBase()[3];
272 
273 
274  file += "Separator {\n";
275 
276  file += addLine(fpos, spos, ivLineWidth);
277 
278  file += "Material { ambientColor 0.0 1.0 0.0 diffuseColor 0.0 1.0 0.0 }\n";
279 
280  file += printToString("Sphere { radius %f }\n", ivSphereRadius);
281 
282  file += printToString("Transform { translation %f %f %f\n }\n",
283  spos.x, spos.y, spos.z);
284 
285  //file += "File { name \"data/SAH-coord.iv\" }\n";
286 
287 
288 
289  file += "}\n";
290  }
291 
292 
293  }
294 
295 
296  for (unsigned int j=0; j<globalFunctions.size(); j++)
297  {
298  globalFunctions[j]->getDistance();
299  CMatrix pose = globalFunctions[j]->first->getRelativeToBase();
300  CMathLib::getRotationFromMatrix(pose, axis, angle);
301  pose.a[12] = globalFunctions[j]->mean[0];
302  pose.a[13] = globalFunctions[j]->mean[1];
303  pose.a[14] = globalFunctions[j]->mean[2];
304 
305  file += "Separator {\n";
306 
307  file += printToString("Transform { translation %f %f %f\n rotation %f %f %f %f\n }\n",
308  pose.a[12], pose.a[13], pose.a[14],
309  axis.x, axis.y, axis.z, angle);
310 
311  file += getInventorCoordinateSystem(ivCoordScale, ivCoordFilename);
312 
313  file += "Material { ambientColor 1.0 0.0 0.0 diffuseColor 1.0 0.0 0.0 }\n";
314 
315  file += printToString("Sphere { radius %f }\n", ivSphereRadius*1.01);
316 
317  file += "}\n";
318  }
319 
320  file += "}\n";
321 
322  free(st);
323 
324  std::ofstream textstream(filename.c_str());
325  if (textstream.fail())
326  return false;
327 
328  textstream << file;
329  textstream.close();
330 
331  return true;
332 }
333 
std::string addLine(CVec &first, CVec &second, double width)
std::vector< std::vector< double > > examples
Definition: optimizer.h:61
CFrameContainer frames
Definition: optimizer.h:58
PRECISION z
Definition: vecmath.h:60
std::string printToString(const char *format,...)
Definition: utils.cpp:149
bool getFrameByName(const char *name, CFrame *&frame)
Definition: frame.cpp:945
bool generateInventor(std::vector< double > values, std::string filename, std::string ivCoordFilename="data/coord.iv")
PRECISION a[16]
Definition: vecmath.h:190
virtual CMatrix getRelativeToBase()
Returns pose in the base (frame with no predecessor) frame.
Definition: frame.h:508
static void getRotationFromMatrix(const CMatrix &mat, CVec &axis, double &angle)
Transforms homogenous matrix into axis-angle representation.
Definition: vecmath.cpp:463
void setData(std::vector< double > &values)
Definition: optimizer.cpp:71
Homogenous vector.
Definition: vecmath.h:57
void set(PRECISION x, PRECISION y, PRECISION z)
Definition: vecmath.cpp:51
Homogenous matrix.
Definition: vecmath.h:187
void set(PRECISION a0, PRECISION a4, PRECISION a8, PRECISION a12, PRECISION a1, PRECISION a5, PRECISION a9, PRECISION a13, PRECISION a2, PRECISION a6, PRECISION a10, PRECISION a14, PRECISION a3=0.0f, PRECISION a7=0.0f, PRECISION a11=0.0f, PRECISION a15=1.0f)
Definition: vecmath.cpp:144
unsigned int dofs
Definition: optimizer.h:66
Robot types with implemented inverse kinematics.
Definition: frame.h:39
void setValue(double *x)
Definition: optimizer.cpp:40
void normalize()
Definition: vecmath.h:172
std::vector< OptimizerGoalGlobal * > globalFunctions
Definition: optimizer.h:56
PRECISION y
Definition: vecmath.h:60
std::string getInventorCoordinateSystem(double scaleFactor, std::string ivCoordFilename="data/coord.iv")
void invert()
Inverts the matrix (only for homogenous matrices)
Definition: vecmath.h:461
PRECISION x
Definition: vecmath.h:60
std::vector< OptimizerGoal * > functions
Definition: optimizer.h:57


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