Tetrapod Project
xml_utils.cpp
Go to the documentation of this file.
2 #include<iostream>
3 #include<sstream>
4 
5 using p_el = DOMElement*;
6 using p_text = DOMText*;
7 using p_doc = DOMDocument*;
8 using p_impl = DOMImplementation*;
9 
10 void safeSetAttribute(p_el pElem, std::string key, std::string val)
11 {
12  std::cout << "beginning safeSetAttribute" << std::endl;
13  XMLCh* xmlchKey = XMLString::transcode(key.c_str());
14  XMLCh* xmlchVal = XMLString::transcode(val.c_str());
15 
16  pElem->setAttribute(xmlchKey, xmlchVal);
17 
18  XMLString::release(&xmlchKey);
19  XMLString::release(&xmlchVal);
20 }
21 
22 void safeSetTextContent(p_el pElem, std::string textContent)
23 {
24  std::cout << "beginning safeSetTextContent" << std::endl;
25  XMLCh* xmlchText = XMLString::transcode(textContent.c_str());
26 
27  pElem->setTextContent(xmlchText);
28 
29  XMLString::release(&xmlchText);
30  std::cout << "safeSetTextContent finished" << std::endl << std::endl;
31 }
32 
33 std::string safeTranscode(XMLCh* xmlstring)
34 {
35  std::cout << "beginning safeTranscode" << std::endl;
36  char* cString = XMLString::transcode(xmlstring);
37 
38  std::string s(cString);
39 
40  XMLString::release(&cString);
41 
42  std::cout << "safeTranscode finished" << std::endl << std::endl;
43  return s;
44 }
45 
46 p_el safeCreateElement(p_doc pParent, std::string name)
47 {
48  std::cout << "beginning safeCreateElement" << std::endl;
49  XMLCh* xmlchName = XMLString::transcode(name.c_str());
50 
51  p_el pChild = pParent->createElement(xmlchName);
52 
53  XMLString::release(&xmlchName);
54 
55  std::cout << "safeCreateElement finished" << std::endl << std::endl;
56  return pChild;
57 }
58 
60 {
61  XMLPlatformUtils::Initialize();
62  XMLPlatformUtils::Terminate();
63 }
64 
65 std::string getFootstepSDFStringPugi(Eigen::MatrixX3d footsteps)
66 {
67  std::cout << "entered pugi xml" << std::endl;
68  int n_steps = footsteps.rows();
69 
70  pugi::xml_document doc;
71 
72  pugi::xml_node declarationNode = doc.append_child(pugi::node_declaration);
73 
74  declarationNode.append_attribute("version") = "1.0";
75 
76  declarationNode.append_attribute("encoding") = "ISO-8859-1";
77 
78  declarationNode.append_attribute("standalone") = "yes";
79 
80  pugi::xml_node sdf = doc.append_child("sdf");
81 
82  sdf.append_attribute("version") = "1.7";
83 
84  pugi::xml_node model = sdf.append_child("model");
85 
86  model.append_attribute("name") = "footsteps";
87 
88  pugi::xml_node elem_static = model.append_child("static");
89 
90  elem_static.text().set("1");
91 
92  pugi::xml_node pose = model.append_child("pose");
93 
94  pose.text().set("0 0 0 0 0 0");
95 
96  Eigen::Vector3d yellow(1, 1, 0);
97 
98  Eigen::Vector3d green(0, 1, 0);
99 
100  Eigen::Matrix3Xd colors = interpolateColors(yellow, green, n_steps);
101 
103 
104  Eigen::Vector3d step;
105 
106  std::string footstep_name;
107 
108  std::string color_string;
109 
110  std::string pose_string;
111 
112  pugi::xml_node footstep_model;
113 
114  pugi::xml_node link;
115 
116  /*
117  pugi::xml_node inertial;
118 
119  pugi::xml_node mass;
120 
121  pugi::xml_node value;
122 
123  pugi::xml_node ixx;
124 
125  pugi::xml_node ixy;
126 
127  pugi::xml_node ixz;
128 
129  pugi::xml_node iyy;
130 
131  pugi::xml_node iyz;
132 
133  pugi::xml_node izz;
134 
135  pugi::xml_node inertia;
136  */
137  pugi::xml_node visual;
138 
139  pugi::xml_node geometry;
140 
141  pugi::xml_node cylinder;
142 
143  pugi::xml_node radius;
144 
145  pugi::xml_node length;
146 
147  pugi::xml_node material;
148 
149  pugi::xml_node ambient;
150 
151  pugi::xml_node diffuse;
152 
153  pugi::xml_node specular;
154 
155  pugi::xml_node emissive;
156 
157  for (int i = 0; i < n_steps; ++i)
158  {
159  c = colors.col(i);
160  step = footsteps.row(i).transpose();
161 
162  footstep_name = "footstep " + std::to_string(i);
163  color_string = std::to_string(c(0)) + " " + std::to_string(c(1)) + " " + std::to_string(c(2)) + " 1";
164  pose_string = std::to_string(step(0)) + " " + std::to_string(step(1)) + " " + std::to_string(step(2)) + " 0 0 0";
165 
166  footstep_model = model.append_child("model");
167  footstep_model.append_attribute("name") = footstep_name.c_str();
168 
169  elem_static = footstep_model.append_child("static");
170  elem_static.text().set("1");
171 
172  link = footstep_model.append_child("link");
173  link.append_attribute("name") = "footstep_link";
174  /*
175 
176  inertial = link.append_child("inertial");
177 
178  mass = inertial.append_child("mass");
179  value = mass.append_child("value");
180  value.text().set("1.0");
181 
182  inertia = inertial.append_child("inertia");
183  ixx = inertia.append_child("ixx");
184  ixx.text().set("1.0");
185 
186  ixy = inertia.append_child("ixy");
187  ixy.text().set("1.0");
188 
189  ixz = inertia.append_child("ixz");
190  ixz.text().set("1.0");
191 
192  iyy = inertia.append_child("iyy");
193  iyy.text().set("1.0");
194 
195  iyz = inertia.append_child("iyz");
196  iyz.text().set("1.0");
197 
198  izz = inertia.append_child("izz");
199  izz.text().set("1.0");
200  */
201  pose = link.append_child("pose");
202  pose.text().set(pose_string.c_str());
203 
204  visual = link.append_child("visual");
205  visual.append_attribute("name") = "footstep_visual";
206 
207  geometry = visual.append_child("geometry");
208  cylinder = geometry.append_child("cylinder");
209 
210  radius = cylinder.append_child("radius");
211  radius.text().set("0.02");
212 
213  length = cylinder.append_child("length");
214  length.text().set("0.01");
215 
216  material = visual.append_child("material");
217 
218  ambient = material.append_child("ambient");
219  ambient.text().set(color_string.c_str());
220 
221  diffuse = material.append_child("diffuse");
222  diffuse.text().set(color_string.c_str());
223 
224  specular = material.append_child("specular");
225  specular.text().set("0.1 0.1 0.1 1");
226 
227  emissive = material.append_child("emissive");
228  emissive.text().set(color_string.c_str());
229  }
230 
231  std::stringstream ss;
232 
233  doc.print(ss);
234 
235  return ss.str();
236 }
237 
238 std::string getFootstepSDFString(Eigen::MatrixX3d footsteps)
239 {
240  std::cout << "getFootstepSDFString: about to initialize" << std::endl;
241  XMLPlatformUtils::Initialize();
242  std::cout << "right after initialization" << std::endl;
243 
244  p_impl p_DOMImplementation = nullptr;
245 
246  XMLCh* core = XMLString::transcode("core");
247 
248  p_DOMImplementation = DOMImplementationRegistry::getDOMImplementation(core);
249  std::cout << "right after registration" << std::endl;
250 
251  XMLString::release(&core);
252 
253  p_doc p_DOMDocument = nullptr;
254 
255  XMLCh* sdf = XMLString::transcode("sdf");
256 
257  p_DOMDocument = p_DOMImplementation->createDocument(0, sdf, 0);
258 
259  XMLString::release(&sdf);
260 
261  p_el pRoot = p_DOMDocument->getDocumentElement();
262 
263  safeSetAttribute(pRoot, "version", "1.7");
264 
265  int n_steps = footsteps.rows();
266 
267  p_el pModel = safeCreateElement(p_DOMDocument, "model");
268 
269  pRoot->appendChild(pModel);
270 
271  safeSetAttribute(pModel, "name", "footsteps");
272 
273  p_el pStatic = safeCreateElement(p_DOMDocument, "static");
274 
275  pModel->appendChild(pStatic);
276 
277  safeSetTextContent(pStatic, "1");
278 
279  p_el pPose = safeCreateElement(p_DOMDocument, "pose");
280 
281  pModel->appendChild(pPose);
282 
283  safeSetTextContent(pPose, "0 0 0 0 0 0");
284 
285  Eigen::Vector3d yellow(1, 1, 0);
286 
287  Eigen::Vector3d green(0, 1, 0);
288 
289  Eigen::Matrix3Xd colors = interpolateColors(yellow, green, n_steps);
290 
292 
293  Eigen::Vector3d step;
294 
295  p_el pFootstep;
296 
297  p_el pLink;
298 
299  p_el pVisual;
300 
301  p_el pGeometry;
302 
303  p_el pCylinder;
304 
305  p_el pRadius;
306 
307  p_el pLength;
308 
309  p_el pMaterial;
310 
311  p_el pAmbient;
312 
313  p_el pDiffuse;
314 
315  p_el pSpecular;
316 
317  p_el pEmissive;
318 
319  std::string footstep_name;
320 
321  std::string color_string;
322 
323  std::string pose_string;
324  std::cout << "xml:right before for loop" << std::endl;
325  for (int i = 0; i < n_steps; ++i)
326  {
327  c = colors.col(i);
328 
329  step = footsteps.row(i).transpose();
330 
331  pFootstep = safeCreateElement(p_DOMDocument, "model");
332 
333  pModel->appendChild(pFootstep);
334 
335  footstep_name = "footstep " + std::to_string(i);
336 
337  safeSetAttribute(pFootstep, "name", footstep_name);
338 
339  pStatic = safeCreateElement(p_DOMDocument, "static");
340 
341  pFootstep->appendChild(pStatic);
342 
343  safeSetTextContent(pStatic, "1");
344 
345  color_string = std::to_string(c(0)) + " " + std::to_string(c(1)) + " " + std::to_string(c(2)) + " 1";
346 
347  pLink = safeCreateElement(p_DOMDocument, "link");
348 
349  pFootstep->appendChild(pLink);
350 
351  safeSetAttribute(pLink, "name", "footstep_link");
352 
353  pPose = safeCreateElement(p_DOMDocument, "pose");
354 
355  pLink->appendChild(pPose);
356 
357  pose_string = std::to_string(step(0)) + " " + std::to_string(step(1)) + " " + std::to_string(step(2)) + " 0 0 0";
358 
359  safeSetTextContent(pPose, pose_string);
360 
361  pVisual = safeCreateElement(p_DOMDocument, "visual");
362 
363  pLink->appendChild(pVisual);
364 
365  safeSetAttribute(pVisual, "name", "footstep_visual");
366 
367  pGeometry = safeCreateElement(p_DOMDocument, "geometry");
368 
369  pVisual->appendChild(pGeometry);
370 
371  pCylinder = safeCreateElement(p_DOMDocument, "cylinder");
372 
373  pGeometry->appendChild(pCylinder);
374 
375  pRadius = safeCreateElement(p_DOMDocument, "radius");
376 
377  pCylinder->appendChild(pRadius);
378 
379  safeSetTextContent(pRadius, "0.02");
380 
381  pLength = safeCreateElement(p_DOMDocument, "length");
382 
383  pCylinder->appendChild(pLength);
384 
385  safeSetTextContent(pLength, "0.01");
386 
387  pMaterial = safeCreateElement(p_DOMDocument, "material");
388 
389  pVisual->appendChild(pMaterial);
390 
391  pAmbient = safeCreateElement(p_DOMDocument, "ambient");
392 
393  pMaterial->appendChild(pAmbient);
394 
395  safeSetTextContent(pAmbient, color_string);
396 
397  pDiffuse = safeCreateElement(p_DOMDocument, "diffuse");
398 
399  pMaterial->appendChild(pDiffuse);
400 
401  safeSetTextContent(pDiffuse, color_string);
402 
403  pSpecular = safeCreateElement(p_DOMDocument, "specular");
404 
405  pMaterial->appendChild(pSpecular);
406 
407  safeSetTextContent(pSpecular, "0.1 0.1 0.1 1");
408 
409  pEmissive = safeCreateElement(p_DOMDocument, "emissive");
410 
411  pMaterial->appendChild(pEmissive);
412 
413  safeSetTextContent(pEmissive, color_string);
414  }
415  std::cout << "right after for loop" << std::endl;
416 
417  DOMLSSerializer* p_LSSerializer = nullptr;
418 
419  p_LSSerializer = p_DOMImplementation->createLSSerializer();
420 
421  std::string res = XMLString::transcode(p_LSSerializer->writeToString(p_DOMDocument));
422 
423  p_DOMDocument->release();
424 
425  p_LSSerializer->release();
426 
427  XMLPlatformUtils::Terminate();
428 
429  return res;
430 }
Eigen::Matrix3Xd interpolateColors(Eigen::Vector3d begin_rgb, Eigen::Vector3d end_rgb, int nsteps)
Definition: color_interp.cpp:3
Eigen::Vector3d Vector3d
Definition: kinematics.h:49
void safeSetTextContent(p_el pElem, std::string textContent)
Definition: xml_utils.cpp:22
DOMElement * p_el
Definition: xml_utils.cpp:5
std::string getFootstepSDFString(Eigen::MatrixX3d footsteps)
Definition: xml_utils.cpp:238
std::string getFootstepSDFStringPugi(Eigen::MatrixX3d footsteps)
Definition: xml_utils.cpp:65
void safeSetAttribute(p_el pElem, std::string key, std::string val)
Definition: xml_utils.cpp:10
std::string safeTranscode(XMLCh *xmlstring)
Definition: xml_utils.cpp:33
DOMText * p_text
Definition: xml_utils.cpp:6
p_el safeCreateElement(p_doc pParent, std::string name)
Definition: xml_utils.cpp:46
void dummyExample()
Definition: xml_utils.cpp:59
DOMDocument * p_doc
Definition: xml_utils.cpp:7
DOMImplementation * p_impl
Definition: xml_utils.cpp:8