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