40#include <visp3/core/vpXmlParserHomogeneousMatrix.h>
42#if defined(VISP_HAVE_PUGIXML)
47#define LABEL_XML_ROOT "root"
48#define LABEL_XML_M "homogeneous_transformation"
49#define LABEL_XML_M_NAME "name"
50#define LABEL_XML_VALUE "values"
51#define LABEL_XML_TX "tx"
52#define LABEL_XML_TY "ty"
53#define LABEL_XML_TZ "tz"
54#define LABEL_XML_TUX "theta_ux"
55#define LABEL_XML_TUY "theta_uy"
56#define LABEL_XML_TUZ "theta_uz"
59#ifndef DOXYGEN_SHOULD_SKIP_THIS
60class vpXmlParserHomogeneousMatrix::Impl
81 Impl() : m_M(), m_name() { }
83 int parse(vpHomogeneousMatrix &M,
const std::string &filename,
const std::string &name)
85 pugi::xml_document doc;
86 if (!doc.load_file(
filename.c_str())) {
87 std::cerr << std::endl <<
"ERROR:" << std::endl;
88 std::cerr <<
" I cannot open the file " <<
filename << std::endl;
90 return SEQUENCE_ERROR;
93 pugi::xml_node node = doc.document_element();
95 return SEQUENCE_ERROR;
98 int ret = read(node, name);
112 int read(
const pugi::xml_node &node_,
const std::string &name)
116 vpXmlCodeSequenceType back = SEQUENCE_OK;
117 unsigned int nbM = 0;
119 for (pugi::xml_node node = node_.first_child(); node; node = node.next_sibling()) {
120 if (node.type() == pugi::node_element) {
121 if (SEQUENCE_OK != str2xmlcode(node.name(), prop)) {
122 prop = CODE_XML_OTHER;
123 back = SEQUENCE_ERROR;
126 if (prop == CODE_XML_M) {
127 if (SEQUENCE_OK == read_matrix(node, name)) {
132 back = SEQUENCE_ERROR;
138 back = SEQUENCE_ERROR;
139 std::cerr <<
"No Homogeneous matrix is available" << std::endl <<
"with name: " << name << std::endl;
142 back = SEQUENCE_ERROR;
143 std::cerr << nbM <<
" There are more Homogeneous matrix" << std::endl
144 <<
"with the same name : " << std::endl
145 <<
"precise your choice..." << std::endl;
159 int read_matrix(
const pugi::xml_node &node_,
const std::string &name)
163 std::string M_name_tmp =
"";
164 vpHomogeneousMatrix M_tmp;
166 vpXmlCodeSequenceType back = SEQUENCE_OK;
168 for (pugi::xml_node node = node_.first_child(); node; node = node.next_sibling()) {
169 if (node.type() == pugi::node_element) {
170 if (SEQUENCE_OK != str2xmlcode(node.name(), prop)) {
171 prop = CODE_XML_OTHER;
172 back = SEQUENCE_ERROR;
176 case CODE_XML_M_NAME: {
177 M_name_tmp = node.text().as_string();
182 if (name == M_name_tmp) {
183 std::cout <<
"Found Homogeneous Matrix with name: \"" << M_name_tmp <<
"\"" << std::endl;
184 back = read_values(node, M_tmp);
198 back = SEQUENCE_ERROR;
204 if (!(name == M_name_tmp)) {
205 back = SEQUENCE_ERROR;
211 this->m_name = M_name_tmp;
224 vpXmlCodeSequenceType read_values(
const pugi::xml_node &node_, vpHomogeneousMatrix &M)
238 vpXmlCodeSequenceType back = SEQUENCE_OK;
240 for (pugi::xml_node node = node_.first_child(); node; node = node.next_sibling()) {
241 if (node.type() == pugi::node_element) {
242 if (SEQUENCE_OK != str2xmlcode(node.name(), prop)) {
243 prop = CODE_XML_OTHER;
244 back = SEQUENCE_ERROR;
249 tx_ = node.text().as_double();
253 ty_ = node.text().as_double();
257 tz_ = node.text().as_double();
261 tux_ = node.text().as_double();
265 tuy_ = node.text().as_double();
269 tuz_ = node.text().as_double();
276 case CODE_XML_M_NAME:
279 back = SEQUENCE_ERROR;
286 std::cerr <<
"ERROR in 'model' field:\n";
287 std::cerr <<
"it must contain 6 parameters\n";
289 return SEQUENCE_ERROR;
293 M.
buildFrom(tx_, ty_, tz_, tux_, tuy_, tuz_);
306 int save(
const vpHomogeneousMatrix &M,
const std::string &filename,
const std::string &name)
308 pugi::xml_document doc;
311 if (!doc.load_file(
filename.c_str(), pugi::parse_default | pugi::parse_comments)) {
312 node = doc.append_child(pugi::node_declaration);
313 node.append_attribute(
"version") =
"1.0";
314 node = doc.append_child(LABEL_XML_ROOT);
315 pugi::xml_node nodeComment = node.append_child(pugi::node_comment);
316 nodeComment.set_value(
"This file stores homogeneous matrix used\n"
317 " in the vpHomogeneousMatrix Class of ViSP available\n"
318 " at https://visp.inria.fr/download/ .\n"
319 " It can be read with the parse method of\n"
320 " the vpXmlParserHomogeneousMatrix class.");
323 node = doc.document_element();
325 return SEQUENCE_ERROR;
330 int M_isFound = count(node, name);
333 std::cout <<
"There is already an homogeneous matrix " << std::endl
334 <<
"available in the file with the input name: " << name <<
"." << std::endl
335 <<
"Please delete it manually from the xml file." << std::endl;
336 return SEQUENCE_ERROR;
341 doc.save_file(
filename.c_str(), PUGIXML_TEXT(
" "));
356 int count(
const pugi::xml_node &node_,
const std::string &name)
361 for (pugi::xml_node node = node_.first_child(); node; node = node.next_sibling()) {
362 if (node.type() == pugi::node_element) {
363 if (SEQUENCE_OK != str2xmlcode(node.name(), prop)) {
364 prop = CODE_XML_OTHER;
366 if (prop == CODE_XML_M) {
367 if (SEQUENCE_OK == read_matrix(node, name)) {
385 int write(pugi::xml_node &node,
const std::string &name)
387 int back = SEQUENCE_OK;
389 pugi::xml_node node_tmp;
390 pugi::xml_node node_matrix;
391 pugi::xml_node node_values;
397 vpThetaUVector
tu(R);
400 node_tmp = node.append_child(pugi::node_comment);
401 node_tmp.set_value(
"Homogeneous Matrix");
402 node_matrix = node.append_child(LABEL_XML_M);
406 node_tmp = node_matrix.append_child(pugi::node_comment);
407 node_tmp.set_value(
"Name of the homogeneous matrix");
408 node_matrix.append_child(LABEL_XML_M_NAME).append_child(pugi::node_pcdata).set_value(name.c_str());
412 node_values = node_matrix.append_child(LABEL_XML_VALUE);
414 node_tmp = node_values.append_child(pugi::node_comment);
415 node_tmp.set_value(
"Translation vector with values in meters");
418 node_values.append_child(LABEL_XML_TX).append_child(pugi::node_pcdata).text() = m_M[0][3];
421 node_values.append_child(LABEL_XML_TY).append_child(pugi::node_pcdata).text() = m_M[1][3];
424 node_values.append_child(LABEL_XML_TZ).append_child(pugi::node_pcdata).text() = m_M[2][3];
426 node_tmp = node_values.append_child(pugi::node_comment);
427 node_tmp.set_value(
"Rotational vector expressed in angle axis "
428 "representation with values in radians");
431 node_values.append_child(LABEL_XML_TUX).append_child(pugi::node_pcdata).text() =
tu[0];
434 node_values.append_child(LABEL_XML_TUY).append_child(pugi::node_pcdata).text() =
tu[1];
437 node_values.append_child(LABEL_XML_TUZ).append_child(pugi::node_pcdata).text() =
tu[2];
451 vpXmlCodeSequenceType str2xmlcode(
const char *str, vpXmlCodeType &res)
453 vpXmlCodeType val_int = CODE_XML_BAD;
456 if (!strcmp(str, LABEL_XML_M)) {
457 val_int = CODE_XML_M;
459 else if (!strcmp(str, LABEL_XML_M_NAME)) {
460 val_int = CODE_XML_M_NAME;
462 else if (!strcmp(str, LABEL_XML_VALUE)) {
463 val_int = CODE_XML_VALUE;
465 else if (!strcmp(str, LABEL_XML_TX)) {
466 val_int = CODE_XML_TX;
468 else if (!strcmp(str, LABEL_XML_TY)) {
469 val_int = CODE_XML_TY;
471 else if (!strcmp(str, LABEL_XML_TZ)) {
472 val_int = CODE_XML_TZ;
474 else if (!strcmp(str, LABEL_XML_TUX)) {
475 val_int = CODE_XML_TUX;
477 else if (!strcmp(str, LABEL_XML_TUY)) {
478 val_int = CODE_XML_TUY;
480 else if (!strcmp(str, LABEL_XML_TUZ)) {
481 val_int = CODE_XML_TUZ;
484 val_int = CODE_XML_OTHER;
491 vpHomogeneousMatrix getHomogeneousMatrix()
const {
return m_M; }
492 std::string getHomogeneousMatrixName()
const {
return m_name; }
494 void setHomogeneousMatrixName(
const std::string &name) { m_name = name; }
497 vpHomogeneousMatrix m_M;
516 return m_impl->parse(M, filename, name);
528 const std::string &name)
530 return m_impl->save(M, filename, name);
535 return m_impl->getHomogeneousMatrix();
540 return m_impl->getHomogeneousMatrixName();
545 m_impl->setHomogeneousMatrixName(name);
548#elif !defined(VISP_BUILD_SHARED_LIBS)
550void dummy_vpXmlParserHomogeneousMatrix() { }
Implementation of an homogeneous matrix and operations on such kind of matrices.
vpHomogeneousMatrix & buildFrom(const vpTranslationVector &t, const vpRotationMatrix &R)
vpHomogeneousMatrix getHomogeneousMatrix() const
int parse(vpHomogeneousMatrix &M, const std::string &filename, const std::string &name)
std::string getHomogeneousMatrixName() const
int save(const vpHomogeneousMatrix &M, const std::string &filename, const std::string &name)
vpXmlParserHomogeneousMatrix()
~vpXmlParserHomogeneousMatrix()
void setHomogeneousMatrixName(const std::string &name)