Visual Servoing Platform  version 3.0.0
vpMbXmlParser.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2015 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See http://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Load XML Parameter for Model Based Tracker.
32  *
33  * Authors:
34  * Aurelien Yol
35  *
36  *****************************************************************************/
37 #include <visp3/core/vpConfig.h>
38 
39 
40 #ifdef VISP_HAVE_XML2
41 
42 #include <iostream>
43 #include <map>
44 
45 #include <libxml/xmlmemory.h> /* Fonctions de la lib XML. */
46 
47 #include <visp3/mbt/vpMbXmlParser.h>
48 
49 
55  : cam(), angleAppear(70), angleDisappear(80),
56  hasNearClipping(false), nearClipping(false),
57  hasFarClipping(false), farClipping(false), fovClipping(false),
58  useLod(false), minLineLengthThreshold(50.0), minPolygonAreaThreshold(2500.0)
59 
60 {
61  init();
62 }
63 
68 {
69 }
70 
74 void
76 {
77  setMainTag("conf");
78 
79  nodeMap["conf"] = conf;
80  nodeMap["face"] = face;
81  nodeMap["angle_appear"] = angle_appear;
82  nodeMap["angle_disappear"] = angle_disappear;
83  nodeMap["near_clipping"] = near_clipping;
84  nodeMap["far_clipping"] = far_clipping;
85  nodeMap["fov_clipping"] = fov_clipping;
86  nodeMap["camera"] = camera;
87  nodeMap["height"] = height;
88  nodeMap["width"] = width;
89  nodeMap["u0"] = u0;
90  nodeMap["v0"] = v0;
91  nodeMap["px"] = px;
92  nodeMap["py"] = py;
93  nodeMap["lod"] = lod;
94  nodeMap["use_lod"] = use_lod;
95  nodeMap["min_line_length_threshold"] = min_line_length_threshold;
96  nodeMap["min_polygon_area_threshold"] = min_polygon_area_threshold;
97 }
98 
105 void
106 vpMbXmlParser::parse(const char * filename)
107 {
108  std::string file = filename;
109  vpXmlParser::parse(file);
110 }
111 
117 void
118 vpMbXmlParser::writeMainClass(xmlNodePtr /*node*/)
119 {
120  throw vpException(vpException::notImplementedError, "Not yet implemented." );
121 }
122 
130 void
131 vpMbXmlParser::readMainClass(xmlDocPtr doc, xmlNodePtr node)
132 {
133  bool camera_node = false;
134  bool face_node = false;
135  bool lod_node = false;
136 
137  for(xmlNodePtr dataNode = node->xmlChildrenNode; dataNode != NULL; dataNode = dataNode->next) {
138  if(dataNode->type == XML_ELEMENT_NODE){
139  std::map<std::string, int>::iterator iter_data= this->nodeMap.find((char*)dataNode->name);
140  if(iter_data != nodeMap.end()){
141  switch (iter_data->second){
142  case camera:{
143  this->read_camera (doc, dataNode);
144  camera_node = true;
145  }break;
146  case face:{
147  this->read_face(doc, dataNode);
148  face_node = true;
149  }break;
150  case lod:{
151  this->read_lod(doc, dataNode);
152  lod_node = true;
153  }break;
154  default:{
155 // vpTRACE("unknown tag in read_sample : %d, %s", iter_data->second, (iter_data->first).c_str());
156  }break;
157  }
158  }
159  }
160  }
161 
162  if(!camera_node) {
163  std::cout << "camera : u0 : "<< this->cam.get_u0() << " (default)" <<std::endl;
164  std::cout << "camera : v0 : "<< this->cam.get_v0() << " (default)" <<std::endl;
165  std::cout << "camera : px : "<< this->cam.get_px() << " (default)" <<std::endl;
166  std::cout << "camera : py : "<< this->cam.get_py() << " (default)" <<std::endl;
167  }
168 
169  if(!face_node) {
170  std::cout << "face : Angle Appear : "<< angleAppear <<" (default)" <<std::endl;
171  std::cout << "face : Angle Disappear : "<< angleDisappear <<" (default)" <<std::endl;
172  }
173 
174  if(!lod_node) {
175  std::cout << "lod : use lod : " << useLod << " (default)" << std::endl;
176  std::cout << "lod : min line length threshold : " << minLineLengthThreshold << " (default)" << std::endl;
177  std::cout << "lod : min polygon area threshold : " << minPolygonAreaThreshold << " (default)" << std::endl;
178  }
179 }
180 
189 void
190 vpMbXmlParser::read_camera (xmlDocPtr doc, xmlNodePtr node)
191 {
192  bool u0_node = false;
193  bool v0_node = false;
194  bool px_node = false;
195  bool py_node = false;
196 
197  // current data values.
198  double d_u0 = this->cam.get_u0();
199  double d_v0 = this->cam.get_v0();
200  double d_px = this->cam.get_px();
201  double d_py = this->cam.get_py();
202 
203  for(xmlNodePtr dataNode = node->xmlChildrenNode; dataNode != NULL; dataNode = dataNode->next) {
204  if(dataNode->type == XML_ELEMENT_NODE){
205  std::map<std::string, int>::iterator iter_data= this->nodeMap.find((char*)dataNode->name);
206  if(iter_data != nodeMap.end()){
207  switch (iter_data->second){
208  case u0:{
209  d_u0 = xmlReadDoubleChild(doc, dataNode);
210  u0_node = true;
211  }break;
212  case v0:{
213  d_v0 = xmlReadDoubleChild(doc, dataNode);
214  v0_node = true;
215  }break;
216  case px:{
217  d_px = xmlReadDoubleChild(doc, dataNode);
218  px_node = true;
219  }break;
220  case py:{
221  d_py = xmlReadDoubleChild(doc, dataNode);
222  py_node = true;
223  }break;
224  default:{
225 // vpTRACE("unknown tag in read_camera : %d, %s", iter_data->second, (iter_data->first).c_str());
226  }break;
227  }
228  }
229  }
230  }
231 
232  this->cam.initPersProjWithoutDistortion(d_px, d_py, d_u0, d_v0) ;
233 
234  if(!u0_node)
235  std::cout << "camera : u0 : "<< this->cam.get_u0() << " (default)" <<std::endl;
236  else
237  std::cout << "camera : u0 : "<< this->cam.get_u0() <<std::endl;
238 
239  if(!v0_node)
240  std::cout << "camera : v0 : "<< this->cam.get_v0() << " (default)" <<std::endl;
241  else
242  std::cout << "camera : v0 : "<< this->cam.get_v0() <<std::endl;
243 
244  if(!px_node)
245  std::cout << "camera : px : "<< this->cam.get_px() << " (default)" <<std::endl;
246  else
247  std::cout << "camera : px : "<< this->cam.get_px() <<std::endl;
248 
249  if(!py_node)
250  std::cout << "camera : py : "<< this->cam.get_py() << " (default)" <<std::endl;
251  else
252  std::cout << "camera : py : "<< this->cam.get_py() <<std::endl;
253 }
254 
263 void
264 vpMbXmlParser::read_face(xmlDocPtr doc, xmlNodePtr node)
265 {
266  bool angle_appear_node = false;
267  bool angle_disappear_node = false;
268  bool near_clipping_node = false;
269  bool far_clipping_node = false;
270  bool fov_clipping_node = false;
271 
272  for(xmlNodePtr dataNode = node->xmlChildrenNode; dataNode != NULL; dataNode = dataNode->next) {
273  if(dataNode->type == XML_ELEMENT_NODE){
274  std::map<std::string, int>::iterator iter_data= this->nodeMap.find((char*)dataNode->name);
275  if(iter_data != nodeMap.end()){
276  switch (iter_data->second){
277  case angle_appear:{
278  angleAppear = xmlReadDoubleChild(doc, dataNode);
279  angle_appear_node = true;
280  }break;
281  case angle_disappear:{
282  angleDisappear = xmlReadDoubleChild(doc, dataNode);
283  angle_disappear_node = true;
284  }break;
285  case near_clipping:{
286  nearClipping = xmlReadDoubleChild(doc, dataNode);
287  near_clipping_node = true;
288  hasNearClipping = true;
289  }break;
290  case far_clipping:{
291  farClipping = xmlReadDoubleChild(doc, dataNode);
292  far_clipping_node = true;
293  hasFarClipping = true;
294  }break;
295  case fov_clipping:{
296  if (xmlReadIntChild(doc, dataNode))
297  fovClipping = true;
298  else
299  fovClipping = false;
300  fov_clipping_node = true;
301  }break;
302  default:{
303 // vpTRACE("unknown tag in read_camera : %d, %s", iter_data->second, (iter_data->first).c_str());
304  }break;
305  }
306  }
307  }
308  }
309 
310  if(!angle_appear_node)
311  std::cout << "face : Angle Appear : "<< angleAppear << " (default)" <<std::endl;
312  else
313  std::cout << "face : Angle Appear : "<< angleAppear <<std::endl;
314 
315  if(!angle_disappear_node)
316  std::cout << "face : Angle Disappear : "<< angleDisappear << " (default)" <<std::endl;
317  else
318  std::cout << "face : Angle Disappear : "<< angleDisappear <<std::endl;
319 
320  if(near_clipping_node)
321  std::cout << "face : Near Clipping : "<< nearClipping <<std::endl;
322 
323  if(far_clipping_node)
324  std::cout << "face : Far Clipping : "<< farClipping <<std::endl;
325 
326  if(fov_clipping_node) {
327  if(fovClipping)
328  std::cout << "face : Fov Clipping : True" <<std::endl;
329  else
330  std::cout << "face : Fov Clipping : False" <<std::endl;
331  }
332 }
333 
334 void
335 vpMbXmlParser::read_lod (xmlDocPtr doc, xmlNodePtr node) {
336  bool use_lod_node = false;
337  bool min_line_length_threshold_node = false;
338  bool min_polygon_area_threshold_node = false;
339 
340 
341  for(xmlNodePtr dataNode = node->xmlChildrenNode; dataNode != NULL; dataNode = dataNode->next) {
342  if(dataNode->type == XML_ELEMENT_NODE){
343  std::map<std::string, int>::iterator iter_data= this->nodeMap.find((char*)dataNode->name);
344  if(iter_data != nodeMap.end()){
345  switch (iter_data->second){
346  case use_lod:
347  useLod = (xmlReadIntChild(doc, dataNode) != 0);
348  use_lod_node = true;
349  break;
352  min_line_length_threshold_node = true;
353  break;
356  min_polygon_area_threshold_node = true;
357  break;
358  default:{
359 // vpTRACE("unknown tag in read_contrast : %d, %s", iter_data->second, (iter_data->first).c_str());
360  }break;
361  }
362  }
363  }
364  }
365 
366  if(!use_lod_node)
367  std::cout << "lod : use lod : " << useLod << " (default)" <<std::endl;
368  else
369  std::cout << "lod : use lod : " << useLod << std::endl;
370 
371  if(!min_line_length_threshold_node)
372  std::cout <<"lod : min line length threshold : " << minLineLengthThreshold <<" (default)" <<std::endl;
373  else
374  std::cout <<"lod : min line length threshold : " << minLineLengthThreshold <<std::endl;
375 
376  if(!min_polygon_area_threshold_node)
377  std::cout <<"lod : min polygon area threshold : " << minPolygonAreaThreshold <<" (default)" <<std::endl;
378  else
379  std::cout <<"lod : min polygon area threshold : " << minPolygonAreaThreshold <<std::endl;
380 }
381 
382 #elif !defined(VISP_BUILD_SHARED_LIBS)
383 // Work arround to avoid warning: libvisp_mbt.a(vpMbXmlParser.cpp.o) has no symbols
384 void dummy_vpMbXmlParser() {};
385 #endif
386 
vpCameraParameters cam
Camera parameters.
Definition: vpMbXmlParser.h:67
double get_u0() const
void setMainTag(const std::string &tag)
Definition: vpXmlParser.h:276
void read_lod(xmlDocPtr doc, xmlNodePtr node)
double nearClipping
Near clipping distance.
Definition: vpMbXmlParser.h:75
double angleAppear
Angle to determine if a face appeared.
Definition: vpMbXmlParser.h:69
error that can be emited by ViSP classes.
Definition: vpException.h:73
double minLineLengthThreshold
Minimum line length to track a segment when LOD is enabled.
Definition: vpMbXmlParser.h:85
virtual void readMainClass(xmlDocPtr doc, xmlNodePtr node)
double xmlReadDoubleChild(xmlDocPtr doc, xmlNodePtr node)
double get_py() const
bool hasFarClipping
Is far clipping distance specified?
Definition: vpMbXmlParser.h:77
virtual ~vpMbXmlParser()
double angleDisappear
Angle to determine if a face disappeared.
Definition: vpMbXmlParser.h:71
void initPersProjWithoutDistortion(const double px, const double py, const double u0, const double v0)
double farClipping
Near clipping distance.
Definition: vpMbXmlParser.h:79
void read_camera(xmlDocPtr doc, xmlNodePtr node)
double get_v0() const
void writeMainClass(xmlNodePtr node)
int xmlReadIntChild(xmlDocPtr doc, xmlNodePtr node)
void read_face(xmlDocPtr doc, xmlNodePtr node)
double get_px() const
bool hasNearClipping
Is near clipping distance specified?
Definition: vpMbXmlParser.h:73
void parse(const char *filename)
bool fovClipping
Fov Clipping.
Definition: vpMbXmlParser.h:81
std::map< std::string, int > nodeMap
Definition: vpXmlParser.h:193
double minPolygonAreaThreshold
Minimum polygon area to track a face when LOD is enabled.
Definition: vpMbXmlParser.h:87
bool useLod
If true, the LOD is enabled, otherwise it is not.
Definition: vpMbXmlParser.h:83
void parse(const std::string &filename)