Home > . > geometry_calib.m

# geometry_calib

## PURPOSE

'geometry_calib': performs geometric calibration from a set of reference points

## SYNOPSIS

function varargout = geometry_calib(varargin)

## DESCRIPTION

    'geometry_calib': performs geometric calibration from a set of reference points

     function varargout = geometry_calib(varargin)

    A%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
      Copyright Joel Sommeria, 2008, LEGI / CNRS-UJF-INPG, sommeria@coriolis-legi.org.
    AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
         This file is part of the toolbox UVMAT.

         UVMAT is free software; you can redistribute it and/or modify
         it under the terms of the GNU General Public License as published by
         the Free Software Foundation; either version 2 of the License, or
         (at your option) any later version.

         UVMAT is distributed in the hope that it will be useful,
         but WITHOUT ANY WARRANTY; without even the implied warranty of
         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
         GNU General Public License (file UVMAT/COPYING.txt) for more details.
    AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

## CROSS-REFERENCE INFORMATION

This function calls:

- ListDir 'ListDir': scan the structure of the directory tree (for dataview.m)
- cell2tab 'cell2tab': transform a Matlab cell in a character array suitable for display in a table
- dataview TEST 'dataview': function for scanning directories in a campaign (TEST)
- delete_object 'delete_object': delete a projection object, defined by its index in the Uvmat list or by its graphic handle
- msgbox_uvmat 'msgbox_uvmat': associated with GUI msgbox_uvmat.fig to display message boxes, for error, warning or input calls
- name2display 'name2display': extracts the root name and field numbers from an input filename
- plot_field 'plot_field': plot any field with the structure defined in the uvmat package
- px 'px': transform fields from physical coordinates (phys) to image (px) coordinates
- px_XYZ 'px_XYZ': transform phys coordinates to image coordinates (px)
- read_geometry_calib 'read_geometry_calib': read data on the GUI geometry_calib
- struct2xml 'struct2xml': transform a matlab structure to a xml tree.
- uvmat 'uvmat': function associated with the GUI 'uvmat.fig' for images and data field visualization
- warndlg_uvmat 'warndlg_uvmat': display warning message (error, warning, confirmation) in a given figure

This function is called by:

- mouse_down 'mouse_down': function activated when the mouse button is pressed on a figure (callback for 'WindowButtonDownFcn'
- uvmat 'uvmat': function associated with the GUI 'uvmat.fig' for images and data field visualization

## SUBFUNCTIONS

- function geometry_calib_OpeningFcn(hObject, eventdata, handles, handles_uvmat,pos,inputfile)
- function varargout = geometry_calib_OutputFcn(hObject, eventdata, handles)
- function Phi_Callback(hObject, eventdata, handles)
- function OPEN_Callback(hObject, eventdata, handles)
- function loadfile(handles,fileinput)
- function closefcn(gcbo,eventdata,handles_uvmat)
- function PLOT_Callback(hObject, eventdata, handles)
- function MenuCoord_Callback(hObject, eventdata, handles)
- function delete_Callback(hObject, eventdata, handles)
- function APPLY_Callback(hObject, eventdata, handles)
- function REPLICATE_Callback(hObject, eventdata, handles)
- function update_imadoc(GeometryCalib,outputfile)
- function GeometryCalib=calib_rescale(Coord)
- function GeometryCalib=calib_linear(Coord)
- function GeometryCalib=calib_tsai(Coord)
- function translation_Callback(hObject, eventdata, handles)
- function T_x_Callback(hObject, eventdata, handles)
- function T_y_Callback(hObject, eventdata, handles)
- function T_z_Callback(hObject, eventdata, handles)
- function rotation_Callback(hObject, eventdata, handles)
- function XImage_Callback(hObject, eventdata, handles)
- function YImage_Callback(hObject, eventdata, handles)
- function XObject_Callback(hObject, eventdata, handles)
- function YObject_Callback(hObject, eventdata, handles)
- function ZObject_Callback(hObject, eventdata, handles)
- function update_list(hObject, eventdata, handles)
- function ListCoord_Callback(hObject, eventdata, handles)
- function translation_plus_Callback(hObject, eventdata, handles)
- function translation_minus_Callback(hObject, eventdata, handles)
- function translation(handles,T)
- function rotation_plus_Callback(hObject, eventdata, handles)
- function rotation_minus_Callback(hObject, eventdata, handles)
- function rotation(handles,Phi)
- function O_x_Callback(hObject, eventdata, handles)
- function O_y_Callback(hObject, eventdata, handles)
- function O_z_Callback(hObject, eventdata, handles)
- function edit_append_Callback(hObject, eventdata, handles)
- function NEW_Callback(hObject, eventdata, handles)
- function HELP_Callback(hObject, eventdata, handles)
- function key_press_fcn(hObject,eventdata,handles)
- function append_point_Callback(hObject, eventdata, handles)

## SOURCE CODE

    0001 %'geometry_calib': performs geometric calibration from a set of reference points
    0002 %
    0003 % function varargout = geometry_calib(varargin)
    0004 %
    0005 %A%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
    0006 %  Copyright Joel Sommeria, 2008, LEGI / CNRS-UJF-INPG, sommeria@coriolis-legi.org.
    0007 %AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
    0008 %     This file is part of the toolbox UVMAT.
    0009 %
    0010 %     UVMAT is free software; you can redistribute it and/or modify
    0011 %     it under the terms of the GNU General Public License as published by
    0012 %     the Free Software Foundation; either version 2 of the License, or
    0013 %     (at your option) any later version.
    0014 %
    0015 %     UVMAT is distributed in the hope that it will be useful,
    0016 %     but WITHOUT ANY WARRANTY; without even the implied warranty of
    0017 %     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    0018 %     GNU General Public License (file UVMAT/COPYING.txt) for more details.
    0019 %AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
    0020
    0021 function varargout = geometry_calib(varargin)
    0022 % GEOMETRY_CALIB M-file for geometry_calib.fig
    0023 %      GEOMETRY_CALIB, by itself, creates a MenuCoord GEOMETRY_CALIB or raises the existing
    0024 %      singleton*.
    0025 %
    0026 %      H = GEOMETRY_CALIB returns the handle to a MenuCoord GEOMETRY_CALIB or the handle to
    0027 %      the existing singleton*.
    0028 %
    0029 %      GEOMETRY_CALIB('CALLBACK',hObject,eventData,handles,...) calls the local
    0030 %      function named CALLBACK in GEOMETRY_CALIB.M with the given input arguments.
    0031 %
    0032 %      GEOMETRY_CALIB('Property','Value',...) creates a MenuCoord GEOMETRY_CALIB or raises the
    0033 %      existing singleton*.  Starting from the left, property value pairs are
    0034 %      applied to the GUI before geometry_calib_OpeningFunction gets called.  An
    0035 %      unrecognized property name or invalid value makes property application
    0036 %      stop.  All inputs are passed to geometry_calib_OpeningFcn via varargin.
    0037 %
    0038 %      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
    0039 %      instance to run (singleton)".
    0040 %
    0041 % See also: GUIDE, GUIDATA, GUIHANDLES
    0042
    0043 % Edit the above text to modify the response to help geometry_calib
    0044
    0045 % Last Modified by GUIDE v2.5 07-Nov-2009 21:53:58
    0046
    0047 % Begin initialization code - DO NOT edit
    0048 gui_Singleton = 1;
    0049 gui_State = struct('gui_Name',       mfilename, ...
    0050                    'gui_Singleton',  gui_Singleton, ...
    0051                    'gui_OpeningFcn', @geometry_calib_OpeningFcn, ...
    0052                    'gui_OutputFcn',  @geometry_calib_OutputFcn, ...
    0053                    'gui_LayoutFcn',  [] , ...
    0054                    'gui_Callback',   []);
    0055 if nargin & isstr(varargin{1})
    0056     gui_State.gui_Callback = str2func(varargin{1});
    0057 end
    0058
    0059 if nargout
    0060     [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
    0061 else
    0062     gui_mainfcn(gui_State, varargin{:});
    0063 end
    0064 % End initialization code - DO NOT edit
    0065
    0066
    0067 % --- Executes just before geometry_calib is made visible.
    0068 %INPUT:
    0069 %handles: handles of the geometry_calib interface elements
    0070 % PlotHandles: set of handles of the elements contolling the plotting
    0071 % parameters on the uvmat interface (obtained by 'get_plot_handle.m')
    0072 function geometry_calib_OpeningFcn(hObject, eventdata, handles, handles_uvmat,pos,inputfile)
    0073 set(handles.Phi,'TooltipString','Phi: rotation angle of the physical point coordiantes (in degrees)')% TO PUT IN GUIDE
    0074 % Choose default command line output for geometry_calib
    0075 handles.output = hObject;
    0076
    0077 % Update handles structure
    0078 guidata(hObject, handles);
    0079 movegui(hObject,'east');% position the GUI ton the right of the screen
    0080 if exist('handles_uvmat','var') %& isfield(data,'ParentButton')
    0081      set(hObject,'DeleteFcn',{@closefcn,handles_uvmat})%
    0082 end
    0083 %set the position of the interface
    0084 if exist('pos','var')& length(pos)>2
    0085     pos_gui=get(hObject,'Position');
    0086     pos_gui(1)=pos(1);
    0087     pos_gui(2)=pos(2);
    0088     set(hObject,'Position',pos_gui);
    0089 end
    0090 inputxml='';
    0091 if exist('inputfile','var')& ~isempty(inputfile)
    0092     [Path,Name,ext]=fileparts(inputfile);
    0093     form=imformats(ext([2:end]));
    0094     if ~isempty(form)% if the input file is an image
    0095         set(hObject,'UserData',inputfile)
    0096         [Pathsub,RootFile,field_count,str2,str_a,str_b,ext,nom_type,subdir]=name2display(inputfile);
    0097         inputxml=[fullfile(Pathsub,RootFile) '.xml'];
    0098     end
    0099 end
    0100 set(handles.ListCoord,'String',{''})
    0101 if exist(inputxml,'file')
    0102     loadfile(handles,inputxml)% load the point coordiantes existing in the xml file
    0103 end
    0104
    0105 set(handles.ListCoord,'KeyPressFcn',{@key_press_fcn,handles})%set keyboard action function
    0106 %set(hObject,'KeyPressFcn',{'keyboard_callback',handles})%set keyboard action function on uvmat interface when geometry_calib is on top
    0107 %htable=uitable(10,5)
    0108 %set(htable,'ColumnNames',{'x','y','z','X(pixels)','Y(pixels)'})
    0109
    0110 % --- Outputs from this function are returned to the command line.
    0111 function varargout = geometry_calib_OutputFcn(hObject, eventdata, handles)
    0112 % varargout  cell array for returning output args (see VARARGOUT);
    0113 % hObject    handle to figure
    0114 % eventdata  reserved - to be defined in a future version of MATLAB
    0115 % handles    structure with handles and user data (see GUIDATA)
    0116
    0117 % Get default command line output from handles structure
    0118 varargout{1} = handles.output;
    0119 varargout{2}=handles;
    0120
    0121 %------------
    0122 function Phi_Callback(hObject, eventdata, handles)
    0123
    0124
    0125 %-----------------------------------------------------
    0126 % --- Executes on button press in OPEN.
    0127 function OPEN_Callback(hObject, eventdata, handles)
    0128 %get the object file
    0129 huvmat=findobj(allchild(0),'Name','uvmat');
    0130 UvData=get(huvmat,'UserData');
    0131 hchild=get(huvmat,'Children');
    0132 hrootpath=findobj(hchild,'Tag','RootPath');
    0133 oldfile=get(hrootpath,'String');
    0134 if isempty(oldfile)
    0135     oldfile='';
    0136 end
    0137 %[FileName,PathName] = uigetfile('*.civ','Select a .civ file',oldfile)
    0138 [FileName, PathName, filterindex] = uigetfile( ...
    0139        {'*.xml;*.mat', ' (*.xml,*.mat)';
    0140        '*.xml',  '.xml files '; ...
    0141         '*.mat',  '.mat matlab files '}, ...
    0142         'Pick a file',oldfile);
    0143 fileinput=[PathName FileName];%complete file name
    0144 testblank=findstr(fileinput,' ');%look for blanks
    0145 if ~isempty(testblank)
    0146     warndlg_uvmat('forbidden input file name or path: no blank character allowed','ERROR')
    0147     return
    0148 end
    0149 sizf=size(fileinput);
    0150 if (~ischar(fileinput)|~isequal(sizf(1),1)),return;end
    0151 loadfile(handles,fileinput)
    0152
    0153 %--------------------------------------------------
    0154 %read input xml file and update the edit boxes
    0155 function loadfile(handles,fileinput)
    0156
    0157 %read the input xml file
    0158 t=xmltree(fileinput);
    0159 s=convert(t);%convert to matlab structure
    0160 %read data currently displayed on the interface
    0161 PointCoord=[];
    0162 Coord_cell=get(handles.ListCoord,'String');
    0163 data=read_geometry_calib(Coord_cell);
    0164 %data=read_geometry_calib(handles);
    0165 Coord=[]; %default
    0166 if isfield(data,'Coord')
    0167     Coord=data.Coord;
    0168 end
    0169 TabChar_0=get(handles.ListCoord,'String');
    0170 nbcoord_0=size(TabChar_0,1);
    0171 if isequal(get(handles.edit_append,'Value'),2) %edit mode  A REVOIR
    0172     val=get(handles.ListCoord,'Value')-1;
    0173 else
    0174    val=length(TabChar_0);
    0175 end
    0176 nbcoord=0;
    0177
    0178 %case of calibration (ImaDoc) input file
    0179 if isfield(s,'Heading')
    0180     CalibData=s.Heading;
    0181 end
    0182 CalibData.XmlInput=fileinput;
    0183 hcalib=get(handles.OPEN,'parent');
    0184 set(hcalib,'UserData',CalibData);%store the heading in the interface 'UserData'
    0185 if isfield(s,'GeometryCalib')
    0186     Calib=s.GeometryCalib;
    0187     if isfield(Calib,'CalibrationType')
    0188         CalibrationType=Calib.CalibrationType;
    0189         switch CalibrationType
    0190             case 'linear'
    0191                 set(handles.calib_type,'Value',2)
    0192             case 'tsai'
    0193                 set(handles.calib_type,'Value',3)
    0194         end
    0195     end
    0196     if isfield(Calib,'SourceCalib')
    0197         if isfield(Calib.SourceCalib,'PointCoord')
    0198             PointCoord=Calib.SourceCalib.PointCoord;
    0199         end
    0200 %         if isfield(Calib.SourceCalib,'ImageCalib')
    0201 %             hcalib=get(handles.OPEN,'parent');
    0202 %             set(hcalib,'UserData',Calib.SourceCalib.ImageCalib);%store the source image name in the interface 'UserData'
    0203 %         end
    0204     end
    0205     nbcoord=length(PointCoord);
    0206     if ~isfield(Calib,'ErrorRms')&~isfield(Calib,'ErrorMax') %old convention of Gauthier (cord in mm)
    0207         for i=1:length(PointCoord)
    0208           line=str2num(PointCoord{i});
    0209           Coord(i+val,4:5)=line(4:5);%px x
    0210           Coord(i+val,1:3)=line(1:3)/10;%phys x
    0211         end
    0212     else
    0213         for i=1:length(PointCoord)
    0214           line=str2num(PointCoord{i});
    0215           Coord(i,4:5)=line(4:5);%px x
    0216           Coord(i,1:3)=line(1:3);%phys x
    0217        end
    0218     end
    0219 end
    0220 %case of xml files of points
    0221 if isfield(s,'Coord')
    0222     PointCoord=s.Coord;
    0223     nbcoord=length(PointCoord);
    0224      %case of image coordinates
    0225     if isfield(s,'CoordType')& isequal(s.CoordType,'px')
    0226         for i=1:nbcoord
    0227            line=str2num(PointCoord{i});
    0228            Coord(i+val,4:5)=line(1:2);
    0229         end
    0230      %case of  physical coordinates
    0231     else
    0232         for i=1:nbcoord
    0233            line=str2num(PointCoord{i});
    0234            Coord(i+val,1:3)=line(1:3);
    0235            nbcolumn=size(Coord,2);
    0236            if nbcolumn<5
    0237                Coord(i+val,nbcolumn+1:5)=zeros(1,5-nbcolumn);
    0238            end
    0239         end
    0240      end
    0241 end
    0242 CoordCell={};
    0243 for iline=1:size(Coord,1)
    0244     for j=1:5
    0245         CoordCell{iline,j}=num2str(Coord(iline,j),4);
    0246     end
    0247 end
    0248 Tabchar=cell2tab(CoordCell,'    |    ');%transform cells into table ready for display
    0249 set(handles.ListCoord,'Value',1)
    0250 set(handles.ListCoord,'String',Tabchar)
    0251
    0252
    0253 %----------------------------------------------------
    0254 % executed when closing: set the parent interface button to value 0
    0255 function closefcn(gcbo,eventdata,handles_uvmat)
    0256 huvmat=findobj(allchild(0),'Name','uvmat');
    0257 if exist('handles_uvmat','var')
    0258     set(handles_uvmat.cal,'Value',0)
    0259     uvmat('cal_Callback',huvmat,[],handles_uvmat);
    0260 %     set(parent_button,'Value',0)%put unactivated buttons to green
    0261 %     set(parent_button,'BackgroundColor',[0 1 0]);
    0262 end
    0263
    0264 %-----------------------------------------------------------------------
    0265 % --- Executes on button press in PLOT (plot): PLOT the defined object and its projected field
    0266 function PLOT_Callback(hObject, eventdata, handles)
    0267
    0268 huvmat=findobj(allchild(0),'Name','uvmat');%find the current uvmat interface handle
    0269 UvData=get(huvmat,'UserData');%Data associated to the current uvmat interface
    0270 hhuvmat=guidata(huvmat); %handles of GUI elements in uvmat
    0271 hplot=findobj(huvmat,'Tag','axes3');%main plotting axis of uvmat
    0272 h_menu_coord=findobj(huvmat,'Tag','menu_coord');
    0273 menu=get(h_menu_coord,'String');
    0274 choice=get(h_menu_coord,'Value');
    0275 if iscell(menu)
    0276     option=menu{choice};
    0277 else
    0278     option='px'; %default
    0279 end
    0280 Coord_cell=get(handles.ListCoord,'String');
    0281 ObjectData=read_geometry_calib(Coord_cell);
    0282 %ObjectData=read_geometry_calib(handles);%read the interface input parameters defining the object
    0283 if isequal(option,'phys')
    0284     ObjectData.Coord=ObjectData.Coord(:,[1:3]);
    0285 elseif isequal(option,'px')||isequal(option,'')
    0286     ObjectData.Coord=ObjectData.Coord(:,[4:5]);
    0287 else
    0288     warndlg_uvmat('the choice in menu_coord of uvmat must be px or phys ','ERROR')
    0289 end
    0290 axes(hhuvmat.axes3)
    0291 hh=findobj('Tag','calib_points');
    0292 if isempty(hh)
    0293     hh=line(ObjectData.Coord(:,1),ObjectData.Coord(:,2),'Color','m','Tag','calib_points','LineStyle','.','Marker','+');
    0294 else
    0295     set(hh,'XData',ObjectData.Coord(:,1))
    0296     set(hh,'YData',ObjectData.Coord(:,2))
    0297 end
    0298
    0299 % --- Executes on button press in MenuCoord.
    0300 function MenuCoord_Callback(hObject, eventdata, handles)
    0301
    0302
    0303 % --- Executes on button press in delete.
    0304 function delete_Callback(hObject, eventdata, handles)
    0305 SetData=get(gcbf,'UserData');%get the interface data
    0306 IndexObj=SetData.IndexObj;
    0307 delete_object(IndexObj);
    0308
    0309
    0310 %------------------------------------------------------------------
    0311 % --- Executes on button press in calibrate_lin.
    0312 function APPLY_Callback(hObject, eventdata, handles)
    0313 %------------------------------------------------------------------
    0314 calib_cell=get(handles.calib_type,'String');
    0315 val=get(handles.calib_type,'Value');
    0316 calib_type=calib_cell{val};
    0317 Coord_cell=get(handles.ListCoord,'String');
    0318 Object=read_geometry_calib(Coord_cell);
    0319
    0320 if isequal(calib_type,'rescale')
    0321     GeometryCalib=calib_rescale(Object.Coord);
    0322 elseif isequal(calib_type,'linear')
    0323     GeometryCalib=calib_linear(Object.Coord);
    0324 elseif isequal(calib_type,'tsai')
    0325     GeometryCalib=calib_tsai(Object.Coord);
    0326 end
    0327 unitlist=get(handles.CoordUnit,'String');
    0328 unit=unitlist{get(handles.CoordUnit,'value')};
    0329 GeometryCalib.CoordUnit=unit;
    0330
    0331 huvmat=findobj(allchild(0),'Name','uvmat');
    0332 hhuvmat=guidata(huvmat);%handles of elements in the GUI uvmat
    0333 RootPath='';
    0334 RootFile='';
    0335 if ~isempty(hhuvmat.RootPath)& ~isempty(hhuvmat.RootFile)
    0336     testhandle=1;
    0337     RootPath=get(hhuvmat.RootPath,'String');
    0338     RootFile=get(hhuvmat.RootFile,'String');
    0339     filebase=fullfile(RootPath,RootFile);
    0340     outputfile=[filebase '.xml'];
    0341 else
    0342     question={'save the calibration data and point coordinates in'};
    0343     def={fullfile(RootPath,['ObjectCalib.xml'])};
    0344     options.Resize='on';
    0345     answer=inputdlg(question,'save average in a new file',1,def,options);
    0346     outputfile=answer{1};
    0347 end
    0348 testappend=0;
    0349 if exist(outputfile,'file');%=1 if the output file already exists, 0 else
    0350     t=xmltree(outputfile); %read the file
    0351     uid=find(t,'ImaDoc');
    0352     if ~isequal(uid,1)%if the xml file is not ImaDoc, delete it (after backup)
    0353         backupfile=outputfile;
    0354         testexist=2;
    0355         while testexist==2
    0356             backupfile=[backupfile '~'];
    0357             testexist=exist(backupfile,'file');
    0358         end
    0359         [success,message]=copyfile(outputfile,backupfile);%make backup
    0360         if isequal(success,1)
    0361             delete(outputfile)
    0362         else
    0363             return
    0364         end
    0365     else
    0366         uid_calib=find(t,'ImaDoc/GeometryCalib');
    0367         if ~isempty(uid) %if GeometryCalib already exists, delete its content
    0368             backupfile=outputfile;
    0369             testexist=2;
    0370             while testexist==2
    0371                backupfile=[backupfile '~'];
    0372                testexist=exist(backupfile,'file');
    0373             end
    0374             [success,message]=copyfile(outputfile,backupfile);%make backup
    0375             if isequal(success,1)
    0376                 delete(outputfile)
    0377             else
    0378                 return
    0379             end
    0380             uid_child=children(t,uid_calib);
    0381             t=delete(t,uid_child);
    0382             testappend=1;
    0383         end
    0384     end
    0385 end
    0386 if ~testappend
    0387     t=xmltree;
    0388     t=set(t,1,'name','ImaDoc');
    0389     [t,uid_calib]=add(t,1,'element','GeometryCalib');
    0390 %     t=struct2xml(GeometryCalib,t,uid_calib);
    0391 end
    0392 % hgrid=get(handles.REPLICATE,'parent');%read the calibration image source on the interface userdata
    0393 % imagename=get(hgrid,'UserData');
    0394 % if exist(imagename,'file')
    0395 %     GeometryCalib.SourceCalib.ImageCalib=imagename;
    0396 % end
    0397 GeometryCalib.SourceCalib.PointCoord=Object.Coord;
    0398 t=struct2xml(GeometryCalib,t,uid_calib);
    0399 save(t,outputfile);
    0400 warndlg_uvmat({[outputfile ' updated with calibration data'];...
    0401     ['Error rms (along x,y)=' num2str(GeometryCalib.ErrorRms) ' pixels'];...
    0402     ['Error max (along x,y)=' num2str(GeometryCalib.ErrorMax) ' pixels']},'CONFIRMATION')
    0403
    0404 %display image with new calibration in the currently opened uvmat interface
    0405 hhh=findobj(hhuvmat.axes3,'Tag','calib_marker');% delete calib points and markers
    0406 if ~isempty(hhh)
    0407     delete(hhh);
    0408 end
    0409 hhh=findobj(hhuvmat.axes3,'Tag','calib_points');
    0410 if ~isempty(hhh)
    0411     delete(hhh);
    0412 end
    0413 set(hhuvmat.FixedLimits,'Value',0)% put FixedLimits option to 'off'
    0414 set(hhuvmat.FixedLimits,'BackgroundColor',[0.7 0.7 0.7])
    0415 uvmat('RootPath_Callback',hObject,eventdata,hhuvmat); %file input with xml reading  in uvmat
    0416
    0417 %------------------------------------------------------------------
    0418 % --- Executes on button press in calibrate_lin.
    0419 function REPLICATE_Callback(hObject, eventdata, handles)
    0420 %------------------------------------------------------------------
    0421 calib_cell=get(handles.calib_type,'String');
    0422 val=get(handles.calib_type,'Value');
    0423 calib_type=calib_cell{val};
    0424 Coord_cell=get(handles.ListCoord,'String');
    0425 Object=read_geometry_calib(Coord_cell);
    0426
    0427 if isequal(calib_type,'rescale')
    0428     GeometryCalib=calib_rescale(Object.Coord);
    0429 elseif isequal(calib_type,'linear')
    0430     GeometryCalib=calib_linear(Object.Coord);
    0431 elseif isequal(calib_type,'tsai')
    0432     GeometryCalib=calib_tsai(Object.Coord);
    0433 end
    0434 % %record image source
    0435 % hgrid=get(handles.REPLICATE,'parent');%read the calibration image source on the interface userdata
    0436 % imagename=get(hgrid,'UserData');
    0437 % if exist(imagename,'file')
    0438 %     GeometryCalib.SourceCalib.ImageCalib=imagename;
    0439 % end
    0440 GeometryCalib.SourceCalib.PointCoord=Object.Coord;
    0441
    0442 %open and read the dataview GUI
    0443 h_dataview=findobj(allchild(0),'name','dataview');
    0444 Device=[];%default
    0445 if isempty(h_dataview)
    0446     h_dataview=dataview;
    0447     hhdataview=guidata(h_dataview);
    0448     drawnow
    0449     hGUI=get(handles.REPLICATE,'parent');%read the calibration image source on the interface userdata
    0450     Heading=get(hGUI,'UserData');
    0451     if isfield(Heading,'XmlInput')
    0452         XmlInput=fileparts(Heading.XmlInput);
    0453         [XmlInput,filename,ext]=fileparts(XmlInput);
    0454     end
    0455     if isfield(Heading,'Record') && isequal([filename ext],Heading.Record)
    0456         [XmlInput,filename,ext]=fileparts(XmlInput);
    0457     end
    0458     if isfield(Heading,'Device') && isequal([filename ext],Heading.Device)
    0459         [XmlInput,filename,ext]=fileparts(XmlInput);
    0460         Device=Heading.Device;
    0461     end
    0462     if isfield(Heading,'Experiment') && isequal([filename ext],Heading.Experiment)
    0463         [PP,filename,ext]=fileparts(XmlInput);
    0464     end
    0465     testinput=0;
    0466     if isfield(Heading,'SubCampaign') && isequal([filename ext],Heading.SubCampaign)
    0467         set(hhdataview.RootDirectory,'String',XmlInput)
    0468         set(hhdataview.SubCampaignTest,'Value',1)
    0469         testinput=1;
    0470     elseif isfield(Heading,'Campaign') && isequal([filename ext],Heading.Campaign)
    0471         set(hhdataview.RootDirectory,'String',XmlInput)
    0472         set(hhdataview.SubCampaignTest,'Value',0)
    0473         testinput=1;
    0474     end
    0475     if testinput
    0476         dataview('RootDirectory_Callback',hObject,eventdata,hhdataview)
    0477         ListDevices=get(hhdataview.ListDevices,'String');
    0478         for ilist=1:length(ListDevices)
    0479             if isequal(ListDevices{ilist},Device)
    0480                 set(hhdataview.ListDevices,'Value',ilist)
    0481                 dataview('ListDevices_Callback',hObject,eventdata,hhdataview)
    0482                 break
    0483             end
    0484         end
    0485     end
    0486     return
    0487 end
    0488
    0489 hhdataview=guidata(h_dataview);
    0490 CurrentPath=get(hhdataview.RootDirectory,'String');
    0491 ListExperiments=get(hhdataview.ListExperiments,'String');
    0492 Value=get(hhdataview.ListExperiments,'Value');
    0493 if ~isequal(Value,1)
    0494     ListExperiments=ListExperiments(Value);
    0495 end
    0496 ListDevices=get(hhdataview.ListDevices,'String');
    0497 Value=get(hhdataview.ListDevices,'Value');
    0498 if isequal(Value,1)
    0499     warndlg_uvmat('manually select in the GUI dataview the device being calibrated','ERROR')
    0500     return
    0501 else
    0502     ListDevices=ListDevices(Value);
    0503 end
    0504 ListRecords=get(hhdataview.ListRecords,'String');
    0505 Value=get(hhdataview.ListRecords,'Value');
    0506 if ~isequal(Value,1)
    0507     ListRecords=ListRecords(Value);
    0508 end
    0509 [ListDevices,ListRecords,ListXml,List]=ListDir(CurrentPath,ListExperiments,ListDevices,ListRecords);
    0510 ListXml=get(hhdataview.ListXml,'String');
    0511 Value=get(hhdataview.ListXml,'Value');
    0512 if isequal(Value,1)
    0513     warndlg_uvmat('you need to select in the GUI dataview the xml files to edit','ERROR')
    0514     return
    0515 else
    0516     ListXml=ListXml(Value);
    0517 end
    0518
    0519 %update all the selected xml files
    0520 answer=msgbox_uvmat('INPUT_Y-N',[num2str(length(Value)) ' xml files for device ' ListDevices{1} ' will be refreshed with ' calib_type ' calibration data']);
    0521 if ~isequal(answer{1},'OK')
    0522     return
    0523 end
    0524 % 'TEST'
    0525 % List
    0526 % return
    0527 for iexp=1:length(List.Experiment)
    0528     ExpName=List.Experiment{iexp}.name;
    0529     if isfield(List.Experiment{iexp},'Device')
    0530         for idevice=1:length(List.Experiment{iexp}.Device)
    0531             DeviceName=List.Experiment{iexp}.Device{idevice}.name;
    0532             if isfield(List.Experiment{iexp}.Device{idevice},'xmlfile')
    0533                 for ixml=1:length(List.Experiment{iexp}.Device{idevice}.xmlfile)
    0534                     FileName=List.Experiment{iexp}.Device{idevice}.xmlfile{ixml};
    0535                     for ilistxml=1:length(ListXml)
    0536                         if isequal(FileName,ListXml{ilistxml})
    0537                             set(hhdataview.ListXml,'Value',Value(ilistxml))
    0538                             drawnow
    0539                             xmlfullname=fullfile(CurrentPath,ExpName,DeviceName,FileName);
    0540                             update_imadoc(GeometryCalib,xmlfullname)
    0541                             break
    0542                         end
    0543                     end
    0544                 end
    0545              elseif isfield(List.Experiment{iexp}.Device{idevice},'Record')
    0546                 for irecord=1:length(List.Experiment{iexp}.Device{idevice}.Record)
    0547                     RecordName=List.Experiment{iexp}.Device{idevice}.Record{irecord}.name;
    0548                     if isfield(List.Experiment{iexp}.Device{idevice}.Record{irecord},'xmlfile')
    0549                         for ixml=1:length(List.Experiment{iexp}.Device{idevice}.Record{irecord}.xmlfile)
    0550                             FileName=List.Experiment{iexp}.Device{idevice}.Record{irecord}.xmlfile{ixml};
    0551                             for ilistxml=1:length(ListXml)
    0552                                 if isequal(FileName,ListXml{ilistxml})
    0553                                     set(hhdataview.ListXml,'Value',Value(ilistxml))
    0554                                     drawnow
    0555                                     xmlfullname=fullfile(CurrentPath,ExpName,DeviceName,RecordName,FileName);
    0556                                     update_imadoc(GeometryCalib,xmlfullname)
    0557                                     break
    0558                                 end
    0559                             end
    0560                         end
    0561                     end
    0562                 end
    0563             end
    0564         end
    0565     end
    0566 end
    0567 set(hhdataview.ListXml,'Value',Value)
    0568
    0569
    0570 %-------------------------------------------------------------
    0571 function update_imadoc(GeometryCalib,outputfile)
    0572 testappend=0;
    0573 if exist(outputfile,'file');%=1 if the output file already exists, 0 else
    0574     t=xmltree(outputfile); %read the file
    0575     uid=find(t,'ImaDoc');
    0576     if isequal(uid,1)%if the xml file is  ImaDoc
    0577         uid_calib=find(t,'ImaDoc/GeometryCalib');
    0578         if ~isempty(uid) %if GeometryCalib already exists, delete its content
    0579             backupfile=outputfile;
    0580             testexist=2;
    0581             while testexist==2
    0582                backupfile=[backupfile '~'];
    0583                testexist=exist(backupfile,'file');
    0584             end
    0585             [success,message]=copyfile(outputfile,backupfile);%make backup
    0586             if isequal(success,1)
    0587                 delete(outputfile)
    0588             else
    0589                 return
    0590             end
    0591             uid_child=children(t,uid_calib);
    0592             t=delete(t,uid_child);
    0593             testappend=1;
    0594         end
    0595     end
    0596 end
    0597 if ~testappend
    0598     t=xmltree;
    0599     t=set(t,1,'name','ImaDoc');
    0600     [t,uid_calib]=add(t,1,'element','GeometryCalib');
    0601 %     t=struct2xml(GeometryCalib,t,uid_calib);
    0602 end
    0603
    0604 t=struct2xml(GeometryCalib,t,uid_calib);
    0605 save(t,outputfile);
    0606
    0607
    0608 %-----------------------------------------------------------------
    0609 % determine the parameters for a calibration by an affine function (rescaling and offset, no rotation)
    0610 function GeometryCalib=calib_rescale(Coord)
    0611 %------------------------------------------------------------------
    0612
    0613 X=Coord(:,1);
    0614 Y=Coord(:,2);
    0615 x_ima=Coord(:,4);
    0616 y_ima=Coord(:,5);
    0617 [px,sx]=polyfit(X,x_ima,1);
    0618 [py,sy]=polyfit(Y,y_ima,1);
    0619 T_x=px(2);
    0620 T_y=py(2);
    0621 GeometryCalib.CalibrationType='rescale';
    0622 GeometryCalib.focal=1;
    0623 GeometryCalib.CoordUnit=[];% default value, to be updated by the calling function
    0624 GeometryCalib.Tx_Ty_Tz=[T_x T_y 1];
    0625 GeometryCalib.R=[px(1),0,0;0,py(1),0;0,0,1];
    0626
    0627 %check error
    0628 Calib.dpx=1;
    0629 Calib.dpy=1;
    0630 Calib.sx=1;
    0631 Calib.Cx=0;
    0632 Calib.Cy=0;
    0633 Calib.Tz=1;
    0634 Calib.kappa1=0;
    0635 Calib.f=GeometryCalib.focal;
    0636 Calib.Tx=T_x;
    0637 Calib.Ty=T_y;
    0638 Calib.R=GeometryCalib.R;
    0639 [Xpoints,Ypoints]=px_XYZ(Calib,X,Y,0);
    0640 GeometryCalib.ErrorRms(1)=sqrt(mean((Xpoints-x_ima).*(Xpoints-x_ima)));
    0641 GeometryCalib.ErrorMax(1)=max(abs(Xpoints-x_ima));
    0642 GeometryCalib.ErrorRms(2)=sqrt(mean((Ypoints-y_ima).*(Ypoints-y_ima)));
    0643 GeometryCalib.ErrorMax(2)=max(abs(Ypoints-y_ima));
    0644
    0645
    0646 %------------------------------------------------------------------
    0647 % determine the parameters for a calibration by a linear transform matrix (rescale and rotation)
    0648 function GeometryCalib=calib_linear(Coord)
    0649 %------------------------------------------------------------------
    0650 X=Coord(:,1);
    0651 Y=Coord(:,2);
    0652 x_ima=Coord(:,4);
    0653 y_ima=Coord(:,5);
    0654 XY_mat=[ones(size(X)) X Y];
    0655 a_X1=XY_mat\x_ima; %transformation matrix for X
    0656 x1=XY_mat*a_X1;%reconstruction
    0657 err_X1=max(abs(x1-x_ima));%error
    0658 a_Y1=XY_mat\y_ima;%transformation matrix for X
    0659 y1=XY_mat*a_Y1;
    0660 err_Y1=max(abs(y1-y_ima));%error
    0661 T_x=a_X1(1);
    0662 T_y=a_Y1(1);
    0663 GeometryCalib.CalibrationType='linear';
    0664 GeometryCalib.focal=1;
    0665 GeometryCalib.CoordUnit=[];% default value, to be updated by the calling function
    0666 GeometryCalib.Tx_Ty_Tz=[T_x T_y 1];
    0667 GeometryCalib.R=[a_X1(2),a_X1(3),0;a_Y1(2),a_Y1(3),0;0,0,1];
    0668
    0669 %check error
    0670 GeometryCalib.ErrorRms(1)=sqrt(mean((x1-x_ima).*(x1-x_ima)));
    0671 GeometryCalib.ErrorMax(1)=max(abs(x1-x_ima));
    0672 GeometryCalib.ErrorRms(2)=sqrt(mean((y1-y_ima).*(y1-y_ima)));
    0673 GeometryCalib.ErrorMax(2)=max(abs(y1-y_ima));
    0674
    0675
    0676
    0677
    0678 %------------------------------------------------------------------
    0679 function GeometryCalib=calib_tsai(Coord)
    0680 %------------------------------------------------------------------
    0681 %TSAI
    0682 % 'calibration_lin' provides a linear transform on coordinates,
    0683 path_uvmat=which('uvmat');% check the path detected for source file uvmat
    0684 path_UVMAT=fileparts(path_uvmat); %path to UVMAT
    0685 if isunix
    0686     %fid = fopen(fullfile(path_UVMAT,'PARAM_LINUX.txt'),'r');%open the file with civ binary names
    0687     xmlfile=fullfile(path_UVMAT,'PARAM_LINUX.xml');
    0688     if exist(xmlfile,'file')
    0689         t=xmltree(xmlfile);
    0690         sparam=convert(t);
    0691     end
    0692 else
    0693     %fid = fopen(fullfile(path_UVMAT,'PARAM_WIN.txt'),'r');%open the file with civ binary names
    0694     xmlfile=fullfile(path_UVMAT,'PARAM_WIN.xml');
    0695     if exist(xmlfile,'file')
    0696         t=xmltree(xmlfile);
    0697         sparam=convert(t);
    0698     end
    0699 end
    0700 if ~isfield(sparam,'GeometryCalib_exe')
    0701     warndlg_uvmat(['calibration program <GeometryCalib_exe> undefined in parameter file ' xmlfile],'ERROR')
    0702     return
    0703 end
    0704 Tsai_exe=sparam.GeometryCalib_exe;
    0705 if ~exist(Tsai_exe,'file')
    0706     warndlg_uvmat(['calibration program ' Tsai_exe ' does not exist'],'ERROR')
    0707     return
    0708 end
    0709
    0710 textcoord=num2str(Coord,4);
    0711 dlmwrite('t.txt',textcoord,'');
    0712 % ['!' Tsai_exe ' -f1 0 -f2 t.txt']
    0713     eval(['!' Tsai_exe ' -f t.txt > tsaicalib.log']);
    0714 if ~exist('calib.dat','file')
    0715     warndlg_uvmat('no output from calibration program Tsai_exe: possibly too few points','ERROR')
    0716 end
    0717 calibdat=dlmread('calib.dat');
    0718 GeometryCalib.CalibrationType='tsai';
    0719 GeometryCalib.focal=calibdat(10);
    0720 GeometryCalib.dpx_dpy=[calibdat(5) calibdat(6)];
    0721 GeometryCalib.Cx_Cy=[calibdat(7) calibdat(8)];
    0722 GeometryCalib.sx=calibdat(9);
    0723 GeometryCalib.kappa1=calibdat(11);
    0724 GeometryCalib.CoordUnit=[];% default value, to be updated by the calling function
    0725 GeometryCalib.Tx_Ty_Tz=[calibdat(12) calibdat(13) calibdat(14)];
    0726 Rx_Ry_Rz=calibdat([15:17]);
    0727 sa = sin(Rx_Ry_Rz(1)) ;
    0728 ca=cos(Rx_Ry_Rz(1));
    0729 sb=sin(Rx_Ry_Rz(2));
    0730 cb =cos(Rx_Ry_Rz(2));
    0731 sg =sin(Rx_Ry_Rz(3));
    0732 cg =cos(Rx_Ry_Rz(3));
    0733 r1 = cb * cg;
    0734 r2 = cg * sa * sb - ca * sg;
    0735 r3 = sa * sg + ca * cg * sb;
    0736 r4 = cb * sg;
    0737 r5 = sa * sb * sg + ca * cg;
    0738 r6 = ca * sb * sg - cg * sa;
    0739 r7 = -sb;
    0740 r8 = cb * sa;
    0741 r9 = ca * cb;
    0742 %EN DEDUIRE MATRICE R ??
    0743 GeometryCalib.R=[r1,r2,r3;r4,r5,r6;r7,r8,r9];
    0744 %erreur a caracteriser?
    0745 %check error
    0746 Calib.dpx=GeometryCalib.dpx_dpy(1);
    0747 Calib.dpy=GeometryCalib.dpx_dpy(2);
    0748 Calib.sx=GeometryCalib.sx;
    0749 Calib.Cx=GeometryCalib.Cx_Cy(1);
    0750 Calib.Cy=GeometryCalib.Cx_Cy(2);
    0751 Calib.kappa1=GeometryCalib.kappa1;
    0752 Calib.f=GeometryCalib.focal;
    0753 Calib.Tx=GeometryCalib.Tx_Ty_Tz(1);
    0754 Calib.Ty=GeometryCalib.Tx_Ty_Tz(2);
    0755 Calib.Tz=GeometryCalib.Tx_Ty_Tz(3);
    0756 Calib.R=GeometryCalib.R;
    0757 X=Coord(:,1);
    0758 Y=Coord(:,2);
    0759 Z=Coord(:,3);
    0760 x_ima=Coord(:,4);
    0761 y_ima=Coord(:,5);
    0762 [Xpoints,Ypoints]=px_XYZ(Calib,X,Y,Z);
    0763
    0764 GeometryCalib.ErrorRms(1)=sqrt(mean((Xpoints-x_ima).*(Xpoints-x_ima)));
    0765 GeometryCalib.ErrorMax(1)=max(abs(Xpoints-x_ima));
    0766 GeometryCalib.ErrorRms(2)=sqrt(mean((Ypoints-y_ima).*(Ypoints-y_ima)));
    0767 GeometryCalib.ErrorMax(2)=max(abs(Ypoints-y_ima));
    0768 % Nfx
    0769 % dx
    0770 % dy
    0771 % 5 dpx
    0772 % 6 dpy
    0773 % cx
    0774 % cy
    0775 % sx
    0776 % f
    0777 % kappa1
    0778 % tx
    0779 % ty
    0780 % tz
    0781 % rx
    0782 % ry
    0783 % rz
    0784 % p1
    0785 % p2
    0786
    0787 %calibcoeff=str2num(calibdat)
    0788
    0789 % --- Executes on button press in translation.
    0790 function translation_Callback(hObject, eventdata, handles)
    0791
    0792
    0793 function T_x_Callback(hObject, eventdata, handles)
    0794 % hObject    handle to T_x (see GCBO)
    0795 % eventdata  reserved - to be defined in a future version of MATLAB
    0796 % handles    structure with handles and user data (see GUIDATA)
    0797
    0798 % Hints: get(hObject,'String') returns contents of T_x as text
    0799 %        str2double(get(hObject,'String')) returns contents of T_x as a double
    0800
    0801
    0802
    0803
    0804
    0805 function T_y_Callback(hObject, eventdata, handles)
    0806 % hObject    handle to T_y (see GCBO)
    0807 % eventdata  reserved - to be defined in a future version of MATLAB
    0808 % handles    structure with handles and user data (see GUIDATA)
    0809
    0810 % Hints: get(hObject,'String') returns contents of T_y as text
    0811 %        str2double(get(hObject,'String')) returns contents of T_y as a double
    0812
    0813
    0814 function T_z_Callback(hObject, eventdata, handles)
    0815 % hObject    handle to T_z (see GCBO)
    0816 % eventdata  reserved - to be defined in a future version of MATLAB
    0817 % handles    structure with handles and user data (see GUIDATA)
    0818
    0819 % Hints: get(hObject,'String') returns contents of T_z as text
    0820 %        str2double(get(hObject,'String')) returns contents of T_z as a double
    0821
    0822
    0823 % --- Executes on button press in rotation.
    0824 function rotation_Callback(hObject, eventdata, handles)
    0825 angle_rot=(pi/180)*str2num(get(handles.Phi,'String'));
    0826 Coord_cell=get(handles.ListCoord,'String');
    0827 data=read_geometry_calib(Coord_cell);
    0828 data.Coord(:,1)=cos(angle_rot)*data.Coord(:,1)+sin(angle_rot)*data.Coord(:,2);
    0829 data.Coord(:,1)=-sin(angle_rot)*data.Coord(:,1)+cos(angle_rot)*data.Coord(:,2);
    0830 set(handles.XObject,'String',num2str(data.Coord(:,1),4));
    0831 set(handles.YObject,'String',num2str(data.Coord(:,2),4));
    0832
    0833
    0834 function XImage_Callback(hObject, eventdata, handles)
    0835 update_list(hObject, eventdata,handles)
    0836
    0837 function YImage_Callback(hObject, eventdata, handles)
    0838 update_list(hObject, eventdata,handles)
    0839
    0840 function XObject_Callback(hObject, eventdata, handles)
    0841 update_list(hObject, eventdata,handles)
    0842
    0843 function YObject_Callback(hObject, eventdata, handles)
    0844 update_list(hObject, eventdata,handles)
    0845
    0846 function ZObject_Callback(hObject, eventdata, handles)
    0847 update_list(hObject, eventdata,handles)
    0848
    0849 function update_list(hObject, eventdata, handles)
    0850 str4=get(handles.XImage,'String');
    0851 str5=get(handles.YImage,'String');
    0852 str1=get(handles.XObject,'String');
    0853 tt=double(str1);
    0854 str2=get(handles.YObject,'String');
    0855 str3=get(handles.ZObject,'String');
    0856 if ~isempty(str1) & ~isequal(double(str1),32) & (isempty(str3)|isequal(double(str3),32))
    0857     str3='0';%put z to 0 by default
    0858 end
    0859 strline=[str1 '    |    ' str2 '    |    ' str3 '    |    ' str4 '    |    ' str5];
    0860 Coord=get(handles.ListCoord,'String');
    0861 val=get(handles.ListCoord,'Value');
    0862 Coord{val}=strline;
    0863 set(handles.ListCoord,'String',Coord)
    0864
    0865 %--------------------------------------------------------------------
    0866 % --- Executes on selection change in ListCoord.
    0867 %--------------------------------------------------------------------
    0868 function ListCoord_Callback(hObject, eventdata, handles)
    0869 % hObject    handle to ListCoord (see GCBO)
    0870 % eventdata  reserved - to be defined in a future version of MATLAB
    0871 % handles    structure with handles and user data (see GUIDATA)
    0872
    0873 % Hints: contents = get(hObject,'String') returns ListCoord contents as cell array
    0874 %        contents{get(hObject,'Value')} returns selected item from ListCoord
    0875 %set(handles.edit_append,'Value',2); %set to edit mode
    0876 Coord_cell=get(handles.ListCoord,'String');
    0877 val=get(handles.ListCoord,'Value');
    0878 if length(Coord_cell)>0
    0879     coord_str=Coord_cell{val};
    0880     k=findstr('|',coord_str);
    0881     if isempty(k)
    0882         return
    0883     end
    0884     set(handles.XObject,'String',coord_str(1:k(1)-5))
    0885     set(handles.YObject,'String',coord_str(k(1)+5:k(2)-5))
    0886     set(handles.ZObject,'String',coord_str(k(2)+5:k(3)-5))
    0887     set(handles.XImage,'String',coord_str(k(3)+5:k(4)-5))
    0888     set(handles.YImage,'String',coord_str(k(4)+5:end))
    0889     huvmat=findobj(allchild(0),'Name','uvmat');%find the current uvmat interface handle
    0890     hplot=findobj(huvmat,'Tag','axes3');%main plotting axis of uvmat
    0891     h_menu_coord=findobj(huvmat,'Tag','menu_coord');
    0892     menu=get(h_menu_coord,'String');
    0893     choice=get(h_menu_coord,'Value');
    0894     if iscell(menu)
    0895         option=menu{choice};
    0896     else
    0897         option='px'; %default
    0898     end
    0899     if isequal(option,'phys')
    0900         XCoord=str2num(coord_str(1:k(1)-5));
    0901         YCoord=str2num(coord_str(k(1)+5:k(2)-5));
    0902     elseif isequal(option,'px')|| isequal(option,'')
    0903         XCoord=str2num(coord_str(k(3)+5:k(4)-5));
    0904         YCoord=str2num(coord_str(k(4)+5:end));
    0905     else
    0906         warndlg_uvmat('the choice in menu_coord of uvmat must be px or phys ','ERROR')
    0907     end
    0908     huvmat=findobj(allchild(0),'Name','uvmat');%find the current uvmat interface handle
    0909     hplot=findobj(huvmat,'Tag','axes3');%main plotting axis of uvmat
    0910     hhh=findobj(hplot,'Tag','calib_marker');
    0911     if isempty(hhh)
    0912         axes(hplot)
    0913         line(XCoord,YCoord,'Color','m','Tag','calib_marker','LineStyle','.','Marker','o','MarkerSize',20);
    0914     else
    0915         set(hhh,'XData',XCoord)
    0916         set(hhh,'YData',YCoord)
    0917     end
    0918 end
    0919 %------------------------------------------------------
    0920 % --- Executes on button press in translation_plus.
    0921 function translation_plus_Callback(hObject, eventdata, handles)
    0922
    0923 T=[0 0 0];
    0924 T_x=get(handles.T_x,'String');
    0925 T_y=get(handles.T_y,'String');
    0926 T_z=get(handles.T_z,'String');
    0927 if ~isempty(T_x)
    0928     T(1)=str2num(T_x);
    0929 end
    0930 if ~isempty(T_y)
    0931     T(2)=str2num(T_y);
    0932 end
    0933 if ~isempty(T_z)
    0934     T(3)=str2num(T_z);
    0935 end
    0936 translation(handles,T)
    0937
    0938
    0939
    0940 % --- Executes on button press in translation_minus.
    0941 function translation_minus_Callback(hObject, eventdata, handles)
    0942
    0943 T=[0 0 0];
    0944 T_x=get(handles.T_x,'String');
    0945 T_y=get(handles.T_y,'String');
    0946 T_z=get(handles.T_z,'String');
    0947 if ~isempty(T_x)
    0948     T(1)=-str2num(T_x);
    0949 end
    0950 if ~isempty(T_y)
    0951     T(2)=-str2num(T_y);
    0952 end
    0953 if ~isempty(T_z)
    0954     T(3)=-str2num(T_z);
    0955 end
    0956 translation(handles,T)
    0957
    0958
    0959 %%%--------------------------------------
    0960 function translation(handles,T)
    0961 Coord_cell=get(handles.ListCoord,'String');
    0962 data=read_geometry_calib(Coord_cell);
    0963 % data=read_geometry_calib(handles);
    0964 data.Coord(:,1)=T(1)+data.Coord(:,1);
    0965 data.Coord(:,2)=T(2)+data.Coord(:,2);
    0966 data.Coord(:,3)=T(3)+data.Coord(:,3);
    0967 data.Coord(:,[4 5])=data.Coord(:,[4 5]);
    0968 for i=1:size(data.Coord,1)
    0969     for j=1:5
    0970           Coord{i,j}=num2str(data.Coord(i,j),4);%phys x,y,z
    0971    end
    0972 end
    0973 Tabchar=cell2tab(Coord,'    |    ');
    0974 set(handles.ListCoord,'Value',1)
    0975 set(handles.ListCoord,'String',Tabchar)
    0976
    0977 %----------------------------------------------------
    0978 % --- Executes on button press in rotation_plus.
    0979 function rotation_plus_Callback(hObject, eventdata, handles)
    0980 Phi=0;
    0981 Phi=get(handles.Phi,'String');
    0982 if ~isempty(Phi)
    0983     Phi=str2num(Phi);
    0984 end
    0985 rotation(handles,Phi)
    0986
    0987 %-------------------------------------------------
    0988 % --- Executes on button press in rotation_minus.
    0989 function rotation_minus_Callback(hObject, eventdata, handles)
    0990 Phi=0;
    0991 Phi=get(handles.Phi,'String');
    0992 if ~isempty(Phi)
    0993     Phi=-str2num(Phi);
    0994 end
    0995 rotation(handles,Phi)
    0996
    0997 %-----------------------------------------------------
    0998 %rotation
    0999 function rotation(handles,Phi)
    1000 O_x=str2num(get(handles.O_x,'String'));
    1001 O_y=str2num(get(handles.O_y,'String'));
    1002 if isempty(O_x)
    1003     O_x=0;%default
    1004 end
    1005 if isempty(O_y)
    1006     O_y=0;%default
    1007 end
    1008 Coord_cell=get(handles.ListCoord,'String');
    1009 data=read_geometry_calib(Coord_cell);
    1010 %data=read_geometry_calib(handles);
    1011 r1=cos(pi*Phi/180);
    1012 r2=-sin(pi*Phi/180);
    1013 r3=sin(pi*Phi/180);
    1014 r4=cos(pi*Phi/180);
    1015 x=data.Coord(:,1);
    1016 y=data.Coord(:,2);
    1017 data.Coord(:,1)=r1*x+r2*y;
    1018 data.Coord(:,2)=r3*x+r4*y;
    1019 % data.Coord(:,[4 5])=data.Coord(:,[4 5]);
    1020 for i=1:size(data.Coord,1)
    1021     for j=1:5
    1022           Coord{i,j}=num2str(data.Coord(i,j),4);%phys x,y,z
    1023    end
    1024 end
    1025 Tabchar=cell2tab(Coord,'    |    ');
    1026 set(handles.ListCoord,'Value',1)
    1027 set(handles.ListCoord,'String',Tabchar)
    1028
    1029
    1030 function O_x_Callback(hObject, eventdata, handles)
    1031
    1032
    1033 function O_y_Callback(hObject, eventdata, handles)
    1034
    1035
    1036 function O_z_Callback(hObject, eventdata, handles)
    1037
    1038
    1039 % --- Executes on selection change in edit_append.
    1040 function edit_append_Callback(hObject, eventdata, handles)
    1041 % val=get(handles.PLOT_append,'Value');
    1042 % if isequal(val,2); %append mode
    1043 %     %appeler mouse
    1044 % end
    1045 choice=get(handles.edit_append,'Value');
    1046 if choice==1
    1047        Coord=get(handles.ListCoord,'String');
    1048        val=length(Coord);
    1049        if val>=1 & isequal(Coord{val},'')
    1050             val=val-1; %do not take into account blank
    1051        end
    1052        Coord{val+1}='';
    1053        set(handles.ListCoord,'String',Coord)
    1054        set(handles.ListCoord,'Value',val+1)
    1055 end
    1056
    1057
    1058 %A REVOIR
    1059 % if choice==2
    1060 %     %display image with px coordinates
    1061 %     hrootpath=findobj(huvmat,'Tag','RootPath');
    1062 %     hrootfile=findobj(huvmat,'Tag','RootFile');
    1063 %     RootPath='';
    1064 %     RootFile='';
    1065 % %     if ~isempty(hrootpath)& ~isempty(hrootfile)
    1066 %         testhandle=1;
    1067 %         RootPath=get(hrootpath,'String');
    1068 %         RootFile=get(hrootfile,'String');
    1069 % %         filebase=fullfile(RootPath,RootFile);
    1070 % %         outputfile=[filebase '.xml'];
    1071 %         Indices=get(findobj(huvmat,'Tag','FileIndex'),'String');
    1072 %         Ext=get(findobj(huvmat,'Tag','FileExt'),'String');
    1073 %         imagename=[fullfile(RootPath,RootFile) Indices Ext];
    1074 %         % input.menu_coord=1;
    1075 %          h_menu_coord=findobj(huvmat,'Tag','menu_coord');
    1076 %         set(h_menu_coord,'Value',3)
    1077 %         huvmat=uvmat(imagename);%open uvmat, set phys coord (Value 1)
    1078 %
    1079 % %     end
    1080 % end
    1081
    1082 function NEW_Callback(hObject, eventdata, handles)
    1083 %A METTRE SOUS UN BOUTON
    1084 huvmat=findobj(allchild(0),'Name','uvmat');
    1085 hchild=get(huvmat,'children');
    1086 hcoord=findobj(hchild,'Tag','menu_coord');
    1087 coordtype=get(hcoord,'Value');
    1088 haxes=findobj(hchild,'Tag','axes3');
    1089 AxeData=get(haxes,'UserData');
    1090 if ~isequal(hcoord,2)
    1091     set(hcoord,'Value',2)
    1092     huvmat=uvmat(AxeData);
    1093     'relancer uvmat'
    1094 end
    1095 if ~isfield(AxeData,'ZoomAxes')
    1096     warndlg_uvmat('first draw a window around a grid marker','ERRROR')
    1097     return
    1098 end
    1099 XLim=get(AxeData.ZoomAxes,'XLim');
    1100 YLim=get(AxeData.ZoomAxes,'YLim');
    1101 np=size(AxeData.A);
    1102 ind_sub_x=round(XLim)
    1103 ind_sub_y=np(1)-round(YLim)
    1104 Mfiltre=AxeData.A([ind_sub_y(2):ind_sub_y(1)] ,ind_sub_x,:);
    1105 Mfiltre_norm=double(Mfiltre);
    1106 Mfiltre_norm=Mfiltre_norm/sum(sum(Mfiltre_norm));
    1107 Mfiltre_norm=100*(Mfiltre_norm-mean(mean(Mfiltre_norm)));
    1108 Atype=class(AxeData.A)
    1109 Data.NbDim=2;
    1110 Data.A=filter2(Mfiltre_norm,double(AxeData.A));
    1111 Data.A=feval(Atype,Data.A);
    1112 Data.AName='image';
    1113 Data.AX=AxeData.AX;
    1114 Data.AY=AxeData.AY;
    1115 Data.CoordType='px';
    1116 plot_field(Data)
    1117
    1118
    1119 % --- Executes on button press in HELP.
    1120 function HELP_Callback(hObject, eventdata, handles)
    1121 % hObject    handle to HELP (see GCBO)
    1122 % eventdata  reserved - to be defined in a future version of MATLAB
    1123 % handles    structure with handles and user data (see GUIDATA)
    1124 path_to_uvmat=which ('uvmat');% check the path of uvmat
    1125 pathelp=fileparts(path_to_uvmat);
    1126     helpfile=fullfile(pathelp,'UVMAT_DOC','uvmat_doc.html');
    1127 if isempty(dir(helpfile)), warndlg_uvmat('The help file uvmat_doc.html needs to be put in the directory UVMAT/UVMAT_DOC','ERROR')
    1128 else
    1129    web([helpfile '#geometry_calib'])
    1130 end
    1131
    1132 %'key_press_fcn:' function activated when a key is pressed on the keyboard
    1133 %-----------------------------------
    1134 function key_press_fcn(hObject,eventdata,handles)
    1135 hh=get(hObject,'parent');
    1136 xx=double(get(hh,'CurrentCharacter')) %get the keyboard character
    1137
    1138 if isequal(xx,8)%move arrow right
    1139     Coord_cell=get(handles.ListCoord,'String');
    1140     data=read_geometry_calib(Coord_cell);
    1141     Coord=[]; %default
    1142     if isfield(data,'Coord')
    1143         Coord=data.Coord;
    1144     end
    1145     val=get(handles.ListCoord,'Value');
    1146     Coord(val,:)=[];%suppress the selected item in the list
    1147     CoordCell={};
    1148     for iline=1:size(Coord,1)
    1149         for j=1:5
    1150             CoordCell{iline,j}=num2str(Coord(iline,j),4);
    1151         end
    1152     end
    1153     Tabchar=cell2tab(CoordCell,'    |    ');%transform cells into table ready for display
    1154     val=min(size(Coord,1),val);
    1155     set(handles.ListCoord,'Value',max(val,1))
    1156     set(handles.ListCoord,'String',Tabchar)
    1157     ListCoord_Callback(hObject, eventdata, handles)
    1158 end
    1159
    1160
    1161 % --- Executes on button press in append_point.
    1162 function append_point_Callback(hObject, eventdata, handles)
    1163
    1164        Coord=get(handles.ListCoord,'String');
    1165        val=length(Coord);
    1166        if val>=1 & isequal(Coord{val},'')
    1167             val=val-1; %do not take into account blank
    1168        end
    1169        Coord{val+1}='';
    1170        set(handles.ListCoord,'String',Coord)
    1171        set(handles.ListCoord,'Value',val+1)
    1172
    1173
    1174