Visual Servoing Platform  version 3.2.0 under development (2019-01-22)
vpMbtXmlParser.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 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 http://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  * Load XML parameters of the Model based tracker (using edges).
33  *
34  * Authors:
35  * Nicolas Melchior
36  * Romain Tallonneau
37  * Eric Marchand
38  * Aurelien Yol
39  *
40  *****************************************************************************/
41 #include <visp3/core/vpConfig.h>
42 
43 #ifdef VISP_HAVE_XML2
44 
45 #include <iostream>
46 #include <map>
47 
48 #include <libxml/xmlmemory.h> /* Fonctions de la lib XML. */
49 
50 #include <visp3/mbt/vpMbtXmlParser.h>
51 
57 
62 
67 {
69 
70  nodeMap["ecm"] = ecm;
71  nodeMap["mask"] = mask;
72  nodeMap["size"] = size;
73  nodeMap["nb_mask"] = nb_mask;
74  nodeMap["range"] = range;
75  nodeMap["tracking"] = tracking;
76  nodeMap["contrast"] = contrast;
77  nodeMap["edge_threshold"] = edge_threshold;
78  nodeMap["mu1"] = mu1;
79  nodeMap["mu2"] = mu2;
80  nodeMap["sample"] = sample;
81  nodeMap["step"] = step;
82 }
83 
89 void vpMbtXmlParser::writeMainClass(xmlNodePtr /*node*/)
90 {
91  throw vpException(vpException::notImplementedError, "Not yet implemented.");
92 }
93 
101 void vpMbtXmlParser::readMainClass(xmlDocPtr doc, xmlNodePtr node)
102 {
103  bool camera_node = false;
104  bool face_node = false;
105  bool ecm_node = false;
106  bool lod_node = false;
107 
108  for (xmlNodePtr dataNode = node->xmlChildrenNode; dataNode != NULL; dataNode = dataNode->next) {
109  if (dataNode->type == XML_ELEMENT_NODE) {
110  std::map<std::string, int>::iterator iter_data = this->nodeMap.find((char *)dataNode->name);
111  if (iter_data != nodeMap.end()) {
112  switch (iter_data->second) {
113  case camera: {
114  this->read_camera(doc, dataNode);
115  camera_node = true;
116  } break;
117  case face: {
118  this->read_face(doc, dataNode);
119  face_node = true;
120  } break;
121  case ecm: {
122  this->read_ecm(doc, dataNode);
123  ecm_node = true;
124  } break;
125  case sample: {
126  this->read_sample_deprecated(doc, dataNode);
127  } break;
128  case lod: {
129  this->read_lod(doc, dataNode);
130  lod_node = true;
131  } break;
132  default: {
133  // vpTRACE("unknown tag in read_sample : %d, %s",
134  // iter_data->second, (iter_data->first).c_str());
135  } break;
136  }
137  }
138  }
139  }
140 
141  if (!camera_node) {
142  std::cout << "camera : u0 : " << this->cam.get_u0() << " (default)" << std::endl;
143  std::cout << "camera : v0 : " << this->cam.get_v0() << " (default)" << std::endl;
144  std::cout << "camera : px : " << this->cam.get_px() << " (default)" << std::endl;
145  std::cout << "camera : py : " << this->cam.get_py() << " (default)" << std::endl;
146  }
147 
148  if (!face_node) {
149  std::cout << "face : Angle Appear : " << angleAppear << " (default)" << std::endl;
150  std::cout << "face : Angle Disappear : " << angleDisappear << " (default)" << std::endl;
151  }
152 
153  if (!ecm_node) {
154  std::cout << "ecm : mask : size : " << this->m_ecm.getMaskSize() << " (default)" << std::endl;
155  std::cout << "ecm : mask : nb_mask : " << this->m_ecm.getMaskNumber() << " (default)" << std::endl;
156  std::cout << "ecm : range : tracking : " << this->m_ecm.getRange() << " (default)" << std::endl;
157  std::cout << "ecm : contrast : threshold : " << this->m_ecm.getThreshold() << " (default)" << std::endl;
158  std::cout << "ecm : contrast : mu1 : " << this->m_ecm.getMu1() << " (default)" << std::endl;
159  std::cout << "ecm : contrast : mu2 : " << this->m_ecm.getMu2() << " (default)" << std::endl;
160  std::cout << "ecm : sample : sample_step : " << this->m_ecm.getSampleStep() << " (default)" << std::endl;
161  }
162 
163  if (!lod_node) {
164  std::cout << "lod : use lod : " << useLod << " (default)" << std::endl;
165  std::cout << "lod : min line length threshold : " << minLineLengthThreshold << " (default)" << std::endl;
166  std::cout << "lod : min polygon area threshold : " << minPolygonAreaThreshold << " (default)" << std::endl;
167  }
168 }
169 
178 void vpMbtXmlParser::read_ecm(xmlDocPtr doc, xmlNodePtr node)
179 {
180  bool mask_node = false;
181  bool range_node = false;
182  bool contrast_node = false;
183  bool sample_node = false;
184 
185  for (xmlNodePtr dataNode = node->xmlChildrenNode; dataNode != NULL; dataNode = dataNode->next) {
186  if (dataNode->type == XML_ELEMENT_NODE) {
187  std::map<std::string, int>::iterator iter_data = this->nodeMap.find((char *)dataNode->name);
188  if (iter_data != nodeMap.end()) {
189  switch (iter_data->second) {
190  case mask: {
191  this->read_mask(doc, dataNode);
192  mask_node = true;
193  } break;
194  case range: {
195  this->read_range(doc, dataNode);
196  range_node = true;
197  } break;
198  case contrast: {
199  this->read_contrast(doc, dataNode);
200  contrast_node = true;
201  } break;
202  case sample: {
203  this->read_sample(doc, dataNode);
204  sample_node = true;
205  } break;
206  default: {
207  // vpTRACE("unknown tag in read_ecm : %d, %s",
208  // iter_data->second, (iter_data->first).c_str());
209  } break;
210  }
211  }
212  }
213  }
214 
215  if (!mask_node) {
216  std::cout << "ecm : mask : size : " << this->m_ecm.getMaskSize() << " (default)" << std::endl;
217  std::cout << "ecm : mask : nb_mask : " << this->m_ecm.getMaskNumber() << " (default)" << std::endl;
218  }
219 
220  if (!range_node) {
221  std::cout << "ecm : range : tracking : " << this->m_ecm.getRange() << " (default)" << std::endl;
222  }
223 
224  if (!contrast_node) {
225  std::cout << "ecm : contrast : threshold " << this->m_ecm.getThreshold() << " (default)" << std::endl;
226  std::cout << "ecm : contrast : mu1 " << this->m_ecm.getMu1() << " (default)" << std::endl;
227  std::cout << "ecm : contrast : mu2 " << this->m_ecm.getMu2() << " (default)" << std::endl;
228  }
229 
230  if (!sample_node) {
231  std::cout << "ecm : sample : sample_step : " << this->m_ecm.getSampleStep() << " (default)" << std::endl;
232  }
233 }
234 
243 void vpMbtXmlParser::read_sample(xmlDocPtr doc, xmlNodePtr node)
244 {
245  bool step_node = false;
246 
247  // current data values.
248  double d_stp = this->m_ecm.getSampleStep();
249 
250  for (xmlNodePtr dataNode = node->xmlChildrenNode; dataNode != NULL; dataNode = dataNode->next) {
251  if (dataNode->type == XML_ELEMENT_NODE) {
252  std::map<std::string, int>::iterator iter_data = this->nodeMap.find((char *)dataNode->name);
253  if (iter_data != nodeMap.end()) {
254  switch (iter_data->second) {
255  case step: {
256  d_stp = xmlReadIntChild(doc, dataNode);
257  step_node = true;
258  } break;
259  default: {
260  // vpTRACE("unknown tag in read_sample : %d, %s",
261  // iter_data->second, (iter_data->first).c_str());
262  } break;
263  }
264  }
265  }
266  }
267 
268  this->m_ecm.setSampleStep(d_stp);
269  // this->m_ecm.setNbTotalSample(d_nb_sample);
270 
271  if (!step_node)
272  std::cout << "ecm : sample : sample_step : " << this->m_ecm.getSampleStep() << " (default)" << std::endl;
273  else
274  std::cout << "ecm : sample : sample_step : " << this->m_ecm.getSampleStep() << std::endl;
275 }
276 
285 void vpMbtXmlParser::read_sample_deprecated(xmlDocPtr doc, xmlNodePtr node)
286 {
287  bool step_node = false;
288  // bool nb_sample_node = false;
289 
290  // current data values.
291  double d_stp = this->m_ecm.getSampleStep();
292  // int d_nb_sample = this->m_ecm.getNbTotalSample();
293 
294  for (xmlNodePtr dataNode = node->xmlChildrenNode; dataNode != NULL; dataNode = dataNode->next) {
295  if (dataNode->type == XML_ELEMENT_NODE) {
296  std::map<std::string, int>::iterator iter_data = this->nodeMap.find((char *)dataNode->name);
297  if (iter_data != nodeMap.end()) {
298  switch (iter_data->second) {
299  case step: {
300  d_stp = xmlReadIntChild(doc, dataNode);
301  step_node = true;
302  } break;
303  // case nb_sample:{
304  // d_nb_sample = xmlReadIntChild(doc, dataNode);
305  // nb_sample_node = true;
306  // }break;
307  default: {
308  // vpTRACE("unknown tag in read_sample : %d, %s",
309  // iter_data->second, (iter_data->first).c_str());
310  } break;
311  }
312  }
313  }
314  }
315 
316  this->m_ecm.setSampleStep(d_stp);
317  // this->m_ecm.setNbTotalSample(d_nb_sample);
318 
319  if (!step_node)
320  std::cout << "[DEPRECATED] sample : sample_step : " << this->m_ecm.getSampleStep() << " (default)" << std::endl;
321  else
322  std::cout << "[DEPRECATED] sample : sample_step : " << this->m_ecm.getSampleStep() << std::endl;
323 
324  // if(!nb_sample_node)
325  // std::cout <<"sample : n_total_sample : "<<
326  // this->m_ecm.getNbTotalSample()<< " (default)"<<std::endl;
327  // else
328  // std::cout <<"sample : n_total_sample : "<<
329  // this->m_ecm.getNbTotalSample()<<std::endl;
330 
331  std::cout << " WARNING : This node (sample) is deprecated." << std::endl;
332  std::cout << " It should be moved in the ecm node (ecm : sample)." << std::endl;
333 }
334 
343 void vpMbtXmlParser::read_mask(xmlDocPtr doc, xmlNodePtr node)
344 {
345  bool size_node = false;
346  bool nb_mask_node = false;
347 
348  // current data values.
349  unsigned int d_size = this->m_ecm.getMaskSize();
350  unsigned int d_nb_mask = this->m_ecm.getMaskNumber();
351 
352  for (xmlNodePtr dataNode = node->xmlChildrenNode; dataNode != NULL; dataNode = dataNode->next) {
353  if (dataNode->type == XML_ELEMENT_NODE) {
354  std::map<std::string, int>::iterator iter_data = this->nodeMap.find((char *)dataNode->name);
355  if (iter_data != nodeMap.end()) {
356  switch (iter_data->second) {
357  case size: {
358  d_size = xmlReadUnsignedIntChild(doc, dataNode);
359  size_node = true;
360  } break;
361  case nb_mask: {
362  d_nb_mask = xmlReadUnsignedIntChild(doc, dataNode);
363  nb_mask_node = true;
364  } break;
365  default: {
366  // vpTRACE("unknown tag in read_mask : %d, %s",
367  // iter_data->second, (iter_data->first).c_str());
368  } break;
369  }
370  }
371  }
372  }
373 
374  this->m_ecm.setMaskSize(d_size);
375  // Check to ensure that d_nb_mask > 0
376  if (!d_nb_mask)
377  throw(vpException(vpException::badValue, "Model-based tracker mask size "
378  "parameter should be different "
379  "from zero in xml file"));
380  this->m_ecm.setMaskNumber(d_nb_mask);
381 
382  if (!size_node)
383  std::cout << "ecm : mask : size : " << this->m_ecm.getMaskSize() << " (default)" << std::endl;
384  else
385  std::cout << "ecm : mask : size : " << this->m_ecm.getMaskSize() << std::endl;
386 
387  if (!nb_mask_node)
388  std::cout << "ecm : mask : nb_mask : " << this->m_ecm.getMaskNumber() << " (default)" << std::endl;
389  else
390  std::cout << "ecm : mask : nb_mask : " << this->m_ecm.getMaskNumber() << std::endl;
391 }
392 
401 void vpMbtXmlParser::read_range(xmlDocPtr doc, xmlNodePtr node)
402 {
403  bool tracking_node = false;
404 
405  // current data values.
406  unsigned int m_range_tracking = this->m_ecm.getRange();
407 
408  for (xmlNodePtr dataNode = node->xmlChildrenNode; dataNode != NULL; dataNode = dataNode->next) {
409  if (dataNode->type == XML_ELEMENT_NODE) {
410  std::map<std::string, int>::iterator iter_data = this->nodeMap.find((char *)dataNode->name);
411  if (iter_data != nodeMap.end()) {
412  switch (iter_data->second) {
413  case tracking: {
414  m_range_tracking = xmlReadUnsignedIntChild(doc, dataNode);
415  tracking_node = true;
416  } break;
417  default: {
418  // vpTRACE("unknown tag in read_range : %d, %s",
419  // iter_data->second, (iter_data->first).c_str());
420  } break;
421  }
422  }
423  }
424  }
425 
426  this->m_ecm.setRange(m_range_tracking);
427 
428  if (!tracking_node)
429  std::cout << "ecm : range : tracking : " << this->m_ecm.getRange() << " (default)" << std::endl;
430  else
431  std::cout << "ecm : range : tracking : " << this->m_ecm.getRange() << std::endl;
432 }
433 
442 void vpMbtXmlParser::read_contrast(xmlDocPtr doc, xmlNodePtr node)
443 {
444  bool edge_threshold_node = false;
445  bool mu1_node = false;
446  bool mu2_node = false;
447 
448  // current data values.
449  double d_edge_threshold = this->m_ecm.getThreshold();
450  double d_mu1 = this->m_ecm.getMu1();
451  double d_mu2 = this->m_ecm.getMu2();
452 
453  for (xmlNodePtr dataNode = node->xmlChildrenNode; dataNode != NULL; dataNode = dataNode->next) {
454  if (dataNode->type == XML_ELEMENT_NODE) {
455  std::map<std::string, int>::iterator iter_data = this->nodeMap.find((char *)dataNode->name);
456  if (iter_data != nodeMap.end()) {
457  switch (iter_data->second) {
458  case edge_threshold: {
459  d_edge_threshold = xmlReadDoubleChild(doc, dataNode);
460  edge_threshold_node = true;
461  } break;
462  case mu1: {
463  d_mu1 = xmlReadDoubleChild(doc, dataNode);
464  mu1_node = true;
465  } break;
466  case mu2: {
467  d_mu2 = xmlReadDoubleChild(doc, dataNode);
468  mu2_node = true;
469  } break;
470  default: {
471  // vpTRACE("unknown tag in read_contrast : %d, %s",
472  // iter_data->second, (iter_data->first).c_str());
473  } break;
474  }
475  }
476  }
477  }
478 
479  this->m_ecm.setMu1(d_mu1);
480  this->m_ecm.setMu2(d_mu2);
481  this->m_ecm.setThreshold(d_edge_threshold);
482 
483  if (!edge_threshold_node)
484  std::cout << "ecm : contrast : threshold " << this->m_ecm.getThreshold() << " (default)" << std::endl;
485  else
486  std::cout << "ecm : contrast : threshold " << this->m_ecm.getThreshold() << std::endl;
487 
488  if (!mu1_node)
489  std::cout << "ecm : contrast : mu1 " << this->m_ecm.getMu1() << " (default)" << std::endl;
490  else
491  std::cout << "ecm : contrast : mu1 " << this->m_ecm.getMu1() << std::endl;
492 
493  if (!mu2_node)
494  std::cout << "ecm : contrast : mu2 " << this->m_ecm.getMu2() << " (default)" << std::endl;
495  else
496  std::cout << "ecm : contrast : mu2 " << this->m_ecm.getMu2() << std::endl;
497 }
498 
499 #elif !defined(VISP_BUILD_SHARED_LIBS)
500 // Work arround to avoid warning: libvisp_mbt.a(vpMbtXmlParser.cpp.o) has no
501 // symbols
502 void dummy_vpMbtXmlParser(){};
503 #endif
virtual ~vpMbtXmlParser()
void writeMainClass(xmlNodePtr node)
Used to indicate that a value is not in the allowed range.
Definition: vpException.h:97
unsigned int getRange() const
Definition: vpMe.h:179
unsigned int getMaskSize() const
Definition: vpMe.h:142
void read_contrast(xmlDocPtr doc, xmlNodePtr node)
vpCameraParameters cam
Camera parameters.
Definition: vpMbXmlParser.h:69
virtual void readMainClass(xmlDocPtr doc, xmlNodePtr node)
double get_u0() const
unsigned int getMaskNumber() const
Definition: vpMe.h:128
void read_lod(xmlDocPtr doc, xmlNodePtr node)
double getMu1() const
Definition: vpMe.h:155
void read_range(xmlDocPtr doc, xmlNodePtr node)
void setMaskNumber(const unsigned int &a)
Definition: vpMe.cpp:454
void setSampleStep(const double &s)
Definition: vpMe.h:278
double angleAppear
Angle to determine if a face appeared.
Definition: vpMbXmlParser.h:71
error that can be emited by ViSP classes.
Definition: vpException.h:71
double minLineLengthThreshold
Minimum line length to track a segment when LOD is enabled.
Definition: vpMbXmlParser.h:87
double xmlReadDoubleChild(xmlDocPtr doc, xmlNodePtr node)
double get_py() const
double angleDisappear
Angle to determine if a face disappeared.
Definition: vpMbXmlParser.h:73
void setMu1(const double &mu_1)
Definition: vpMe.h:241
void read_sample_deprecated(xmlDocPtr doc, xmlNodePtr node)
double getThreshold() const
Definition: vpMe.h:193
void read_camera(xmlDocPtr doc, xmlNodePtr node)
vpMe m_ecm
Moving edges parameters.
void setMaskSize(const unsigned int &a)
Definition: vpMe.cpp:461
double get_v0() const
int xmlReadIntChild(xmlDocPtr doc, xmlNodePtr node)
void read_face(xmlDocPtr doc, xmlNodePtr node)
double get_px() const
void read_sample(xmlDocPtr doc, xmlNodePtr node)
unsigned int xmlReadUnsignedIntChild(xmlDocPtr doc, xmlNodePtr node)
void setMu2(const double &mu_2)
Definition: vpMe.h:248
double getMu2() const
Definition: vpMe.h:161
void read_mask(xmlDocPtr doc, xmlNodePtr node)
void setThreshold(const double &t)
Definition: vpMe.h:300
void read_ecm(xmlDocPtr doc, xmlNodePtr node)
void setRange(const unsigned int &r)
Definition: vpMe.h:271
double getSampleStep() const
Definition: vpMe.h:285
std::map< std::string, int > nodeMap
Definition: vpXmlParser.h:226
double minPolygonAreaThreshold
Minimum polygon area to track a face when LOD is enabled.
Definition: vpMbXmlParser.h:89
bool useLod
If true, the LOD is enabled, otherwise it is not.
Definition: vpMbXmlParser.h:85