Visual Servoing Platform  version 3.1.0
vpMbtXmlParser.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 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 
90 void vpMbtXmlParser::parse(const char *filename)
91 {
92  std::string file = filename;
93  vpXmlParser::parse(file);
94 }
95 
101 void vpMbtXmlParser::writeMainClass(xmlNodePtr /*node*/)
102 {
103  throw vpException(vpException::notImplementedError, "Not yet implemented.");
104 }
105 
113 void vpMbtXmlParser::readMainClass(xmlDocPtr doc, xmlNodePtr node)
114 {
115  bool camera_node = false;
116  bool face_node = false;
117  bool ecm_node = false;
118  bool lod_node = false;
119 
120  for (xmlNodePtr dataNode = node->xmlChildrenNode; dataNode != NULL; dataNode = dataNode->next) {
121  if (dataNode->type == XML_ELEMENT_NODE) {
122  std::map<std::string, int>::iterator iter_data = this->nodeMap.find((char *)dataNode->name);
123  if (iter_data != nodeMap.end()) {
124  switch (iter_data->second) {
125  case camera: {
126  this->read_camera(doc, dataNode);
127  camera_node = true;
128  } break;
129  case face: {
130  this->read_face(doc, dataNode);
131  face_node = true;
132  } break;
133  case ecm: {
134  this->read_ecm(doc, dataNode);
135  ecm_node = true;
136  } break;
137  case sample: {
138  this->read_sample_deprecated(doc, dataNode);
139  } break;
140  case lod: {
141  this->read_lod(doc, dataNode);
142  lod_node = true;
143  } break;
144  default: {
145  // vpTRACE("unknown tag in read_sample : %d, %s",
146  // iter_data->second, (iter_data->first).c_str());
147  } break;
148  }
149  }
150  }
151  }
152 
153  if (!camera_node) {
154  std::cout << "camera : u0 : " << this->cam.get_u0() << " (default)" << std::endl;
155  std::cout << "camera : v0 : " << this->cam.get_v0() << " (default)" << std::endl;
156  std::cout << "camera : px : " << this->cam.get_px() << " (default)" << std::endl;
157  std::cout << "camera : py : " << this->cam.get_py() << " (default)" << std::endl;
158  }
159 
160  if (!face_node) {
161  std::cout << "face : Angle Appear : " << angleAppear << " (default)" << std::endl;
162  std::cout << "face : Angle Disappear : " << angleDisappear << " (default)" << std::endl;
163  }
164 
165  if (!ecm_node) {
166  std::cout << "ecm : mask : size : " << this->m_ecm.getMaskSize() << " (default)" << std::endl;
167  std::cout << "ecm : mask : nb_mask : " << this->m_ecm.getMaskNumber() << " (default)" << std::endl;
168  std::cout << "ecm : range : tracking : " << this->m_ecm.getRange() << " (default)" << std::endl;
169  std::cout << "ecm : contrast : threshold : " << this->m_ecm.getThreshold() << " (default)" << std::endl;
170  std::cout << "ecm : contrast : mu1 : " << this->m_ecm.getMu1() << " (default)" << std::endl;
171  std::cout << "ecm : contrast : mu2 : " << this->m_ecm.getMu2() << " (default)" << std::endl;
172  std::cout << "ecm : sample : sample_step : " << this->m_ecm.getSampleStep() << " (default)" << std::endl;
173  }
174 
175  if (!lod_node) {
176  std::cout << "lod : use lod : " << useLod << " (default)" << std::endl;
177  std::cout << "lod : min line length threshold : " << minLineLengthThreshold << " (default)" << std::endl;
178  std::cout << "lod : min polygon area threshold : " << minPolygonAreaThreshold << " (default)" << std::endl;
179  }
180 }
181 
190 void vpMbtXmlParser::read_ecm(xmlDocPtr doc, xmlNodePtr node)
191 {
192  bool mask_node = false;
193  bool range_node = false;
194  bool contrast_node = false;
195  bool sample_node = false;
196 
197  for (xmlNodePtr dataNode = node->xmlChildrenNode; dataNode != NULL; dataNode = dataNode->next) {
198  if (dataNode->type == XML_ELEMENT_NODE) {
199  std::map<std::string, int>::iterator iter_data = this->nodeMap.find((char *)dataNode->name);
200  if (iter_data != nodeMap.end()) {
201  switch (iter_data->second) {
202  case mask: {
203  this->read_mask(doc, dataNode);
204  mask_node = true;
205  } break;
206  case range: {
207  this->read_range(doc, dataNode);
208  range_node = true;
209  } break;
210  case contrast: {
211  this->read_contrast(doc, dataNode);
212  contrast_node = true;
213  } break;
214  case sample: {
215  this->read_sample(doc, dataNode);
216  sample_node = true;
217  } break;
218  default: {
219  // vpTRACE("unknown tag in read_ecm : %d, %s",
220  // iter_data->second, (iter_data->first).c_str());
221  } break;
222  }
223  }
224  }
225  }
226 
227  if (!mask_node) {
228  std::cout << "ecm : mask : size : " << this->m_ecm.getMaskSize() << " (default)" << std::endl;
229  std::cout << "ecm : mask : nb_mask : " << this->m_ecm.getMaskNumber() << " (default)" << std::endl;
230  }
231 
232  if (!range_node) {
233  std::cout << "ecm : range : tracking : " << this->m_ecm.getRange() << " (default)" << std::endl;
234  }
235 
236  if (!contrast_node) {
237  std::cout << "ecm : contrast : threshold " << this->m_ecm.getThreshold() << " (default)" << std::endl;
238  std::cout << "ecm : contrast : mu1 " << this->m_ecm.getMu1() << " (default)" << std::endl;
239  std::cout << "ecm : contrast : mu2 " << this->m_ecm.getMu2() << " (default)" << std::endl;
240  }
241 
242  if (!sample_node) {
243  std::cout << "ecm : sample : sample_step : " << this->m_ecm.getSampleStep() << " (default)" << std::endl;
244  }
245 }
246 
255 void vpMbtXmlParser::read_sample(xmlDocPtr doc, xmlNodePtr node)
256 {
257  bool step_node = false;
258 
259  // current data values.
260  double d_stp = this->m_ecm.getSampleStep();
261 
262  for (xmlNodePtr dataNode = node->xmlChildrenNode; dataNode != NULL; dataNode = dataNode->next) {
263  if (dataNode->type == XML_ELEMENT_NODE) {
264  std::map<std::string, int>::iterator iter_data = this->nodeMap.find((char *)dataNode->name);
265  if (iter_data != nodeMap.end()) {
266  switch (iter_data->second) {
267  case step: {
268  d_stp = xmlReadIntChild(doc, dataNode);
269  step_node = true;
270  } break;
271  default: {
272  // vpTRACE("unknown tag in read_sample : %d, %s",
273  // iter_data->second, (iter_data->first).c_str());
274  } break;
275  }
276  }
277  }
278  }
279 
280  this->m_ecm.setSampleStep(d_stp);
281  // this->m_ecm.setNbTotalSample(d_nb_sample);
282 
283  if (!step_node)
284  std::cout << "ecm : sample : sample_step : " << this->m_ecm.getSampleStep() << " (default)" << std::endl;
285  else
286  std::cout << "ecm : sample : sample_step : " << this->m_ecm.getSampleStep() << std::endl;
287 }
288 
297 void vpMbtXmlParser::read_sample_deprecated(xmlDocPtr doc, xmlNodePtr node)
298 {
299  bool step_node = false;
300  // bool nb_sample_node = false;
301 
302  // current data values.
303  double d_stp = this->m_ecm.getSampleStep();
304  // int d_nb_sample = this->m_ecm.getNbTotalSample();
305 
306  for (xmlNodePtr dataNode = node->xmlChildrenNode; dataNode != NULL; dataNode = dataNode->next) {
307  if (dataNode->type == XML_ELEMENT_NODE) {
308  std::map<std::string, int>::iterator iter_data = this->nodeMap.find((char *)dataNode->name);
309  if (iter_data != nodeMap.end()) {
310  switch (iter_data->second) {
311  case step: {
312  d_stp = xmlReadIntChild(doc, dataNode);
313  step_node = true;
314  } break;
315  // case nb_sample:{
316  // d_nb_sample = xmlReadIntChild(doc, dataNode);
317  // nb_sample_node = true;
318  // }break;
319  default: {
320  // vpTRACE("unknown tag in read_sample : %d, %s",
321  // iter_data->second, (iter_data->first).c_str());
322  } break;
323  }
324  }
325  }
326  }
327 
328  this->m_ecm.setSampleStep(d_stp);
329  // this->m_ecm.setNbTotalSample(d_nb_sample);
330 
331  if (!step_node)
332  std::cout << "[DEPRECATED] sample : sample_step : " << this->m_ecm.getSampleStep() << " (default)" << std::endl;
333  else
334  std::cout << "[DEPRECATED] sample : sample_step : " << this->m_ecm.getSampleStep() << std::endl;
335 
336  // if(!nb_sample_node)
337  // std::cout <<"sample : n_total_sample : "<<
338  // this->m_ecm.getNbTotalSample()<< " (default)"<<std::endl;
339  // else
340  // std::cout <<"sample : n_total_sample : "<<
341  // this->m_ecm.getNbTotalSample()<<std::endl;
342 
343  std::cout << " WARNING : This node (sample) is deprecated." << std::endl;
344  std::cout << " It should be moved in the ecm node (ecm : sample)." << std::endl;
345 }
346 
355 void vpMbtXmlParser::read_mask(xmlDocPtr doc, xmlNodePtr node)
356 {
357  bool size_node = false;
358  bool nb_mask_node = false;
359 
360  // current data values.
361  unsigned int d_size = this->m_ecm.getMaskSize();
362  unsigned int d_nb_mask = this->m_ecm.getMaskNumber();
363 
364  for (xmlNodePtr dataNode = node->xmlChildrenNode; dataNode != NULL; dataNode = dataNode->next) {
365  if (dataNode->type == XML_ELEMENT_NODE) {
366  std::map<std::string, int>::iterator iter_data = this->nodeMap.find((char *)dataNode->name);
367  if (iter_data != nodeMap.end()) {
368  switch (iter_data->second) {
369  case size: {
370  d_size = xmlReadUnsignedIntChild(doc, dataNode);
371  size_node = true;
372  } break;
373  case nb_mask: {
374  d_nb_mask = xmlReadUnsignedIntChild(doc, dataNode);
375  nb_mask_node = true;
376  } break;
377  default: {
378  // vpTRACE("unknown tag in read_mask : %d, %s",
379  // iter_data->second, (iter_data->first).c_str());
380  } break;
381  }
382  }
383  }
384  }
385 
386  this->m_ecm.setMaskSize(d_size);
387  // Check to ensure that d_nb_mask > 0
388  if (!d_nb_mask)
389  throw(vpException(vpException::badValue, "Model-based tracker mask size "
390  "parameter should be different "
391  "from zero in xml file"));
392  this->m_ecm.setMaskNumber(d_nb_mask);
393 
394  if (!size_node)
395  std::cout << "ecm : mask : size : " << this->m_ecm.getMaskSize() << " (default)" << std::endl;
396  else
397  std::cout << "ecm : mask : size : " << this->m_ecm.getMaskSize() << std::endl;
398 
399  if (!nb_mask_node)
400  std::cout << "ecm : mask : nb_mask : " << this->m_ecm.getMaskNumber() << " (default)" << std::endl;
401  else
402  std::cout << "ecm : mask : nb_mask : " << this->m_ecm.getMaskNumber() << std::endl;
403 }
404 
413 void vpMbtXmlParser::read_range(xmlDocPtr doc, xmlNodePtr node)
414 {
415  bool tracking_node = false;
416 
417  // current data values.
418  unsigned int m_range_tracking = this->m_ecm.getRange();
419 
420  for (xmlNodePtr dataNode = node->xmlChildrenNode; dataNode != NULL; dataNode = dataNode->next) {
421  if (dataNode->type == XML_ELEMENT_NODE) {
422  std::map<std::string, int>::iterator iter_data = this->nodeMap.find((char *)dataNode->name);
423  if (iter_data != nodeMap.end()) {
424  switch (iter_data->second) {
425  case tracking: {
426  m_range_tracking = xmlReadUnsignedIntChild(doc, dataNode);
427  tracking_node = true;
428  } break;
429  default: {
430  // vpTRACE("unknown tag in read_range : %d, %s",
431  // iter_data->second, (iter_data->first).c_str());
432  } break;
433  }
434  }
435  }
436  }
437 
438  this->m_ecm.setRange(m_range_tracking);
439 
440  if (!tracking_node)
441  std::cout << "ecm : range : tracking : " << this->m_ecm.getRange() << " (default)" << std::endl;
442  else
443  std::cout << "ecm : range : tracking : " << this->m_ecm.getRange() << std::endl;
444 }
445 
454 void vpMbtXmlParser::read_contrast(xmlDocPtr doc, xmlNodePtr node)
455 {
456  bool edge_threshold_node = false;
457  bool mu1_node = false;
458  bool mu2_node = false;
459 
460  // current data values.
461  double d_edge_threshold = this->m_ecm.getThreshold();
462  double d_mu1 = this->m_ecm.getMu1();
463  double d_mu2 = this->m_ecm.getMu2();
464 
465  for (xmlNodePtr dataNode = node->xmlChildrenNode; dataNode != NULL; dataNode = dataNode->next) {
466  if (dataNode->type == XML_ELEMENT_NODE) {
467  std::map<std::string, int>::iterator iter_data = this->nodeMap.find((char *)dataNode->name);
468  if (iter_data != nodeMap.end()) {
469  switch (iter_data->second) {
470  case edge_threshold: {
471  d_edge_threshold = xmlReadDoubleChild(doc, dataNode);
472  edge_threshold_node = true;
473  } break;
474  case mu1: {
475  d_mu1 = xmlReadDoubleChild(doc, dataNode);
476  mu1_node = true;
477  } break;
478  case mu2: {
479  d_mu2 = xmlReadDoubleChild(doc, dataNode);
480  mu2_node = true;
481  } break;
482  default: {
483  // vpTRACE("unknown tag in read_contrast : %d, %s",
484  // iter_data->second, (iter_data->first).c_str());
485  } break;
486  }
487  }
488  }
489  }
490 
491  this->m_ecm.setMu1(d_mu1);
492  this->m_ecm.setMu2(d_mu2);
493  this->m_ecm.setThreshold(d_edge_threshold);
494 
495  if (!edge_threshold_node)
496  std::cout << "ecm : contrast : threshold " << this->m_ecm.getThreshold() << " (default)" << std::endl;
497  else
498  std::cout << "ecm : contrast : threshold " << this->m_ecm.getThreshold() << std::endl;
499 
500  if (!mu1_node)
501  std::cout << "ecm : contrast : mu1 " << this->m_ecm.getMu1() << " (default)" << std::endl;
502  else
503  std::cout << "ecm : contrast : mu1 " << this->m_ecm.getMu1() << std::endl;
504 
505  if (!mu2_node)
506  std::cout << "ecm : contrast : mu2 " << this->m_ecm.getMu2() << " (default)" << std::endl;
507  else
508  std::cout << "ecm : contrast : mu2 " << this->m_ecm.getMu2() << std::endl;
509 }
510 
511 #elif !defined(VISP_BUILD_SHARED_LIBS)
512 // Work arround to avoid warning: libvisp_mbt.a(vpMbtXmlParser.cpp.o) has no
513 // symbols
514 void dummy_vpMbtXmlParser(){};
515 #endif
virtual ~vpMbtXmlParser()
void writeMainClass(xmlNodePtr node)
void read_contrast(xmlDocPtr doc, xmlNodePtr node)
vpCameraParameters cam
Camera parameters.
Definition: vpMbXmlParser.h:69
virtual void readMainClass(xmlDocPtr doc, xmlNodePtr node)
void read_lod(xmlDocPtr doc, xmlNodePtr node)
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)
unsigned int getMaskSize() const
Definition: vpMe.h:142
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 getMu1() const
Definition: vpMe.h:155
void read_camera(xmlDocPtr doc, xmlNodePtr node)
double getThreshold() const
Definition: vpMe.h:193
vpMe m_ecm
Moving edges parameters.
void setMaskSize(const unsigned int &a)
Definition: vpMe.cpp:461
double getSampleStep() const
Definition: vpMe.h:285
int xmlReadIntChild(xmlDocPtr doc, xmlNodePtr node)
void read_face(xmlDocPtr doc, xmlNodePtr node)
unsigned int getMaskNumber() const
Definition: vpMe.h:128
void parse(const char *filename)
void read_sample(xmlDocPtr doc, xmlNodePtr node)
unsigned int xmlReadUnsignedIntChild(xmlDocPtr doc, xmlNodePtr node)
void setMu2(const double &mu_2)
Definition: vpMe.h:248
void read_mask(xmlDocPtr doc, xmlNodePtr node)
void setThreshold(const double &t)
Definition: vpMe.h:300
double getMu2() const
Definition: vpMe.h:161
void read_ecm(xmlDocPtr doc, xmlNodePtr node)
void setRange(const unsigned int &r)
Definition: vpMe.h:271
std::map< std::string, int > nodeMap
Definition: vpXmlParser.h:226
unsigned int getRange() const
Definition: vpMe.h:179
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
void parse(const std::string &filename)