Visual Servoing Platform  version 3.2.0 under development (2019-01-22)
vpMbXmlParser.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 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 
98 void vpMbXmlParser::writeMainClass(xmlNodePtr /*node*/)
99 {
100  throw vpException(vpException::notImplementedError, "Not yet implemented.");
101 }
102 
110 void vpMbXmlParser::readMainClass(xmlDocPtr doc, xmlNodePtr node)
111 {
112  bool camera_node = false;
113  bool face_node = false;
114  bool lod_node = false;
115 
116  for (xmlNodePtr dataNode = node->xmlChildrenNode; dataNode != NULL; dataNode = dataNode->next) {
117  if (dataNode->type == XML_ELEMENT_NODE) {
118  std::map<std::string, int>::iterator iter_data = this->nodeMap.find((char *)dataNode->name);
119  if (iter_data != nodeMap.end()) {
120  switch (iter_data->second) {
121  case camera: {
122  this->read_camera(doc, dataNode);
123  camera_node = true;
124  } break;
125  case face: {
126  this->read_face(doc, dataNode);
127  face_node = true;
128  } break;
129  case lod: {
130  this->read_lod(doc, dataNode);
131  lod_node = true;
132  } break;
133  default: {
134  // vpTRACE("unknown tag in read_sample : %d, %s",
135  // iter_data->second, (iter_data->first).c_str());
136  } break;
137  }
138  }
139  }
140  }
141 
142  if (!camera_node) {
143  std::cout << "camera : u0 : " << this->cam.get_u0() << " (default)" << std::endl;
144  std::cout << "camera : v0 : " << this->cam.get_v0() << " (default)" << std::endl;
145  std::cout << "camera : px : " << this->cam.get_px() << " (default)" << std::endl;
146  std::cout << "camera : py : " << this->cam.get_py() << " (default)" << std::endl;
147  }
148 
149  if (!face_node) {
150  std::cout << "face : Angle Appear : " << angleAppear << " (default)" << std::endl;
151  std::cout << "face : Angle Disappear : " << angleDisappear << " (default)" << std::endl;
152  }
153 
154  if (!lod_node) {
155  std::cout << "lod : use lod : " << useLod << " (default)" << std::endl;
156  std::cout << "lod : min line length threshold : " << minLineLengthThreshold << " (default)" << std::endl;
157  std::cout << "lod : min polygon area threshold : " << minPolygonAreaThreshold << " (default)" << std::endl;
158  }
159 }
160 
169 void vpMbXmlParser::read_camera(xmlDocPtr doc, xmlNodePtr node)
170 {
171  bool u0_node = false;
172  bool v0_node = false;
173  bool px_node = false;
174  bool py_node = false;
175 
176  // current data values.
177  double d_u0 = this->cam.get_u0();
178  double d_v0 = this->cam.get_v0();
179  double d_px = this->cam.get_px();
180  double d_py = this->cam.get_py();
181 
182  for (xmlNodePtr dataNode = node->xmlChildrenNode; dataNode != NULL; dataNode = dataNode->next) {
183  if (dataNode->type == XML_ELEMENT_NODE) {
184  std::map<std::string, int>::iterator iter_data = this->nodeMap.find((char *)dataNode->name);
185  if (iter_data != nodeMap.end()) {
186  switch (iter_data->second) {
187  case u0: {
188  d_u0 = xmlReadDoubleChild(doc, dataNode);
189  u0_node = true;
190  } break;
191  case v0: {
192  d_v0 = xmlReadDoubleChild(doc, dataNode);
193  v0_node = true;
194  } break;
195  case px: {
196  d_px = xmlReadDoubleChild(doc, dataNode);
197  px_node = true;
198  } break;
199  case py: {
200  d_py = xmlReadDoubleChild(doc, dataNode);
201  py_node = true;
202  } break;
203  default: {
204  // vpTRACE("unknown tag in read_camera : %d, %s",
205  // iter_data->second, (iter_data->first).c_str());
206  } break;
207  }
208  }
209  }
210  }
211 
212  this->cam.initPersProjWithoutDistortion(d_px, d_py, d_u0, d_v0);
213 
214  if (!u0_node)
215  std::cout << "camera : u0 : " << this->cam.get_u0() << " (default)" << std::endl;
216  else
217  std::cout << "camera : u0 : " << this->cam.get_u0() << std::endl;
218 
219  if (!v0_node)
220  std::cout << "camera : v0 : " << this->cam.get_v0() << " (default)" << std::endl;
221  else
222  std::cout << "camera : v0 : " << this->cam.get_v0() << std::endl;
223 
224  if (!px_node)
225  std::cout << "camera : px : " << this->cam.get_px() << " (default)" << std::endl;
226  else
227  std::cout << "camera : px : " << this->cam.get_px() << std::endl;
228 
229  if (!py_node)
230  std::cout << "camera : py : " << this->cam.get_py() << " (default)" << std::endl;
231  else
232  std::cout << "camera : py : " << this->cam.get_py() << std::endl;
233 }
234 
243 void vpMbXmlParser::read_face(xmlDocPtr doc, xmlNodePtr node)
244 {
245  bool angle_appear_node = false;
246  bool angle_disappear_node = false;
247  bool near_clipping_node = false;
248  bool far_clipping_node = false;
249  bool fov_clipping_node = false;
250 
251  for (xmlNodePtr dataNode = node->xmlChildrenNode; dataNode != NULL; dataNode = dataNode->next) {
252  if (dataNode->type == XML_ELEMENT_NODE) {
253  std::map<std::string, int>::iterator iter_data = this->nodeMap.find((char *)dataNode->name);
254  if (iter_data != nodeMap.end()) {
255  switch (iter_data->second) {
256  case angle_appear: {
257  angleAppear = xmlReadDoubleChild(doc, dataNode);
258  angle_appear_node = true;
259  } break;
260  case angle_disappear: {
261  angleDisappear = xmlReadDoubleChild(doc, dataNode);
262  angle_disappear_node = true;
263  } break;
264  case near_clipping: {
265  nearClipping = xmlReadDoubleChild(doc, dataNode);
266  near_clipping_node = true;
267  hasNearClipping = true;
268  } break;
269  case far_clipping: {
270  farClipping = xmlReadDoubleChild(doc, dataNode);
271  far_clipping_node = true;
272  hasFarClipping = true;
273  } break;
274  case fov_clipping: {
275  if (xmlReadIntChild(doc, dataNode))
276  fovClipping = true;
277  else
278  fovClipping = false;
279  fov_clipping_node = true;
280  } break;
281  default: {
282  // vpTRACE("unknown tag in read_camera : %d, %s",
283  // iter_data->second, (iter_data->first).c_str());
284  } break;
285  }
286  }
287  }
288  }
289 
290  if (!angle_appear_node)
291  std::cout << "face : Angle Appear : " << angleAppear << " (default)" << std::endl;
292  else
293  std::cout << "face : Angle Appear : " << angleAppear << std::endl;
294 
295  if (!angle_disappear_node)
296  std::cout << "face : Angle Disappear : " << angleDisappear << " (default)" << std::endl;
297  else
298  std::cout << "face : Angle Disappear : " << angleDisappear << std::endl;
299 
300  if (near_clipping_node)
301  std::cout << "face : Near Clipping : " << nearClipping << std::endl;
302 
303  if (far_clipping_node)
304  std::cout << "face : Far Clipping : " << farClipping << std::endl;
305 
306  if (fov_clipping_node) {
307  if (fovClipping)
308  std::cout << "face : Fov Clipping : True" << std::endl;
309  else
310  std::cout << "face : Fov Clipping : False" << std::endl;
311  }
312 }
313 
314 void vpMbXmlParser::read_lod(xmlDocPtr doc, xmlNodePtr node)
315 {
316  bool use_lod_node = false;
317  bool min_line_length_threshold_node = false;
318  bool min_polygon_area_threshold_node = false;
319 
320  for (xmlNodePtr dataNode = node->xmlChildrenNode; dataNode != NULL; dataNode = dataNode->next) {
321  if (dataNode->type == XML_ELEMENT_NODE) {
322  std::map<std::string, int>::iterator iter_data = this->nodeMap.find((char *)dataNode->name);
323  if (iter_data != nodeMap.end()) {
324  switch (iter_data->second) {
325  case use_lod:
326  useLod = (xmlReadIntChild(doc, dataNode) != 0);
327  use_lod_node = true;
328  break;
331  min_line_length_threshold_node = true;
332  break;
335  min_polygon_area_threshold_node = true;
336  break;
337  default: {
338  // vpTRACE("unknown tag in read_contrast : %d, %s",
339  // iter_data->second, (iter_data->first).c_str());
340  } break;
341  }
342  }
343  }
344  }
345 
346  if (!use_lod_node)
347  std::cout << "lod : use lod : " << useLod << " (default)" << std::endl;
348  else
349  std::cout << "lod : use lod : " << useLod << std::endl;
350 
351  if (!min_line_length_threshold_node)
352  std::cout << "lod : min line length threshold : " << minLineLengthThreshold << " (default)" << std::endl;
353  else
354  std::cout << "lod : min line length threshold : " << minLineLengthThreshold << std::endl;
355 
356  if (!min_polygon_area_threshold_node)
357  std::cout << "lod : min polygon area threshold : " << minPolygonAreaThreshold << " (default)" << std::endl;
358  else
359  std::cout << "lod : min polygon area threshold : " << minPolygonAreaThreshold << std::endl;
360 }
361 
362 #elif !defined(VISP_BUILD_SHARED_LIBS)
363 // Work arround to avoid warning: libvisp_mbt.a(vpMbXmlParser.cpp.o) has no
364 // symbols
365 void dummy_vpMbXmlParser(){};
366 #endif
vpCameraParameters cam
Camera parameters.
Definition: vpMbXmlParser.h:69
double get_u0() const
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)
double get_py() const
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)
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:75
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