source: trunk/src/mouse_motion.m @ 159

Last change on this file since 159 was 158, checked in by sommeria, 13 years ago

CoordType? not used, replaced by CoordUnit?
bug repair in check_field_structure

File size: 21.9 KB
Line 
1%'mouse_motion': permanently called by mouse motion over a figure (Callback for 'WindowButtonMotionFcn' of the figure)
2%-----------------------------------------------------------------------
3%
4% function mouse_motion(hObject,eventdata,handles)
5% activated by the command:
6% set(hObject,'WindowButtonMotionFcn',{'mouse_motion',handles})
7% where hObject is the handle of the figure
8%
9%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
10%  Copyright Joel Sommeria, 2008, LEGI / CNRS-UJF-INPG, sommeria@coriolis-legi.org.
11%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
12%     This file is part of the toolbox UVMAT.
13%
14%     UVMAT is free software; you can redistribute it and/or modify
15%     it under the terms of the GNU General Public License as published by
16%     the Free Software Foundation; either version 2 of the License, or
17%     (at your option) any later version.
18%
19%     UVMAT is distributed in the hope that it will be useful,
20%     but WITHOUT ANY WARRANTY; without even the implied warranty of
21%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22%     GNU General Public License (file UVMAT/COPYING.txt) for more details.
23%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
24
25function mouse_motion(hObject,eventdata,handles)
26
27if ~exist('handles','var')
28    return
29end
30currentfig=hObject;
31hhcurrentfig=guidata(currentfig);
32test_zoom=get(hhcurrentfig.zoom,'Value');%test for zoom activated on the current figure
33test_draw=0;%test for mouse drawing of object, =0 by default
34test_object=0; %test for object editing or creation
35test_edit_object=0;% edit test for mouse shap: an arrow
36test_zoom_draw=0; % test for zoom drawing
37test_ruler=0;%test for active ruler
38test_piv=0;% test for PIV correlation display
39huvmat=findobj(allchild(0),'tag','uvmat');%find the uvmat interface handle
40if ~isempty(huvmat)
41    hhuvmat=guidata(huvmat);
42    test_edit_object=get(hhuvmat.edit_object,'Value');
43    test_ruler=isequal(get(hhuvmat.MenuRuler,'checked'),'on');
44end
45hciv=findobj(allchild(0),'tag','civ');%find the civ interface handle
46if ~isempty(hciv) && strcmp(get(currentfig,'tag'),'view_field')
47    hhciv=guidata(hciv);
48    test_piv =get(hhciv.TestCiv1,'Value');
49end
50
51%find the current axe 'haxes' and display the current mouse position or uicontrol tag
52text_displ_1='';
53text_displ_2='';
54text_displ_3='';
55text_displ_4='';
56
57AxeData=[];%default
58mouse=[];
59xy=[];%default
60
61pointershape='arrow';% default pointer is an arrow
62
63xy_fig=get(currentfig,'CurrentPoint');% current point of the current figure (gcbo)
64hchild=get(currentfig,'Children');%handles of all objects in the current figure
65
66%% loop on all the objects in the current figure and detect whether the mouse is over a plot  axes
67haxes=[];
68for ichild=1:length(hchild)
69    obj_pos=get(hchild(ichild),'Position');
70    if numel(obj_pos)~=4% for some versions of matlab a uicontextmenu appears
71        continue
72    end%position of the object
73    if xy_fig(1) >=obj_pos(1) && xy_fig(2) >= obj_pos(2)&& xy_fig(1) <=obj_pos(1)+obj_pos(3) && xy_fig(2) <= obj_pos(2)+obj_pos(4);
74        htype=get(hchild(ichild),'Type');%type of the crrent child
75        %if the mouse is over an axis, look at the data
76        if isequal(htype,'axes')
77            haxes=hchild(ichild);
78            xy=get(haxes,'CurrentPoint');%xy(1,1),xy(1,2): current x,y positions in axes coordinates
79            AxeData=get(haxes,'UserData');% data attached to the axis
80            if isfield(AxeData,'Drawing')&& ~isempty(AxeData.Drawing)
81                test_draw=~isequal(AxeData.Drawing,'off');%=1 if mouse drawing of object is active
82            end
83            test_zoom_draw=test_draw && isequal(AxeData.Drawing,'zoom')&& isfield(AxeData,'CurrentOrigin') && isequal(get(gcf,'SelectionType'),'normal');
84            test_object=test_draw && isfield(AxeData,'CurrentObject') && ~isempty(AxeData.CurrentObject) && ishandle(AxeData.CurrentObject);
85            if ~test_edit_object && ~test_zoom_draw && ~test_ruler
86                pointershape='crosshair';%set pointer with cross shape (default when mouse is over an axis)
87            end
88            FigData=get(currentfig,'UserData');
89            tagaxes=get(haxes,'tag');
90            if isfield(FigData,tagaxes)
91                eval(['Field=FigData.' tagaxes ';'])
92                if isfield(Field,'ListVarName')
93                    [CellVarIndex,NbDim,VarType]=find_field_indices(Field);%analyse the physical fields contained in Field
94%                     if isfield(Field,'Mesh') && ~isempty(Field.Mesh)
95                        text_displ_1='';
96                        text_displ_2='';
97                        text_displ_3='';
98                        text_displ_4='';
99                        for icell=1:numel(CellVarIndex)%look for all physical fields
100                            if NbDim(icell)==2 % select 2D field
101                                if  isfield(Field,'Mesh') && ~isempty(Field.Mesh)&& ~isempty(VarType{icell}.coord_x) && ~isempty(VarType{icell}.coord_y)%case of unstructured data
102                                    eval(['X=Field.' Field.ListVarName{VarType{icell}.coord_x} ';'])
103                                    eval(['Y=Field.' Field.ListVarName{VarType{icell}.coord_y} ';'])
104                                    flag_vec=(X<(xy(1,1)+Field.Mesh/3) & X>(xy(1,1)-Field.Mesh/3)) & ...%flagx=1 for the vectors with x position selected by the mouse
105                                        (Y<(xy(1,2)+Field.Mesh/3) & Y>(xy(1,2)-Field.Mesh/3));%f
106                                    ivec=find(flag_vec,1);% search the (first) selected vector index ivec                         
107                                    hhh=findobj(haxes,'Tag','vector_marker');
108                                    if ~isempty(ivec)
109                                        % mark the vectors with a circle in the absence of other operations
110                                        if ~test_object && ~test_edit_object && ~test_ruler
111                                            pointershape='arrow'; %mouse indicates  the detection of a vector
112                                            if isempty(hhh)
113                                                set(0,'CurrentFigure',currentfig)
114                                                set(currentfig,'CurrentAxes',haxes)
115                                                rectangle('Curvature',[1 1],...
116                                                    'Position',[X(ivec)-Field.Mesh/2 Y(ivec)-Field.Mesh/2 Field.Mesh Field.Mesh],'EdgeColor','m',...
117                                                    'LineStyle','-','Tag','vector_marker');
118                                            else
119                                                set(hhh,'Visible','on')
120                                                set(hhh,'Position',[X(ivec)-Field.Mesh/2 Y(ivec)-Field.Mesh/2 Field.Mesh Field.Mesh])
121                                            end
122                                        end
123                                        %display the field values
124                                        for ivar=1:numel(CellVarIndex{icell})
125                                            VarName=Field.ListVarName{CellVarIndex{icell}(ivar)};
126                                            eval(['VarVal=Field.' VarName '(ivec);'])
127                                            var_text=[VarName '=' num2str(VarVal,3) ','];
128                                            if isequal(ivar,VarType{icell}.coord_x)||isequal(ivar,VarType{icell}.coord_y)||isequal(ivar,VarType{icell}.coord_z)
129                                                text_displ_1=[text_displ_1 var_text];
130                                            elseif isequal(ivar,VarType{icell}.vector_x)||isequal(ivar,VarType{icell}.vector_y)||isequal(ivar,VarType{icell}.vector_z)
131                                                text_displ_3=[text_displ_3 var_text];
132                                            else
133                                                text_displ_4=[text_displ_4 var_text];
134                                            end
135                                        end
136                                    else
137                                        if ~isempty(hhh)
138                                            set(hhh,'Visible','off')
139                                        end
140                                    end
141                                elseif numel(VarType{icell}.coord) >=2 %structured coordinates
142                                    eval(['y=Field.' Field.ListVarName{VarType{icell}.coord(1)} ';'])
143                                    eval(['x=Field.' Field.ListVarName{VarType{icell}.coord(2)} ';'])
144                                    VarName=Field.ListVarName{CellVarIndex{icell}(1)};
145                                    eval(['nxy=size(Field.' VarName ');']);
146%                                     nxy(1)=numel(y);
147%                                     nxy(2)=numel(x);
148                                    MaxAY=max(y(1),y(end)); %#ok<COLND>
149                                    MinAY=min(y(1),y(end)); %#ok<COLND>
150                                    if (xy(1,1)>x(1))&(xy(1,1)<x(end))&(xy(1,2)<MaxAY)&(xy(1,2)>MinAY) %#ok<COLND>
151                                        indx0=1+round((nxy(2)-1)*(xy(1,1)-x(1))/(x(end)-x(1)));%#ok<COLND> % index x of pixel
152                                        indy0=1+round((nxy(1)-1)*(xy(1,2)-y(1))/(y(end)-y(1)));%#ok<COLND> % index y of pixel
153                                        if indx0>=1 & indx0<=nxy(2) & indy0>=1 & indy0<=nxy(1)
154                                            text_displ_2=['i='  num2str(indx0) ',j=' num2str(indy0) ','];
155                                            for ivar=1:numel(CellVarIndex{icell})
156                                                VarName=Field.ListVarName{CellVarIndex{icell}(ivar)};
157                                                eval(['VarVal=Field.' VarName '(indy0,indx0,:);'])
158                                                var_text=[VarName '=' num2str(VarVal) ','];
159                                                text_displ_2=[text_displ_2 var_text];
160                                            end
161                                        end
162                                    end
163                                end
164                            end
165                        end
166%                     end
167                    if strcmp(text_displ_1,'')
168                        text_displ_1=['x=' num2str(xy(1,1),3) ',y=' num2str(xy(1,2),3) ','];
169                    end
170                    if isfield(Field,'PlaneCoord') && isfield(Field,'ZIndex')
171                        text_displ_1=[text_displ_1 ' z=' num2str(Field.PlaneCoord(Field.ZIndex,3))]; %TODO: generaliser au cas avec angle
172                    end
173                    %coordinate transform if proj_coord differs from menu_coord A REVOIR
174                    if isfield(Field,'CoordUnit')
175                        mouse.CoordUnit=Field.CoordUnit;
176                    end
177%                     if isfield(Field,'CoordUnit')
178%                         mouse.CoordUnit=Field.CoordUnit;
179%                     end
180%                     if isfield(mouse,'CoordType')
181%                         if isequal(mouse.CoordType,'px')
182%                             mouse.CoordUnit='px';
183%                         end
184%                     else
185%                         mouse.CoordUnit='';%default
186%                     end
187                    if test_piv
188                       par=civ('read_param_civ1',hhciv);
189                        if isfield(Field,'A')
190                            dx=str2double(par.dx);
191                            dy=str2double(par.dy);
192                            xround=x(1)+dx*round((xy(1,1)-x(1))/dx);% index x of pixel
193                            yround=y(1)+dy*round((xy(1,2)-y(1))/dy);% index y of pixel
194                        end
195                        % mark the correlation box with a rectangle
196                        ibx2=floor((str2double(par.ibx)-1)/2);
197                        iby2=floor((str2double(par.iby)-1)/2);
198                        isx2=floor((str2double(par.isx)-1)/2);
199                        isy2=floor((str2double(par.isy)-1)/2);
200                        hhh=findobj(haxes,'Tag','PIV_box_marker');
201                        hhhh=findobj(haxes,'Tag','PIV_search_marker');
202                        if isempty(hhh)
203                            %hstack=findobj(allchild(0),'Type','figure');%current stack order of figures in matlab
204                            set(0,'CurrentFigure',currentfig)
205                            set(currentfig,'CurrentAxes',haxes)
206                            rectangle('Curvature',[0 0],...
207                                'Position',[xround-ibx2 yround-iby2 2*ibx2 2*iby2],'EdgeColor','m',...
208                                'LineStyle','-','Tag','PIV_box_marker');
209                            rectangle('Curvature',[0 0],...
210                                'Position',[xround-isx2 yround-isy2 2*isx2 2*isy2],'EdgeColor','m',...
211                                'LineStyle','- -','Tag','PIV_search_marker');
212                            % set(0,'Children',hstack);%put back the initial figure stack after plot creation
213                        else
214%                             set(hhh,'Visible','on')
215                            set(hhh,'Position',[xround-ibx2 yround-iby2 2*ibx2 2*iby2])
216                            set(hhhh,'Position',[xround-isx2 yround-isy2 2*isx2 2*isy2])
217                        end
218                        Asub=Field.A(xround-ibx2:xround+ibx2,yround-iby2:yround+iby2);
219                        Asub=reshape(Asub,[],1);
220                        rangx(1)=-(isx2-ibx2);
221                        rangx(2)=isx2-ibx2;
222                        rangy(1)=isy2-iby2;
223                        rangy(2)=-(isy2-iby2);
224                        for id=rangx(1):rangx(2)
225                            for jd=rangy(2):rangy(1)
226                                Bsub=Field.B(xround-ibx2+id:xround+ibx2+id,yround-iby2+jd:yround+iby2+jd);
227                                Bsub=reshape(Bsub,[],1);
228                                correl(jd-rangy(2)+1,id-rangx(1)+1)=corr(double(Asub),double(Bsub));
229                            end
230                        end
231                        %correl=uint8(63.5*correl+63.5);
232                        hcorr=[];
233                        if isfield(AxeData,'CurrentCorrImage')                       
234                            hcorr=AxeData.CurrentCorrImage;
235                            if ~ishandle(hcorr)
236                                hcorr=[];
237                            end
238                        end
239                        if isempty(hcorr)
240                            corrfig=findobj(allchild(0),'tag','corrfig');
241                            if isempty(corrfig)
242                                corrfig=figure;
243                                set(corrfig,'tag','corrfig')
244                            end
245                            set(0,'CurrentFigure',corrfig)
246                            AxeData.CurrentCorrImage=imagesc(rangx,rangy,correl,[0 1]);
247                            colorbar
248%                             map_r=ones(42,1)*[1 0 0];
249%                             map_g=ones(11,1)*[0 1 0];
250%                             map_b=ones(11,1)*[0 0 1];
251%                             map=[map_r;map_g;map_b];
252%                             set(corrfig,'colormap',map)
253                            set(haxes,'UserData',AxeData)
254                        else
255                            set(AxeData.CurrentCorrImage,'CData',correl)
256                        end       
257                    end
258                end
259            end     
260        end
261    end
262end
263set(handles.text_display_1,'String',text_displ_1);
264set(handles.text_display_2,'String',text_displ_2);
265set(handles.text_display_3,'String',text_displ_3);
266set(handles.text_display_4,'String',text_displ_4);
267
268%%%%%%%%%%%%%
269%% draw a zoom rectangle if no object creation is selected
270if test_zoom_draw
271   xy_rect=AxeData.CurrentOrigin;
272   if ~isempty(xy_rect)
273        rect(1)=min(xy(1,1),xy_rect(1));%origin rectangle, x coordinate
274        rect(2)=min(xy(1,2),xy_rect(2));%origin rectangle, y coordinate
275        rect(3)=abs(xy(1,1)-xy_rect(1));%rectangle width
276        rect(4)=abs(xy(1,2)-xy_rect(2));%rectangle height
277        if rect(3)>0 & rect(4)>0
278            if isfield(AxeData,'CurrentRectZoom')& ishandle(AxeData.CurrentRectZoom)
279                set(AxeData.CurrentRectZoom,'Position',rect);%update the rectangle position
280            else
281                AxeData.CurrentRectZoom=rectangle('Position',rect,'LineStyle',':','Tag','rect_zoom');
282                set(haxes,'UserData',AxeData)
283            end
284        end
285   end
286    pointershape='arrow';
287end
288
289%%%%%%%%%%%%%%%%%
290%% create or modify an object
291if ~isempty(huvmat) && test_object
292    PlotData=get(AxeData.CurrentObject,'UserData');
293    huvmat=findobj(allchild(0),'Name','uvmat');%find the uvmat interface handle
294    if ~isempty(huvmat)
295        UvData=get(huvmat,'UserData');
296        if ~isfield(PlotData,'IndexObj')
297             return
298        end
299        ObjectData=UvData.Object{PlotData.IndexObj};
300        XYData=AxeData.CurrentOrigin;
301        if isequal(AxeData.Drawing,'create') && isfield(AxeData,'CurrentOrigin') && ~isempty(AxeData.CurrentOrigin)
302           if strcmp(ObjectData.Style,'line')||strcmp(ObjectData.Style,'polyline')||strcmp(ObjectData.Style,'polygon')||strcmp(ObjectData.Style,'points')
303              xy(1,3)=0;
304              ObjectData.Coord=[ObjectData.Coord ;xy(1,:)];
305             % ObjectData.Coord(end,:)=xy(1,:);
306           elseif strcmp(ObjectData.Style,'rectangle')||strcmp(ObjectData.Style,'ellipse')||strcmp(ObjectData.Style,'volume')
307              ObjectData.Coord(1,1)=(xy(1,1)+XYData(1))/2;%origin rectangle, x coordinate
308              ObjectData.Coord(1,2)=(xy(1,2)+XYData(2))/2;
309              ObjectData.RangeX=abs(xy(1,1)-XYData(1))/2;%rectangle width
310              ObjectData.RangeY=abs(xy(1,2)-XYData(2))/2;%rectangle height
311           elseif isequal(ObjectData.Style,'plane') %case of 'plane'
312                DX=(xy(1,1)-ObjectData.Coord(1,1));
313                DY=(xy(1,2)-ObjectData.Coord(1,2));
314                ObjectData.Phi=(angle(DX+i*DY))*180/pi;%rectangle widt
315                if isfield(ObjectData,'RangeX')
316                    XMax=sqrt(DX*DX+DY*DY);
317                    if XMax>max(ObjectData.RangeX)
318                        ObjectData.RangeX=[min(ObjectData.RangeX) XMax];
319                    end
320                end
321           end
322            plot_object(ObjectData,[],AxeData.CurrentObject,'m');
323            pointershape='crosshair';
324        elseif  isequal(AxeData.Drawing,'translate')
325            DX=xy(1,1)-XYData(1);%translation from initial position
326            DY=xy(1,2)-XYData(2);
327            ObjectData.Coord(:,1)=ObjectData.Coord(:,1)+DX;
328            ObjectData.Coord(:,2)=ObjectData.Coord(:,2)+DY;
329            plot_object(ObjectData,[],AxeData.CurrentObject,'m');
330            pointershape='fleur';
331        elseif  isequal(AxeData.Drawing,'deform')
332            ind_move=AxeData.CurrentIndex;
333            ObjectData.Coord(ind_move,1)=xy(1,1);
334            ObjectData.Coord(ind_move,2)=xy(1,2);
335            plot_object(ObjectData,[],AxeData.CurrentObject,'m');
336            pointershape='circle';
337        end
338    end
339end   
340
341%% detect calibration points if the GUI geometry_calib is opened
342h_geometry_calib=findobj(allchild(0),'Name','geometry_calib'); %find the geomterty_calib GUI
343if ~test_zoom && ~isempty(h_geometry_calib)
344    pointershape='crosshair';%default for geometry_calib: ready to create new points
345    hh_geometry_calib=guidata(h_geometry_calib);
346    if  ~isempty(xy) && isfield(hh_geometry_calib,'ListCoord')
347        h_ListCoord=hh_geometry_calib.ListCoord; %findobj(h_geometry_calib,'Tag','ListCoord');
348        Coord=get(h_ListCoord,'String');
349        data=read_geometry_calib(Coord);%transform char cell to numbers
350        if size(data.Coord,2)>=5
351            XCoord=(data.Coord(:,4));
352            YCoord=(data.Coord(:,5));
353            xy=get(haxes,'CurrentPoint');%xy(1,1),xy(1,2): current x,y positions in axes coordinates
354            if ~isempty(xy)
355                xlim=get(haxes,'XLim');
356                ind_range_x=abs((xlim(2)-xlim(1))/50);
357                ylim=get(haxes,'YLim');
358                ind_range_y=abs((ylim(2)-ylim(1))/50);
359                ind_range=sqrt(ind_range_x*ind_range_y);
360                index_point=find((XCoord<xy(1,1)+ind_range) & (XCoord>xy(1,1)-ind_range) & ...%flagx=1 for the vectors with x position selected by the mouse
361                              (YCoord<xy(1,2)+ind_range) & (YCoord>xy(1,2)-ind_range),1);%find the first calibration point in the neighborhood of the mouse
362                if ~isempty(index_point)
363                    pointershape='arrow';% default pointer is an arrow
364                end
365                hh=findobj('Tag','calib_points');%look for handle of calibration points
366               if ~isempty(hh) && ~isempty(get(hh,'UserData')) && get(hh_geometry_calib.edit_append,'Value')
367                    index_point=get(hh,'UserData')
368                    XCoord(index_point)=xy(1,1);
369                    YCoord(index_point)=xy(1,2);
370                    set(hh,'XData',XCoord)
371                    set(hh,'YData',YCoord)
372               end
373                if ~isempty(index_point)
374                    set(h_ListCoord,'Value',index_point)%mrk the point on the GUI geometry_calib
375                    hhh=findobj('Tag','calib_marker');%look for handle of point marker (circle)
376                    if ~isempty(hhh)
377                        set(hhh,'Position',[XCoord(index_point)-ind_range/2 YCoord(index_point)-ind_range/2 ind_range ind_range])
378                    end
379                end
380            end
381        end
382    end
383end
384
385%% draw ruler
386if test_ruler && isfield(AxeData,'Drawing') && isequal(AxeData.Drawing,'ruler')
387    if isfield(AxeData,'RulerHandle')
388        pointershape='crosshair'; %give  the mouse pointer a cross shape
389        RulerCoord=[AxeData.RulerCoord ;xy(1,1:2)]; %coordinates defining the ruler segment
390        set(AxeData.RulerHandle,'XData',RulerCoord(:,1));% updtate the x coordinates for the ruler graphic object
391        set(AxeData.RulerHandle,'YData',RulerCoord(:,2));% updtate the y coordinates for the ruler graphic object
392    end
393end
394
395%% update the mouse pointer
396set(currentfig,'Pointer',pointershape);
Note: See TracBrowser for help on using the repository browser.