ViSP  2.10.0
vpMbtXmlParser.cpp
1 /****************************************************************************
2  *
3  * $Id: vpMbtXmlParser.cpp 5238 2015-01-30 13:52:25Z 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 
61 vpMbtXmlParser::vpMbtXmlParser() : m_ecm(), useLod(false), minLineLengthThreshold(50.0), minPolygonAreaThreshold(2500.0)
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  nodeMap["lod"] = lod;
95  nodeMap["use_lod"] = use_lod;
96  nodeMap["min_line_length_threshold"] = min_line_length_threshold;
97  nodeMap["min_polygon_area_threshold"] = min_polygon_area_threshold;
98 }
99 
106 void
107 vpMbtXmlParser::parse(const char * filename)
108 {
109  std::string file = filename;
110  vpXmlParser::parse(file);
111 }
112 
118 void
119 vpMbtXmlParser::writeMainClass(xmlNodePtr /*node*/)
120 {
121  throw vpException(vpException::notImplementedError, "Not yet implemented." );
122 }
123 
131 void
132 vpMbtXmlParser::readMainClass(xmlDocPtr doc, xmlNodePtr node)
133 {
134  bool camera_node = false;
135  bool face_node = false;
136  bool ecm_node = false;
137  bool sample_node = false;
138  bool lod_node = false;
139 
140  for(xmlNodePtr dataNode = node->xmlChildrenNode; dataNode != NULL; dataNode = dataNode->next) {
141  if(dataNode->type == XML_ELEMENT_NODE){
142  std::map<std::string, int>::iterator iter_data= this->nodeMap.find((char*)dataNode->name);
143  if(iter_data != nodeMap.end()){
144  switch (iter_data->second){
145  case camera:{
146  this->read_camera (doc, dataNode);
147  camera_node = true;
148  }break;
149  case face:{
150  this->read_face(doc, dataNode);
151  face_node = true;
152  }break;
153  case ecm:{
154  this->read_ecm (doc, dataNode);
155  ecm_node = true;
156  }break;
157  case sample:{
158  this->read_sample (doc, dataNode);
159  sample_node = true;
160  }break;
161  case lod:{
162  this->read_lod(doc, dataNode);
163  lod_node = true;
164  }break;
165  default:{
166 // vpTRACE("unknown tag in read_sample : %d, %s", iter_data->second, (iter_data->first).c_str());
167  }break;
168  }
169  }
170  }
171  }
172 
173  if(!camera_node) {
174  std::cout << "camera : u0 : "<< this->cam.get_u0() << " (default)" <<std::endl;
175  std::cout << "camera : v0 : "<< this->cam.get_v0() << " (default)" <<std::endl;
176  std::cout << "camera : px : "<< this->cam.get_px() << " (default)" <<std::endl;
177  std::cout << "camera : py : "<< this->cam.get_py() << " (default)" <<std::endl;
178  }
179 
180  if(!face_node) {
181  std::cout << "face : Angle Appear : "<< angleAppear <<" (default)" <<std::endl;
182  std::cout << "face : Angle Disappear : "<< angleDisappear <<" (default)" <<std::endl;
183  }
184 
185  if(!ecm_node) {
186  std::cout << "ecm : mask : size : "<< this->m_ecm.getMaskSize() << " (default)" <<std::endl;
187  std::cout << "ecm : mask : nb_mask : "<< this->m_ecm.getMaskNumber() << " (default)" <<std::endl;
188  std::cout <<"ecm : range : tracking : "<< this->m_ecm.getRange()<< " (default)" <<std::endl;
189  std::cout <<"ecm : contrast : threshold : " << this->m_ecm.getThreshold()<<" (default)" <<std::endl;
190  std::cout <<"ecm : contrast : mu1 : " << this->m_ecm.getMu1()<<" (default)" <<std::endl;
191  std::cout <<"ecm : contrast : mu2 : " << this->m_ecm.getMu2()<<" (default)" <<std::endl;
192  }
193 
194  if(!sample_node) {
195  std::cout <<"sample : sample_step : "<< this->m_ecm.getSampleStep()<< " (default)" << std::endl;
196  std::cout <<"sample : n_total_sample : "<< this->m_ecm.getNbTotalSample()<< " (default)"<<std::endl;
197  }
198 
199  if(!lod_node) {
200  std::cout << "lod : use lod : " << useLod << " (default)" << std::endl;
201  std::cout << "lod : min line length threshold : " << minLineLengthThreshold << " (default)" << std::endl;
202  std::cout << "lod : min polygon area threshold : " << minPolygonAreaThreshold << " (default)" << std::endl;
203  }
204 }
205 
206 
215 void
216 vpMbtXmlParser::read_ecm (xmlDocPtr doc, xmlNodePtr node)
217 {
218  bool mask_node = false;
219  bool range_node = false;
220  bool contrast_node = false;
221 
222  for(xmlNodePtr dataNode = node->xmlChildrenNode; dataNode != NULL; dataNode = dataNode->next) {
223  if(dataNode->type == XML_ELEMENT_NODE){
224  std::map<std::string, int>::iterator iter_data= this->nodeMap.find((char*)dataNode->name);
225  if(iter_data != nodeMap.end()){
226  switch (iter_data->second){
227  case mask:{
228  this->read_mask (doc, dataNode);
229  mask_node = true;
230  }break;
231  case range:{
232  this->read_range (doc, dataNode);
233  range_node = true;
234  }break;
235  case contrast:{
236  this->read_contrast (doc, dataNode);
237  contrast_node = true;
238  }break;
239  default:{
240 // vpTRACE("unknown tag in read_ecm : %d, %s", iter_data->second, (iter_data->first).c_str());
241  }break;
242  }
243  }
244  }
245  }
246 
247  if(!mask_node) {
248  std::cout << "ecm : mask : size : "<< this->m_ecm.getMaskSize() << " (default)" <<std::endl;
249  std::cout << "ecm : mask : nb_mask : "<< this->m_ecm.getMaskNumber() << " (default)" <<std::endl;
250  }
251 
252  if(!range_node) {
253  std::cout <<"ecm : range : tracking : "<< this->m_ecm.getRange()<< " (default)" <<std::endl;
254  }
255 
256  if(!contrast_node) {
257  std::cout <<"ecm : contrast : threshold " << this->m_ecm.getThreshold()<<" (default)" <<std::endl;
258  std::cout <<"ecm : contrast : mu1 " << this->m_ecm.getMu1()<<" (default)" <<std::endl;
259  std::cout <<"ecm : contrast : mu2 " << this->m_ecm.getMu2()<<" (default)" <<std::endl;
260  }
261 }
262 
271 void
272 vpMbtXmlParser::read_sample (xmlDocPtr doc, xmlNodePtr node)
273 {
274  bool step_node = false;
275  bool nb_sample_node = false;
276 
277  // current data values.
278  double d_stp = this->m_ecm.getSampleStep();
279  int d_nb_sample = this->m_ecm.getNbTotalSample();
280 
281  for(xmlNodePtr dataNode = node->xmlChildrenNode; dataNode != NULL; dataNode = dataNode->next) {
282  if(dataNode->type == XML_ELEMENT_NODE){
283  std::map<std::string, int>::iterator iter_data= this->nodeMap.find((char*)dataNode->name);
284  if(iter_data != nodeMap.end()){
285  switch (iter_data->second){
286  case step:{
287  d_stp = xmlReadIntChild(doc, dataNode);
288  step_node = true;
289  }break;
290  case nb_sample:{
291  d_nb_sample = xmlReadIntChild(doc, dataNode);
292  nb_sample_node = true;
293  }break;
294  default:{
295 // vpTRACE("unknown tag in read_sample : %d, %s", iter_data->second, (iter_data->first).c_str());
296  }break;
297  }
298  }
299  }
300  }
301 
302  this->m_ecm.setSampleStep(d_stp);
303  this->m_ecm.setNbTotalSample(d_nb_sample);
304 
305  if(!step_node)
306  std::cout <<"sample : sample_step : "<< this->m_ecm.getSampleStep()<< " (default)" << std::endl;
307  else
308  std::cout <<"sample : sample_step : "<< this->m_ecm.getSampleStep()<<std::endl;
309 
310  if(!nb_sample_node)
311  std::cout <<"sample : n_total_sample : "<< this->m_ecm.getNbTotalSample()<< " (default)"<<std::endl;
312  else
313  std::cout <<"sample : n_total_sample : "<< this->m_ecm.getNbTotalSample()<<std::endl;
314 }
315 
324 void
325 vpMbtXmlParser::read_mask (xmlDocPtr doc, xmlNodePtr node)
326 {
327  bool size_node = false;
328  bool nb_mask_node = false;
329 
330  // current data values.
331  unsigned int d_size = this->m_ecm.getMaskSize();
332  unsigned int d_nb_mask = this->m_ecm.getMaskNumber();
333 
334  for(xmlNodePtr dataNode = node->xmlChildrenNode; dataNode != NULL; dataNode = dataNode->next) {
335  if(dataNode->type == XML_ELEMENT_NODE){
336  std::map<std::string, int>::iterator iter_data= this->nodeMap.find((char*)dataNode->name);
337  if(iter_data != nodeMap.end()){
338  switch (iter_data->second){
339  case size:{
340  d_size = xmlReadUnsignedIntChild(doc, dataNode);
341  size_node = true;
342  }break;
343  case nb_mask:{
344  d_nb_mask = xmlReadUnsignedIntChild(doc, dataNode);
345  nb_mask_node = true;
346  }break;
347  default:{
348 // vpTRACE("unknown tag in read_mask : %d, %s", iter_data->second, (iter_data->first).c_str());
349  }break;
350  }
351  }
352  }
353  }
354 
355  this->m_ecm.setMaskSize(d_size) ;
356  // Check to ensure that d_nb_mask > 0
357  if (! d_nb_mask)
358  throw(vpException(vpException::badValue, "Model-based tracker mask size parameter should be different from zero in xml file"));
359  this->m_ecm.setMaskNumber(d_nb_mask);
360 
361  if(!size_node)
362  std::cout << "ecm : mask : size : "<< this->m_ecm.getMaskSize() << " (default)" <<std::endl;
363  else
364  std::cout << "ecm : mask : size : "<< this->m_ecm.getMaskSize() <<std::endl;
365 
366  if(!nb_mask_node)
367  std::cout << "ecm : mask : nb_mask : "<< this->m_ecm.getMaskNumber() << " (default)" <<std::endl;
368  else
369  std::cout << "ecm : mask : nb_mask : "<< this->m_ecm.getMaskNumber() <<std::endl;
370 }
371 
380 void
381 vpMbtXmlParser::read_range (xmlDocPtr doc, xmlNodePtr node)
382 {
383  bool tracking_node = false;
384 
385  // current data values.
386  unsigned int m_range_tracking = this->m_ecm.getRange();
387 
388  for(xmlNodePtr dataNode = node->xmlChildrenNode; dataNode != NULL; dataNode = dataNode->next) {
389  if(dataNode->type == XML_ELEMENT_NODE){
390  std::map<std::string, int>::iterator iter_data= this->nodeMap.find((char*)dataNode->name);
391  if(iter_data != nodeMap.end()){
392  switch (iter_data->second){
393  case tracking:{
394  m_range_tracking = xmlReadUnsignedIntChild(doc, dataNode);
395  tracking_node = true;
396  }break;
397  default:{
398 // vpTRACE("unknown tag in read_range : %d, %s", iter_data->second, (iter_data->first).c_str());
399  }break;
400  }
401  }
402  }
403  }
404 
405  this->m_ecm.setRange(m_range_tracking);
406 
407  if(!tracking_node)
408  std::cout <<"ecm : range : tracking : "<< this->m_ecm.getRange()<< " (default)" <<std::endl;
409  else
410  std::cout <<"ecm : range : tracking : "<< this->m_ecm.getRange()<<std::endl;
411 }
412 
413 
422 void
423 vpMbtXmlParser::read_contrast (xmlDocPtr doc, xmlNodePtr node)
424 {
425  bool edge_threshold_node = false;
426  bool mu1_node = false;
427  bool mu2_node = false;
428 
429  // current data values.
430  double d_edge_threshold = this->m_ecm.getThreshold();
431  double d_mu1 = this->m_ecm.getMu1();
432  double d_mu2 = this->m_ecm.getMu2();
433 
434  for(xmlNodePtr dataNode = node->xmlChildrenNode; dataNode != NULL; dataNode = dataNode->next) {
435  if(dataNode->type == XML_ELEMENT_NODE){
436  std::map<std::string, int>::iterator iter_data= this->nodeMap.find((char*)dataNode->name);
437  if(iter_data != nodeMap.end()){
438  switch (iter_data->second){
439  case edge_threshold:{
440  d_edge_threshold = xmlReadDoubleChild(doc, dataNode);
441  edge_threshold_node = true;
442  }break;
443  case mu1:{
444  d_mu1 = xmlReadDoubleChild(doc, dataNode);
445  mu1_node = true;
446  }break;
447  case mu2:{
448  d_mu2 = xmlReadDoubleChild(doc, dataNode);
449  mu2_node = true;
450  }break;
451  default:{
452 // vpTRACE("unknown tag in read_contrast : %d, %s", iter_data->second, (iter_data->first).c_str());
453  }break;
454  }
455  }
456  }
457  }
458 
459  this->m_ecm.setMu1(d_mu1);
460  this->m_ecm.setMu2(d_mu2);
461  this->m_ecm.setThreshold(d_edge_threshold);
462 
463  if(!edge_threshold_node)
464  std::cout <<"ecm : contrast : threshold " << this->m_ecm.getThreshold()<<" (default)" <<std::endl;
465  else
466  std::cout <<"ecm : contrast : threshold " << this->m_ecm.getThreshold()<<std::endl;
467 
468  if(!mu1_node)
469  std::cout <<"ecm : contrast : mu1 " << this->m_ecm.getMu1()<<" (default)" <<std::endl;
470  else
471  std::cout <<"ecm : contrast : mu1 " << this->m_ecm.getMu1()<<std::endl;
472 
473  if(!mu2_node)
474  std::cout <<"ecm : contrast : mu2 " << this->m_ecm.getMu2()<<" (default)" <<std::endl;
475  else
476  std::cout <<"ecm : contrast : mu2 " << this->m_ecm.getMu2()<<std::endl;
477 }
478 
479 void
480 vpMbtXmlParser::read_lod (xmlDocPtr doc, xmlNodePtr node) {
481  bool use_lod_node = false;
482  bool min_line_length_threshold_node = false;
483  bool min_polygon_area_threshold_node = false;
484 
485 
486  for(xmlNodePtr dataNode = node->xmlChildrenNode; dataNode != NULL; dataNode = dataNode->next) {
487  if(dataNode->type == XML_ELEMENT_NODE){
488  std::map<std::string, int>::iterator iter_data= this->nodeMap.find((char*)dataNode->name);
489  if(iter_data != nodeMap.end()){
490  switch (iter_data->second){
491  case use_lod:
492  useLod = (xmlReadIntChild(doc, dataNode) != 0);
493  use_lod_node = true;
494  break;
497  min_line_length_threshold_node = true;
498  break;
501  min_polygon_area_threshold_node = true;
502  break;
503  default:{
504 // vpTRACE("unknown tag in read_contrast : %d, %s", iter_data->second, (iter_data->first).c_str());
505  }break;
506  }
507  }
508  }
509  }
510 
511  if(!use_lod_node)
512  std::cout << "lod : use lod : " << useLod << " (default)" <<std::endl;
513  else
514  std::cout << "lod : use lod : " << useLod << std::endl;
515 
516  if(!min_line_length_threshold_node)
517  std::cout <<"lod : min line length threshold : " << minLineLengthThreshold <<" (default)" <<std::endl;
518  else
519  std::cout <<"lod : min line length threshold : " << minLineLengthThreshold <<std::endl;
520 
521  if(!min_polygon_area_threshold_node)
522  std::cout <<"lod : min polygon area threshold : " << minPolygonAreaThreshold <<" (default)" <<std::endl;
523  else
524  std::cout <<"lod : min polygon area threshold : " << minPolygonAreaThreshold <<std::endl;
525 }
526 
527 #endif
528 
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
double minLineLengthThreshold
Minimum line length to track a segment when LOD is enabled.
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
bool useLod
If true, the LOD is enabled, otherwise it is not.
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
double minPolygonAreaThreshold
Minimum polygon area to track a face when LOD is enabled.
void read_lod(xmlDocPtr doc, xmlNodePtr node)
void parse(const std::string &filename)