ViSP  2.6.2
vpXmlParserCamera.cpp
1 /****************************************************************************
2  *
3  * $Id: vpXmlParserCamera.cpp 3643 2012-03-21 07:16:38Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2012 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  camera_name = "";
85  image_width = 0;
86  image_height = 0;
87  subsampling_width = 0;
88  subsampling_height = 0;
89  full_width = 0;
90  full_height = 0;
91 }
97  this->camera = twinParser.camera;
98  this->camera_name = twinParser.camera_name;
99  this->image_width = twinParser.image_width;
100  this->image_height = twinParser.image_height;
101  this->subsampling_width = twinParser.subsampling_width;
102  this->subsampling_height = twinParser.subsampling_height;
103  this->full_width = twinParser.full_width;
104  this->full_height = twinParser.full_height;
105 }
106 
114  this->camera = twinParser.camera;
115  this->camera_name = twinParser.camera_name;
116  this->image_width = twinParser.image_width;
117  this->image_height = twinParser.image_height;
118  this->subsampling_width = twinParser.subsampling_width;
119  this->subsampling_height = twinParser.subsampling_height;
120  this->full_width = twinParser.full_width;
121  this->full_height = twinParser.full_height;
122  return *this ;
123 }
124 
139 int
140 vpXmlParserCamera::parse(vpCameraParameters &cam, const char * filename,
141  const std::string& camera_name,
143  const unsigned int image_width,
144  const unsigned int image_height)
145 {
146  xmlDocPtr doc;
147  xmlNodePtr node;
148 
149  doc = xmlParseFile(filename);
150  if (doc == NULL)
151  {
152  return SEQUENCE_ERROR;
153  }
154 
155  node = xmlDocGetRootElement(doc);
156  if (node == NULL)
157  {
158  xmlFreeDoc(doc);
159  return SEQUENCE_ERROR;
160  }
161 
162  int ret = this ->read (doc, node, camera_name, projModel, image_width, image_height);
163 
164  cam = camera ;
165 
166  xmlFreeDoc(doc);
167 
168  return ret;
169 }
170 
184 int
185 vpXmlParserCamera::save(const vpCameraParameters &cam, const char * filename,
186  const std::string& camera_name,
187  const unsigned int image_width,
188  const unsigned int image_height)
189 {
190  xmlDocPtr doc;
191  xmlNodePtr node;
192  xmlNodePtr nodeCamera = NULL;
193 
194  doc = xmlReadFile(filename,NULL,XML_PARSE_NOWARNING + XML_PARSE_NOERROR
195  + XML_PARSE_NOBLANKS);
196  if (doc == NULL){
197  doc = xmlNewDoc ((xmlChar*)"1.0");
198  node = xmlNewNode(NULL,(xmlChar*)LABEL_XML_ROOT);
199  xmlDocSetRootElement(doc,node);
200  xmlNodePtr node_tmp = xmlNewComment((xmlChar*)
201  "This file stores intrinsic camera parameters used\n"
202  " in the vpCameraParameters Class of ViSP available\n"
203  " at http://www.irisa.fr/lagadic/visp/visp.html .\n"
204  " It can be read with the parse method of\n"
205  " the vpXmlParserCamera class.");
206  xmlAddChild(node,node_tmp);
207  }
208 
209  node = xmlDocGetRootElement(doc);
210  if (node == NULL)
211  {
212  xmlFreeDoc(doc);
213  return SEQUENCE_ERROR;
214  }
215 
216  int nbCamera = count(doc, node, camera_name,cam.get_projModel(),
217  image_width, image_height);
218  if( nbCamera > 0){
219  vpCERROR << nbCamera
220  << " set(s) of camera parameters is(are) already "<< std::endl
221  << "available in the file with your specifications : "<< std::endl
222  << "precise the grabber parameters or delete manually"<< std::endl
223  << "the previous one."<<std::endl;
224  xmlFreeDoc(doc);
225  return SEQUENCE_ERROR;
226  }
227  this->camera = cam;
228 
229  nodeCamera = find_camera(doc, node, camera_name, image_width, image_height);
230  if(nodeCamera == NULL){
231  write(node, camera_name, image_width, image_height);
232  }
233  else{
234  write_camera(nodeCamera);
235  }
236  xmlSaveFormatFile(filename,doc,1);
237  xmlFreeDoc(doc);
238 
239  return SEQUENCE_OK;
240 }
241 
242 
243 
262 int
263 vpXmlParserCamera::read (xmlDocPtr doc, xmlNodePtr node,
264  const std::string& camera_name,
266  const unsigned int image_width,
267  const unsigned int image_height,
268  const unsigned int subsampling_width,
269  const unsigned int subsampling_height)
270 {
271  // char * val_char;
272  vpXmlCodeType prop;
273 
275  int nbCamera = 0;
276 
277  for (node = node->xmlChildrenNode; node != NULL; node = node->next)
278  {
279  if (node->type != XML_ELEMENT_NODE) continue;
280  if (SEQUENCE_OK != str2xmlcode ((char*)(node ->name), prop))
281  {
282  prop = CODE_XML_OTHER;
283  back = SEQUENCE_ERROR;
284  }
285  /*
286  switch (prop)
287  {
288  case CODE_XML_CAMERA:
289  if (SEQUENCE_OK == this->read_camera (doc, node, camera_name, projModel,
290  image_width, image_height, subsampling_width, subsampling_height)){
291  nbCamera++;
292  }
293  break;
294  default:
295  back = SEQUENCE_ERROR;
296  break;
297  }
298  */
299  if (prop == CODE_XML_CAMERA){
300  if (SEQUENCE_OK == this->read_camera (doc, node, camera_name, projModel,
301  image_width, image_height, subsampling_width, subsampling_height))
302  nbCamera++;
303  }
304  else back = SEQUENCE_ERROR;
305  }
306 
307  if (nbCamera == 0){
308  back = SEQUENCE_ERROR;
309  vpCERROR << "No camera parameters is available" << std::endl
310  << "with your specifications" << std::endl;
311  }
312  else if(nbCamera > 1){
313  back = SEQUENCE_ERROR;
314  vpCERROR << nbCamera << " sets of camera parameters are available" << std::endl
315  << "with your specifications : " << std::endl
316  << "precise your choice..." << std::endl;
317  }
318 
319  return back;
320 }
340 int
341 vpXmlParserCamera::count (xmlDocPtr doc, xmlNodePtr node,
342  const std::string& camera_name,
344  const unsigned int image_width,
345  const unsigned int image_height,
346  const unsigned int subsampling_width,
347  const unsigned int subsampling_height)
348 {
349  // char * val_char;
350  vpXmlCodeType prop;
351  int nbCamera = 0;
352 
353  for (node = node->xmlChildrenNode; node != NULL; node = node->next)
354  {
355  if (node->type != XML_ELEMENT_NODE) continue;
356  if (SEQUENCE_OK != str2xmlcode ((char*)(node ->name), prop))
357  {
358  prop = CODE_XML_OTHER;
359  }
360  /*
361  switch (prop)
362  {
363  case CODE_XML_CAMERA:
364  if (SEQUENCE_OK == this->read_camera (doc, node, camera_name, projModel,
365  image_width, image_height,
366  subsampling_width, subsampling_height)){
367  nbCamera++;
368  }
369  break;
370  default:
371  break;
372  }
373  */
374  if (prop== CODE_XML_CAMERA) {
375  if (SEQUENCE_OK == this->read_camera (doc, node, camera_name, projModel,
376  image_width, image_height,
377  subsampling_width, subsampling_height))
378  nbCamera++;
379  }
380  }
381 
382  return nbCamera;
383 }
403 xmlNodePtr
404 vpXmlParserCamera::find_camera (xmlDocPtr doc, xmlNodePtr node,
405  const std::string& camera_name,
406  const unsigned int image_width,
407  const unsigned int image_height,
408  const unsigned int subsampling_width,
409  const unsigned int subsampling_height)
410 {
411  // char * val_char;
412  vpXmlCodeType prop;
413 
414  for (node = node->xmlChildrenNode; node != NULL; node = node->next)
415  {
416  if (node->type != XML_ELEMENT_NODE) continue;
417  if (SEQUENCE_OK != str2xmlcode ((char*)(node ->name), prop))
418  {
419  prop = CODE_XML_OTHER;
420  }
421  /*
422  switch (prop)
423  {
424  case CODE_XML_CAMERA:
425  if (SEQUENCE_OK == this->read_camera_header(doc, node, camera_name,
426  image_width, image_height,
427  subsampling_width, subsampling_height)){
428  return node;
429  }
430  break;
431  default:
432  break;
433  }
434  */
435  if(prop == CODE_XML_CAMERA){
436  if (SEQUENCE_OK == this->read_camera_header(doc, node, camera_name,
437  image_width, image_height,
438  subsampling_width, subsampling_height))
439  return node;
440  }
441  }
442  return NULL;
443 }
444 
464 int
465 vpXmlParserCamera::read_camera (xmlDocPtr doc, xmlNodePtr node,
466  const std::string& camera_name,
468  const unsigned int image_width,
469  const unsigned int image_height,
470  const unsigned int subsampling_width,
471  const unsigned int subsampling_height)
472 {
473  vpXmlCodeType prop;
474  /* read value in the XML file. */
475  std::string camera_name_tmp = "";
476  unsigned int image_height_tmp = 0 ;
477  unsigned int image_width_tmp = 0 ;
478  unsigned int subsampling_width_tmp = 0;
479  unsigned int subsampling_height_tmp = 0;
480  // unsigned int full_width_tmp = 0;
481  // unsigned int full_height_tmp = 0;
482  vpCameraParameters cam_tmp;
483  vpCameraParameters cam_tmp_model;
484  bool projModelFound = false;
486 
487  for (node = node->xmlChildrenNode; node != NULL; node = node->next)
488  {
489  // vpDEBUG_TRACE (15, "Carac : %s.", node ->name);
490  if (node->type != XML_ELEMENT_NODE) continue;
491  if (SEQUENCE_OK != str2xmlcode ((char*)(node ->name), prop))
492  {
493  prop = CODE_XML_OTHER;
494  back = SEQUENCE_ERROR;
495  }
496 
497 
498  switch (prop)
499  {
500  case CODE_XML_CAMERA_NAME:{
501  char * val_char = xmlReadCharChild(doc, node);
502  camera_name_tmp = val_char;
503  xmlFree(val_char);
504  }break;
505 
506  case CODE_XML_WIDTH:
507  image_width_tmp = xmlReadUnsignedIntChild(doc, node);
508  break;
509 
510  case CODE_XML_HEIGHT:
511  image_height_tmp = xmlReadUnsignedIntChild(doc, node);
512  break;
514  subsampling_width_tmp = xmlReadUnsignedIntChild(doc, node);
515  break;
517  subsampling_height_tmp = xmlReadUnsignedIntChild(doc, node);
518  break;
519  // case CODE_XML_FULL_WIDTH:
520  // full_width_tmp = xmlReadUnsignedIntChild(doc, node);
521  // break;
522 
523  // case CODE_XML_FULL_HEIGHT:
524  // full_height_tmp = xmlReadUnsignedIntChild(doc, node);
525  // break;
526 
527  case CODE_XML_MODEL:
528  back = read_camera_model(doc, node, cam_tmp_model);
529  if(cam_tmp_model.get_projModel() == projModel){
530  cam_tmp = cam_tmp_model;
531  projModelFound = true;
532  }
533  break;
534 
535  case CODE_XML_BAD:
536  case CODE_XML_OTHER:
537  case CODE_XML_CAMERA:
539  case CODE_XML_FULL_WIDTH:
540  case CODE_XML_MODEL_TYPE:
541  case CODE_XML_U0:
542  case CODE_XML_V0:
543  case CODE_XML_PX:
544  case CODE_XML_PY:
545  case CODE_XML_KUD:
546  case CODE_XML_KDU:
547  default:
548  back = SEQUENCE_ERROR;
549  break;
550  }
551 
552  }
553  if( !((projModelFound == true) && (camera_name == camera_name_tmp) &&
554  (abs((int)image_width - (int)image_width_tmp) < allowedPixelDiffOnImageSize || image_width == 0) &&
555  (abs((int)image_height - (int)image_height_tmp) < allowedPixelDiffOnImageSize || image_height == 0) &&
556  ( subsampling_width == 0 ||
557  abs((int)subsampling_width - (int)subsampling_width_tmp) < (allowedPixelDiffOnImageSize * (int)(subsampling_width_tmp / subsampling_width)))&&
558  ( subsampling_height == 0 ||
559  abs((int)subsampling_height - (int)subsampling_height_tmp) < (allowedPixelDiffOnImageSize * (int)(subsampling_width_tmp / subsampling_width))))){
560  back = SEQUENCE_ERROR;
561  }
562  else{
563  this->camera = cam_tmp;
564  this->camera_name = camera_name_tmp;
565  this->image_width = image_width_tmp;
566  this->image_height = image_height_tmp;
567  this->subsampling_width = subsampling_width_tmp;
568  this->subsampling_height = subsampling_height_tmp;
569  this->full_width = subsampling_width_tmp * image_width_tmp;
570  this->full_height = subsampling_height_tmp * image_height_tmp;
571  }
572  return back;
573 }
593 int
594 vpXmlParserCamera::
595 read_camera_header (xmlDocPtr doc, xmlNodePtr node,
596  const std::string& camera_name,
597  const unsigned int image_width,
598  const unsigned int image_height,
599  const unsigned int subsampling_width,
600  const unsigned int subsampling_height)
601 {
602  vpXmlCodeType prop;
603  /* read value in the XML file. */
604  std::string camera_name_tmp = "";
605  unsigned int image_height_tmp = 0 ;
606  unsigned int image_width_tmp = 0 ;
607  unsigned int subsampling_width_tmp = 0;
608  unsigned int subsampling_height_tmp = 0;
609  // unsigned int full_width_tmp = 0;
610  // unsigned int full_height_tmp = 0;
612 
613  for (node = node->xmlChildrenNode; node != NULL; node = node->next)
614  {
615  // vpDEBUG_TRACE (15, "Carac : %s.", node ->name);
616  if (node->type != XML_ELEMENT_NODE) continue;
617  if (SEQUENCE_OK != str2xmlcode ((char*)(node ->name), prop))
618  {
619  prop = CODE_XML_OTHER;
620  back = SEQUENCE_ERROR;
621  }
622 
623 
624  switch (prop)
625  {
626  case CODE_XML_CAMERA_NAME:{
627  char * val_char = xmlReadCharChild(doc, node);
628  camera_name_tmp = val_char;
629  xmlFree(val_char);
630  }break;
631 
632  case CODE_XML_WIDTH:
633  image_width_tmp = xmlReadUnsignedIntChild(doc, node);
634  break;
635 
636  case CODE_XML_HEIGHT:
637  image_height_tmp = xmlReadUnsignedIntChild(doc, node);
638  break;
640  subsampling_width_tmp = xmlReadUnsignedIntChild(doc, node);
641  break;
643  subsampling_height_tmp = xmlReadUnsignedIntChild(doc, node);
644  break;
645  // case CODE_XML_FULL_WIDTH:
646  // full_width_tmp = xmlReadUnsignedIntChild(doc, node);
647  // break;
648 
649  // case CODE_XML_FULL_HEIGHT:
650  // full_height_tmp = xmlReadUnsignedIntChild(doc, node);
651  // break;
652 
653  case CODE_XML_MODEL:
654  break;
655 
656  case CODE_XML_BAD:
657  case CODE_XML_OTHER:
658  case CODE_XML_CAMERA:
660  case CODE_XML_FULL_WIDTH:
661  case CODE_XML_MODEL_TYPE:
662  case CODE_XML_U0:
663  case CODE_XML_V0:
664  case CODE_XML_PX:
665  case CODE_XML_PY:
666  case CODE_XML_KUD:
667  case CODE_XML_KDU:
668  default:
669  back = SEQUENCE_ERROR;
670  break;
671  }
672  }
673  if( !((camera_name == camera_name_tmp) &&
674  (image_width == image_width_tmp || image_width == 0) &&
675  (image_height == image_height_tmp || image_height == 0) &&
676  (subsampling_width == subsampling_width_tmp ||
677  subsampling_width == 0)&&
678  (subsampling_height == subsampling_height_tmp ||
679  subsampling_height == 0))){
680  back = SEQUENCE_ERROR;
681  }
682  return back;
683 }
684 
696 vpXmlParserCamera::read_camera_model (xmlDocPtr doc, xmlNodePtr node,
697  vpCameraParameters &cam_tmp)
698 {
699  // counter of the number of read parameters
700  int nb = 0;
701  vpXmlCodeType prop;
702  /* read value in the XML file. */
703 
704  char* model_type = NULL;
705  double u0 = cam_tmp.get_u0();
706  double v0 = cam_tmp.get_v0();
707  double px = cam_tmp.get_px();
708  double py = cam_tmp.get_py();
709  double kud = cam_tmp.get_kud();
710  double kdu = cam_tmp.get_kdu();
712  int validation = 0;
713 
714  for (node = node->xmlChildrenNode; node != NULL; node = node->next)
715  {
716  // vpDEBUG_TRACE (15, "Carac : %s.", node ->name);
717  if (node->type != XML_ELEMENT_NODE) continue;
718  if (SEQUENCE_OK != str2xmlcode ((char*)(node ->name), prop))
719  {
720  prop = CODE_XML_OTHER;
721  back = SEQUENCE_ERROR;
722  }
723 
724  switch (prop)
725  {
726  case CODE_XML_MODEL_TYPE:{
727  if(model_type != NULL){
728  xmlFree(model_type);
729  }
730  model_type = xmlReadCharChild(doc, node);
731  nb++;
732  validation = validation | 0x01;
733  }break;
734  case CODE_XML_U0:
735  u0 = xmlReadDoubleChild(doc, node);
736  nb++;
737  validation = validation | 0x02;
738  break;
739  case CODE_XML_V0:
740  v0 = xmlReadDoubleChild(doc, node);
741  nb++;
742  validation = validation | 0x04;
743  break;
744  case CODE_XML_PX:
745  px = xmlReadDoubleChild(doc, node);
746  nb++;
747  validation = validation | 0x08;
748  break;
749  case CODE_XML_PY:
750  py = xmlReadDoubleChild(doc, node);
751  nb++;
752  validation = validation | 0x10;
753  break;
754  case CODE_XML_KUD:
755  kud = xmlReadDoubleChild(doc, node);
756  nb++;
757  validation = validation | 0x20;
758  break;
759  case CODE_XML_KDU:
760  kdu = xmlReadDoubleChild(doc, node);
761  nb++;
762  validation = validation | 0x40;
763  break;
764  case CODE_XML_BAD:
765  case CODE_XML_OTHER:
766  case CODE_XML_CAMERA:
768  case CODE_XML_HEIGHT:
769  case CODE_XML_WIDTH:
773  case CODE_XML_FULL_WIDTH:
774  case CODE_XML_MODEL:
775  default:
776  back = SEQUENCE_ERROR;
777  break;
778  }
779  }
780 
781  if( !strcmp(model_type,LABEL_XML_MODEL_WITHOUT_DISTORTION)){
782  if (nb != 5 || validation != 0x1F)
783  {
784  vpCERROR <<"ERROR in 'model' field:\n";
785  vpCERROR << "it must contain 5 parameters\n";
786  if(model_type != NULL){
787  xmlFree(model_type);
788  }
789  return SEQUENCE_ERROR;
790  }
791  cam_tmp.initPersProjWithoutDistortion(px,py,u0,v0) ;
792  }
793  else if( !strcmp(model_type,LABEL_XML_MODEL_WITH_DISTORTION)){
794  if (nb != 7 || validation != 0x7F)
795  {
796  vpCERROR <<"ERROR in 'model' field:\n";
797  vpCERROR << "it must contain 7 parameters\n";
798  if(model_type != NULL){
799  xmlFree(model_type);
800  }
801  return SEQUENCE_ERROR;
802  }
803  cam_tmp.initPersProjWithDistortion(px,py,u0,v0,kud,kdu);
804  }
805  else{
806  vpERROR_TRACE("projection model type doesn't match with any known model !");
807  if(model_type != NULL){
808  xmlFree(model_type);
809  }
810  return SEQUENCE_ERROR;
811  }
812  if(model_type != NULL){
813  xmlFree(model_type);
814  }
815  return back;
816 }
817 
835 int vpXmlParserCamera::
836 write (xmlNodePtr node, const std::string& camera_name,
837  const unsigned int image_width, const unsigned int image_height,
838  const unsigned int subsampling_width,
839  const unsigned int subsampling_height)
840 {
841  int back = SEQUENCE_OK;
842 
843  xmlNodePtr node_tmp;
844  xmlNodePtr node_camera;
845 
846  // <camera>
847  node_camera = xmlNewNode(NULL,(xmlChar*)LABEL_XML_CAMERA);
848  xmlAddChild(node,node_camera);
849  {
850  //<name>
851 
852  if(!camera_name.empty()){
853  node_tmp = xmlNewComment((xmlChar*)"Name of the camera");
854  xmlAddChild(node_camera,node_tmp);
855  xmlNewTextChild(node_camera,NULL,(xmlChar*)LABEL_XML_CAMERA_NAME,
856  (xmlChar*)camera_name.c_str());
857  }
858 
859  if(image_width != 0 || image_height != 0){
860  char str[11];
861  //<image_width>
862  node_tmp = xmlNewComment((xmlChar*)"Size of the image on which camera calibration was performed");
863  xmlAddChild(node_camera,node_tmp);
864 
865  sprintf(str,"%u",image_width);
866  xmlNewTextChild(node_camera,NULL,(xmlChar*)LABEL_XML_WIDTH,(xmlChar*)str);
867  //<image_height>
868 
869  sprintf(str,"%u",image_height);
870  xmlNewTextChild(node_camera,NULL,(xmlChar*)LABEL_XML_HEIGHT,(xmlChar*)str);
871  if(subsampling_width != 0 || subsampling_height != 0){
872  node_tmp = xmlNewComment((xmlChar*)"Subsampling used to obtain the current size of the image.");
873  xmlAddChild(node_camera,node_tmp);
874 
875  //<subsampling_width>
876  sprintf(str,"%u",subsampling_width);
877  xmlNewTextChild(node_camera,NULL,(xmlChar*)LABEL_XML_SUBSAMPLING_WIDTH,
878  (xmlChar*)str);
879  //<subsampling_height>
880  sprintf(str,"%u",subsampling_height);
881  xmlNewTextChild(node_camera,NULL,(xmlChar*)LABEL_XML_SUBSAMPLING_HEIGHT,
882  (xmlChar*)str);
883  node_tmp = xmlNewComment((xmlChar*)"The full size is the sensor size actually used to grab the image. full_width = subsampling_width * image_width");
884  xmlAddChild(node_camera,node_tmp);
885 
886  //<full_width>
887  sprintf(str,"%u",image_width*subsampling_width);
888  xmlNewTextChild(node_camera,NULL,(xmlChar*)LABEL_XML_FULL_WIDTH,
889  (xmlChar*)str);
890  //<full_height>
891  sprintf(str,"%u",image_height*subsampling_height);
892  xmlNewTextChild(node_camera,NULL,(xmlChar*)LABEL_XML_FULL_HEIGHT,
893  (xmlChar*)str);
894  }
895  }
896 
897  node_tmp = xmlNewComment((xmlChar*)"Intrinsic camera parameters computed for each projection model");
898 
899  xmlAddChild(node_camera,node_tmp);
900 
901  back = write_camera(node_camera);
902  }
903  return back;
904 }
912 int vpXmlParserCamera::
913 write_camera(xmlNodePtr node_camera){
914  xmlNodePtr node_model;
915  xmlNodePtr node_tmp;
916 
917  int back = SEQUENCE_OK;
918  switch(camera.get_projModel()){
920  //<model>
921  node_model = xmlNewNode(NULL,(xmlChar*)LABEL_XML_MODEL);
922  xmlAddChild(node_camera,node_model);
923  {
924  char str[21];
925  node_tmp = xmlNewComment((xmlChar*)"Projection model type");
926  xmlAddChild(node_model,node_tmp);
927 
928  //<type>without_distortion</type>
929  xmlNewTextChild(node_model,NULL,(xmlChar*)LABEL_XML_MODEL_TYPE,
930  (xmlChar*)LABEL_XML_MODEL_WITHOUT_DISTORTION);
931 
932  node_tmp = xmlNewComment((xmlChar*)"Pixel ratio");
933  xmlAddChild(node_model,node_tmp);
934  //<px>
935  sprintf(str,"%.10f",camera.get_px());
936  xmlNewTextChild(node_model,NULL,(xmlChar*)LABEL_XML_PX,(xmlChar*)str);
937  //<py>
938  sprintf(str,"%.10f",camera.get_py());
939  xmlNewTextChild(node_model,NULL,(xmlChar*)LABEL_XML_PY,(xmlChar*)str);
940 
941  node_tmp = xmlNewComment((xmlChar*)"Principal point");
942  xmlAddChild(node_model,node_tmp);
943 
944  //<u0>
945  sprintf(str,"%.10f",camera.get_u0());
946  xmlNewTextChild(node_model,NULL,(xmlChar*)LABEL_XML_U0,(xmlChar*)str);
947  //<v0>
948  sprintf(str,"%.10f",camera.get_v0());
949  xmlNewTextChild(node_model,NULL,(xmlChar*)LABEL_XML_V0,(xmlChar*)str);
950  }
951  break;
953  //<model>
954  node_model = xmlNewNode(NULL,(xmlChar*)LABEL_XML_MODEL);
955  xmlAddChild(node_camera,node_model);
956  {
957  char str[21];
958  node_tmp = xmlNewComment((xmlChar*)"Projection model type");
959  xmlAddChild(node_model,node_tmp);
960  //<type>with_distortion</type>
961  xmlNewTextChild(node_model,NULL,(xmlChar*)LABEL_XML_MODEL_TYPE,
962  (xmlChar*)LABEL_XML_MODEL_WITH_DISTORTION);
963 
964  node_tmp = xmlNewComment((xmlChar*)"Pixel ratio");
965  xmlAddChild(node_model,node_tmp);
966  //<px>
967  sprintf(str,"%.10f",camera.get_px());
968  xmlNewTextChild(node_model,NULL,(xmlChar*)LABEL_XML_PX,(xmlChar*)str);
969  //<py>
970  sprintf(str,"%.10f",camera.get_py());
971  xmlNewTextChild(node_model,NULL,(xmlChar*)LABEL_XML_PY,(xmlChar*)str);
972 
973  node_tmp = xmlNewComment((xmlChar*)"Principal point");
974  xmlAddChild(node_model,node_tmp);
975  //<u0>
976  sprintf(str,"%.10f",camera.get_u0());
977  xmlNewTextChild(node_model,NULL,(xmlChar*)LABEL_XML_U0,(xmlChar*)str);
978  //<v0>
979  sprintf(str,"%.10f",camera.get_v0());
980  xmlNewTextChild(node_model,NULL,(xmlChar*)LABEL_XML_V0,(xmlChar*)str);
981 
982  //<kud>
983  node_tmp = xmlNewComment((xmlChar*)"Undistorted to distorted distortion parameter");
984  xmlAddChild(node_model,node_tmp);
985  sprintf(str,"%.10f",camera.get_kud());
986  xmlNewTextChild(node_model,NULL,(xmlChar*)LABEL_XML_KUD,(xmlChar*)str);
987 
988  //<kud>
989  node_tmp = xmlNewComment((xmlChar*)"Distorted to undistorted distortion parameter");
990  xmlAddChild(node_model,node_tmp);
991  sprintf(str,"%.10f",camera.get_kdu());
992  xmlNewTextChild(node_model,NULL,(xmlChar*)LABEL_XML_KDU,(xmlChar*)str);
993  }
994  break;
995  }
996  return back;
997 }
998 
1008 vpXmlParserCamera::str2xmlcode (char * str, vpXmlCodeType & res)
1009 {
1010  vpXmlCodeType val_int = CODE_XML_BAD;
1012 
1013  // DEBUG_TRACE (9, "# Entree :str=%s.", str);
1014 
1015  if (! strcmp (str, LABEL_XML_CAMERA))
1016  {
1017  val_int = CODE_XML_CAMERA;
1018  }
1019  else if (! strcmp (str, LABEL_XML_CAMERA_NAME))
1020  {
1021  val_int = CODE_XML_CAMERA_NAME;
1022  }
1023  else if (! strcmp (str, LABEL_XML_MODEL))
1024  {
1025  val_int = CODE_XML_MODEL;
1026  }
1027  else if (! strcmp (str, LABEL_XML_MODEL_TYPE))
1028  {
1029  val_int = CODE_XML_MODEL_TYPE;
1030  }
1031  else if (! strcmp (str, LABEL_XML_WIDTH))
1032  {
1033  val_int = CODE_XML_WIDTH;
1034  }
1035  else if (! strcmp (str, LABEL_XML_HEIGHT))
1036  {
1037  val_int = CODE_XML_HEIGHT;
1038  }
1039  else if (! strcmp (str, LABEL_XML_SUBSAMPLING_WIDTH))
1040  {
1041  val_int = CODE_XML_SUBSAMPLING_WIDTH;
1042  }
1043  else if (! strcmp (str, LABEL_XML_SUBSAMPLING_HEIGHT))
1044  {
1045  val_int = CODE_XML_SUBSAMPLING_HEIGHT;
1046  }
1047  else if (! strcmp (str, LABEL_XML_FULL_WIDTH))
1048  {
1049  val_int = CODE_XML_FULL_WIDTH;
1050  }
1051  else if (! strcmp (str, LABEL_XML_FULL_HEIGHT))
1052  {
1053  val_int = CODE_XML_FULL_HEIGHT;
1054  }
1055  else if (! strcmp (str, LABEL_XML_U0))
1056  {
1057  val_int = CODE_XML_U0;
1058  }
1059  else if (! strcmp (str, LABEL_XML_V0))
1060  {
1061  val_int = CODE_XML_V0;
1062  }
1063  else if (! strcmp (str, LABEL_XML_PX))
1064  {
1065  val_int = CODE_XML_PX;
1066  }
1067  else if (! strcmp (str, LABEL_XML_PY))
1068  {
1069  val_int = CODE_XML_PY;
1070  }
1071  else if (! strcmp (str, LABEL_XML_KUD))
1072  {
1073  val_int = CODE_XML_KUD;
1074  }
1075  else if (! strcmp (str, LABEL_XML_KDU))
1076  {
1077  val_int = CODE_XML_KDU;
1078  }
1079  else
1080  {
1081  val_int = CODE_XML_OTHER;
1082  }
1083  res = val_int;
1084 
1085  return back;
1086 }
1087 #endif //VISP_HAVE_XML2
double get_u0() const
Perspective projection without distortion model.
vpXmlParserCamera & operator=(const vpXmlParserCamera &twinparser)
#define vpERROR_TRACE
Definition: vpDebug.h:379
#define vpCERROR
Definition: vpDebug.h:354
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)