Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
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
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See http://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Load XML parameters of the Model based tracker (using edges).
32  *
33  * Authors:
34  * Nicolas Melchior
35  * Romain Tallonneau
36  * Eric Marchand
37  * Aurelien Yol
38  *
39  *****************************************************************************/
40 #include <visp3/core/vpConfig.h>
41 
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 
52 
58 {
59  init();
60 }
61 
66 {
67 }
68 
72 void
74 {
76 
77  nodeMap["ecm"] = ecm;
78  nodeMap["mask"] = mask;
79  nodeMap["size"] = size;
80  nodeMap["nb_mask"] = nb_mask;
81  nodeMap["range"] = range;
82  nodeMap["tracking"] = tracking;
83  nodeMap["contrast"] = contrast;
84  nodeMap["edge_threshold"] = edge_threshold;
85  nodeMap["mu1"] = mu1;
86  nodeMap["mu2"] = mu2;
87  nodeMap["sample"] = sample;
88  nodeMap["step"] = step;
89 }
90 
97 void
98 vpMbtXmlParser::parse(const char * filename)
99 {
100  std::string file = filename;
101  vpXmlParser::parse(file);
102 }
103 
109 void
110 vpMbtXmlParser::writeMainClass(xmlNodePtr /*node*/)
111 {
112  throw vpException(vpException::notImplementedError, "Not yet implemented." );
113 }
114 
122 void
123 vpMbtXmlParser::readMainClass(xmlDocPtr doc, xmlNodePtr node)
124 {
125  bool camera_node = false;
126  bool face_node = false;
127  bool ecm_node = false;
128  bool lod_node = false;
129 
130  for(xmlNodePtr dataNode = node->xmlChildrenNode; dataNode != NULL; dataNode = dataNode->next) {
131  if(dataNode->type == XML_ELEMENT_NODE){
132  std::map<std::string, int>::iterator iter_data= this->nodeMap.find((char*)dataNode->name);
133  if(iter_data != nodeMap.end()){
134  switch (iter_data->second){
135  case camera:{
136  this->read_camera (doc, dataNode);
137  camera_node = true;
138  }break;
139  case face:{
140  this->read_face(doc, dataNode);
141  face_node = true;
142  }break;
143  case ecm:{
144  this->read_ecm (doc, dataNode);
145  ecm_node = true;
146  }break;
147  case sample:{
148  this->read_sample_deprecated (doc, dataNode);
149  }break;
150  case lod:{
151  this->read_lod(doc, dataNode);
152  lod_node = true;
153  }break;
154  default:{
155 // vpTRACE("unknown tag in read_sample : %d, %s", iter_data->second, (iter_data->first).c_str());
156  }break;
157  }
158  }
159  }
160  }
161 
162  if(!camera_node) {
163  std::cout << "camera : u0 : "<< this->cam.get_u0() << " (default)" <<std::endl;
164  std::cout << "camera : v0 : "<< this->cam.get_v0() << " (default)" <<std::endl;
165  std::cout << "camera : px : "<< this->cam.get_px() << " (default)" <<std::endl;
166  std::cout << "camera : py : "<< this->cam.get_py() << " (default)" <<std::endl;
167  }
168 
169  if(!face_node) {
170  std::cout << "face : Angle Appear : "<< angleAppear <<" (default)" <<std::endl;
171  std::cout << "face : Angle Disappear : "<< angleDisappear <<" (default)" <<std::endl;
172  }
173 
174  if(!ecm_node) {
175  std::cout << "ecm : mask : size : "<< this->m_ecm.getMaskSize() << " (default)" <<std::endl;
176  std::cout << "ecm : mask : nb_mask : "<< this->m_ecm.getMaskNumber() << " (default)" <<std::endl;
177  std::cout <<"ecm : range : tracking : "<< this->m_ecm.getRange()<< " (default)" <<std::endl;
178  std::cout <<"ecm : contrast : threshold : " << this->m_ecm.getThreshold()<<" (default)" <<std::endl;
179  std::cout <<"ecm : contrast : mu1 : " << this->m_ecm.getMu1()<<" (default)" <<std::endl;
180  std::cout <<"ecm : contrast : mu2 : " << this->m_ecm.getMu2()<<" (default)" <<std::endl;
181  std::cout <<"ecm : sample : sample_step : "<< this->m_ecm.getSampleStep()<< " (default)" << std::endl;
182  }
183 
184  if(!lod_node) {
185  std::cout << "lod : use lod : " << useLod << " (default)" << std::endl;
186  std::cout << "lod : min line length threshold : " << minLineLengthThreshold << " (default)" << std::endl;
187  std::cout << "lod : min polygon area threshold : " << minPolygonAreaThreshold << " (default)" << std::endl;
188  }
189 }
190 
191 
200 void
201 vpMbtXmlParser::read_ecm (xmlDocPtr doc, xmlNodePtr node)
202 {
203  bool mask_node = false;
204  bool range_node = false;
205  bool contrast_node = false;
206  bool sample_node = false;
207 
208  for(xmlNodePtr dataNode = node->xmlChildrenNode; dataNode != NULL; dataNode = dataNode->next) {
209  if(dataNode->type == XML_ELEMENT_NODE){
210  std::map<std::string, int>::iterator iter_data= this->nodeMap.find((char*)dataNode->name);
211  if(iter_data != nodeMap.end()){
212  switch (iter_data->second){
213  case mask:{
214  this->read_mask (doc, dataNode);
215  mask_node = true;
216  }break;
217  case range:{
218  this->read_range (doc, dataNode);
219  range_node = true;
220  }break;
221  case contrast:{
222  this->read_contrast (doc, dataNode);
223  contrast_node = true;
224  }break;
225  case sample:{
226  this->read_sample (doc, dataNode);
227  sample_node = true;
228  }break;
229  default:{
230 // vpTRACE("unknown tag in read_ecm : %d, %s", iter_data->second, (iter_data->first).c_str());
231  }break;
232  }
233  }
234  }
235  }
236 
237  if(!mask_node) {
238  std::cout << "ecm : mask : size : "<< this->m_ecm.getMaskSize() << " (default)" <<std::endl;
239  std::cout << "ecm : mask : nb_mask : "<< this->m_ecm.getMaskNumber() << " (default)" <<std::endl;
240  }
241 
242  if(!range_node) {
243  std::cout <<"ecm : range : tracking : "<< this->m_ecm.getRange()<< " (default)" <<std::endl;
244  }
245 
246  if(!contrast_node) {
247  std::cout <<"ecm : contrast : threshold " << this->m_ecm.getThreshold()<<" (default)" <<std::endl;
248  std::cout <<"ecm : contrast : mu1 " << this->m_ecm.getMu1()<<" (default)" <<std::endl;
249  std::cout <<"ecm : contrast : mu2 " << this->m_ecm.getMu2()<<" (default)" <<std::endl;
250  }
251 
252  if(!sample_node) {
253  std::cout <<"ecm : sample : sample_step : "<< this->m_ecm.getSampleStep()<< " (default)" << std::endl;
254  }
255 }
256 
265 void
266 vpMbtXmlParser::read_sample (xmlDocPtr doc, xmlNodePtr node)
267 {
268  bool step_node = false;
269 
270  // current data values.
271  double d_stp = this->m_ecm.getSampleStep();
272 
273  for(xmlNodePtr dataNode = node->xmlChildrenNode; dataNode != NULL; dataNode = dataNode->next) {
274  if(dataNode->type == XML_ELEMENT_NODE){
275  std::map<std::string, int>::iterator iter_data= this->nodeMap.find((char*)dataNode->name);
276  if(iter_data != nodeMap.end()){
277  switch (iter_data->second){
278  case step:{
279  d_stp = xmlReadIntChild(doc, dataNode);
280  step_node = true;
281  }break;
282  default:{
283 // vpTRACE("unknown tag in read_sample : %d, %s", iter_data->second, (iter_data->first).c_str());
284  }break;
285  }
286  }
287  }
288  }
289 
290  this->m_ecm.setSampleStep(d_stp);
291 // this->m_ecm.setNbTotalSample(d_nb_sample);
292 
293  if(!step_node)
294  std::cout <<"ecm : sample : sample_step : "<< this->m_ecm.getSampleStep()<< " (default)" << std::endl;
295  else
296  std::cout <<"ecm : sample : sample_step : "<< this->m_ecm.getSampleStep()<<std::endl;
297 }
298 
307 void
308 vpMbtXmlParser::read_sample_deprecated (xmlDocPtr doc, xmlNodePtr node)
309 {
310  bool step_node = false;
311  //bool nb_sample_node = false;
312 
313  // current data values.
314  double d_stp = this->m_ecm.getSampleStep();
315 // int d_nb_sample = this->m_ecm.getNbTotalSample();
316 
317  for(xmlNodePtr dataNode = node->xmlChildrenNode; dataNode != NULL; dataNode = dataNode->next) {
318  if(dataNode->type == XML_ELEMENT_NODE){
319  std::map<std::string, int>::iterator iter_data= this->nodeMap.find((char*)dataNode->name);
320  if(iter_data != nodeMap.end()){
321  switch (iter_data->second){
322  case step:{
323  d_stp = xmlReadIntChild(doc, dataNode);
324  step_node = true;
325  }break;
326 // case nb_sample:{
327 // d_nb_sample = xmlReadIntChild(doc, dataNode);
328 // nb_sample_node = true;
329 // }break;
330  default:{
331 // vpTRACE("unknown tag in read_sample : %d, %s", iter_data->second, (iter_data->first).c_str());
332  }break;
333  }
334  }
335  }
336  }
337 
338  this->m_ecm.setSampleStep(d_stp);
339 // this->m_ecm.setNbTotalSample(d_nb_sample);
340 
341  if(!step_node)
342  std::cout <<"[DEPRECATED] sample : sample_step : "<< this->m_ecm.getSampleStep()<< " (default)" << std::endl;
343  else
344  std::cout <<"[DEPRECATED] sample : sample_step : "<< this->m_ecm.getSampleStep()<<std::endl;
345 
346 // if(!nb_sample_node)
347 // std::cout <<"sample : n_total_sample : "<< this->m_ecm.getNbTotalSample()<< " (default)"<<std::endl;
348 // else
349 // std::cout <<"sample : n_total_sample : "<< this->m_ecm.getNbTotalSample()<<std::endl;
350 
351  std::cout <<" WARNING : This node (sample) is deprecated." << std::endl;
352  std::cout << " It should be moved in the ecm node (ecm : sample)." << std::endl;
353 }
354 
363 void
364 vpMbtXmlParser::read_mask (xmlDocPtr doc, xmlNodePtr node)
365 {
366  bool size_node = false;
367  bool nb_mask_node = false;
368 
369  // current data values.
370  unsigned int d_size = this->m_ecm.getMaskSize();
371  unsigned int d_nb_mask = this->m_ecm.getMaskNumber();
372 
373  for(xmlNodePtr dataNode = node->xmlChildrenNode; dataNode != NULL; dataNode = dataNode->next) {
374  if(dataNode->type == XML_ELEMENT_NODE){
375  std::map<std::string, int>::iterator iter_data= this->nodeMap.find((char*)dataNode->name);
376  if(iter_data != nodeMap.end()){
377  switch (iter_data->second){
378  case size:{
379  d_size = xmlReadUnsignedIntChild(doc, dataNode);
380  size_node = true;
381  }break;
382  case nb_mask:{
383  d_nb_mask = xmlReadUnsignedIntChild(doc, dataNode);
384  nb_mask_node = true;
385  }break;
386  default:{
387 // vpTRACE("unknown tag in read_mask : %d, %s", iter_data->second, (iter_data->first).c_str());
388  }break;
389  }
390  }
391  }
392  }
393 
394  this->m_ecm.setMaskSize(d_size) ;
395  // Check to ensure that d_nb_mask > 0
396  if (! d_nb_mask)
397  throw(vpException(vpException::badValue, "Model-based tracker mask size parameter should be different from zero in xml file"));
398  this->m_ecm.setMaskNumber(d_nb_mask);
399 
400  if(!size_node)
401  std::cout << "ecm : mask : size : "<< this->m_ecm.getMaskSize() << " (default)" <<std::endl;
402  else
403  std::cout << "ecm : mask : size : "<< this->m_ecm.getMaskSize() <<std::endl;
404 
405  if(!nb_mask_node)
406  std::cout << "ecm : mask : nb_mask : "<< this->m_ecm.getMaskNumber() << " (default)" <<std::endl;
407  else
408  std::cout << "ecm : mask : nb_mask : "<< this->m_ecm.getMaskNumber() <<std::endl;
409 }
410 
419 void
420 vpMbtXmlParser::read_range (xmlDocPtr doc, xmlNodePtr node)
421 {
422  bool tracking_node = false;
423 
424  // current data values.
425  unsigned int m_range_tracking = this->m_ecm.getRange();
426 
427  for(xmlNodePtr dataNode = node->xmlChildrenNode; dataNode != NULL; dataNode = dataNode->next) {
428  if(dataNode->type == XML_ELEMENT_NODE){
429  std::map<std::string, int>::iterator iter_data= this->nodeMap.find((char*)dataNode->name);
430  if(iter_data != nodeMap.end()){
431  switch (iter_data->second){
432  case tracking:{
433  m_range_tracking = xmlReadUnsignedIntChild(doc, dataNode);
434  tracking_node = true;
435  }break;
436  default:{
437 // vpTRACE("unknown tag in read_range : %d, %s", iter_data->second, (iter_data->first).c_str());
438  }break;
439  }
440  }
441  }
442  }
443 
444  this->m_ecm.setRange(m_range_tracking);
445 
446  if(!tracking_node)
447  std::cout <<"ecm : range : tracking : "<< this->m_ecm.getRange()<< " (default)" <<std::endl;
448  else
449  std::cout <<"ecm : range : tracking : "<< this->m_ecm.getRange()<<std::endl;
450 }
451 
452 
461 void
462 vpMbtXmlParser::read_contrast (xmlDocPtr doc, xmlNodePtr node)
463 {
464  bool edge_threshold_node = false;
465  bool mu1_node = false;
466  bool mu2_node = false;
467 
468  // current data values.
469  double d_edge_threshold = this->m_ecm.getThreshold();
470  double d_mu1 = this->m_ecm.getMu1();
471  double d_mu2 = this->m_ecm.getMu2();
472 
473  for(xmlNodePtr dataNode = node->xmlChildrenNode; dataNode != NULL; dataNode = dataNode->next) {
474  if(dataNode->type == XML_ELEMENT_NODE){
475  std::map<std::string, int>::iterator iter_data= this->nodeMap.find((char*)dataNode->name);
476  if(iter_data != nodeMap.end()){
477  switch (iter_data->second){
478  case edge_threshold:{
479  d_edge_threshold = xmlReadDoubleChild(doc, dataNode);
480  edge_threshold_node = true;
481  }break;
482  case mu1:{
483  d_mu1 = xmlReadDoubleChild(doc, dataNode);
484  mu1_node = true;
485  }break;
486  case mu2:{
487  d_mu2 = xmlReadDoubleChild(doc, dataNode);
488  mu2_node = true;
489  }break;
490  default:{
491 // vpTRACE("unknown tag in read_contrast : %d, %s", iter_data->second, (iter_data->first).c_str());
492  }break;
493  }
494  }
495  }
496  }
497 
498  this->m_ecm.setMu1(d_mu1);
499  this->m_ecm.setMu2(d_mu2);
500  this->m_ecm.setThreshold(d_edge_threshold);
501 
502  if(!edge_threshold_node)
503  std::cout <<"ecm : contrast : threshold " << this->m_ecm.getThreshold()<<" (default)" <<std::endl;
504  else
505  std::cout <<"ecm : contrast : threshold " << this->m_ecm.getThreshold()<<std::endl;
506 
507  if(!mu1_node)
508  std::cout <<"ecm : contrast : mu1 " << this->m_ecm.getMu1()<<" (default)" <<std::endl;
509  else
510  std::cout <<"ecm : contrast : mu1 " << this->m_ecm.getMu1()<<std::endl;
511 
512  if(!mu2_node)
513  std::cout <<"ecm : contrast : mu2 " << this->m_ecm.getMu2()<<" (default)" <<std::endl;
514  else
515  std::cout <<"ecm : contrast : mu2 " << this->m_ecm.getMu2()<<std::endl;
516 }
517 
518 #elif !defined(VISP_BUILD_SHARED_LIBS)
519 // Work arround to avoid warning: libvisp_mbt.a(vpMbtXmlParser.cpp.o) has no symbols
520 void dummy_vpMbtXmlParser() {};
521 #endif
522 
virtual ~vpMbtXmlParser()
void writeMainClass(xmlNodePtr node)
unsigned int getRange() const
Definition: vpMe.h:167
unsigned int getMaskSize() const
Definition: vpMe.h:131
void read_contrast(xmlDocPtr doc, xmlNodePtr node)
vpCameraParameters cam
Camera parameters.
Definition: vpMbXmlParser.h:67
virtual void readMainClass(xmlDocPtr doc, xmlNodePtr node)
double get_u0() const
unsigned int getMaskNumber() const
Definition: vpMe.h:118
void read_lod(xmlDocPtr doc, xmlNodePtr node)
double getMu1() const
Definition: vpMe.h:143
void read_range(xmlDocPtr doc, xmlNodePtr node)
void setMaskNumber(const unsigned int &a)
Definition: vpMe.cpp:488
void setSampleStep(const double &s)
Definition: vpMe.h:263
double angleAppear
Angle to determine if a face appeared.
Definition: vpMbXmlParser.h:69
error that can be emited by ViSP classes.
Definition: vpException.h:73
double minLineLengthThreshold
Minimum line length to track a segment when LOD is enabled.
Definition: vpMbXmlParser.h:85
double xmlReadDoubleChild(xmlDocPtr doc, xmlNodePtr node)
double get_py() const
double angleDisappear
Angle to determine if a face disappeared.
Definition: vpMbXmlParser.h:71
void setMu1(const double &mu_1)
Definition: vpMe.h:226
void read_sample_deprecated(xmlDocPtr doc, xmlNodePtr node)
double getThreshold() const
Definition: vpMe.h:180
void read_camera(xmlDocPtr doc, xmlNodePtr node)
vpMe m_ecm
Moving edges parameters.
void setMaskSize(const unsigned int &a)
Definition: vpMe.cpp:496
double get_v0() const
int xmlReadIntChild(xmlDocPtr doc, xmlNodePtr node)
void read_face(xmlDocPtr doc, xmlNodePtr node)
double get_px() const
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:233
double getMu2() const
Definition: vpMe.h:149
void read_mask(xmlDocPtr doc, xmlNodePtr node)
void setThreshold(const double &t)
Definition: vpMe.h:284
void read_ecm(xmlDocPtr doc, xmlNodePtr node)
void setRange(const unsigned int &r)
Definition: vpMe.h:256
double getSampleStep() const
Definition: vpMe.h:270
std::map< std::string, int > nodeMap
Definition: vpXmlParser.h:224
double minPolygonAreaThreshold
Minimum polygon area to track a face when LOD is enabled.
Definition: vpMbXmlParser.h:87
bool useLod
If true, the LOD is enabled, otherwise it is not.
Definition: vpMbXmlParser.h:83
void parse(const std::string &filename)