Visual Servoing Platform  version 3.6.1 under development (2024-03-29)
vpXmlParserHomogeneousMatrix.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See https://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * XML parser to load and save Homogeneous Matrix in a XML file
33  *
34  * Authors:
35  * Giovanni Claudio
36  *
37 *****************************************************************************/
38 
45 #include <visp3/core/vpXmlParserHomogeneousMatrix.h>
46 
47 #if defined(VISP_HAVE_PUGIXML)
48 #include <pugixml.hpp>
49 
50 /* ----------------------------- LABEL XML ----------------------------- */
51 /* --------------------------------------------------------------------- */
52 #define LABEL_XML_ROOT "root"
53 #define LABEL_XML_M "homogeneous_transformation"
54 #define LABEL_XML_M_NAME "name"
55 #define LABEL_XML_VALUE "values"
56 #define LABEL_XML_TRANSLATION "translation"
57 #define LABEL_XML_TX "tx"
58 #define LABEL_XML_TY "ty"
59 #define LABEL_XML_TZ "tz"
60 #define LABEL_XML_ROTATION "rotation"
61 #define LABEL_XML_TUX "theta_ux"
62 #define LABEL_XML_TUY "theta_uy"
63 #define LABEL_XML_TUZ "theta_uz"
64 
65 #ifndef DOXYGEN_SHOULD_SKIP_THIS
66 class vpXmlParserHomogeneousMatrix::Impl
67 {
68 private:
69  /* --- XML Code------------------------------------------------------------
70  */
71  enum vpXmlCodeType
72  {
73  CODE_XML_BAD = -1,
74  CODE_XML_OTHER,
75  CODE_XML_M,
76  CODE_XML_M_NAME,
77  CODE_XML_VALUE,
78  CODE_XML_TX,
79  CODE_XML_TY,
80  CODE_XML_TZ,
81  CODE_XML_TUX,
82  CODE_XML_TUY,
83  CODE_XML_TUZ
84  };
85 
86 public:
87  Impl() : m_M(), m_name() { }
88 
89  int parse(vpHomogeneousMatrix &M, const std::string &filename, const std::string &name)
90  {
91  pugi::xml_document doc;
92  if (!doc.load_file(filename.c_str())) {
93  std::cerr << std::endl << "ERROR:" << std::endl;
94  std::cerr << " I cannot open the file " << filename << std::endl;
95 
96  return SEQUENCE_ERROR;
97  }
98 
99  pugi::xml_node node = doc.document_element();
100  if (!node) {
101  return SEQUENCE_ERROR;
102  }
103 
104  int ret = read(node, name);
105 
106  M = m_M;
107 
108  return ret;
109  }
110 
118  int read(const pugi::xml_node &node_, const std::string &name)
119  {
120  vpXmlCodeType prop;
121 
123  unsigned int nbM = 0;
124 
125  for (pugi::xml_node node = node_.first_child(); node; node = node.next_sibling()) {
126  if (node.type() != pugi::node_element)
127  continue;
128 
129  if (SEQUENCE_OK != str2xmlcode(node.name(), prop)) {
130  prop = CODE_XML_OTHER;
131  back = SEQUENCE_ERROR;
132  }
133 
134  if (prop == CODE_XML_M) {
135  if (SEQUENCE_OK == read_matrix(node, name))
136  nbM++;
137  }
138  else
139  back = SEQUENCE_ERROR;
140  }
141 
142  if (nbM == 0) {
143  back = SEQUENCE_ERROR;
144  std::cerr << "No Homogeneous matrix is available" << std::endl << "with name: " << name << std::endl;
145  }
146  else if (nbM > 1) {
147  back = SEQUENCE_ERROR;
148  std::cerr << nbM << " There are more Homogeneous matrix" << std::endl
149  << "with the same name : " << std::endl
150  << "precise your choice..." << std::endl;
151  }
152 
153  return back;
154  }
155 
164  int read_matrix(const pugi::xml_node &node_, const std::string &name)
165  {
166  vpXmlCodeType prop;
167  /* read value in the XML file. */
168  std::string M_name_tmp = "";
169  vpHomogeneousMatrix M_tmp;
170 
172 
173  for (pugi::xml_node node = node_.first_child(); node; node = node.next_sibling()) {
174  if (node.type() != pugi::node_element)
175  continue;
176 
177  if (SEQUENCE_OK != str2xmlcode(node.name(), prop)) {
178  prop = CODE_XML_OTHER;
179  back = SEQUENCE_ERROR;
180  }
181 
182  switch (prop) {
183  case CODE_XML_M_NAME: {
184  M_name_tmp = node.text().as_string();
185  break;
186  }
187 
188  case CODE_XML_VALUE: // VALUE
189  if (name == M_name_tmp) {
190  std::cout << "Found Homogeneous Matrix with name: \"" << M_name_tmp << "\"" << std::endl;
191  back = read_values(node, M_tmp);
192  }
193  break;
194 
195  case CODE_XML_BAD:
196  case CODE_XML_OTHER:
197  case CODE_XML_M:
198  case CODE_XML_TX:
199  case CODE_XML_TY:
200  case CODE_XML_TZ:
201  case CODE_XML_TUX:
202  case CODE_XML_TUY:
203  case CODE_XML_TUZ:
204 
205  default:
206  back = SEQUENCE_ERROR;
207  break;
208  }
209  }
210 
211  if (!(name == M_name_tmp)) {
212  back = SEQUENCE_ERROR;
213  }
214  else {
215  this->m_M = M_tmp;
216  // std::cout << "Convert in Homogeneous Matrix:"<< std::endl;
217  // std::cout << this-> M << std::endl;
218  this->m_name = M_name_tmp;
219  }
220  return back;
221  }
222 
231  vpXmlCodeSequenceType read_values(const pugi::xml_node &node_, vpHomogeneousMatrix &M)
232  {
233  // counter of the number of read parameters
234  int nb = 0;
235  vpXmlCodeType prop;
236  /* read value in the XML file. */
237 
238  double tx_ = 0.;
239  double ty_ = 0.;
240  double tz_ = 0.;
241  double tux_ = 0.;
242  double tuy_ = 0.;
243  double tuz_ = 0.;
244 
246 
247  for (pugi::xml_node node = node_.first_child(); node; node = node.next_sibling()) {
248  if (node.type() != pugi::node_element)
249  continue;
250 
251  if (SEQUENCE_OK != str2xmlcode(node.name(), prop)) {
252  prop = CODE_XML_OTHER;
253  back = SEQUENCE_ERROR;
254  }
255 
256  switch (prop) {
257  case CODE_XML_TX:
258  tx_ = node.text().as_double();
259  nb++;
260  break;
261  case CODE_XML_TY:
262  ty_ = node.text().as_double();
263  nb++;
264  break;
265  case CODE_XML_TZ:
266  tz_ = node.text().as_double();
267  nb++;
268  break;
269  case CODE_XML_TUX:
270  tux_ = node.text().as_double();
271  nb++;
272  break;
273  case CODE_XML_TUY:
274  tuy_ = node.text().as_double();
275  nb++;
276  break;
277  case CODE_XML_TUZ:
278  tuz_ = node.text().as_double();
279  nb++;
280  break;
281 
282  case CODE_XML_BAD:
283  case CODE_XML_OTHER:
284  case CODE_XML_M:
285  case CODE_XML_M_NAME:
286  case CODE_XML_VALUE:
287 
288  default:
289  back = SEQUENCE_ERROR;
290  break;
291  }
292  }
293 
294  if (nb != 6) {
295  std::cerr << "ERROR in 'model' field:\n";
296  std::cerr << "it must contain 6 parameters\n";
297 
298  return SEQUENCE_ERROR;
299  }
300 
301  // Create the Homogeneous matrix
302  M.buildFrom(tx_, ty_, tz_, tux_, tuy_, tuz_);
303 
304  // std::cout << "Read values from file:" << std::endl;
305  // std::cout << "tx:" << tx_<< std::endl;
306  // std::cout << "ty:" << ty_<< std::endl;
307  // std::cout << "tz:" << tz_<< std::endl;
308  // std::cout << "tux:" << tux_<< std::endl;
309  // std::cout << "tuy:" << tuy_<< std::endl;
310  // std::cout << "tuz:" << tuz_<< std::endl;
311 
312  return back;
313  }
314 
315  int save(const vpHomogeneousMatrix &M, const std::string &filename, const std::string &name)
316  {
317  pugi::xml_document doc;
318  pugi::xml_node node;
319 
320  if (!doc.load_file(filename.c_str(), pugi::parse_default | pugi::parse_comments)) {
321  node = doc.append_child(pugi::node_declaration);
322  node.append_attribute("version") = "1.0";
323  node = doc.append_child(LABEL_XML_ROOT);
324  pugi::xml_node nodeComment = node.append_child(pugi::node_comment);
325  nodeComment.set_value("This file stores homogeneous matrix used\n"
326  " in the vpHomogeneousMatrix Class of ViSP available\n"
327  " at https://visp.inria.fr/download/ .\n"
328  " It can be read with the parse method of\n"
329  " the vpXmlParserHomogeneousMatrix class.");
330  }
331 
332  node = doc.document_element();
333  if (!node) {
334  return SEQUENCE_ERROR;
335  }
336 
337  m_M = M;
338 
339  int M_isFound = count(node, name);
340 
341  if (M_isFound) {
342  std::cout << "There is already an homogeneous matrix " << std::endl
343  << "available in the file with the input name: " << name << "." << std::endl
344  << "Please delete it manually from the xml file." << std::endl;
345  return SEQUENCE_ERROR;
346  }
347 
348  write(node, name);
349 
350  doc.save_file(filename.c_str(), PUGIXML_TEXT(" "));
351 
352  return SEQUENCE_OK;
353  }
354 
365  int count(const pugi::xml_node &node_, const std::string &name)
366  {
367  vpXmlCodeType prop;
368  int nbM = 0;
369 
370  for (pugi::xml_node node = node_.first_child(); node; node = node.next_sibling()) {
371  if (node.type() != pugi::node_element)
372  continue;
373 
374  if (SEQUENCE_OK != str2xmlcode(node.name(), prop)) {
375  prop = CODE_XML_OTHER;
376  }
377  if (prop == CODE_XML_M) {
378  if (SEQUENCE_OK == read_matrix(node, name))
379  nbM++;
380  }
381  }
382 
383  return nbM;
384  }
385 
394  int write(pugi::xml_node &node, const std::string &name)
395  {
396  int back = SEQUENCE_OK;
397 
398  pugi::xml_node node_tmp;
399  pugi::xml_node node_matrix;
400  pugi::xml_node node_values;
401 
402  // Convert from Rotational matrix to Theta-U vector
404  m_M.extract(R);
405 
406  vpThetaUVector tu(R);
407 
408  // <homogeneous_transformation>
409  node_tmp = node.append_child(pugi::node_comment);
410  node_tmp.set_value("Homogeneous Matrix");
411  node_matrix = node.append_child(LABEL_XML_M);
412  {
413  //<name>
414  if (!name.empty()) {
415  node_tmp = node_matrix.append_child(pugi::node_comment);
416  node_tmp.set_value("Name of the homogeneous matrix");
417  node_matrix.append_child(LABEL_XML_M_NAME).append_child(pugi::node_pcdata).set_value(name.c_str());
418  }
419 
420  //<values>
421  node_values = node_matrix.append_child(LABEL_XML_VALUE);
422  {
423  node_tmp = node_values.append_child(pugi::node_comment);
424  node_tmp.set_value("Translation vector with values in meters");
425 
426  //<tx>
427  node_values.append_child(LABEL_XML_TX).append_child(pugi::node_pcdata).text() = m_M[0][3];
428 
429  //<ty>
430  node_values.append_child(LABEL_XML_TY).append_child(pugi::node_pcdata).text() = m_M[1][3];
431 
432  //<tz>
433  node_values.append_child(LABEL_XML_TZ).append_child(pugi::node_pcdata).text() = m_M[2][3];
434 
435  node_tmp = node_values.append_child(pugi::node_comment);
436  node_tmp.set_value("Rotational vector expressed in angle axis "
437  "representation with values in radians");
438 
439  //<tux>
440  node_values.append_child(LABEL_XML_TUX).append_child(pugi::node_pcdata).text() = tu[0];
441 
442  //<tuy>
443  node_values.append_child(LABEL_XML_TUY).append_child(pugi::node_pcdata).text() = tu[1];
444 
445  //<tuz>
446  node_values.append_child(LABEL_XML_TUZ).append_child(pugi::node_pcdata).text() = tu[2];
447  }
448  }
449 
450  return back;
451  }
452 
460  vpXmlCodeSequenceType str2xmlcode(const char *str, vpXmlCodeType &res)
461  {
462  vpXmlCodeType val_int = CODE_XML_BAD;
464 
465  if (!strcmp(str, LABEL_XML_M)) {
466  val_int = CODE_XML_M;
467  }
468  else if (!strcmp(str, LABEL_XML_M_NAME)) {
469  val_int = CODE_XML_M_NAME;
470  }
471  else if (!strcmp(str, LABEL_XML_VALUE)) {
472  val_int = CODE_XML_VALUE;
473  }
474  else if (!strcmp(str, LABEL_XML_TX)) {
475  val_int = CODE_XML_TX;
476  }
477  else if (!strcmp(str, LABEL_XML_TY)) {
478  val_int = CODE_XML_TY;
479  }
480  else if (!strcmp(str, LABEL_XML_TZ)) {
481  val_int = CODE_XML_TZ;
482  }
483  else if (!strcmp(str, LABEL_XML_TUX)) {
484  val_int = CODE_XML_TUX;
485  }
486  else if (!strcmp(str, LABEL_XML_TUY)) {
487  val_int = CODE_XML_TUY;
488  }
489  else if (!strcmp(str, LABEL_XML_TUZ)) {
490  val_int = CODE_XML_TUZ;
491  }
492  else {
493  val_int = CODE_XML_OTHER;
494  }
495  res = val_int;
496 
497  return back;
498  }
499 
500  vpHomogeneousMatrix getHomogeneousMatrix() const { return m_M; }
501  std::string getHomogeneousMatrixName() const { return m_name; }
502 
503  void setHomogeneousMatrixName(const std::string &name) { m_name = name; }
504 
505 private:
507  std::string m_name;
508 };
509 #endif // DOXYGEN_SHOULD_SKIP_THIS
510 
512 
514 
523 int vpXmlParserHomogeneousMatrix::parse(vpHomogeneousMatrix &M, const std::string &filename, const std::string &name)
524 {
525  return m_impl->parse(M, filename, name);
526 }
527 
536 int vpXmlParserHomogeneousMatrix::save(const vpHomogeneousMatrix &M, const std::string &filename,
537  const std::string &name)
538 {
539  return m_impl->save(M, filename, name);
540 }
541 
543 {
544  return m_impl->getHomogeneousMatrix();
545 }
546 
548 {
549  return m_impl->getHomogeneousMatrixName();
550 }
551 
553 {
554  m_impl->setHomogeneousMatrixName(name);
555 }
556 
557 #elif !defined(VISP_BUILD_SHARED_LIBS)
558 // Work around to avoid warning: libvisp_core.a(vpXmlParserHomogeneousMatrix.cpp.o) has no symbols
559 void dummy_vpXmlParserHomogeneousMatrix() { };
560 
561 #endif
Implementation of an homogeneous matrix and operations on such kind of matrices.
void buildFrom(const vpTranslationVector &t, const vpRotationMatrix &R)
Implementation of a rotation matrix and operations on such kind of matrices.
Implementation of a rotation vector as axis-angle minimal representation.
vpHomogeneousMatrix getHomogeneousMatrix() const
int parse(vpHomogeneousMatrix &M, const std::string &filename, const std::string &name)
int save(const vpHomogeneousMatrix &M, const std::string &filename, const std::string &name)
void setHomogeneousMatrixName(const std::string &name)