ViSP  2.8.0
vpMbtXmlParser.cpp
1 /****************************************************************************
2  *
3  * $Id: vpMbtXmlParser.cpp 4320 2013-07-17 15:37:27Z ayol $
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  hasNearClipping = false;
63  hasFarClipping = false;
64  fovClipping = false;
65  init();
66 }
67 
72 {
73 }
74 
78 void
80 {
81  setMainTag("conf");
82 
83  nodeMap["conf"] = conf;
84  nodeMap["ecm"] = ecm;
85  nodeMap["mask"] = mask;
86  nodeMap["size"] = size;
87  nodeMap["nb_mask"] = nb_mask;
88  nodeMap["range"] = range;
89  nodeMap["tracking"] = tracking;
90  nodeMap["contrast"] = contrast;
91  nodeMap["edge_threshold"] = edge_threshold;
92  nodeMap["mu1"] = mu1;
93  nodeMap["mu2"] = mu2;
94  nodeMap["sample"] = sample;
95  nodeMap["step"] = step;
96  nodeMap["nb_sample"] = nb_sample;
97  nodeMap["face"] = face;
98  nodeMap["angle_appear"] = angle_appear;
99  nodeMap["angle_disappear"] = angle_disappear;
100  nodeMap["near_clipping"] = near_clipping;
101  nodeMap["far_clipping"] = far_clipping;
102  nodeMap["fov_clipping"] = fov_clipping;
103  nodeMap["camera"] = camera;
104  nodeMap["height"] = height;
105  nodeMap["width"] = width;
106  nodeMap["u0"] = u0;
107  nodeMap["v0"] = v0;
108  nodeMap["px"] = px;
109  nodeMap["py"] = py;
110 
111 }
112 
119 void
120 vpMbtXmlParser::parse(const char * filename)
121 {
122  std::string file = filename;
123  vpXmlParser::parse(file);
124 }
125 
131 void
132 vpMbtXmlParser::writeMainClass(xmlNodePtr /*node*/)
133 {
134  throw vpException(vpException::notImplementedError, "Not yet implemented." );
135 }
136 
144 void
145 vpMbtXmlParser::readMainClass(xmlDocPtr doc, xmlNodePtr node)
146 {
147  bool ecm_node = false;
148  bool sample_node = false;
149  bool camera_node = false;
150  bool face_node = false;
151 
152  for(xmlNodePtr dataNode = node->xmlChildrenNode; dataNode != NULL; dataNode = dataNode->next) {
153  if(dataNode->type == XML_ELEMENT_NODE){
154  std::map<std::string, int>::iterator iter_data= this->nodeMap.find((char*)dataNode->name);
155  if(iter_data != nodeMap.end()){
156  switch (iter_data->second){
157  case ecm:{
158  this->read_ecm (doc, dataNode);
159  ecm_node = true;
160  }break;
161  case sample:{
162  this->read_sample (doc, dataNode);
163  sample_node = true;
164  }break;
165  case camera:{
166  this->read_camera (doc, dataNode);
167  camera_node = true;
168  }break;
169  case face:{
170  this->read_face(doc, dataNode);
171  face_node = true;
172  }break;
173  default:{
174 // vpTRACE("unknown tag in read_sample : %d, %s", iter_data->second, (iter_data->first).c_str());
175  }break;
176  }
177  }
178  }
179  }
180 
181  if(!ecm_node)
182  std::cout << "WARNING: ECM Node not specified, default values used" << std::endl;
183 
184  if(!sample_node)
185  std::cout << "WARNING: SAMPLE Node not specified, default values used" << std::endl;
186 
187  if(!camera_node)
188  std::cout << "WARNING: CAMERA Node not specified, default values used" << std::endl;
189 
190  if(!face_node)
191  std::cout << "WARNING: FACE Node not specified, default values used" << std::endl;
192 }
193 
194 
203 void
204 vpMbtXmlParser::read_ecm (xmlDocPtr doc, xmlNodePtr node)
205 {
206  bool mask_node = false;
207  bool range_node = false;
208  bool contrast_node = false;
209 
210  for(xmlNodePtr dataNode = node->xmlChildrenNode; dataNode != NULL; dataNode = dataNode->next) {
211  if(dataNode->type == XML_ELEMENT_NODE){
212  std::map<std::string, int>::iterator iter_data= this->nodeMap.find((char*)dataNode->name);
213  if(iter_data != nodeMap.end()){
214  switch (iter_data->second){
215  case mask:{
216  this->read_mask (doc, dataNode);
217  mask_node = true;
218  }break;
219  case range:{
220  this->read_range (doc, dataNode);
221  range_node = true;
222  }break;
223  case contrast:{
224  this->read_contrast (doc, dataNode);
225  contrast_node = true;
226  }break;
227  default:{
228 // vpTRACE("unknown tag in read_ecm : %d, %s", iter_data->second, (iter_data->first).c_str());
229  }break;
230  }
231  }
232  }
233  }
234 
235  if(!mask_node)
236  std::cout << "WARNING: In ECM Node, MASK Node not specified, default values used" << std::endl;
237 
238  if(!range_node)
239  std::cout << "WARNING: In ECM Node, RANGE Node not specified, default values used" << std::endl;
240 
241  if(!contrast_node)
242  std::cout << "WARNING: In ECM Node, CONTRAST Node not specified, default values used" << std::endl;
243 }
244 
253 void
254 vpMbtXmlParser::read_sample (xmlDocPtr doc, xmlNodePtr node)
255 {
256  bool step_node = false;
257  bool nb_sample_node = false;
258 
259  // current data values.
260  double d_stp = this->m_ecm.getSampleStep();
261  int d_nb_sample = this->m_ecm.getNbTotalSample();
262 
263  for(xmlNodePtr dataNode = node->xmlChildrenNode; dataNode != NULL; dataNode = dataNode->next) {
264  if(dataNode->type == XML_ELEMENT_NODE){
265  std::map<std::string, int>::iterator iter_data= this->nodeMap.find((char*)dataNode->name);
266  if(iter_data != nodeMap.end()){
267  switch (iter_data->second){
268  case step:{
269  d_stp = xmlReadIntChild(doc, dataNode);
270  step_node = true;
271  }break;
272  case nb_sample:{
273  d_nb_sample = xmlReadIntChild(doc, dataNode);
274  nb_sample_node = true;
275  }break;
276  default:{
277 // vpTRACE("unknown tag in read_sample : %d, %s", iter_data->second, (iter_data->first).c_str());
278  }break;
279  }
280  }
281  }
282  }
283 
284  this->m_ecm.setSampleStep(d_stp);
285  this->m_ecm.setNbTotalSample(d_nb_sample);
286 
287  if(!step_node)
288  std::cout << "WARNING: In SAMPLE Node, STEP Node not specified, default value used : " << this->m_ecm.getSampleStep() << std::endl;
289  else
290  std::cout <<"sample : sample_step "<< this->m_ecm.getSampleStep()<<std::endl;
291 
292  if(!nb_sample_node)
293  std::cout << "WARNING: In SAMPLE Node, NB_SAMPLE Node not specified, default value used : " << this->m_ecm.getNbTotalSample() << std::endl;
294  else
295  std::cout <<"sample : n_total_sample "<< this->m_ecm.getNbTotalSample()<<std::endl;
296 }
297 
306 void
307 vpMbtXmlParser::read_camera (xmlDocPtr doc, xmlNodePtr node)
308 {
309  bool height_node = false;
310  bool width_node = false;
311  bool u0_node = false;
312  bool v0_node = false;
313  bool px_node = false;
314  bool py_node = false;
315 
316  // current data values.
317 // int d_height=0 ;
318 // int d_width= 0 ;
319  double d_u0 = this->cam.get_u0();
320  double d_v0 = this->cam.get_v0();
321  double d_px = this->cam.get_px();
322  double d_py = this->cam.get_py();
323 
324  for(xmlNodePtr dataNode = node->xmlChildrenNode; dataNode != NULL; dataNode = dataNode->next) {
325  if(dataNode->type == XML_ELEMENT_NODE){
326  std::map<std::string, int>::iterator iter_data= this->nodeMap.find((char*)dataNode->name);
327  if(iter_data != nodeMap.end()){
328  switch (iter_data->second){
329  case height:{
330  /* d_height = */ xmlReadIntChild(doc, dataNode);
331  height_node = true;
332  }break;
333  case width:{
334  /* d_width = */ xmlReadIntChild(doc, dataNode);
335  width_node = true;
336  }break;
337  case u0:{
338  d_u0 = xmlReadDoubleChild(doc, dataNode);
339  u0_node = true;
340  }break;
341  case v0:{
342  d_v0 = xmlReadDoubleChild(doc, dataNode);
343  v0_node = true;
344  }break;
345  case px:{
346  d_px = xmlReadDoubleChild(doc, dataNode);
347  px_node = true;
348  }break;
349  case py:{
350  d_py = xmlReadDoubleChild(doc, dataNode);
351  py_node = true;
352  }break;
353  default:{
354 // vpTRACE("unknown tag in read_camera : %d, %s", iter_data->second, (iter_data->first).c_str());
355  }break;
356  }
357  }
358  }
359  }
360 
361  this->cam.initPersProjWithoutDistortion(d_px, d_py, d_u0, d_v0) ;
362 
363  if(!height_node)
364  std::cout << "WARNING: In CAMERA Node, HEIGHT Node not specified, default value used" << std::endl;
365 
366  if(!width_node)
367  std::cout << "WARNING: In CAMERA Node, WIDTH Node not specified, default value used" << std::endl;
368 
369  if(!u0_node)
370  std::cout << "WARNING: In CAMERA Node, u0 Node not specified, default value used : " << this->cam.get_u0() << std::endl;
371  else
372  std::cout << "camera : u0 "<< this->cam.get_u0() <<std::endl;
373 
374  if(!v0_node)
375  std::cout << "WARNING: In CAMERA Node, v0 Node not specified, default value used : " << this->cam.get_v0() << std::endl;
376  else
377  std::cout << "camera : v0 "<< this->cam.get_v0() <<std::endl;
378 
379  if(!px_node)
380  std::cout << "WARNING: In CAMERA Node, px Node not specified, default value used : " << this->cam.get_px() << std::endl;
381  else
382  std::cout << "camera : px "<< this->cam.get_px() <<std::endl;
383 
384  if(!py_node)
385  std::cout << "WARNING: In CAMERA Node, py Node not specified, default value used : " << this->cam.get_py() << std::endl;
386  else
387  std::cout << "camera : py "<< this->cam.get_py() <<std::endl;
388 }
389 
398 void
399 vpMbtXmlParser::read_face(xmlDocPtr doc, xmlNodePtr node)
400 {
401  bool angle_appear_node = false;
402  bool angle_disappear_node = false;
403  bool near_clipping_node = false;
404  bool far_clipping_node = false;
405  bool fov_clipping_node = false;
406 
407  for(xmlNodePtr dataNode = node->xmlChildrenNode; dataNode != NULL; dataNode = dataNode->next) {
408  if(dataNode->type == XML_ELEMENT_NODE){
409  std::map<std::string, int>::iterator iter_data= this->nodeMap.find((char*)dataNode->name);
410  if(iter_data != nodeMap.end()){
411  switch (iter_data->second){
412  case angle_appear:{
413  angleAppear = xmlReadDoubleChild(doc, dataNode);
414  angle_appear_node = true;
415  }break;
416  case angle_disappear:{
417  angleDisappear = xmlReadDoubleChild(doc, dataNode);
418  angle_disappear_node = true;
419  }break;
420  case near_clipping:{
421  nearClipping = xmlReadDoubleChild(doc, dataNode);
422  near_clipping_node = true;
423  hasNearClipping = true;
424  }break;
425  case far_clipping:{
426  farClipping = xmlReadDoubleChild(doc, dataNode);
427  far_clipping_node = true;
428  hasFarClipping = true;
429  }break;
430  case fov_clipping:{
431  fovClipping = (bool)xmlReadIntChild(doc, dataNode);
432  fov_clipping_node = true;
433  }break;
434  default:{
435 // vpTRACE("unknown tag in read_camera : %d, %s", iter_data->second, (iter_data->first).c_str());
436  }break;
437  }
438  }
439  }
440  }
441 
442  if(!angle_appear_node)
443  std::cout << "WARNING: In FACE Node, ANGLE_APPEAR Node not specified, default value used : " << angleAppear << std::endl;
444  else
445  std::cout << "face : Angle Appear "<< angleAppear <<std::endl;
446 
447  if(!angle_disappear_node)
448  std::cout << "WARNING: In FACE Node, ANGLE_DESAPPEAR Node not specified, default value used : " << angleDisappear << std::endl;
449  else
450  std::cout << "face : Angle Disappear : "<< angleDisappear <<std::endl;
451 
452  if(!near_clipping_node)
453  std::cout << "WARNING: In FACE Node, NEAR_CLIPPING Node not specified, no near clipping used" << std::endl;
454  else
455  std::cout << "face : Near Clipping : "<< nearClipping <<std::endl;
456 
457  if(!far_clipping_node)
458  std::cout << "WARNING: In FACE Node, FAR_CLIPPING Node not specified, no far clipping used" << std::endl;
459  else
460  std::cout << "face : Far Clipping : "<< farClipping <<std::endl;
461 
462  if(!fov_clipping_node)
463  std::cout << "WARNING: In FACE Node, FOV_CLIPPING Node not specified, no fov clipping used" << std::endl;
464  else{
465  if(fovClipping)
466  std::cout << "face : Fov Clipping : True" <<std::endl;
467  else
468  std::cout << "face : Fov Clipping : False" <<std::endl;
469  }
470 }
471 
480 void
481 vpMbtXmlParser::read_mask (xmlDocPtr doc, xmlNodePtr node)
482 {
483  bool size_node = false;
484  bool nb_mask_node = false;
485 
486  // current data values.
487  unsigned int d_size = this->m_ecm.getMaskSize();
488  unsigned int d_nb_mask = this->m_ecm.getMaskNumber();
489 
490  for(xmlNodePtr dataNode = node->xmlChildrenNode; dataNode != NULL; dataNode = dataNode->next) {
491  if(dataNode->type == XML_ELEMENT_NODE){
492  std::map<std::string, int>::iterator iter_data= this->nodeMap.find((char*)dataNode->name);
493  if(iter_data != nodeMap.end()){
494  switch (iter_data->second){
495  case size:{
496  d_size = xmlReadUnsignedIntChild(doc, dataNode);
497  size_node = true;
498  }break;
499  case nb_mask:{
500  d_nb_mask = xmlReadUnsignedIntChild(doc, dataNode);
501  nb_mask_node = true;
502  }break;
503  default:{
504 // vpTRACE("unknown tag in read_mask : %d, %s", iter_data->second, (iter_data->first).c_str());
505  }break;
506  }
507  }
508  }
509  }
510 
511  this->m_ecm.setMaskSize(d_size) ;
512  this->m_ecm.setMaskNumber(d_nb_mask);
513 
514  if(!size_node)
515  std::cout << "WARNING: In MASK Node, SIZE Node not specified, default value used : " << this->m_ecm.getMaskSize() << std::endl;
516  else
517  std::cout << "ecm : mask : size "<< this->m_ecm.getMaskSize() <<std::endl;
518 
519  if(!nb_mask_node)
520  std::cout << "WARNING: In MASK Node, NB_MASK Node not specified, default value used : " << this->m_ecm.getMaskNumber() << std::endl;
521  else
522  std::cout << "ecm : mask : nb_mask "<< this->m_ecm.getMaskNumber() <<std::endl;
523 }
524 
533 void
534 vpMbtXmlParser::read_range (xmlDocPtr doc, xmlNodePtr node)
535 {
536  bool tracking_node = false;
537 
538  // current data values.
539  unsigned int m_range_tracking = this->m_ecm.getRange();
540 
541  for(xmlNodePtr dataNode = node->xmlChildrenNode; dataNode != NULL; dataNode = dataNode->next) {
542  if(dataNode->type == XML_ELEMENT_NODE){
543  std::map<std::string, int>::iterator iter_data= this->nodeMap.find((char*)dataNode->name);
544  if(iter_data != nodeMap.end()){
545  switch (iter_data->second){
546  case tracking:{
547  m_range_tracking = xmlReadUnsignedIntChild(doc, dataNode);
548  tracking_node = true;
549  }break;
550  default:{
551 // vpTRACE("unknown tag in read_range : %d, %s", iter_data->second, (iter_data->first).c_str());
552  }break;
553  }
554  }
555  }
556  }
557 
558  this->m_ecm.setRange(m_range_tracking);
559 
560  if(!tracking_node)
561  std::cout << "WARNING: In RANGE Node, TRACKING Node not specified, default value used : " << this->m_ecm.getRange() << std::endl;
562  else
563  std::cout <<"ecm : range : tracking "<< this->m_ecm.getRange()<<std::endl;
564 }
565 
566 
575 void
576 vpMbtXmlParser::read_contrast (xmlDocPtr doc, xmlNodePtr node)
577 {
578  bool edge_threshold_node = false;
579  bool mu1_node = false;
580  bool mu2_node = false;
581 
582  // current data values.
583  double d_edge_threshold = this->m_ecm.getThreshold();
584  double d_mu1 = this->m_ecm.getMu1();
585  double d_mu2 = this->m_ecm.getMu2();
586 
587  for(xmlNodePtr dataNode = node->xmlChildrenNode; dataNode != NULL; dataNode = dataNode->next) {
588  if(dataNode->type == XML_ELEMENT_NODE){
589  std::map<std::string, int>::iterator iter_data= this->nodeMap.find((char*)dataNode->name);
590  if(iter_data != nodeMap.end()){
591  switch (iter_data->second){
592  case edge_threshold:{
593  d_edge_threshold = xmlReadDoubleChild(doc, dataNode);
594  edge_threshold_node = true;
595  }break;
596  case mu1:{
597  d_mu1 = xmlReadDoubleChild(doc, dataNode);
598  mu1_node = true;
599  }break;
600  case mu2:{
601  d_mu2 = xmlReadDoubleChild(doc, dataNode);
602  mu2_node = true;
603  }break;
604  default:{
605 // vpTRACE("unknown tag in read_contrast : %d, %s", iter_data->second, (iter_data->first).c_str());
606  }break;
607  }
608  }
609  }
610  }
611 
612  this->m_ecm.setMu1(d_mu1);
613  this->m_ecm.setMu2(d_mu2);
614  this->m_ecm.setThreshold(d_edge_threshold);
615 
616  if(!edge_threshold_node)
617  std::cout << "WARNING: In CONTRAST Node, EDGE_THRESHOLD Node not specified, default value used : " << this->m_ecm.getThreshold() << std::endl;
618  else
619  std::cout <<"ecm : contrast : threshold " << this->m_ecm.getThreshold()<<std::endl;
620 
621  if(!mu1_node)
622  std::cout << "WARNING: In CONTRAST Node, mu1 Node not specified, default value used : " << this->m_ecm.getMu1() << std::endl;
623  else
624  std::cout <<"ecm : contrast : mu1 " << this->m_ecm.getMu1()<<std::endl;
625 
626  if(!mu2_node)
627  std::cout << "WARNING: In CONTRAST Node, mu2 Node not specified, default value used : " << this->m_ecm.getMu2() << std::endl;
628  else
629  std::cout <<"ecm : contrast : mu2 " << this->m_ecm.getMu2()<<std::endl;
630 }
631 
632 #endif
633 
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)
double farClipping
Near clipping distance.
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
bool fovClipping
Fov Clipping.
error that can be emited by ViSP classes.
Definition: vpException.h:75
double xmlReadDoubleChild(xmlDocPtr doc, xmlNodePtr node)
double get_py() const
bool hasNearClipping
Is near clipping distance specified?
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)
bool hasFarClipping
Is far clipping distance specified?
double nearClipping
Near clipping distance.
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)