40 #include <visp3/core/vpXmlParserHomogeneousMatrix.h>
42 #if defined(VISP_HAVE_PUGIXML)
43 #include <pugixml.hpp>
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_TRANSLATION "translation"
52 #define LABEL_XML_TX "tx"
53 #define LABEL_XML_TY "ty"
54 #define LABEL_XML_TZ "tz"
55 #define LABEL_XML_ROTATION "rotation"
56 #define LABEL_XML_TUX "theta_ux"
57 #define LABEL_XML_TUY "theta_uy"
58 #define LABEL_XML_TUZ "theta_uz"
61 #ifndef DOXYGEN_SHOULD_SKIP_THIS
62 class vpXmlParserHomogeneousMatrix::Impl
83 Impl() : m_M(), m_name() { }
87 pugi::xml_document doc;
88 if (!doc.load_file(filename.c_str())) {
89 std::cerr << std::endl <<
"ERROR:" << std::endl;
90 std::cerr <<
" I cannot open the file " << filename << std::endl;
95 pugi::xml_node node = doc.document_element();
100 int ret = read(node, name);
114 int read(
const pugi::xml_node &node_,
const std::string &name)
119 unsigned int nbM = 0;
121 for (pugi::xml_node node = node_.first_child(); node; node = node.next_sibling()) {
122 if (node.type() == pugi::node_element) {
123 if (
SEQUENCE_OK != str2xmlcode(node.name(), prop)) {
124 prop = CODE_XML_OTHER;
128 if (prop == CODE_XML_M) {
141 std::cerr <<
"No Homogeneous matrix is available" << std::endl <<
"with name: " << name << std::endl;
145 std::cerr << nbM <<
" There are more Homogeneous matrix" << std::endl
146 <<
"with the same name : " << std::endl
147 <<
"precise your choice..." << std::endl;
161 int read_matrix(
const pugi::xml_node &node_,
const std::string &name)
165 std::string M_name_tmp =
"";
170 for (pugi::xml_node node = node_.first_child(); node; node = node.next_sibling()) {
171 if (node.type() == pugi::node_element) {
172 if (
SEQUENCE_OK != str2xmlcode(node.name(), prop)) {
173 prop = CODE_XML_OTHER;
178 case CODE_XML_M_NAME: {
179 M_name_tmp = node.text().as_string();
184 if (name == M_name_tmp) {
185 std::cout <<
"Found Homogeneous Matrix with name: \"" << M_name_tmp <<
"\"" << std::endl;
186 back = read_values(node, M_tmp);
207 if (!(name == M_name_tmp)) {
214 this->m_name = M_name_tmp;
243 for (pugi::xml_node node = node_.first_child(); node; node = node.next_sibling()) {
244 if (node.type() == pugi::node_element) {
245 if (
SEQUENCE_OK != str2xmlcode(node.name(), prop)) {
246 prop = CODE_XML_OTHER;
252 tx_ = node.text().as_double();
256 ty_ = node.text().as_double();
260 tz_ = node.text().as_double();
264 tux_ = node.text().as_double();
268 tuy_ = node.text().as_double();
272 tuz_ = node.text().as_double();
279 case CODE_XML_M_NAME:
290 std::cerr <<
"ERROR in 'model' field:\n";
291 std::cerr <<
"it must contain 6 parameters\n";
297 M.
buildFrom(tx_, ty_, tz_, tux_, tuy_, tuz_);
312 pugi::xml_document doc;
315 if (!doc.load_file(filename.c_str(), pugi::parse_default | pugi::parse_comments)) {
316 node = doc.append_child(pugi::node_declaration);
317 node.append_attribute(
"version") =
"1.0";
318 node = doc.append_child(LABEL_XML_ROOT);
319 pugi::xml_node nodeComment = node.append_child(pugi::node_comment);
320 nodeComment.set_value(
"This file stores homogeneous matrix used\n"
321 " in the vpHomogeneousMatrix Class of ViSP available\n"
322 " at https://visp.inria.fr/download/ .\n"
323 " It can be read with the parse method of\n"
324 " the vpXmlParserHomogeneousMatrix class.");
327 node = doc.document_element();
334 int M_isFound = count(node, name);
337 std::cout <<
"There is already an homogeneous matrix " << std::endl
338 <<
"available in the file with the input name: " << name <<
"." << std::endl
339 <<
"Please delete it manually from the xml file." << std::endl;
345 doc.save_file(filename.c_str(), PUGIXML_TEXT(
" "));
360 int count(
const pugi::xml_node &node_,
const std::string &name)
365 for (pugi::xml_node node = node_.first_child(); node; node = node.next_sibling()) {
366 if (node.type() == pugi::node_element) {
367 if (
SEQUENCE_OK != str2xmlcode(node.name(), prop)) {
368 prop = CODE_XML_OTHER;
370 if (prop == CODE_XML_M) {
389 int write(pugi::xml_node &node,
const std::string &name)
393 pugi::xml_node node_tmp;
394 pugi::xml_node node_matrix;
395 pugi::xml_node node_values;
404 node_tmp = node.append_child(pugi::node_comment);
405 node_tmp.set_value(
"Homogeneous Matrix");
406 node_matrix = node.append_child(LABEL_XML_M);
410 node_tmp = node_matrix.append_child(pugi::node_comment);
411 node_tmp.set_value(
"Name of the homogeneous matrix");
412 node_matrix.append_child(LABEL_XML_M_NAME).append_child(pugi::node_pcdata).set_value(name.c_str());
416 node_values = node_matrix.append_child(LABEL_XML_VALUE);
418 node_tmp = node_values.append_child(pugi::node_comment);
419 node_tmp.set_value(
"Translation vector with values in meters");
422 node_values.append_child(LABEL_XML_TX).append_child(pugi::node_pcdata).text() = m_M[0][3];
425 node_values.append_child(LABEL_XML_TY).append_child(pugi::node_pcdata).text() = m_M[1][3];
428 node_values.append_child(LABEL_XML_TZ).append_child(pugi::node_pcdata).text() = m_M[2][3];
430 node_tmp = node_values.append_child(pugi::node_comment);
431 node_tmp.set_value(
"Rotational vector expressed in angle axis "
432 "representation with values in radians");
435 node_values.append_child(LABEL_XML_TUX).append_child(pugi::node_pcdata).text() = tu[0];
438 node_values.append_child(LABEL_XML_TUY).append_child(pugi::node_pcdata).text() = tu[1];
441 node_values.append_child(LABEL_XML_TUZ).append_child(pugi::node_pcdata).text() = tu[2];
457 vpXmlCodeType val_int = CODE_XML_BAD;
460 if (!strcmp(str, LABEL_XML_M)) {
461 val_int = CODE_XML_M;
463 else if (!strcmp(str, LABEL_XML_M_NAME)) {
464 val_int = CODE_XML_M_NAME;
466 else if (!strcmp(str, LABEL_XML_VALUE)) {
467 val_int = CODE_XML_VALUE;
469 else if (!strcmp(str, LABEL_XML_TX)) {
470 val_int = CODE_XML_TX;
472 else if (!strcmp(str, LABEL_XML_TY)) {
473 val_int = CODE_XML_TY;
475 else if (!strcmp(str, LABEL_XML_TZ)) {
476 val_int = CODE_XML_TZ;
478 else if (!strcmp(str, LABEL_XML_TUX)) {
479 val_int = CODE_XML_TUX;
481 else if (!strcmp(str, LABEL_XML_TUY)) {
482 val_int = CODE_XML_TUY;
484 else if (!strcmp(str, LABEL_XML_TUZ)) {
485 val_int = CODE_XML_TUZ;
488 val_int = CODE_XML_OTHER;
520 return m_impl->parse(M, filename, name);
532 const std::string &name)
534 return m_impl->save(M, filename, name);
539 return m_impl->getHomogeneousMatrix();
544 return m_impl->getHomogeneousMatrixName();
549 m_impl->setHomogeneousMatrixName(name);
552 #elif !defined(VISP_BUILD_SHARED_LIBS)
554 void dummy_vpXmlParserHomogeneousMatrix() { };
Implementation of an homogeneous matrix and operations on such kind of matrices.
vpHomogeneousMatrix & 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)