45 #include <visp3/core/vpXmlParserHomogeneousMatrix.h>
47 #if defined(VISP_HAVE_PUGIXML)
48 #include <pugixml.hpp>
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"
65 #ifndef DOXYGEN_SHOULD_SKIP_THIS
66 class vpXmlParserHomogeneousMatrix::Impl
87 Impl() : m_M(), m_name() { }
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;
99 pugi::xml_node node = doc.document_element();
104 int ret = read(node, name);
118 int read(
const pugi::xml_node &node_,
const std::string &name)
123 unsigned int nbM = 0;
125 for (pugi::xml_node node = node_.first_child(); node; node = node.next_sibling()) {
126 if (node.type() != pugi::node_element)
129 if (
SEQUENCE_OK != str2xmlcode(node.name(), prop)) {
130 prop = CODE_XML_OTHER;
134 if (prop == CODE_XML_M) {
146 std::cerr <<
"No Homogeneous matrix is available" << std::endl <<
"with name: " << name << std::endl;
150 std::cerr << nbM <<
" There are more Homogeneous matrix" << std::endl
151 <<
"with the same name : " << std::endl
152 <<
"precise your choice..." << std::endl;
166 int read_matrix(
const pugi::xml_node &node_,
const std::string &name)
170 std::string M_name_tmp =
"";
175 for (pugi::xml_node node = node_.first_child(); node; node = node.next_sibling()) {
176 if (node.type() != pugi::node_element)
179 if (
SEQUENCE_OK != str2xmlcode(node.name(), prop)) {
180 prop = CODE_XML_OTHER;
185 case CODE_XML_M_NAME: {
186 M_name_tmp = node.text().as_string();
191 if (name == M_name_tmp) {
192 std::cout <<
"Found Homogeneous Matrix with name: \"" << M_name_tmp <<
"\"" << std::endl;
193 back = read_values(node, M_tmp);
213 if (!(name == M_name_tmp)) {
220 this->m_name = M_name_tmp;
249 for (pugi::xml_node node = node_.first_child(); node; node = node.next_sibling()) {
250 if (node.type() != pugi::node_element)
253 if (
SEQUENCE_OK != str2xmlcode(node.name(), prop)) {
254 prop = CODE_XML_OTHER;
260 tx_ = node.text().as_double();
264 ty_ = node.text().as_double();
268 tz_ = node.text().as_double();
272 tux_ = node.text().as_double();
276 tuy_ = node.text().as_double();
280 tuz_ = node.text().as_double();
287 case CODE_XML_M_NAME:
297 std::cerr <<
"ERROR in 'model' field:\n";
298 std::cerr <<
"it must contain 6 parameters\n";
304 M.
buildFrom(tx_, ty_, tz_, tux_, tuy_, tuz_);
319 pugi::xml_document doc;
322 if (!doc.load_file(filename.c_str(), pugi::parse_default | pugi::parse_comments)) {
323 node = doc.append_child(pugi::node_declaration);
324 node.append_attribute(
"version") =
"1.0";
325 node = doc.append_child(LABEL_XML_ROOT);
326 pugi::xml_node nodeComment = node.append_child(pugi::node_comment);
327 nodeComment.set_value(
"This file stores homogeneous matrix used\n"
328 " in the vpHomogeneousMatrix Class of ViSP available\n"
329 " at https://visp.inria.fr/download/ .\n"
330 " It can be read with the parse method of\n"
331 " the vpXmlParserHomogeneousMatrix class.");
334 node = doc.document_element();
341 int M_isFound = count(node, name);
344 std::cout <<
"There is already an homogeneous matrix " << std::endl
345 <<
"available in the file with the input name: " << name <<
"." << std::endl
346 <<
"Please delete it manually from the xml file." << std::endl;
352 doc.save_file(filename.c_str(), PUGIXML_TEXT(
" "));
367 int count(
const pugi::xml_node &node_,
const std::string &name)
372 for (pugi::xml_node node = node_.first_child(); node; node = node.next_sibling()) {
373 if (node.type() != pugi::node_element)
376 if (
SEQUENCE_OK != str2xmlcode(node.name(), prop)) {
377 prop = CODE_XML_OTHER;
379 if (prop == CODE_XML_M) {
397 int write(pugi::xml_node &node,
const std::string &name)
401 pugi::xml_node node_tmp;
402 pugi::xml_node node_matrix;
403 pugi::xml_node node_values;
412 node_tmp = node.append_child(pugi::node_comment);
413 node_tmp.set_value(
"Homogeneous Matrix");
414 node_matrix = node.append_child(LABEL_XML_M);
418 node_tmp = node_matrix.append_child(pugi::node_comment);
419 node_tmp.set_value(
"Name of the homogeneous matrix");
420 node_matrix.append_child(LABEL_XML_M_NAME).append_child(pugi::node_pcdata).set_value(name.c_str());
424 node_values = node_matrix.append_child(LABEL_XML_VALUE);
426 node_tmp = node_values.append_child(pugi::node_comment);
427 node_tmp.set_value(
"Translation vector with values in meters");
430 node_values.append_child(LABEL_XML_TX).append_child(pugi::node_pcdata).text() = m_M[0][3];
433 node_values.append_child(LABEL_XML_TY).append_child(pugi::node_pcdata).text() = m_M[1][3];
436 node_values.append_child(LABEL_XML_TZ).append_child(pugi::node_pcdata).text() = m_M[2][3];
438 node_tmp = node_values.append_child(pugi::node_comment);
439 node_tmp.set_value(
"Rotational vector expressed in angle axis "
440 "representation with values in radians");
443 node_values.append_child(LABEL_XML_TUX).append_child(pugi::node_pcdata).text() = tu[0];
446 node_values.append_child(LABEL_XML_TUY).append_child(pugi::node_pcdata).text() = tu[1];
449 node_values.append_child(LABEL_XML_TUZ).append_child(pugi::node_pcdata).text() = tu[2];
465 vpXmlCodeType val_int = CODE_XML_BAD;
468 if (!strcmp(str, LABEL_XML_M)) {
469 val_int = CODE_XML_M;
471 else if (!strcmp(str, LABEL_XML_M_NAME)) {
472 val_int = CODE_XML_M_NAME;
474 else if (!strcmp(str, LABEL_XML_VALUE)) {
475 val_int = CODE_XML_VALUE;
477 else if (!strcmp(str, LABEL_XML_TX)) {
478 val_int = CODE_XML_TX;
480 else if (!strcmp(str, LABEL_XML_TY)) {
481 val_int = CODE_XML_TY;
483 else if (!strcmp(str, LABEL_XML_TZ)) {
484 val_int = CODE_XML_TZ;
486 else if (!strcmp(str, LABEL_XML_TUX)) {
487 val_int = CODE_XML_TUX;
489 else if (!strcmp(str, LABEL_XML_TUY)) {
490 val_int = CODE_XML_TUY;
492 else if (!strcmp(str, LABEL_XML_TUZ)) {
493 val_int = CODE_XML_TUZ;
496 val_int = CODE_XML_OTHER;
528 return m_impl->parse(M, filename, name);
540 const std::string &name)
542 return m_impl->save(M, filename, name);
547 return m_impl->getHomogeneousMatrix();
552 return m_impl->getHomogeneousMatrixName();
557 m_impl->setHomogeneousMatrixName(name);
560 #elif !defined(VISP_BUILD_SHARED_LIBS)
562 void dummy_vpXmlParserHomogeneousMatrix() { };
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)
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)