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