ViSP  2.9.0
vpXmlParserCamera.cpp
1 /****************************************************************************
2  *
3  * $Id: vpXmlParserCamera.cpp 4649 2014-02-07 14:57:11Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2014 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  * XML parser to load and save camera intrinsic parameters.
36  *
37  * Authors:
38  * Anthony Saunier
39  *
40  *****************************************************************************/
41 
42 
49 #include <visp/vpXmlParserCamera.h>
50 #ifdef VISP_HAVE_XML2
51 
52 #include <stdlib.h>
53 #include <string.h>
54 
55 #include <visp/vpDebug.h>
56 /* -------------------------------------------------------------------------- */
57 /* --- LABEL XML ------------------------------------------------------------ */
58 /* -------------------------------------------------------------------------- */
59 
60 #define LABEL_XML_ROOT "root"
61 #define LABEL_XML_CAMERA "camera"
62 #define LABEL_XML_CAMERA_NAME "name"
63 #define LABEL_XML_WIDTH "image_width"
64 #define LABEL_XML_HEIGHT "image_height"
65 #define LABEL_XML_SUBSAMPLING_WIDTH "subsampling_width"
66 #define LABEL_XML_SUBSAMPLING_HEIGHT "subsampling_height"
67 #define LABEL_XML_FULL_WIDTH "full_width"
68 #define LABEL_XML_FULL_HEIGHT "full_height"
69 #define LABEL_XML_MODEL "model"
70 #define LABEL_XML_MODEL_TYPE "type"
71 #define LABEL_XML_U0 "u0"
72 #define LABEL_XML_V0 "v0"
73 #define LABEL_XML_PX "px"
74 #define LABEL_XML_PY "py"
75 #define LABEL_XML_KUD "kud"
76 #define LABEL_XML_KDU "kdu"
77 
78 #define LABEL_XML_MODEL_WITHOUT_DISTORTION "perspectiveProjWithoutDistortion"
79 #define LABEL_XML_MODEL_WITH_DISTORTION "perspectiveProjWithDistortion"
80 
84  : vpXmlParser(),
85  camera(), camera_name(), image_width(0), image_height(0),
86  subsampling_width(0), subsampling_height(0), full_width(0), full_height(0)
87 {
88 }
94  : vpXmlParser(twinParser),
95  camera(), camera_name(), image_width(0), image_height(0),
96  subsampling_width(0), subsampling_height(0), full_width(0), full_height(0)
97 
98 {
99  this->camera = twinParser.camera;
100  this->camera_name = twinParser.camera_name;
101  this->image_width = twinParser.image_width;
102  this->image_height = twinParser.image_height;
103  this->subsampling_width = twinParser.subsampling_width;
104  this->subsampling_height = twinParser.subsampling_height;
105  this->full_width = twinParser.full_width;
106  this->full_height = twinParser.full_height;
107 }
108 
116  this->camera = twinParser.camera;
117  this->camera_name = twinParser.camera_name;
118  this->image_width = twinParser.image_width;
119  this->image_height = twinParser.image_height;
120  this->subsampling_width = twinParser.subsampling_width;
121  this->subsampling_height = twinParser.subsampling_height;
122  this->full_width = twinParser.full_width;
123  this->full_height = twinParser.full_height;
124  return *this ;
125 }
126 
141 int
142 vpXmlParserCamera::parse(vpCameraParameters &cam, const char * filename,
143  const std::string& cam_name,
145  const unsigned int im_width,
146  const unsigned int im_height)
147 {
148  xmlDocPtr doc;
149  xmlNodePtr node;
150 
151  doc = xmlParseFile(filename);
152  if (doc == NULL)
153  {
154  return SEQUENCE_ERROR;
155  }
156 
157  node = xmlDocGetRootElement(doc);
158  if (node == NULL)
159  {
160  xmlFreeDoc(doc);
161  return SEQUENCE_ERROR;
162  }
163 
164  int ret = this ->read (doc, node, cam_name, projModel, im_width, im_height);
165 
166  cam = camera ;
167 
168  xmlFreeDoc(doc);
169 
170  return ret;
171 }
172 
186 int
187 vpXmlParserCamera::save(const vpCameraParameters &cam, const char * filename,
188  const std::string& cam_name,
189  const unsigned int im_width,
190  const unsigned int im_height)
191 {
192  xmlDocPtr doc;
193  xmlNodePtr node;
194  xmlNodePtr nodeCamera = NULL;
195 
196  doc = xmlReadFile(filename,NULL,XML_PARSE_NOWARNING + XML_PARSE_NOERROR
197  + XML_PARSE_NOBLANKS);
198  if (doc == NULL){
199  doc = xmlNewDoc ((xmlChar*)"1.0");
200  node = xmlNewNode(NULL,(xmlChar*)LABEL_XML_ROOT);
201  xmlDocSetRootElement(doc,node);
202  xmlNodePtr node_tmp = xmlNewComment((xmlChar*)
203  "This file stores intrinsic camera parameters used\n"
204  " in the vpCameraParameters Class of ViSP available\n"
205  " at http://www.irisa.fr/lagadic/visp/visp.html .\n"
206  " It can be read with the parse method of\n"
207  " the vpXmlParserCamera class.");
208  xmlAddChild(node,node_tmp);
209  }
210 
211  node = xmlDocGetRootElement(doc);
212  if (node == NULL)
213  {
214  xmlFreeDoc(doc);
215  return SEQUENCE_ERROR;
216  }
217 
218  this->camera = cam;
219 
220  int nbCamera = count(doc, node, cam_name,cam.get_projModel(),
221  im_width, im_height);
222  if( nbCamera > 0){
223 // vpCERROR << nbCamera
224 // << " set(s) of camera parameters is(are) already "<< std::endl
225 // << "available in the file with your specifications : "<< std::endl
226 // << "precise the grabber parameters or delete manually"<< std::endl
227 // << "the previous one."<<std::endl;
228  xmlFreeDoc(doc);
229  return SEQUENCE_ERROR;
230  }
231 
232  nodeCamera = find_camera(doc, node, camera_name, image_width, image_height);
233  if(nodeCamera == NULL){
234  write(node, camera_name, image_width, image_height);
235  }
236  else{
237  write_camera(nodeCamera);
238  }
239 
240  xmlSaveFormatFile(filename,doc,1);
241  xmlFreeDoc(doc);
242 
243  return SEQUENCE_OK;
244 }
245 
246 
247 
266 int
267 vpXmlParserCamera::read (xmlDocPtr doc, xmlNodePtr node,
268  const std::string& cam_name,
270  const unsigned int im_width,
271  const unsigned int im_height,
272  const unsigned int subsampl_width,
273  const unsigned int subsampl_height)
274 {
275  // char * val_char;
276  vpXmlCodeType prop;
277 
279  int nbCamera = 0;
280 
281  for (node = node->xmlChildrenNode; node != NULL; node = node->next)
282  {
283  if (node->type != XML_ELEMENT_NODE) continue;
284  if (SEQUENCE_OK != str2xmlcode ((char*)(node ->name), prop))
285  {
286  prop = CODE_XML_OTHER;
287  back = SEQUENCE_ERROR;
288  }
289  /*
290  switch (prop)
291  {
292  case CODE_XML_CAMERA:
293  if (SEQUENCE_OK == this->read_camera (doc, node, camera_name, projModel,
294  image_width, image_height, subsampling_width, subsampling_height)){
295  nbCamera++;
296  }
297  break;
298  default:
299  back = SEQUENCE_ERROR;
300  break;
301  }
302  */
303  if (prop == CODE_XML_CAMERA){
304  if (SEQUENCE_OK == this->read_camera (doc, node, cam_name, projModel,
305  im_width, im_height, subsampl_width, subsampl_height))
306  nbCamera++;
307  }
308  else back = SEQUENCE_ERROR;
309  }
310 
311  if (nbCamera == 0){
312  back = SEQUENCE_ERROR;
313  vpCERROR << "No camera parameters is available" << std::endl
314  << "with your specifications" << std::endl;
315  }
316  else if(nbCamera > 1){
317  back = SEQUENCE_ERROR;
318  vpCERROR << nbCamera << " sets of camera parameters are available" << std::endl
319  << "with your specifications : " << std::endl
320  << "precise your choice..." << std::endl;
321  }
322 
323  return back;
324 }
344 int
345 vpXmlParserCamera::count (xmlDocPtr doc, xmlNodePtr node,
346  const std::string& cam_name,
348  const unsigned int im_width,
349  const unsigned int im_height,
350  const unsigned int subsampl_width,
351  const unsigned int subsampl_height)
352 {
353  // char * val_char;
354  vpXmlCodeType prop;
355  int nbCamera = 0;
356 
357  for (node = node->xmlChildrenNode; node != NULL; node = node->next)
358  {
359  if (node->type != XML_ELEMENT_NODE) continue;
360  if (SEQUENCE_OK != str2xmlcode ((char*)(node ->name), prop))
361  {
362  prop = CODE_XML_OTHER;
363  }
364  /*
365  switch (prop)
366  {
367  case CODE_XML_CAMERA:
368  if (SEQUENCE_OK == this->read_camera (doc, node, camera_name, projModel,
369  image_width, image_height,
370  subsampling_width, subsampling_height)){
371  nbCamera++;
372  }
373  break;
374  default:
375  break;
376  }
377  */
378  if (prop== CODE_XML_CAMERA) {
379  if (SEQUENCE_OK == this->read_camera (doc, node, cam_name, projModel,
380  im_width, im_height,
381  subsampl_width, subsampl_height))
382  nbCamera++;
383  }
384  }
385 
386  return nbCamera;
387 }
407 xmlNodePtr
408 vpXmlParserCamera::find_camera (xmlDocPtr doc, xmlNodePtr node,
409  const std::string& cam_name,
410  const unsigned int im_width,
411  const unsigned int im_height,
412  const unsigned int subsampl_width,
413  const unsigned int subsampl_height)
414 {
415  // char * val_char;
416  vpXmlCodeType prop;
417 
418  for (node = node->xmlChildrenNode; node != NULL; node = node->next)
419  {
420  if (node->type != XML_ELEMENT_NODE) continue;
421  if (SEQUENCE_OK != str2xmlcode ((char*)(node ->name), prop))
422  {
423  prop = CODE_XML_OTHER;
424  }
425  /*
426  switch (prop)
427  {
428  case CODE_XML_CAMERA:
429  if (SEQUENCE_OK == this->read_camera_header(doc, node, camera_name,
430  image_width, image_height,
431  subsampling_width, subsampling_height)){
432  return node;
433  }
434  break;
435  default:
436  break;
437  }
438  */
439  if(prop == CODE_XML_CAMERA){
440  if (SEQUENCE_OK == this->read_camera_header(doc, node, cam_name,
441  im_width, im_height,
442  subsampl_width, subsampl_height))
443  return node;
444  }
445  }
446  return NULL;
447 }
448 
468 int
469 vpXmlParserCamera::read_camera (xmlDocPtr doc, xmlNodePtr node,
470  const std::string& cam_name,
472  const unsigned int im_width,
473  const unsigned int im_height,
474  const unsigned int subsampl_width,
475  const unsigned int subsampl_height)
476 {
477  vpXmlCodeType prop;
478  /* read value in the XML file. */
479  std::string camera_name_tmp = "";
480  unsigned int image_height_tmp = 0 ;
481  unsigned int image_width_tmp = 0 ;
482  unsigned int subsampling_width_tmp = 0;
483  unsigned int subsampling_height_tmp = 0;
484  // unsigned int full_width_tmp = 0;
485  // unsigned int full_height_tmp = 0;
486  vpCameraParameters cam_tmp;
487  vpCameraParameters cam_tmp_model;
488  bool projModelFound = false;
490 
491  for (node = node->xmlChildrenNode; node != NULL; node = node->next)
492  {
493  // vpDEBUG_TRACE (15, "Carac : %s.", node ->name);
494  if (node->type != XML_ELEMENT_NODE) continue;
495  if (SEQUENCE_OK != str2xmlcode ((char*)(node ->name), prop))
496  {
497  prop = CODE_XML_OTHER;
498  back = SEQUENCE_ERROR;
499  }
500 
501 
502  switch (prop)
503  {
504  case CODE_XML_CAMERA_NAME:{
505  char * val_char = xmlReadCharChild(doc, node);
506  camera_name_tmp = val_char;
507  xmlFree(val_char);
508  }break;
509 
510  case CODE_XML_WIDTH:
511  image_width_tmp = xmlReadUnsignedIntChild(doc, node);
512  break;
513 
514  case CODE_XML_HEIGHT:
515  image_height_tmp = xmlReadUnsignedIntChild(doc, node);
516  break;
518  subsampling_width_tmp = xmlReadUnsignedIntChild(doc, node);
519  break;
521  subsampling_height_tmp = xmlReadUnsignedIntChild(doc, node);
522  break;
523  // case CODE_XML_FULL_WIDTH:
524  // full_width_tmp = xmlReadUnsignedIntChild(doc, node);
525  // break;
526 
527  // case CODE_XML_FULL_HEIGHT:
528  // full_height_tmp = xmlReadUnsignedIntChild(doc, node);
529  // break;
530 
531  case CODE_XML_MODEL:
532  back = read_camera_model(doc, node, cam_tmp_model);
533  if(cam_tmp_model.get_projModel() == projModel){
534  cam_tmp = cam_tmp_model;
535  projModelFound = true;
536  }
537  break;
538 
539  case CODE_XML_BAD:
540  case CODE_XML_OTHER:
541  case CODE_XML_CAMERA:
543  case CODE_XML_FULL_WIDTH:
544  case CODE_XML_MODEL_TYPE:
545  case CODE_XML_U0:
546  case CODE_XML_V0:
547  case CODE_XML_PX:
548  case CODE_XML_PY:
549  case CODE_XML_KUD:
550  case CODE_XML_KDU:
551  default:
552  back = SEQUENCE_ERROR;
553  break;
554  }
555 
556  }
557  // Create a specific test for subsampling_width and subsampling_height to ensure that division by zero is not possible in the next test
558  bool test_subsampling_width = true;
559  bool test_subsampling_height = true;
560 
561  if (subsampling_width) {
562  test_subsampling_width = (abs((int)subsampl_width - (int)subsampling_width_tmp) < (allowedPixelDiffOnImageSize * (int)(subsampling_width_tmp / subsampling_width)));
563  }
564  if (subsampling_height) {
565  test_subsampling_height = (abs((int)subsampl_height - (int)subsampling_height_tmp) < (allowedPixelDiffOnImageSize * (int)(subsampling_height_tmp / subsampling_height)));
566  }
567  if( !((projModelFound == true) && (cam_name == camera_name_tmp) &&
568  (abs((int)im_width - (int)image_width_tmp) < allowedPixelDiffOnImageSize || im_width == 0) &&
569  (abs((int)im_height - (int)image_height_tmp) < allowedPixelDiffOnImageSize || im_height == 0) &&
570  (test_subsampling_width)&&
571  (test_subsampling_height))){
572  back = SEQUENCE_ERROR;
573  }
574  else{
575  this->camera = cam_tmp;
576  this->camera_name = camera_name_tmp;
577  this->image_width = image_width_tmp;
578  this->image_height = image_height_tmp;
579  this->subsampling_width = subsampling_width_tmp;
580  this->subsampling_height = subsampling_height_tmp;
581  this->full_width = subsampling_width_tmp * image_width_tmp;
582  this->full_height = subsampling_height_tmp * image_height_tmp;
583  }
584  return back;
585 }
605 int
606 vpXmlParserCamera::
607 read_camera_header (xmlDocPtr doc, xmlNodePtr node,
608  const std::string& cam_name,
609  const unsigned int im_width,
610  const unsigned int im_height,
611  const unsigned int subsampl_width,
612  const unsigned int subsampl_height)
613 {
614  vpXmlCodeType prop;
615  /* read value in the XML file. */
616  std::string camera_name_tmp = "";
617  unsigned int image_height_tmp = 0 ;
618  unsigned int image_width_tmp = 0 ;
619  unsigned int subsampling_width_tmp = 0;
620  unsigned int subsampling_height_tmp = 0;
621  // unsigned int full_width_tmp = 0;
622  // unsigned int full_height_tmp = 0;
624 
625  for (node = node->xmlChildrenNode; node != NULL; node = node->next)
626  {
627  // vpDEBUG_TRACE (15, "Carac : %s.", node ->name);
628  if (node->type != XML_ELEMENT_NODE) continue;
629  if (SEQUENCE_OK != str2xmlcode ((char*)(node ->name), prop))
630  {
631  prop = CODE_XML_OTHER;
632  back = SEQUENCE_ERROR;
633  }
634 
635 
636  switch (prop)
637  {
638  case CODE_XML_CAMERA_NAME:{
639  char * val_char = xmlReadCharChild(doc, node);
640  camera_name_tmp = val_char;
641  xmlFree(val_char);
642  }break;
643 
644  case CODE_XML_WIDTH:
645  image_width_tmp = xmlReadUnsignedIntChild(doc, node);
646  break;
647 
648  case CODE_XML_HEIGHT:
649  image_height_tmp = xmlReadUnsignedIntChild(doc, node);
650  break;
652  subsampling_width_tmp = xmlReadUnsignedIntChild(doc, node);
653  break;
655  subsampling_height_tmp = xmlReadUnsignedIntChild(doc, node);
656  break;
657  // case CODE_XML_FULL_WIDTH:
658  // full_width_tmp = xmlReadUnsignedIntChild(doc, node);
659  // break;
660 
661  // case CODE_XML_FULL_HEIGHT:
662  // full_height_tmp = xmlReadUnsignedIntChild(doc, node);
663  // break;
664 
665  case CODE_XML_MODEL:
666  break;
667 
668  case CODE_XML_BAD:
669  case CODE_XML_OTHER:
670  case CODE_XML_CAMERA:
672  case CODE_XML_FULL_WIDTH:
673  case CODE_XML_MODEL_TYPE:
674  case CODE_XML_U0:
675  case CODE_XML_V0:
676  case CODE_XML_PX:
677  case CODE_XML_PY:
678  case CODE_XML_KUD:
679  case CODE_XML_KDU:
680  default:
681  back = SEQUENCE_ERROR;
682  break;
683  }
684  }
685  if( !((cam_name == camera_name_tmp) &&
686  (im_width == image_width_tmp || im_width == 0) &&
687  (im_height == image_height_tmp || im_height == 0) &&
688  (subsampl_width == subsampling_width_tmp ||
689  subsampl_width == 0)&&
690  (subsampl_height == subsampling_height_tmp ||
691  subsampl_height == 0))){
692  back = SEQUENCE_ERROR;
693  }
694  return back;
695 }
696 
708 vpXmlParserCamera::read_camera_model (xmlDocPtr doc, xmlNodePtr node,
709  vpCameraParameters &cam_tmp)
710 {
711  // counter of the number of read parameters
712  int nb = 0;
713  vpXmlCodeType prop;
714  /* read value in the XML file. */
715 
716  char* model_type = NULL;
717  double u0 = cam_tmp.get_u0();
718  double v0 = cam_tmp.get_v0();
719  double px = cam_tmp.get_px();
720  double py = cam_tmp.get_py();
721  double kud = cam_tmp.get_kud();
722  double kdu = cam_tmp.get_kdu();
724  int validation = 0;
725 
726  for (node = node->xmlChildrenNode; node != NULL; node = node->next)
727  {
728  // vpDEBUG_TRACE (15, "Carac : %s.", node ->name);
729  if (node->type != XML_ELEMENT_NODE) continue;
730  if (SEQUENCE_OK != str2xmlcode ((char*)(node ->name), prop))
731  {
732  prop = CODE_XML_OTHER;
733  back = SEQUENCE_ERROR;
734  }
735 
736  switch (prop)
737  {
738  case CODE_XML_MODEL_TYPE:{
739  if(model_type != NULL){
740  xmlFree(model_type);
741  }
742  model_type = xmlReadCharChild(doc, node);
743  nb++;
744  validation = validation | 0x01;
745  }break;
746  case CODE_XML_U0:
747  u0 = xmlReadDoubleChild(doc, node);
748  nb++;
749  validation = validation | 0x02;
750  break;
751  case CODE_XML_V0:
752  v0 = xmlReadDoubleChild(doc, node);
753  nb++;
754  validation = validation | 0x04;
755  break;
756  case CODE_XML_PX:
757  px = xmlReadDoubleChild(doc, node);
758  nb++;
759  validation = validation | 0x08;
760  break;
761  case CODE_XML_PY:
762  py = xmlReadDoubleChild(doc, node);
763  nb++;
764  validation = validation | 0x10;
765  break;
766  case CODE_XML_KUD:
767  kud = xmlReadDoubleChild(doc, node);
768  nb++;
769  validation = validation | 0x20;
770  break;
771  case CODE_XML_KDU:
772  kdu = xmlReadDoubleChild(doc, node);
773  nb++;
774  validation = validation | 0x40;
775  break;
776  case CODE_XML_BAD:
777  case CODE_XML_OTHER:
778  case CODE_XML_CAMERA:
780  case CODE_XML_HEIGHT:
781  case CODE_XML_WIDTH:
785  case CODE_XML_FULL_WIDTH:
786  case CODE_XML_MODEL:
787  default:
788  back = SEQUENCE_ERROR;
789  break;
790  }
791  }
792 
793  if(model_type == NULL) {
794  vpERROR_TRACE("projection model type doesn't match with any known model !");
795  return SEQUENCE_ERROR;
796  }
797 
798  if( !strcmp(model_type,LABEL_XML_MODEL_WITHOUT_DISTORTION)){
799  if (nb != 5 || validation != 0x1F)
800  {
801  vpCERROR <<"ERROR in 'model' field:\n";
802  vpCERROR << "it must contain 5 parameters\n";
803  xmlFree(model_type);
804 
805  return SEQUENCE_ERROR;
806  }
807  cam_tmp.initPersProjWithoutDistortion(px,py,u0,v0) ;
808  }
809  else if( !strcmp(model_type,LABEL_XML_MODEL_WITH_DISTORTION)){
810  if (nb != 7 || validation != 0x7F)
811  {
812  vpCERROR <<"ERROR in 'model' field:\n";
813  vpCERROR << "it must contain 7 parameters\n";
814  xmlFree(model_type);
815 
816  return SEQUENCE_ERROR;
817  }
818  cam_tmp.initPersProjWithDistortion(px,py,u0,v0,kud,kdu);
819  }
820  else{
821  vpERROR_TRACE("projection model type doesn't match with any known model !");
822  xmlFree(model_type);
823 
824  return SEQUENCE_ERROR;
825  }
826  xmlFree(model_type);
827 
828  return back;
829 }
830 
848 int vpXmlParserCamera::
849 write (xmlNodePtr node, const std::string& cam_name,
850  const unsigned int im_width, const unsigned int im_height,
851  const unsigned int subsampl_width,
852  const unsigned int subsampl_height)
853 {
854  int back = SEQUENCE_OK;
855 
856  xmlNodePtr node_tmp;
857  xmlNodePtr node_camera;
858 
859  // <camera>
860  node_camera = xmlNewNode(NULL,(xmlChar*)LABEL_XML_CAMERA);
861  xmlAddChild(node,node_camera);
862  {
863  //<name>
864 
865  if(!cam_name.empty()){
866  node_tmp = xmlNewComment((xmlChar*)"Name of the camera");
867  xmlAddChild(node_camera,node_tmp);
868  xmlNewTextChild(node_camera,NULL,(xmlChar*)LABEL_XML_CAMERA_NAME,
869  (xmlChar*)cam_name.c_str());
870  }
871 
872  if(im_width != 0 || im_height != 0){
873  char str[11];
874  //<image_width>
875  node_tmp = xmlNewComment((xmlChar*)"Size of the image on which camera calibration was performed");
876  xmlAddChild(node_camera,node_tmp);
877 
878  sprintf(str,"%u",im_width);
879  xmlNewTextChild(node_camera,NULL,(xmlChar*)LABEL_XML_WIDTH,(xmlChar*)str);
880  //<image_height>
881 
882  sprintf(str,"%u",im_height);
883  xmlNewTextChild(node_camera,NULL,(xmlChar*)LABEL_XML_HEIGHT,(xmlChar*)str);
884  if(subsampling_width != 0 || subsampling_height != 0){
885  node_tmp = xmlNewComment((xmlChar*)"Subsampling used to obtain the current size of the image.");
886  xmlAddChild(node_camera,node_tmp);
887 
888  //<subsampling_width>
889  sprintf(str,"%u",subsampl_width);
890  xmlNewTextChild(node_camera,NULL,(xmlChar*)LABEL_XML_SUBSAMPLING_WIDTH,
891  (xmlChar*)str);
892  //<subsampling_height>
893  sprintf(str,"%u",subsampl_height);
894  xmlNewTextChild(node_camera,NULL,(xmlChar*)LABEL_XML_SUBSAMPLING_HEIGHT,
895  (xmlChar*)str);
896  node_tmp = xmlNewComment((xmlChar*)"The full size is the sensor size actually used to grab the image. full_width = subsampling_width * image_width");
897  xmlAddChild(node_camera,node_tmp);
898 
899  //<full_width>
900  sprintf(str,"%u",im_width*subsampl_width);
901  xmlNewTextChild(node_camera,NULL,(xmlChar*)LABEL_XML_FULL_WIDTH,
902  (xmlChar*)str);
903  //<full_height>
904  sprintf(str,"%u",im_height*subsampl_height);
905  xmlNewTextChild(node_camera,NULL,(xmlChar*)LABEL_XML_FULL_HEIGHT,
906  (xmlChar*)str);
907  }
908  }
909 
910  node_tmp = xmlNewComment((xmlChar*)"Intrinsic camera parameters computed for each projection model");
911 
912  xmlAddChild(node_camera,node_tmp);
913 
914  back = write_camera(node_camera);
915  }
916  return back;
917 }
925 int vpXmlParserCamera::
926 write_camera(xmlNodePtr node_camera){
927  xmlNodePtr node_model;
928  xmlNodePtr node_tmp;
929 
930  int back = SEQUENCE_OK;
931  switch(camera.get_projModel()){
933  //<model>
934  node_model = xmlNewNode(NULL,(xmlChar*)LABEL_XML_MODEL);
935  xmlAddChild(node_camera,node_model);
936  {
937  char str[21];
938  node_tmp = xmlNewComment((xmlChar*)"Projection model type");
939  xmlAddChild(node_model,node_tmp);
940 
941  //<type>without_distortion</type>
942  xmlNewTextChild(node_model,NULL,(xmlChar*)LABEL_XML_MODEL_TYPE,
943  (xmlChar*)LABEL_XML_MODEL_WITHOUT_DISTORTION);
944 
945  node_tmp = xmlNewComment((xmlChar*)"Pixel ratio");
946  xmlAddChild(node_model,node_tmp);
947  //<px>
948  sprintf(str,"%.10f",camera.get_px());
949  xmlNewTextChild(node_model,NULL,(xmlChar*)LABEL_XML_PX,(xmlChar*)str);
950  //<py>
951  sprintf(str,"%.10f",camera.get_py());
952  xmlNewTextChild(node_model,NULL,(xmlChar*)LABEL_XML_PY,(xmlChar*)str);
953 
954  node_tmp = xmlNewComment((xmlChar*)"Principal point");
955  xmlAddChild(node_model,node_tmp);
956 
957  //<u0>
958  sprintf(str,"%.10f",camera.get_u0());
959  xmlNewTextChild(node_model,NULL,(xmlChar*)LABEL_XML_U0,(xmlChar*)str);
960  //<v0>
961  sprintf(str,"%.10f",camera.get_v0());
962  xmlNewTextChild(node_model,NULL,(xmlChar*)LABEL_XML_V0,(xmlChar*)str);
963  }
964  break;
966  //<model>
967  node_model = xmlNewNode(NULL,(xmlChar*)LABEL_XML_MODEL);
968  xmlAddChild(node_camera,node_model);
969  {
970  char str[21];
971  node_tmp = xmlNewComment((xmlChar*)"Projection model type");
972  xmlAddChild(node_model,node_tmp);
973  //<type>with_distortion</type>
974  xmlNewTextChild(node_model,NULL,(xmlChar*)LABEL_XML_MODEL_TYPE,
975  (xmlChar*)LABEL_XML_MODEL_WITH_DISTORTION);
976 
977  node_tmp = xmlNewComment((xmlChar*)"Pixel ratio");
978  xmlAddChild(node_model,node_tmp);
979  //<px>
980  sprintf(str,"%.10f",camera.get_px());
981  xmlNewTextChild(node_model,NULL,(xmlChar*)LABEL_XML_PX,(xmlChar*)str);
982  //<py>
983  sprintf(str,"%.10f",camera.get_py());
984  xmlNewTextChild(node_model,NULL,(xmlChar*)LABEL_XML_PY,(xmlChar*)str);
985 
986  node_tmp = xmlNewComment((xmlChar*)"Principal point");
987  xmlAddChild(node_model,node_tmp);
988  //<u0>
989  sprintf(str,"%.10f",camera.get_u0());
990  xmlNewTextChild(node_model,NULL,(xmlChar*)LABEL_XML_U0,(xmlChar*)str);
991  //<v0>
992  sprintf(str,"%.10f",camera.get_v0());
993  xmlNewTextChild(node_model,NULL,(xmlChar*)LABEL_XML_V0,(xmlChar*)str);
994 
995  //<kud>
996  node_tmp = xmlNewComment((xmlChar*)"Undistorted to distorted distortion parameter");
997  xmlAddChild(node_model,node_tmp);
998  sprintf(str,"%.10f",camera.get_kud());
999  xmlNewTextChild(node_model,NULL,(xmlChar*)LABEL_XML_KUD,(xmlChar*)str);
1000 
1001  //<kud>
1002  node_tmp = xmlNewComment((xmlChar*)"Distorted to undistorted distortion parameter");
1003  xmlAddChild(node_model,node_tmp);
1004  sprintf(str,"%.10f",camera.get_kdu());
1005  xmlNewTextChild(node_model,NULL,(xmlChar*)LABEL_XML_KDU,(xmlChar*)str);
1006  }
1007  break;
1008  }
1009  return back;
1010 }
1011 
1021 vpXmlParserCamera::str2xmlcode (char * str, vpXmlCodeType & res)
1022 {
1023  vpXmlCodeType val_int = CODE_XML_BAD;
1025 
1026  // DEBUG_TRACE (9, "# Entree :str=%s.", str);
1027 
1028  if (! strcmp (str, LABEL_XML_CAMERA))
1029  {
1030  val_int = CODE_XML_CAMERA;
1031  }
1032  else if (! strcmp (str, LABEL_XML_CAMERA_NAME))
1033  {
1034  val_int = CODE_XML_CAMERA_NAME;
1035  }
1036  else if (! strcmp (str, LABEL_XML_MODEL))
1037  {
1038  val_int = CODE_XML_MODEL;
1039  }
1040  else if (! strcmp (str, LABEL_XML_MODEL_TYPE))
1041  {
1042  val_int = CODE_XML_MODEL_TYPE;
1043  }
1044  else if (! strcmp (str, LABEL_XML_WIDTH))
1045  {
1046  val_int = CODE_XML_WIDTH;
1047  }
1048  else if (! strcmp (str, LABEL_XML_HEIGHT))
1049  {
1050  val_int = CODE_XML_HEIGHT;
1051  }
1052  else if (! strcmp (str, LABEL_XML_SUBSAMPLING_WIDTH))
1053  {
1054  val_int = CODE_XML_SUBSAMPLING_WIDTH;
1055  }
1056  else if (! strcmp (str, LABEL_XML_SUBSAMPLING_HEIGHT))
1057  {
1058  val_int = CODE_XML_SUBSAMPLING_HEIGHT;
1059  }
1060  else if (! strcmp (str, LABEL_XML_FULL_WIDTH))
1061  {
1062  val_int = CODE_XML_FULL_WIDTH;
1063  }
1064  else if (! strcmp (str, LABEL_XML_FULL_HEIGHT))
1065  {
1066  val_int = CODE_XML_FULL_HEIGHT;
1067  }
1068  else if (! strcmp (str, LABEL_XML_U0))
1069  {
1070  val_int = CODE_XML_U0;
1071  }
1072  else if (! strcmp (str, LABEL_XML_V0))
1073  {
1074  val_int = CODE_XML_V0;
1075  }
1076  else if (! strcmp (str, LABEL_XML_PX))
1077  {
1078  val_int = CODE_XML_PX;
1079  }
1080  else if (! strcmp (str, LABEL_XML_PY))
1081  {
1082  val_int = CODE_XML_PY;
1083  }
1084  else if (! strcmp (str, LABEL_XML_KUD))
1085  {
1086  val_int = CODE_XML_KUD;
1087  }
1088  else if (! strcmp (str, LABEL_XML_KDU))
1089  {
1090  val_int = CODE_XML_KDU;
1091  }
1092  else
1093  {
1094  val_int = CODE_XML_OTHER;
1095  }
1096  res = val_int;
1097 
1098  return back;
1099 }
1100 #endif //VISP_HAVE_XML2
double get_u0() const
Perspective projection without distortion model.
vpXmlParserCamera & operator=(const vpXmlParserCamera &twinparser)
#define vpERROR_TRACE
Definition: vpDebug.h:395
#define vpCERROR
Definition: vpDebug.h:369
double xmlReadDoubleChild(xmlDocPtr doc, xmlNodePtr node)
double get_py() const
int save(const vpCameraParameters &cam, const char *filename, const std::string &camera_name, const unsigned int image_width=0, const unsigned int image_height=0)
XML parser to load and save intrinsic camera parameters.
char * xmlReadCharChild(xmlDocPtr doc, xmlNodePtr node)
void initPersProjWithoutDistortion(const double px, const double py, const double u0, const double v0)
This class intends to simplify the creation of xml parser based on the libxml2 third party library...
Definition: vpXmlParser.h:178
double get_v0() const
Generic class defining intrinsic camera parameters.
Perspective projection with distortion model.
double get_px() const
int parse(vpCameraParameters &cam, const char *filename, const std::string &camera_name, const vpCameraParameters::vpCameraParametersProjType &projModel, const unsigned int image_width=0, const unsigned int image_height=0)
double get_kud() const
unsigned int xmlReadUnsignedIntChild(xmlDocPtr doc, xmlNodePtr node)
vpCameraParametersProjType get_projModel() const
double get_kdu() const
void initPersProjWithDistortion(const double px, const double py, const double u0, const double v0, const double kud, const double kdu)