1 | %'update_imadoc': update an xml file with geometric calibration parameters |
---|
2 | %-------------------------------------------------------------------------- |
---|
3 | % function update_imadoc(Struct,outputfile) |
---|
4 | % |
---|
5 | %INPUT: |
---|
6 | % Struct: structure containing the calibration parameters |
---|
7 | % outputfile: xml file to modify |
---|
8 | % StructName : Name of the field in the xml file |
---|
9 | %------------------------------------------------------------- |
---|
10 | |
---|
11 | %======================================================================= |
---|
12 | % Copyright 2008-2024, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France |
---|
13 | % http://www.legi.grenoble-inp.fr |
---|
14 | % Joel.Sommeria - Joel.Sommeria (A) univ-grenoble-alpes.fr |
---|
15 | % |
---|
16 | % This file is part of the toolbox UVMAT. |
---|
17 | % |
---|
18 | % UVMAT is free software; you can redistribute it and/or modify |
---|
19 | % it under the terms of the GNU General Public License as published |
---|
20 | % by the Free Software Foundation; either version 2 of the license, |
---|
21 | % or (at your option) any later version. |
---|
22 | % |
---|
23 | % UVMAT is distributed in the hope that it will be useful, |
---|
24 | % but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
25 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
26 | % GNU General Public License (see LICENSE.txt) for more details. |
---|
27 | %======================================================================= |
---|
28 | |
---|
29 | function errormsg=update_imadoc(Struct,outputfile,StructName) |
---|
30 | errormsg=''; |
---|
31 | testappend=0; |
---|
32 | |
---|
33 | %% set the output xml file at the root, hide other existing xml files |
---|
34 | dotchar=regexp(outputfile,'\.'); |
---|
35 | for idot=1:numel(dotchar) |
---|
36 | outputfile=[outputfile(1:dotchar(end-idot+1)-1) '.xml']; |
---|
37 | if exist(outputfile,'file') |
---|
38 | backupfile=outputfile; |
---|
39 | testexist=2; |
---|
40 | while testexist==2 |
---|
41 | backupfile=[backupfile '~']; |
---|
42 | testexist=exist(backupfile,'file'); |
---|
43 | end |
---|
44 | [success,message]=movefile(outputfile,backupfile);%make backup |
---|
45 | if success~=1 |
---|
46 | errormsg=['errror in xml file backup: ' message]; |
---|
47 | return |
---|
48 | end |
---|
49 | end |
---|
50 | end |
---|
51 | |
---|
52 | |
---|
53 | %% backup the output file if it already exist, and read it |
---|
54 | if exist(outputfile,'file')%=1 if the output file already exists, 0 else |
---|
55 | testappend=1; |
---|
56 | t=xmltree(outputfile); %read the file |
---|
57 | title=get(t,1,'name'); |
---|
58 | if ~strcmp(title,'ImaDoc') |
---|
59 | errormsg=[outputfile ' not appropriate for calibration']; |
---|
60 | return |
---|
61 | end |
---|
62 | uid_calib=find(t,['ImaDoc/' StructName]); |
---|
63 | if isempty(uid_calib) %if Struct does not already exists, create it |
---|
64 | [t,uid_calib]=add(t,1,'element',StructName); |
---|
65 | else %if Struct already exists, delete its content |
---|
66 | uid_child=children(t,uid_calib); |
---|
67 | t=delete(t,uid_child); |
---|
68 | end |
---|
69 | end |
---|
70 | |
---|
71 | %% create a new xml file |
---|
72 | if ~testappend |
---|
73 | t=xmltree; |
---|
74 | t=set(t,1,'name','ImaDoc'); |
---|
75 | % in case of movie (avi file), copy timing info in the new xml file |
---|
76 | [pp,outputroot]=fileparts(outputfile); |
---|
77 | % imainfo=[]; |
---|
78 | if exist(fullfile(pp,[outputroot '.avi']),'file') |
---|
79 | FileName=fullfile(pp,[outputroot '.avi']); |
---|
80 | hhh=which('videoreader'); |
---|
81 | if isempty(hhh)%use old video function of matlab |
---|
82 | imainfo=aviinfo(FileName); |
---|
83 | imainfo.FrameRate=imainfo.FramesPerSecond; |
---|
84 | imainfo.NumberOfFrames=imainfo.NumFrames; |
---|
85 | else %use video function videoreader of matlab |
---|
86 | imainfo=get(videoreader(FileName)); |
---|
87 | end |
---|
88 | if ~isempty(imainfo) |
---|
89 | [t,uid_camera]=add(t,1,'element','Camera'); |
---|
90 | Camera.TimeUnit='s'; |
---|
91 | Camera.BurstTiming.Time=0; |
---|
92 | Camera.BurstTiming.Dti=1/imainfo.FrameRate; |
---|
93 | Camera.BurstTiming.NbDti=imainfo.NumberOfFrames-1; |
---|
94 | t=struct2xml(Camera,t,uid_camera); |
---|
95 | end |
---|
96 | end |
---|
97 | [t,uid_calib]=add(t,1,'element',StructName); |
---|
98 | end |
---|
99 | |
---|
100 | %% save the output file |
---|
101 | t=struct2xml(Struct,t,uid_calib); |
---|
102 | try |
---|
103 | save(t,outputfile); |
---|
104 | catch ME |
---|
105 | errormsg=['error in saving ' outputfile ': ' ME.message]; |
---|
106 | end |
---|