ViSP  2.7.0
vpMbtXmlParser.cpp
1 /****************************************************************************
2  *
3  * $Id: vpMbtXmlParser.cpp 4056 2013-01-05 13:04:42Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2013 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  *
34  * Description:
35  * Make the complete tracking of an object by using its CAD model
36  *
37  * Authors:
38  * Nicolas Melchior
39  * Romain Tallonneau
40  * Eric Marchand
41  *
42  *****************************************************************************/
43 #include <visp/vpConfig.h>
44 
45 
46 #ifdef VISP_HAVE_XML2
47 
48 #include <iostream>
49 #include <map>
50 
51 #include <libxml/xmlmemory.h> /* Fonctions de la lib XML. */
52 
53 #include <visp/vpMbtXmlParser.h>
54 
55 
61 {
62  init();
63 }
64 
69 {
70 }
71 
75 void
77 {
78  setMainTag("conf");
79 
80  nodeMap["conf"] = conf;
81  nodeMap["ecm"] = ecm;
82  nodeMap["mask"] = mask;
83  nodeMap["size"] = size;
84  nodeMap["nb_mask"] = nb_mask;
85  nodeMap["range"] = range;
86  nodeMap["tracking"] = tracking;
87  nodeMap["contrast"] = contrast;
88  nodeMap["edge_threshold"] = edge_threshold;
89  nodeMap["mu1"] = mu1;
90  nodeMap["mu2"] = mu2;
91  nodeMap["sample"] = sample;
92  nodeMap["step"] = step;
93  nodeMap["nb_sample"] = nb_sample;
94  nodeMap["face"] = face;
95  nodeMap["angle_appear"] = angle_appear;
96  nodeMap["angle_disappear"] = angle_disappear;
97  nodeMap["camera"] = camera;
98  nodeMap["height"] = height;
99  nodeMap["width"] = width;
100  nodeMap["u0"] = u0;
101  nodeMap["v0"] = v0;
102  nodeMap["px"] = px;
103  nodeMap["py"] = py;
104 
105 }
106 
113 void
114 vpMbtXmlParser::parse(const char * filename)
115 {
116  std::string file = filename;
117  vpXmlParser::parse(file);
118 }
119 
125 void
126 vpMbtXmlParser::writeMainClass(xmlNodePtr /*node*/)
127 {
128  throw vpException(vpException::notImplementedError, "Not yet implemented." );
129 }
130 
138 void
139 vpMbtXmlParser::readMainClass(xmlDocPtr doc, xmlNodePtr node)
140 {
141  bool ecm_node = false;
142  bool sample_node = false;
143  bool camera_node = false;
144  bool face_node = false;
145 
146  for(xmlNodePtr dataNode = node->xmlChildrenNode; dataNode != NULL; dataNode = dataNode->next) {
147  if(dataNode->type == XML_ELEMENT_NODE){
148  std::map<std::string, int>::iterator iter_data= this->nodeMap.find((char*)dataNode->name);
149  if(iter_data != nodeMap.end()){
150  switch (iter_data->second){
151  case ecm:{
152  this->read_ecm (doc, dataNode);
153  ecm_node = true;
154  }break;
155  case sample:{
156  this->read_sample (doc, dataNode);
157  sample_node = true;
158  }break;
159  case camera:{
160  this->read_camera (doc, dataNode);
161  camera_node = true;
162  }break;
163  case face:{
164  this->read_face(doc, dataNode);
165  face_node = true;
166  }break;
167  default:{
168 // vpTRACE("unknown tag in read_sample : %d, %s", iter_data->second, (iter_data->first).c_str());
169  }break;
170  }
171  }
172  }
173  }
174 
175  if(!ecm_node)
176  std::cout << "WARNING: ECM Node not specified, default values used" << std::endl;
177 
178  if(!sample_node)
179  std::cout << "WARNING: SAMPLE Node not specified, default values used" << std::endl;
180 
181  if(!camera_node)
182  std::cout << "WARNING: CAMERA Node not specified, default values used" << std::endl;
183 
184  if(!face_node)
185  std::cout << "WARNING: FACE Node not specified, default values used" << std::endl;
186 }
187 
188 
197 void
198 vpMbtXmlParser::read_ecm (xmlDocPtr doc, xmlNodePtr node)
199 {
200  bool mask_node = false;
201  bool range_node = false;
202  bool contrast_node = false;
203 
204  for(xmlNodePtr dataNode = node->xmlChildrenNode; dataNode != NULL; dataNode = dataNode->next) {
205  if(dataNode->type == XML_ELEMENT_NODE){
206  std::map<std::string, int>::iterator iter_data= this->nodeMap.find((char*)dataNode->name);
207  if(iter_data != nodeMap.end()){
208  switch (iter_data->second){
209  case mask:{
210  this->read_mask (doc, dataNode);
211  mask_node = true;
212  }break;
213  case range:{
214  this->read_range (doc, dataNode);
215  range_node = true;
216  }break;
217  case contrast:{
218  this->read_contrast (doc, dataNode);
219  contrast_node = true;
220  }break;
221  default:{
222 // vpTRACE("unknown tag in read_ecm : %d, %s", iter_data->second, (iter_data->first).c_str());
223  }break;
224  }
225  }
226  }
227  }
228 
229  if(!mask_node)
230  std::cout << "WARNING: In ECM Node, MASK Node not specified, default values used" << std::endl;
231 
232  if(!range_node)
233  std::cout << "WARNING: In ECM Node, RANGE Node not specified, default values used" << std::endl;
234 
235  if(!contrast_node)
236  std::cout << "WARNING: In ECM Node, CONTRAST Node not specified, default values used" << std::endl;
237 }
238 
247 void
248 vpMbtXmlParser::read_sample (xmlDocPtr doc, xmlNodePtr node)
249 {
250  bool step_node = false;
251  bool nb_sample_node = false;
252 
253  // current data values.
254  double d_stp = this->m_ecm.getSampleStep();
255  int d_nb_sample = this->m_ecm.getNbTotalSample();
256 
257  for(xmlNodePtr dataNode = node->xmlChildrenNode; dataNode != NULL; dataNode = dataNode->next) {
258  if(dataNode->type == XML_ELEMENT_NODE){
259  std::map<std::string, int>::iterator iter_data= this->nodeMap.find((char*)dataNode->name);
260  if(iter_data != nodeMap.end()){
261  switch (iter_data->second){
262  case step:{
263  d_stp = xmlReadIntChild(doc, dataNode);
264  step_node = true;
265  }break;
266  case nb_sample:{
267  d_nb_sample = xmlReadIntChild(doc, dataNode);
268  nb_sample_node = true;
269  }break;
270  default:{
271 // vpTRACE("unknown tag in read_sample : %d, %s", iter_data->second, (iter_data->first).c_str());
272  }break;
273  }
274  }
275  }
276  }
277 
278  this->m_ecm.setSampleStep(d_stp);
279  this->m_ecm.setNbTotalSample(d_nb_sample);
280 
281  if(!step_node)
282  std::cout << "WARNING: In SAMPLE Node, STEP Node not specified, default value used : " << this->m_ecm.getSampleStep() << std::endl;
283  else
284  std::cout <<"sample : sample_step "<< this->m_ecm.getSampleStep()<<std::endl;
285 
286  if(!nb_sample_node)
287  std::cout << "WARNING: In SAMPLE Node, NB_SAMPLE Node not specified, default value used : " << this->m_ecm.getNbTotalSample() << std::endl;
288  else
289  std::cout <<"sample : n_total_sample "<< this->m_ecm.getNbTotalSample()<<std::endl;
290 }
291 
300 void
301 vpMbtXmlParser::read_camera (xmlDocPtr doc, xmlNodePtr node)
302 {
303  bool height_node = false;
304  bool width_node = false;
305  bool u0_node = false;
306  bool v0_node = false;
307  bool px_node = false;
308  bool py_node = false;
309 
310  // current data values.
311 // int d_height=0 ;
312 // int d_width= 0 ;
313  double d_u0 = this->cam.get_u0();
314  double d_v0 = this->cam.get_v0();
315  double d_px = this->cam.get_px();
316  double d_py = this->cam.get_py();
317 
318  for(xmlNodePtr dataNode = node->xmlChildrenNode; dataNode != NULL; dataNode = dataNode->next) {
319  if(dataNode->type == XML_ELEMENT_NODE){
320  std::map<std::string, int>::iterator iter_data= this->nodeMap.find((char*)dataNode->name);
321  if(iter_data != nodeMap.end()){
322  switch (iter_data->second){
323  case height:{
324  /* d_height = */ xmlReadIntChild(doc, dataNode);
325  height_node = true;
326  }break;
327  case width:{
328  /* d_width = */ xmlReadIntChild(doc, dataNode);
329  width_node = true;
330  }break;
331  case u0:{
332  d_u0 = xmlReadDoubleChild(doc, dataNode);
333  u0_node = true;
334  }break;
335  case v0:{
336  d_v0 = xmlReadDoubleChild(doc, dataNode);
337  v0_node = true;
338  }break;
339  case px:{
340  d_px = xmlReadDoubleChild(doc, dataNode);
341  px_node = true;
342  }break;
343  case py:{
344  d_py = xmlReadDoubleChild(doc, dataNode);
345  py_node = true;
346  }break;
347  default:{
348 // vpTRACE("unknown tag in read_camera : %d, %s", iter_data->second, (iter_data->first).c_str());
349  }break;
350  }
351  }
352  }
353  }
354 
355  this->cam.initPersProjWithoutDistortion(d_px, d_py, d_u0, d_v0) ;
356 
357  if(!height_node)
358  std::cout << "WARNING: In CAMERA Node, HEIGHT Node not specified, default value used" << std::endl;
359 
360  if(!width_node)
361  std::cout << "WARNING: In CAMERA Node, WIDTH Node not specified, default value used" << std::endl;
362 
363  if(!u0_node)
364  std::cout << "WARNING: In CAMERA Node, u0 Node not specified, default value used : " << this->cam.get_u0() << std::endl;
365  else
366  std::cout << "camera : u0 "<< this->cam.get_u0() <<std::endl;
367 
368  if(!v0_node)
369  std::cout << "WARNING: In CAMERA Node, v0 Node not specified, default value used : " << this->cam.get_v0() << std::endl;
370  else
371  std::cout << "camera : v0 "<< this->cam.get_v0() <<std::endl;
372 
373  if(!px_node)
374  std::cout << "WARNING: In CAMERA Node, px Node not specified, default value used : " << this->cam.get_px() << std::endl;
375  else
376  std::cout << "camera : px "<< this->cam.get_px() <<std::endl;
377 
378  if(!py_node)
379  std::cout << "WARNING: In CAMERA Node, py Node not specified, default value used : " << this->cam.get_py() << std::endl;
380  else
381  std::cout << "camera : py "<< this->cam.get_py() <<std::endl;
382 }
383 
392 void
393 vpMbtXmlParser::read_face(xmlDocPtr doc, xmlNodePtr node)
394 {
395  bool angle_appear_node = false;
396  bool angle_disappear_node = false;
397 
398  for(xmlNodePtr dataNode = node->xmlChildrenNode; dataNode != NULL; dataNode = dataNode->next) {
399  if(dataNode->type == XML_ELEMENT_NODE){
400  std::map<std::string, int>::iterator iter_data= this->nodeMap.find((char*)dataNode->name);
401  if(iter_data != nodeMap.end()){
402  switch (iter_data->second){
403  case angle_appear:{
404  angleAppear = xmlReadDoubleChild(doc, dataNode);
405  angle_appear_node = true;
406  }break;
407  case angle_disappear:{
408  angleDisappear = xmlReadDoubleChild(doc, dataNode);
409  angle_disappear_node = true;
410  }break;
411  default:{
412 // vpTRACE("unknown tag in read_camera : %d, %s", iter_data->second, (iter_data->first).c_str());
413  }break;
414  }
415  }
416  }
417  }
418 
419  if(!angle_appear_node)
420  std::cout << "WARNING: In FACE Node, ANGLE_APPEAR Node not specified, default value used : " << angleAppear << std::endl;
421  else
422  std::cout << "face : Angle Appear "<< angleAppear <<std::endl;
423 
424  if(!angle_disappear_node)
425  std::cout << "WARNING: In FACE Node, ANGLE_DESAPPEAR Node not specified, default value used : " << angleDisappear << std::endl;
426  else
427  std::cout << "face : Angle Disappear : "<< angleDisappear <<std::endl;
428 }
429 
438 void
439 vpMbtXmlParser::read_mask (xmlDocPtr doc, xmlNodePtr node)
440 {
441  bool size_node = false;
442  bool nb_mask_node = false;
443 
444  // current data values.
445  unsigned int d_size = this->m_ecm.getMaskSize();
446  unsigned int d_nb_mask = this->m_ecm.getMaskNumber();
447 
448  for(xmlNodePtr dataNode = node->xmlChildrenNode; dataNode != NULL; dataNode = dataNode->next) {
449  if(dataNode->type == XML_ELEMENT_NODE){
450  std::map<std::string, int>::iterator iter_data= this->nodeMap.find((char*)dataNode->name);
451  if(iter_data != nodeMap.end()){
452  switch (iter_data->second){
453  case size:{
454  d_size = xmlReadUnsignedIntChild(doc, dataNode);
455  size_node = true;
456  }break;
457  case nb_mask:{
458  d_nb_mask = xmlReadUnsignedIntChild(doc, dataNode);
459  nb_mask_node = true;
460  }break;
461  default:{
462 // vpTRACE("unknown tag in read_mask : %d, %s", iter_data->second, (iter_data->first).c_str());
463  }break;
464  }
465  }
466  }
467  }
468 
469  this->m_ecm.setMaskSize(d_size) ;
470  this->m_ecm.setMaskNumber(d_nb_mask);
471 
472  if(!size_node)
473  std::cout << "WARNING: In MASK Node, SIZE Node not specified, default value used : " << this->m_ecm.getMaskSize() << std::endl;
474  else
475  std::cout << "ecm : mask : size "<< this->m_ecm.getMaskSize() <<std::endl;
476 
477  if(!nb_mask_node)
478  std::cout << "WARNING: In MASK Node, NB_MASK Node not specified, default value used : " << this->m_ecm.getMaskNumber() << std::endl;
479  else
480  std::cout << "ecm : mask : nb_mask "<< this->m_ecm.getMaskNumber() <<std::endl;
481 }
482 
491 void
492 vpMbtXmlParser::read_range (xmlDocPtr doc, xmlNodePtr node)
493 {
494  bool tracking_node = false;
495 
496  // current data values.
497  unsigned int m_range_tracking = this->m_ecm.getRange();
498 
499  for(xmlNodePtr dataNode = node->xmlChildrenNode; dataNode != NULL; dataNode = dataNode->next) {
500  if(dataNode->type == XML_ELEMENT_NODE){
501  std::map<std::string, int>::iterator iter_data= this->nodeMap.find((char*)dataNode->name);
502  if(iter_data != nodeMap.end()){
503  switch (iter_data->second){
504  case tracking:{
505  m_range_tracking = xmlReadUnsignedIntChild(doc, dataNode);
506  tracking_node = true;
507  }break;
508  default:{
509 // vpTRACE("unknown tag in read_range : %d, %s", iter_data->second, (iter_data->first).c_str());
510  }break;
511  }
512  }
513  }
514  }
515 
516  this->m_ecm.setRange(m_range_tracking);
517 
518  if(!tracking_node)
519  std::cout << "WARNING: In RANGE Node, TRACKING Node not specified, default value used : " << this->m_ecm.getRange() << std::endl;
520  else
521  std::cout <<"ecm : range : tracking "<< this->m_ecm.getRange()<<std::endl;
522 }
523 
524 
533 void
534 vpMbtXmlParser::read_contrast (xmlDocPtr doc, xmlNodePtr node)
535 {
536  bool edge_threshold_node = false;
537  bool mu1_node = false;
538  bool mu2_node = false;
539 
540  // current data values.
541  double d_edge_threshold = this->m_ecm.getThreshold();
542  double d_mu1 = this->m_ecm.getMu1();
543  double d_mu2 = this->m_ecm.getMu2();
544 
545  for(xmlNodePtr dataNode = node->xmlChildrenNode; dataNode != NULL; dataNode = dataNode->next) {
546  if(dataNode->type == XML_ELEMENT_NODE){
547  std::map<std::string, int>::iterator iter_data= this->nodeMap.find((char*)dataNode->name);
548  if(iter_data != nodeMap.end()){
549  switch (iter_data->second){
550  case edge_threshold:{
551  d_edge_threshold = xmlReadDoubleChild(doc, dataNode);
552  edge_threshold_node = true;
553  }break;
554  case mu1:{
555  d_mu1 = xmlReadDoubleChild(doc, dataNode);
556  mu1_node = true;
557  }break;
558  case mu2:{
559  d_mu2 = xmlReadDoubleChild(doc, dataNode);
560  mu2_node = true;
561  }break;
562  default:{
563 // vpTRACE("unknown tag in read_contrast : %d, %s", iter_data->second, (iter_data->first).c_str());
564  }break;
565  }
566  }
567  }
568  }
569 
570  this->m_ecm.setMu1(d_mu1);
571  this->m_ecm.setMu2(d_mu2);
572  this->m_ecm.setThreshold(d_edge_threshold);
573 
574  if(!edge_threshold_node)
575  std::cout << "WARNING: In CONTRAST Node, EDGE_THRESHOLD Node not specified, default value used : " << this->m_ecm.getThreshold() << std::endl;
576  else
577  std::cout <<"ecm : contrast : threshold " << this->m_ecm.getThreshold()<<std::endl;
578 
579  if(!mu1_node)
580  std::cout << "WARNING: In CONTRAST Node, mu1 Node not specified, default value used : " << this->m_ecm.getMu1() << std::endl;
581  else
582  std::cout <<"ecm : contrast : mu1 " << this->m_ecm.getMu1()<<std::endl;
583 
584  if(!mu2_node)
585  std::cout << "WARNING: In CONTRAST Node, mu2 Node not specified, default value used : " << this->m_ecm.getMu2() << std::endl;
586  else
587  std::cout <<"ecm : contrast : mu2 " << this->m_ecm.getMu2()<<std::endl;
588 }
589 
590 #endif
591 
virtual ~vpMbtXmlParser()
void writeMainClass(xmlNodePtr node)
unsigned int getRange() const
Definition: vpMe.h:236
unsigned int getMaskSize() const
Definition: vpMe.h:164
void read_contrast(xmlDocPtr doc, xmlNodePtr node)
void readMainClass(xmlDocPtr doc, xmlNodePtr node)
double get_u0() const
void setMainTag(const std::string &tag)
Definition: vpXmlParser.h:280
unsigned int getMaskNumber() const
Definition: vpMe.h:134
vpCameraParameters cam
Camera parameters.
double getMu1() const
Definition: vpMe.h:178
void read_range(xmlDocPtr doc, xmlNodePtr node)
void setMaskNumber(const unsigned int &a)
Definition: vpMe.cpp:486
void setSampleStep(const double &s)
Definition: vpMe.h:271
void setMu2(const double &mu2)
Definition: vpMe.h:185
void setNbTotalSample(const int &nb)
Definition: vpMe.h:199
error that can be emited by ViSP classes.
Definition: vpException.h:75
double xmlReadDoubleChild(xmlDocPtr doc, xmlNodePtr node)
double get_py() const
void initPersProjWithoutDistortion(const double px, const double py, const double u0, const double v0)
int getNbTotalSample() const
Definition: vpMe.h:206
double getThreshold() const
Definition: vpMe.h:306
void read_face(xmlDocPtr doc, xmlNodePtr node)
vpMe m_ecm
Moving edges parameters.
void setMaskSize(const unsigned int &a)
Definition: vpMe.cpp:494
double get_v0() const
double angleAppear
Angle to determine if a face appeared.
int xmlReadIntChild(xmlDocPtr doc, xmlNodePtr node)
double angleDisappear
Angle to determine if a face disappeared.
double get_px() const
void parse(const char *filename)
void read_sample(xmlDocPtr doc, xmlNodePtr node)
unsigned int xmlReadUnsignedIntChild(xmlDocPtr doc, xmlNodePtr node)
double getMu2() const
Definition: vpMe.h:192
void read_mask(xmlDocPtr doc, xmlNodePtr node)
void setThreshold(const double &t)
Definition: vpMe.h:299
void read_ecm(xmlDocPtr doc, xmlNodePtr node)
void setRange(const unsigned int &r)
Definition: vpMe.h:229
double getSampleStep() const
Definition: vpMe.h:278
std::map< std::string, int > nodeMap
Definition: vpXmlParser.h:197
void setMu1(const double &mu1)
Definition: vpMe.h:171
void read_camera(xmlDocPtr doc, xmlNodePtr node)
void parse(const std::string &filename)