ViSP  2.9.0
vpMbtXmlParser.cpp
1 /****************************************************************************
2  *
3  * $Id: vpMbtXmlParser.cpp 4632 2014-02-03 17:06:40Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2014 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  * Load XML parameters of the Model based tracker (using edges).
36  *
37  * Authors:
38  * Nicolas Melchior
39  * Romain Tallonneau
40  * Eric Marchand
41  * Aurelien Yol
42  *
43  *****************************************************************************/
44 #include <visp/vpConfig.h>
45 
46 
47 #ifdef VISP_HAVE_XML2
48 
49 #include <iostream>
50 #include <map>
51 
52 #include <libxml/xmlmemory.h> /* Fonctions de la lib XML. */
53 
54 #include <visp/vpMbtXmlParser.h>
55 
56 
62 {
63  init();
64 }
65 
70 {
71 }
72 
76 void
78 {
80 
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 }
95 
102 void
103 vpMbtXmlParser::parse(const char * filename)
104 {
105  std::string file = filename;
106  vpXmlParser::parse(file);
107 }
108 
114 void
115 vpMbtXmlParser::writeMainClass(xmlNodePtr /*node*/)
116 {
117  throw vpException(vpException::notImplementedError, "Not yet implemented." );
118 }
119 
127 void
128 vpMbtXmlParser::readMainClass(xmlDocPtr doc, xmlNodePtr node)
129 {
130  bool camera_node = false;
131  bool face_node = false;
132  bool ecm_node = false;
133  bool sample_node = false;
134 
135  for(xmlNodePtr dataNode = node->xmlChildrenNode; dataNode != NULL; dataNode = dataNode->next) {
136  if(dataNode->type == XML_ELEMENT_NODE){
137  std::map<std::string, int>::iterator iter_data= this->nodeMap.find((char*)dataNode->name);
138  if(iter_data != nodeMap.end()){
139  switch (iter_data->second){
140  case camera:{
141  this->read_camera (doc, dataNode);
142  camera_node = true;
143  }break;
144  case face:{
145  this->read_face(doc, dataNode);
146  face_node = true;
147  }break;
148  case ecm:{
149  this->read_ecm (doc, dataNode);
150  ecm_node = true;
151  }break;
152  case sample:{
153  this->read_sample (doc, dataNode);
154  sample_node = true;
155  }break;
156  default:{
157 // vpTRACE("unknown tag in read_sample : %d, %s", iter_data->second, (iter_data->first).c_str());
158  }break;
159  }
160  }
161  }
162  }
163 
164  if(!camera_node) {
165  std::cout << "camera : u0 : "<< this->cam.get_u0() << " (default)" <<std::endl;
166  std::cout << "camera : v0 : "<< this->cam.get_v0() << " (default)" <<std::endl;
167  std::cout << "camera : px : "<< this->cam.get_px() << " (default)" <<std::endl;
168  std::cout << "camera : py : "<< this->cam.get_py() << " (default)" <<std::endl;
169  }
170 
171  if(!face_node) {
172  std::cout << "face : Angle Appear : "<< angleAppear <<" (default)" <<std::endl;
173  std::cout << "face : Angle Disappear : "<< angleDisappear <<" (default)" <<std::endl;
174  }
175 
176  if(!ecm_node) {
177  std::cout << "ecm : mask : size : "<< this->m_ecm.getMaskSize() << " (default)" <<std::endl;
178  std::cout << "ecm : mask : nb_mask : "<< this->m_ecm.getMaskNumber() << " (default)" <<std::endl;
179  std::cout <<"ecm : range : tracking : "<< this->m_ecm.getRange()<< " (default)" <<std::endl;
180  std::cout <<"ecm : contrast : threshold : " << this->m_ecm.getThreshold()<<" (default)" <<std::endl;
181  std::cout <<"ecm : contrast : mu1 : " << this->m_ecm.getMu1()<<" (default)" <<std::endl;
182  std::cout <<"ecm : contrast : mu2 : " << this->m_ecm.getMu2()<<" (default)" <<std::endl;
183  }
184 
185  if(!sample_node) {
186  std::cout <<"sample : sample_step : "<< this->m_ecm.getSampleStep()<< " (default)" << std::endl;
187  std::cout <<"sample : n_total_sample : "<< this->m_ecm.getNbTotalSample()<< " (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 
207  for(xmlNodePtr dataNode = node->xmlChildrenNode; dataNode != NULL; dataNode = dataNode->next) {
208  if(dataNode->type == XML_ELEMENT_NODE){
209  std::map<std::string, int>::iterator iter_data= this->nodeMap.find((char*)dataNode->name);
210  if(iter_data != nodeMap.end()){
211  switch (iter_data->second){
212  case mask:{
213  this->read_mask (doc, dataNode);
214  mask_node = true;
215  }break;
216  case range:{
217  this->read_range (doc, dataNode);
218  range_node = true;
219  }break;
220  case contrast:{
221  this->read_contrast (doc, dataNode);
222  contrast_node = true;
223  }break;
224  default:{
225 // vpTRACE("unknown tag in read_ecm : %d, %s", iter_data->second, (iter_data->first).c_str());
226  }break;
227  }
228  }
229  }
230  }
231 
232  if(!mask_node) {
233  std::cout << "ecm : mask : size : "<< this->m_ecm.getMaskSize() << " (default)" <<std::endl;
234  std::cout << "ecm : mask : nb_mask : "<< this->m_ecm.getMaskNumber() << " (default)" <<std::endl;
235  }
236 
237  if(!range_node) {
238  std::cout <<"ecm : range : tracking : "<< this->m_ecm.getRange()<< " (default)" <<std::endl;
239  }
240 
241  if(!contrast_node) {
242  std::cout <<"ecm : contrast : threshold " << this->m_ecm.getThreshold()<<" (default)" <<std::endl;
243  std::cout <<"ecm : contrast : mu1 " << this->m_ecm.getMu1()<<" (default)" <<std::endl;
244  std::cout <<"ecm : contrast : mu2 " << this->m_ecm.getMu2()<<" (default)" <<std::endl;
245  }
246 }
247 
256 void
257 vpMbtXmlParser::read_sample (xmlDocPtr doc, xmlNodePtr node)
258 {
259  bool step_node = false;
260  bool nb_sample_node = false;
261 
262  // current data values.
263  double d_stp = this->m_ecm.getSampleStep();
264  int d_nb_sample = this->m_ecm.getNbTotalSample();
265 
266  for(xmlNodePtr dataNode = node->xmlChildrenNode; dataNode != NULL; dataNode = dataNode->next) {
267  if(dataNode->type == XML_ELEMENT_NODE){
268  std::map<std::string, int>::iterator iter_data= this->nodeMap.find((char*)dataNode->name);
269  if(iter_data != nodeMap.end()){
270  switch (iter_data->second){
271  case step:{
272  d_stp = xmlReadIntChild(doc, dataNode);
273  step_node = true;
274  }break;
275  case nb_sample:{
276  d_nb_sample = xmlReadIntChild(doc, dataNode);
277  nb_sample_node = true;
278  }break;
279  default:{
280 // vpTRACE("unknown tag in read_sample : %d, %s", iter_data->second, (iter_data->first).c_str());
281  }break;
282  }
283  }
284  }
285  }
286 
287  this->m_ecm.setSampleStep(d_stp);
288  this->m_ecm.setNbTotalSample(d_nb_sample);
289 
290  if(!step_node)
291  std::cout <<"sample : sample_step : "<< this->m_ecm.getSampleStep()<< " (default)" << std::endl;
292  else
293  std::cout <<"sample : sample_step : "<< this->m_ecm.getSampleStep()<<std::endl;
294 
295  if(!nb_sample_node)
296  std::cout <<"sample : n_total_sample : "<< this->m_ecm.getNbTotalSample()<< " (default)"<<std::endl;
297  else
298  std::cout <<"sample : n_total_sample : "<< this->m_ecm.getNbTotalSample()<<std::endl;
299 }
300 
309 void
310 vpMbtXmlParser::read_mask (xmlDocPtr doc, xmlNodePtr node)
311 {
312  bool size_node = false;
313  bool nb_mask_node = false;
314 
315  // current data values.
316  unsigned int d_size = this->m_ecm.getMaskSize();
317  unsigned int d_nb_mask = this->m_ecm.getMaskNumber();
318 
319  for(xmlNodePtr dataNode = node->xmlChildrenNode; dataNode != NULL; dataNode = dataNode->next) {
320  if(dataNode->type == XML_ELEMENT_NODE){
321  std::map<std::string, int>::iterator iter_data= this->nodeMap.find((char*)dataNode->name);
322  if(iter_data != nodeMap.end()){
323  switch (iter_data->second){
324  case size:{
325  d_size = xmlReadUnsignedIntChild(doc, dataNode);
326  size_node = true;
327  }break;
328  case nb_mask:{
329  d_nb_mask = xmlReadUnsignedIntChild(doc, dataNode);
330  nb_mask_node = true;
331  }break;
332  default:{
333 // vpTRACE("unknown tag in read_mask : %d, %s", iter_data->second, (iter_data->first).c_str());
334  }break;
335  }
336  }
337  }
338  }
339 
340  this->m_ecm.setMaskSize(d_size) ;
341  this->m_ecm.setMaskNumber(d_nb_mask);
342 
343  if(!size_node)
344  std::cout << "ecm : mask : size : "<< this->m_ecm.getMaskSize() << " (default)" <<std::endl;
345  else
346  std::cout << "ecm : mask : size : "<< this->m_ecm.getMaskSize() <<std::endl;
347 
348  if(!nb_mask_node)
349  std::cout << "ecm : mask : nb_mask : "<< this->m_ecm.getMaskNumber() << " (default)" <<std::endl;
350  else
351  std::cout << "ecm : mask : nb_mask : "<< this->m_ecm.getMaskNumber() <<std::endl;
352 }
353 
362 void
363 vpMbtXmlParser::read_range (xmlDocPtr doc, xmlNodePtr node)
364 {
365  bool tracking_node = false;
366 
367  // current data values.
368  unsigned int m_range_tracking = this->m_ecm.getRange();
369 
370  for(xmlNodePtr dataNode = node->xmlChildrenNode; dataNode != NULL; dataNode = dataNode->next) {
371  if(dataNode->type == XML_ELEMENT_NODE){
372  std::map<std::string, int>::iterator iter_data= this->nodeMap.find((char*)dataNode->name);
373  if(iter_data != nodeMap.end()){
374  switch (iter_data->second){
375  case tracking:{
376  m_range_tracking = xmlReadUnsignedIntChild(doc, dataNode);
377  tracking_node = true;
378  }break;
379  default:{
380 // vpTRACE("unknown tag in read_range : %d, %s", iter_data->second, (iter_data->first).c_str());
381  }break;
382  }
383  }
384  }
385  }
386 
387  this->m_ecm.setRange(m_range_tracking);
388 
389  if(!tracking_node)
390  std::cout <<"ecm : range : tracking : "<< this->m_ecm.getRange()<< " (default)" <<std::endl;
391  else
392  std::cout <<"ecm : range : tracking : "<< this->m_ecm.getRange()<<std::endl;
393 }
394 
395 
404 void
405 vpMbtXmlParser::read_contrast (xmlDocPtr doc, xmlNodePtr node)
406 {
407  bool edge_threshold_node = false;
408  bool mu1_node = false;
409  bool mu2_node = false;
410 
411  // current data values.
412  double d_edge_threshold = this->m_ecm.getThreshold();
413  double d_mu1 = this->m_ecm.getMu1();
414  double d_mu2 = this->m_ecm.getMu2();
415 
416  for(xmlNodePtr dataNode = node->xmlChildrenNode; dataNode != NULL; dataNode = dataNode->next) {
417  if(dataNode->type == XML_ELEMENT_NODE){
418  std::map<std::string, int>::iterator iter_data= this->nodeMap.find((char*)dataNode->name);
419  if(iter_data != nodeMap.end()){
420  switch (iter_data->second){
421  case edge_threshold:{
422  d_edge_threshold = xmlReadDoubleChild(doc, dataNode);
423  edge_threshold_node = true;
424  }break;
425  case mu1:{
426  d_mu1 = xmlReadDoubleChild(doc, dataNode);
427  mu1_node = true;
428  }break;
429  case mu2:{
430  d_mu2 = xmlReadDoubleChild(doc, dataNode);
431  mu2_node = true;
432  }break;
433  default:{
434 // vpTRACE("unknown tag in read_contrast : %d, %s", iter_data->second, (iter_data->first).c_str());
435  }break;
436  }
437  }
438  }
439  }
440 
441  this->m_ecm.setMu1(d_mu1);
442  this->m_ecm.setMu2(d_mu2);
443  this->m_ecm.setThreshold(d_edge_threshold);
444 
445  if(!edge_threshold_node)
446  std::cout <<"ecm : contrast : threshold " << this->m_ecm.getThreshold()<<" (default)" <<std::endl;
447  else
448  std::cout <<"ecm : contrast : threshold " << this->m_ecm.getThreshold()<<std::endl;
449 
450  if(!mu1_node)
451  std::cout <<"ecm : contrast : mu1 " << this->m_ecm.getMu1()<<" (default)" <<std::endl;
452  else
453  std::cout <<"ecm : contrast : mu1 " << this->m_ecm.getMu1()<<std::endl;
454 
455  if(!mu2_node)
456  std::cout <<"ecm : contrast : mu2 " << this->m_ecm.getMu2()<<" (default)" <<std::endl;
457  else
458  std::cout <<"ecm : contrast : mu2 " << this->m_ecm.getMu2()<<std::endl;
459 }
460 
461 #endif
462 
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)
vpCameraParameters cam
Camera parameters.
Definition: vpMbXmlParser.h:71
virtual void readMainClass(xmlDocPtr doc, xmlNodePtr node)
double get_u0() const
unsigned int getMaskNumber() const
Definition: vpMe.h:134
double getMu1() const
Definition: vpMe.h:178
void read_range(xmlDocPtr doc, xmlNodePtr node)
void setMaskNumber(const unsigned int &a)
Definition: vpMe.cpp:480
void setSampleStep(const double &s)
Definition: vpMe.h:271
void setNbTotalSample(const int &nb)
Definition: vpMe.h:199
double angleAppear
Angle to determine if a face appeared.
Definition: vpMbXmlParser.h:73
error that can be emited by ViSP classes.
Definition: vpException.h:76
double xmlReadDoubleChild(xmlDocPtr doc, xmlNodePtr node)
double get_py() const
double angleDisappear
Angle to determine if a face disappeared.
Definition: vpMbXmlParser.h:75
void setMu1(const double &mu_1)
Definition: vpMe.h:171
int getNbTotalSample() const
Definition: vpMe.h:206
double getThreshold() const
Definition: vpMe.h:306
void read_camera(xmlDocPtr doc, xmlNodePtr node)
vpMe m_ecm
Moving edges parameters.
void setMaskSize(const unsigned int &a)
Definition: vpMe.cpp:488
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:185
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 parse(const std::string &filename)