ViSP  2.6.2
vpMbtXmlParser.cpp
1 /****************************************************************************
2  *
3  * $Id: vpMbtXmlParser.cpp 3672 2012-04-04 15:49:57Z ayol $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2012 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 #ifndef DOXYGEN_SHOULD_SKIP_THIS
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()
62 {
63  init();
64 }
65 
69 vpMbtXmlParser::~vpMbtXmlParser()
70 {
71 }
72 
76 void
77 vpMbtXmlParser::init()
78 {
79  setMainTag("conf");
80 
81  nodeMap["conf"] = conf;
82  nodeMap["ecm"] = ecm;
83  nodeMap["mask"] = mask;
84  nodeMap["size"] = size;
85  nodeMap["nb_mask"] = nb_mask;
86  nodeMap["range"] = range;
87  nodeMap["tracking"] = tracking;
88  nodeMap["contrast"] = contrast;
89  nodeMap["edge_threshold"] = edge_threshold;
90  nodeMap["mu1"] = mu1;
91  nodeMap["mu2"] = mu2;
92  nodeMap["sample"] = sample;
93  nodeMap["step"] = step;
94  nodeMap["nb_sample"] = nb_sample;
95  nodeMap["camera"] = camera;
96  nodeMap["height"] = height;
97  nodeMap["width"] = width;
98  nodeMap["u0"] = u0;
99  nodeMap["v0"] = v0;
100  nodeMap["px"] = px;
101  nodeMap["py"] = py;
102 
103 }
104 
111 void
112 vpMbtXmlParser::parse(const char * filename)
113 {
114  std::string file = filename;
115  vpXmlParser::parse(file);
116 }
117 
123 void
124 vpMbtXmlParser::writeMainClass(xmlNodePtr /*node*/)
125 {
126  throw vpException(vpException::notImplementedError, "Not yet implemented." );
127 }
128 
136 void
137 vpMbtXmlParser::readMainClass(xmlDocPtr doc, xmlNodePtr node)
138 {
139  // current data values.
140  unsigned int nb=0;
141  for(xmlNodePtr dataNode = node->xmlChildrenNode; dataNode != NULL; dataNode = dataNode->next) {
142  if(dataNode->type == XML_ELEMENT_NODE){
143  std::map<std::string, int>::iterator iter_data= this->nodeMap.find((char*)dataNode->name);
144  if(iter_data != nodeMap.end()){
145  switch (iter_data->second){
146  case ecm:{
147  this->lecture_ecm (doc, dataNode);
148  nb++;
149  }break;
150  case sample:{
151  this->lecture_sample (doc, dataNode);
152  nb++;
153  }break;
154  case camera:{
155  this->lecture_camera (doc, dataNode);
156  nb++;
157  }break;
158  default:{
159 // vpTRACE("unknown tag in lecture_sample : %d, %s", iter_data->second, (iter_data->first).c_str());
160  }break;
161  }
162  }
163  }
164  }
165 
166  if(nb != 3){
167  std::cout <<"ERROR in 'ECM' field:\n";
168  std::cout << "it must contain 3 parameters\n";
169  throw vpException(vpException::fatalError, "Bad number of data to extract ECM informations.");
170  }
171 }
172 
173 
182 void
183 vpMbtXmlParser::lecture_ecm (xmlDocPtr doc, xmlNodePtr node)
184 {
185  // current data values.
186  unsigned int nb=0;
187  for(xmlNodePtr dataNode = node->xmlChildrenNode; dataNode != NULL; dataNode = dataNode->next) {
188  if(dataNode->type == XML_ELEMENT_NODE){
189  std::map<std::string, int>::iterator iter_data= this->nodeMap.find((char*)dataNode->name);
190  if(iter_data != nodeMap.end()){
191  switch (iter_data->second){
192  case mask:{
193  this->lecture_mask (doc, dataNode);
194  nb++;
195  }break;
196  case range:{
197  this->lecture_range (doc, dataNode);
198  nb++;
199  }break;
200  case contrast:{
201  this->lecture_contrast (doc, dataNode);
202  nb++;
203  }break;
204  default:{
205 // vpTRACE("unknown tag in lecture_ecm : %d, %s", iter_data->second, (iter_data->first).c_str());
206  }break;
207  }
208  }
209  }
210  }
211 
212  if(nb != 3){
213  std::cout <<"ERROR in 'ECM' field:\n";
214  std::cout << "it must contain 3 parameters\n";
215  throw vpException(vpException::fatalError, "Bad number of data to extract ECM informations.");
216  }
217 }
218 
227 void
228 vpMbtXmlParser::lecture_sample (xmlDocPtr doc, xmlNodePtr node)
229 {
230  // current data values.
231  double d_stp = this->m_ecm.getSampleStep();
232  int d_nb_sample = this->m_ecm.getNbTotalSample();
233 
234  unsigned int nb=0;
235  for(xmlNodePtr dataNode = node->xmlChildrenNode; dataNode != NULL; dataNode = dataNode->next) {
236  if(dataNode->type == XML_ELEMENT_NODE){
237  std::map<std::string, int>::iterator iter_data= this->nodeMap.find((char*)dataNode->name);
238  if(iter_data != nodeMap.end()){
239  switch (iter_data->second){
240  case step:{
241  d_stp = xmlReadIntChild(doc, dataNode);
242  nb++;
243  }break;
244  case nb_sample:{
245  d_nb_sample = xmlReadIntChild(doc, dataNode);
246  nb++;
247  }break;
248  default:{
249 // vpTRACE("unknown tag in lecture_sample : %d, %s", iter_data->second, (iter_data->first).c_str());
250  }break;
251  }
252  }
253  }
254  }
255 
256  if(nb == 2){
257  this->m_ecm.setSampleStep(d_stp);
258  this->m_ecm.setNbTotalSample(d_nb_sample);
259 
260  std::cout <<"**** sample:\n";
261  std::cout <<"sample_step "<< this->m_ecm.getSampleStep()<<std::endl;
262  std::cout <<"n_total_sample "<< this->m_ecm.getNbTotalSample()<<std::endl;
263  }
264  else{
265  std::cout <<"ERROR in 'sample' field:\n";
266  std::cout << "it must contain 2 parameters\n";
267  throw vpException(vpException::fatalError, "Bad number of data to extract sample informations.");
268  }
269 }
270 
279 void
280 vpMbtXmlParser::lecture_camera (xmlDocPtr doc, xmlNodePtr node)
281 {
282  // current data values.
283 // int d_height=0 ;
284 // int d_width= 0 ;
285  double d_u0 = this->cam.get_u0();
286  double d_v0 = this->cam.get_v0();
287  double d_px = this->cam.get_px();
288  double d_py = this->cam.get_py();
289 
290  unsigned int nb=0;
291  for(xmlNodePtr dataNode = node->xmlChildrenNode; dataNode != NULL; dataNode = dataNode->next) {
292  if(dataNode->type == XML_ELEMENT_NODE){
293  std::map<std::string, int>::iterator iter_data= this->nodeMap.find((char*)dataNode->name);
294  if(iter_data != nodeMap.end()){
295  switch (iter_data->second){
296  case height:{
297  /* d_height = */ xmlReadIntChild(doc, dataNode);
298  nb++;
299  }break;
300  case width:{
301  /* d_width = */ xmlReadIntChild(doc, dataNode);
302  nb++;
303  }break;
304  case u0:{
305  d_u0 = xmlReadDoubleChild(doc, dataNode);
306  nb++;
307  }break;
308  case v0:{
309  d_v0 = xmlReadDoubleChild(doc, dataNode);
310  nb++;
311  }break;
312  case px:{
313  d_px = xmlReadDoubleChild(doc, dataNode);
314  nb++;
315  }break;
316  case py:{
317  d_py = xmlReadDoubleChild(doc, dataNode);
318  nb++;
319  }break;
320  default:{
321 // vpTRACE("unknown tag in lecture_camera : %d, %s", iter_data->second, (iter_data->first).c_str());
322  }break;
323  }
324  }
325  }
326  }
327 
328  if(nb == 6){
329  this->cam.initPersProjWithoutDistortion(d_px, d_py, d_u0, d_v0) ;
330 
331  std::cout <<"**** camera: \n"<<nb <<std::endl;
332  std::cout << "u0 "<< this->cam.get_u0() <<std::endl;
333  std::cout << "v0 "<< this->cam.get_v0() <<std::endl;
334  std::cout << "px "<< this->cam.get_px() <<std::endl;
335  std::cout << "py "<< this->cam.get_py() <<std::endl;
336  }
337  else{
338  std::cout <<"ERROR in 'camera' field:\n";
339  std::cout << "it must contain 6 parameters\n";
340  throw vpException(vpException::fatalError, "Bad number of data to extract camera informations.");
341  }
342 }
343 
352 void
353 vpMbtXmlParser::lecture_mask (xmlDocPtr doc, xmlNodePtr node)
354 {
355  // current data values.
356  unsigned int d_size = this->m_ecm.getMaskSize();
357  unsigned int d_nb_mask = this->m_ecm.getMaskNumber();
358 
359  unsigned int nb=0;
360  for(xmlNodePtr dataNode = node->xmlChildrenNode; dataNode != NULL; dataNode = dataNode->next) {
361  if(dataNode->type == XML_ELEMENT_NODE){
362  std::map<std::string, int>::iterator iter_data= this->nodeMap.find((char*)dataNode->name);
363  if(iter_data != nodeMap.end()){
364  switch (iter_data->second){
365  case size:{
366  d_size = xmlReadUnsignedIntChild(doc, dataNode);
367  nb++;
368  }break;
369  case nb_mask:{
370  d_nb_mask = xmlReadUnsignedIntChild(doc, dataNode);
371  nb++;
372  }break;
373  default:{
374 // vpTRACE("unknown tag in lecture_mask : %d, %s", iter_data->second, (iter_data->first).c_str());
375  }break;
376  }
377  }
378  }
379  }
380 
381  if(nb == 2){
382  this->m_ecm.setMaskSize(d_size) ;
383  this->m_ecm.setMaskNumber(d_nb_mask);
384 
385  std::cout << "**** mask:\n";
386  std::cout << "size "<< this->m_ecm.getMaskSize() <<std::endl;
387  std::cout << "nb_mask "<< this->m_ecm.getMaskNumber() <<std::endl;
388  }
389  else{
390  std::cout <<"ERROR in 'mask' field:\n";
391  std::cout << "it must contain 2 parameters\n";
392  throw vpException(vpException::fatalError, "Bad number of data to extract mask informations.");
393  }
394 }
395 
404 void
405 vpMbtXmlParser::lecture_range (xmlDocPtr doc, xmlNodePtr node)
406 {
407  // current data values.
408  unsigned int m_range_tracking = this->m_ecm.getRange();
409 
410  unsigned int nb=0;
411  for(xmlNodePtr dataNode = node->xmlChildrenNode; dataNode != NULL; dataNode = dataNode->next) {
412  if(dataNode->type == XML_ELEMENT_NODE){
413  std::map<std::string, int>::iterator iter_data= this->nodeMap.find((char*)dataNode->name);
414  if(iter_data != nodeMap.end()){
415  switch (iter_data->second){
416  case tracking:{
417  m_range_tracking = xmlReadUnsignedIntChild(doc, dataNode);
418  nb++;
419  }break;
420  default:{
421 // vpTRACE("unknown tag in lecture_range : %d, %s", iter_data->second, (iter_data->first).c_str());
422  }break;
423  }
424  }
425  }
426  }
427 
428  if(nb == 1){
429  this->m_ecm.setRange(m_range_tracking);
430  std::cout <<"**** range:\n";
431  std::cout <<"tracking "<< this->m_ecm.getRange()<<std::endl;
432  }
433  else{
434  std::cout <<"ERROR in 'range' field:\n";
435  std::cout << "it must contain 1 parameters\n";
436  throw vpException(vpException::fatalError, "Bad number of data to extract range informations.");
437  }
438 }
439 
440 
449 void
450 vpMbtXmlParser::lecture_contrast (xmlDocPtr doc, xmlNodePtr node)
451 {
452  // current data values.
453  double d_edge_threshold = this->m_ecm.getThreshold();
454  double d_mu1 = this->m_ecm.getMu1();
455  double d_mu2 = this->m_ecm.getMu2();
456 
457  unsigned int nb=0;
458  for(xmlNodePtr dataNode = node->xmlChildrenNode; dataNode != NULL; dataNode = dataNode->next) {
459  if(dataNode->type == XML_ELEMENT_NODE){
460  std::map<std::string, int>::iterator iter_data= this->nodeMap.find((char*)dataNode->name);
461  if(iter_data != nodeMap.end()){
462  switch (iter_data->second){
463  case edge_threshold:{
464  d_edge_threshold = xmlReadDoubleChild(doc, dataNode);
465  nb++;
466  }break;
467  case mu1:{
468  d_mu1 = xmlReadDoubleChild(doc, dataNode);
469  nb++;
470  }break;
471  case mu2:{
472  d_mu2 = xmlReadDoubleChild(doc, dataNode);
473  nb++;
474  }break;
475  default:{
476 // vpTRACE("unknown tag in lecture_contrast : %d, %s", iter_data->second, (iter_data->first).c_str());
477  }break;
478  }
479  }
480  }
481  }
482 
483  if(nb == 3){
484  this->m_ecm.setMu1(d_mu1);
485  this->m_ecm.setMu2(d_mu2);
486  this->m_ecm.setThreshold(d_edge_threshold);
487 
488  std::cout <<"**** contrast:\n";
489  std::cout <<"mu1 " << this->m_ecm.getMu1()<<std::endl;
490  std::cout <<"mu2 " << this->m_ecm.getMu2()<<std::endl;
491  std::cout <<"threshold " << this->m_ecm.getThreshold()<<std::endl;
492  }
493  else{
494  std::cout <<"ERROR in 'contrast' field:\n";
495  std::cout << "it must contain 3 parameters\n";
496  throw vpException(vpException::fatalError, "Bad number of data to extract contrast informations.");
497  }
498 }
499 
500 
501 #endif
502 
503 #endif
504 
double get_u0() const
double get_py() const
void initPersProjWithoutDistortion(const double px, const double py, const double u0, const double v0)
double get_v0() const
double get_px() const
void parse(const std::string &filename)