Visual Servoing Platform  version 3.6.1 under development (2024-04-26)
testXmlParserCamera.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2023 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 https://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  * Test vpXmlParserCamera parse / save.
33  *
34 *****************************************************************************/
35 
42 #include <visp3/core/vpIoTools.h>
43 #include <visp3/core/vpXmlParserCamera.h>
44 
45 int main()
46 {
47 #if defined(VISP_HAVE_PUGIXML)
48 
49 #if defined(_WIN32)
50  std::string tmp_dir = "C:/temp/";
51 #else
52  std::string tmp_dir = "/tmp/";
53 #endif
54 
55  // Get the user login name
56  std::string username;
57  vpIoTools::getUserName(username);
58 
59  tmp_dir += username + "/test_xml_parser_camera/";
60  vpIoTools::remove(tmp_dir);
61  std::cout << "Create: " << tmp_dir << std::endl;
62  vpIoTools::makeDirectory(tmp_dir);
63 
64  {
65  std::cout << "-- Test to save/load one single camera without distortion in a single file" << std::endl;
67  cam.initPersProjWithoutDistortion(278.4691184118, 273.9196496040, 162.0747539621, 113.1741829586);
68  std::string filename = tmp_dir + "test_write_cam_without_distortion.xml";
69  {
71  std::cout << "Write to: " << filename << std::endl;
72  if (xml.save(cam, filename, "Camera", 320, 240) != vpXmlParserCamera::SEQUENCE_OK) {
73  std::cerr << "Cannot save XML file: " << filename << std::endl;
74  return EXIT_FAILURE;
75  }
76  }
77 
78  {
79  vpCameraParameters cam_read;
81  xml.parse(cam_read, filename, "Camera", vpCameraParameters::perspectiveProjWithoutDistortion, 320, 240, false);
82  std::cout << "Cam write:\n" << cam << std::endl;
83  std::cout << "Cam read:\n" << cam_read << std::endl;
84  if (cam != cam_read) {
85  std::cerr << "Issue when parsing XML file: " << filename << std::endl;
86  return EXIT_FAILURE;
87  }
88  }
89 
90  {
91  // Without specifying image size
92  vpCameraParameters cam_read;
94  xml.parse(cam_read, filename, "Camera", vpCameraParameters::perspectiveProjWithoutDistortion, 0, 0, false);
95  std::cout << "Cam write:\n" << cam << std::endl;
96  std::cout << "Cam read:\n" << cam_read << std::endl;
97  if (cam != cam_read) {
98  std::cerr << "Issue when parsing XML file: " << filename << std::endl;
99  return EXIT_FAILURE;
100  }
101  }
102  }
103 
104  {
105  std::cout << "-- Test to save/load one single camera with distortion in a single file" << std::endl;
106  std::string filename = tmp_dir + "test_write_cam_with_distortion.xml";
107  vpCameraParameters cam;
108  cam.initPersProjWithDistortion(200, 200, 160, 120, 0.02, -0.02);
109  {
110  vpXmlParserCamera xml;
111  std::cout << "Write to: " << filename << std::endl;
112  if (xml.save(cam, filename, "Camera", 320, 240) != vpXmlParserCamera::SEQUENCE_OK) {
113  std::cerr << "Cannot save XML file: " << filename << std::endl;
114  return EXIT_FAILURE;
115  }
116  }
117 
118  {
119  vpCameraParameters cam_read;
120  vpXmlParserCamera xml;
121  xml.parse(cam_read, filename, "Camera", vpCameraParameters::perspectiveProjWithDistortion, 320, 240, false);
122  std::cout << "Cam write:\n" << cam << std::endl;
123  std::cout << "Cam read:\n" << cam_read << std::endl;
124  if (cam != cam_read) {
125  std::cerr << "Issue when parsing XML file: " << filename << std::endl;
126  return EXIT_FAILURE;
127  }
128  }
129 
130  {
131  // Without specifying image size
132  vpCameraParameters cam_read;
133  vpXmlParserCamera xml;
134  xml.parse(cam_read, filename, "Camera", vpCameraParameters::perspectiveProjWithDistortion, 0, 0, false);
135  std::cout << "Cam write:\n" << cam << std::endl;
136  std::cout << "Cam read:\n" << cam_read << std::endl;
137  if (cam != cam_read) {
138  std::cerr << "Issue when parsing XML file: " << filename << std::endl;
139  return EXIT_FAILURE;
140  }
141  }
142  }
143 
144  {
145  std::cout << "-- Test to save/load multiple cameras with and without distortion in a single file" << std::endl;
146  std::string filename = tmp_dir + "test_write_cam_multiple.xml";
147  vpCameraParameters cam1_w_d;
148  cam1_w_d.initPersProjWithDistortion(200, 200, 160, 120, 0.02, -0.02);
149  {
150  vpXmlParserCamera xml;
151  std::cout << "Write to: " << filename << " camera:\n" << cam1_w_d << std::endl;
152  if (xml.save(cam1_w_d, filename, "Camera 1", 320, 240) != vpXmlParserCamera::SEQUENCE_OK) {
153  std::cerr << "Cannot save XML file: " << filename << std::endl;
154  return EXIT_FAILURE;
155  }
156  }
157 
158  vpCameraParameters cam1_wo_d;
159  cam1_wo_d.initPersProjWithoutDistortion(200, 200, 160, 120);
160  {
161  vpXmlParserCamera xml;
162  std::cout << "Write to: " << filename << " camera:\n" << cam1_wo_d << std::endl;
163  if (xml.save(cam1_wo_d, filename, "Camera 1", 320, 240) != vpXmlParserCamera::SEQUENCE_OK) {
164  std::cerr << "Cannot save XML file: " << filename << std::endl;
165  return EXIT_FAILURE;
166  }
167  }
168  vpCameraParameters cam2_w_d;
169  cam2_w_d.initPersProjWithDistortion(400, 400, 320, 240, 0.02, -0.02);
170  {
171  vpXmlParserCamera xml;
172  std::cout << "Write to: " << filename << " camera:\n" << cam2_w_d << std::endl;
173  if (xml.save(cam2_w_d, filename, "Camera 2", 640, 480) != vpXmlParserCamera::SEQUENCE_OK) {
174  std::cerr << "Cannot save XML file: " << filename << std::endl;
175  return EXIT_FAILURE;
176  }
177  }
178 
179  vpCameraParameters cam2_wo_d;
180  cam2_wo_d.initPersProjWithoutDistortion(400, 400, 320, 240);
181  {
182  vpXmlParserCamera xml;
183  std::cout << "Write to: " << filename << " camera:\n" << cam2_wo_d << std::endl;
184  if (xml.save(cam2_wo_d, filename, "Camera 2", 640, 480) != vpXmlParserCamera::SEQUENCE_OK) {
185  std::cerr << "Cannot save XML file: " << filename << std::endl;
186  return EXIT_FAILURE;
187  }
188  }
189 
190  {
191  vpCameraParameters cam_read;
192  vpXmlParserCamera xml;
193  xml.parse(cam_read, filename, "Camera 1", vpCameraParameters::perspectiveProjWithDistortion, 320, 240, false);
194  std::cout << "Cam write:\n" << cam1_w_d << std::endl;
195  std::cout << "Cam read:\n" << cam_read << std::endl;
196  if (cam1_w_d != cam_read) {
197  std::cerr << "Issue when parsing XML file: " << filename << std::endl;
198  return EXIT_FAILURE;
199  }
200  }
201  {
202  vpCameraParameters cam_read;
203  vpXmlParserCamera xml;
204  xml.parse(cam_read, filename, "Camera 1", vpCameraParameters::perspectiveProjWithoutDistortion, 320, 240, false);
205  std::cout << "Cam write:\n" << cam1_wo_d << std::endl;
206  std::cout << "Cam read:\n" << cam_read << std::endl;
207  if (cam1_wo_d != cam_read) {
208  std::cerr << "Issue when parsing XML file: " << filename << std::endl;
209  return EXIT_FAILURE;
210  }
211  }
212  {
213  vpCameraParameters cam_read;
214  vpXmlParserCamera xml;
215  xml.parse(cam_read, filename, "Camera 2", vpCameraParameters::perspectiveProjWithDistortion, 640, 480, false);
216  std::cout << "Cam write:\n" << cam2_w_d << std::endl;
217  std::cout << "Cam read:\n" << cam_read << std::endl;
218  if (cam2_w_d != cam_read) {
219  std::cerr << "Issue when parsing XML file: " << filename << std::endl;
220  return EXIT_FAILURE;
221  }
222  }
223  {
224  vpCameraParameters cam_read;
225  vpXmlParserCamera xml;
226  xml.parse(cam_read, filename, "Camera 2", vpCameraParameters::perspectiveProjWithoutDistortion, 640, 480, false);
227  std::cout << "Cam write:\n" << cam2_wo_d << std::endl;
228  std::cout << "Cam read:\n" << cam_read << std::endl;
229  if (cam2_wo_d != cam_read) {
230  std::cerr << "Issue when parsing XML file: " << filename << std::endl;
231  return EXIT_FAILURE;
232  }
233  }
234  }
235 
236  {
237  std::cout << "-- Test to save/load one single camera with Kannala Brandt distortion in a single file" << std::endl;
238  vpCameraParameters cam;
239  std::vector<double> distortion_coeffs;
240  distortion_coeffs.push_back(-0.00297341705299914);
241  distortion_coeffs.push_back(0.0352853797376156);
242  distortion_coeffs.push_back(-0.032205019146204);
243  distortion_coeffs.push_back(0.004446716979146);
244  distortion_coeffs.push_back(0);
245  cam.initProjWithKannalaBrandtDistortion(285.523895263672, 286.6708984375, 420.874114990234, 381.085388183594,
246  distortion_coeffs);
247  std::string filename = tmp_dir + "test_write_cam_with_KannalaBrandt_distortion.xml";
248  {
249  vpXmlParserCamera xml;
250  std::cout << "Write to: " << filename << std::endl;
251  if (xml.save(cam, filename, "Camera", 800, 848) != vpXmlParserCamera::SEQUENCE_OK) {
252  std::cerr << "Cannot save XML file: " << filename << std::endl;
253  return EXIT_FAILURE;
254  }
255  }
256 
257  vpCameraParameters cam_read;
258  {
259  vpXmlParserCamera xml;
260  xml.parse(cam_read, filename, "Camera", vpCameraParameters::ProjWithKannalaBrandtDistortion, 800, 848, false);
261  std::cout << "Cam write:\n" << cam << std::endl;
262  std::cout << "Cam read:\n" << cam_read << std::endl;
263  if (cam != cam_read) {
264  std::cerr << "Issue when parsing XML file: " << filename << std::endl;
265  return EXIT_FAILURE;
266  }
267  }
268  }
269 
270  {
271  std::cout << "-- Test to save/load one single camera with Kannala Brandt distortion in a single file wo name" << std::endl;
272  vpCameraParameters cam;
273  std::vector<double> distortion_coeffs;
274  distortion_coeffs.push_back(-0.00297341705299914);
275  distortion_coeffs.push_back(0.0352853797376156);
276  distortion_coeffs.push_back(-0.032205019146204);
277  distortion_coeffs.push_back(0.004446716979146);
278  distortion_coeffs.push_back(0);
279  cam.initProjWithKannalaBrandtDistortion(285.523895263672, 286.6708984375, 420.874114990234, 381.085388183594,
280  distortion_coeffs);
281  std::string filename = tmp_dir + "test_write_cam_with_KannalaBrandt_distortion_wo_name.xml";
282  {
283  vpXmlParserCamera xml;
284  std::cout << "Write to: " << filename << std::endl;
285  if (xml.save(cam, filename, "Camera", 800, 848) != vpXmlParserCamera::SEQUENCE_OK) {
286  std::cerr << "Cannot save XML file: " << filename << std::endl;
287  return EXIT_FAILURE;
288  }
289  }
290 
291  vpCameraParameters cam_read;
292  {
293  vpXmlParserCamera xml;
294  xml.parse(cam_read, filename, "", vpCameraParameters::ProjWithKannalaBrandtDistortion, 800, 848, false);
295  std::cout << "Cam write:\n" << cam << std::endl;
296  std::cout << "Cam read:\n" << cam_read << std::endl;
297  if (cam != cam_read) {
298  std::cerr << "Issue when parsing XML file: " << filename << std::endl;
299  return EXIT_FAILURE;
300  }
301  }
302  }
303 
304  {
305  std::cout << "-- Test to save 2 cameras and parse them wo name thanks they differ in distortion" << std::endl;
306  vpCameraParameters cam1;
307 
308  std::string filename = tmp_dir + "test_write_2_cam_differ_in_distortion.xml";
309 
310  {
311  vpXmlParserCamera xml;
312  std::cout << "Write to: " << filename << std::endl;
313  std::cout << "Cam write:\n" << cam1 << std::endl;
314  if (xml.save(cam1, filename, "Camera 1", 320, 240, "", false) != vpXmlParserCamera::SEQUENCE_OK) {
315  std::cerr << "Cannot save XML file: " << filename << std::endl;
316  return EXIT_FAILURE;
317  }
318  }
319 
320  vpCameraParameters cam2;
321  std::vector<double> distortion_coeffs;
322  distortion_coeffs.push_back(-0.00297341705299914);
323  distortion_coeffs.push_back(0.0352853797376156);
324  distortion_coeffs.push_back(-0.032205019146204);
325  distortion_coeffs.push_back(0.004446716979146);
326  distortion_coeffs.push_back(0);
327  cam2.initProjWithKannalaBrandtDistortion(285.523895263672, 286.6708984375, 420.874114990234, 381.085388183594,
328  distortion_coeffs);
329  {
330  vpXmlParserCamera xml;
331  std::cout << "Write to: " << filename << std::endl;
332  std::cout << "Cam write:\n" << cam2 << std::endl;
333  if (xml.save(cam2, filename, "Camera 2", 800, 848, "", false) != vpXmlParserCamera::SEQUENCE_OK) {
334  std::cerr << "Cannot save XML file: " << filename << std::endl;
335  return EXIT_FAILURE;
336  }
337  }
338 
339  {
340  std::cout << "Attempt to read camera with perspective projection without distortion and without name" << std::endl;
341  vpCameraParameters cam_read;
342  vpXmlParserCamera xml;
343  xml.parse(cam_read, filename, "", vpCameraParameters::perspectiveProjWithoutDistortion, 320, 240, false);
344 
345  std::cout << "Cam read:\n" << cam_read << std::endl;
346  if (cam1 != cam_read) {
347  std::cerr << "Issue when parsing XML file: " << filename << std::endl;
348  return EXIT_FAILURE;
349  }
350  }
351 
352  {
353  std::cout << "Attempt to read camera with Kannala Brandt distortion and without name" << std::endl;
354  vpCameraParameters cam_read;
355  vpXmlParserCamera xml;
356  xml.parse(cam_read, filename, "", vpCameraParameters::ProjWithKannalaBrandtDistortion, 800, 848, false);
357 
358  std::cout << "Cam read:\n" << cam_read << std::endl;
359  if (cam2 != cam_read) {
360  std::cerr << "Issue when parsing XML file: " << filename << std::endl;
361  return EXIT_FAILURE;
362  }
363  }
364  }
365 
366  vpIoTools::remove(tmp_dir);
367 
368  std::cout << "Test succeed" << std::endl;
369 #endif
370 
371  return EXIT_SUCCESS;
372 }
Generic class defining intrinsic camera parameters.
void initPersProjWithoutDistortion(double px, double py, double u0, double v0)
@ perspectiveProjWithDistortion
Perspective projection with distortion model.
@ ProjWithKannalaBrandtDistortion
Projection with Kannala-Brandt distortion model.
@ perspectiveProjWithoutDistortion
Perspective projection without distortion model.
void initPersProjWithDistortion(double px, double py, double u0, double v0, double kud, double kdu)
void initProjWithKannalaBrandtDistortion(double px, double py, double u0, double v0, const std::vector< double > &distortion_coefficients)
static std::string getUserName()
Definition: vpIoTools.cpp:725
static void makeDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:981
static bool remove(const std::string &filename)
Definition: vpIoTools.cpp:1348
XML parser to load and save intrinsic camera parameters.
int save(const vpCameraParameters &cam, const std::string &filename, const std::string &camera_name, unsigned int image_width=0, unsigned int image_height=0, const std::string &additionalInfo="", bool verbose=true)
int parse(vpCameraParameters &cam, const std::string &filename, const std::string &camera_name, const vpCameraParameters::vpCameraParametersProjType &projModel, unsigned int image_width=0, unsigned int image_height=0, bool verbose=true)