source: trunk/src/mouse_motion.m @ 387

Last change on this file since 387 was 387, checked in by sommeria, 12 years ago

several bugs corrected: mask, color image...

File size: 22.7 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
30FigData=get(hObject,'UserData');
31
32if ishandle(FigData)% case of a zoom plot, the handle of the parent rectangle is stored in UserData, its parent is the plotting axes of the rectangle
33    currentfig=get(get(FigData,'parent'),'parent');
34else
35    currentfig=hObject;%usual plot
36end
37hhcurrentfig=guidata(currentfig);
38test_zoom=get(hhcurrentfig.CheckZoom,'Value');%test for zoom activated on the current figure
39test_draw=0;%test for mouse drawing of object, =0 by default
40test_object=0; %test for object editing or creation
41test_edit_object=0;% edit test for mouse shap: an arrow
42test_zoom_draw=0; % test for zoom drawing
43test_ruler=0;%test for active ruler
44% test_piv=0;% test for PIV correlation display
45huvmat=findobj(allchild(0),'tag','uvmat');%find the uvmat interface handle
46if ~isempty(huvmat)
47    hhuvmat=guidata(huvmat);
48    test_edit_object=get(hhuvmat.edit_object,'Value');
49    test_ruler=isequal(get(hhuvmat.MenuRuler,'checked'),'on');
50end
51test_piv=0;
52if isfield(FigData,'CivHandle')
53    if ~ishandle(FigData.CivHandle)
54        delete(hObject)
55        return
56    end
57    hhciv=guidata(FigData.CivHandle);
58    test_piv=1;
59end
60
61%find the current axe 'haxes' and display the current mouse position or uicontrol tag
62text_displ_1='';
63text_displ_2='';
64text_displ_3='';
65text_displ_4='';
66
67AxeData=[];%default
68mouse=[];
69xy=[];%default
70
71pointershape='arrow';% default pointer is an arrow
72
73xy_fig=get(hObject,'CurrentPoint');% current point of the current figure (gcbo)
74hchild=get(hObject,'Children');%handles of all objects in the current figure
75
76%% loop on all the objects in the current figure and detect whether the mouse is over a plot  axes
77haxes=[];
78for ichild=1:length(hchild)
79    obj_pos=get(hchild(ichild),'Position');
80    if numel(obj_pos)~=4% for some versions of matlab a uicontextmenu appears
81        continue
82    end%position of the object
83    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);
84        htype=get(hchild(ichild),'Type');%type of the crrent child
85        %if the mouse is over an axis, look at the data
86        if strcmp(htype,'axes')
87            haxes=hchild(ichild);
88            xy=get(haxes,'CurrentPoint');%xy(1,1),xy(1,2): current x,y positions in axes coordinates
89            AxeData=get(haxes,'UserData');% data attached to the axis
90            if isfield(AxeData,'Drawing')&& ~isempty(AxeData.Drawing)
91                test_draw=~isequal(AxeData.Drawing,'off');%=1 if mouse drawing of object is active
92            end
93            test_zoom_draw=test_draw && isequal(AxeData.Drawing,'zoom')&& isfield(AxeData,'CurrentOrigin') && isequal(get(gcf,'SelectionType'),'normal');
94            test_object=test_draw && isfield(AxeData,'CurrentObject') && ~isempty(AxeData.CurrentObject) && ishandle(AxeData.CurrentObject);
95            if ~test_edit_object && ~test_zoom_draw && ~test_ruler
96                pointershape='crosshair';%set pointer with cross shape (default when mouse is over an axis)
97            end
98            FigData=get(currentfig,'UserData');
99            tagaxes=get(haxes,'tag');
100            if isfield(FigData,tagaxes)
101                eval(['Field=FigData.' tagaxes ';'])
102                if isfield(Field,'ListVarName')
103                    [CellVarIndex,NbDim,VarType]=find_field_indices(Field);%analyse the physical fields contained in Field
104                    %                     if isfield(Field,'Mesh') && ~isempty(Field.Mesh)
105                    text_displ_1='';
106                    text_displ_2='';
107                    text_displ_3='';
108                    text_displ_4='';
109                    ivec=[];
110                    xName='';
111                    z=[];
112                    for icell=1:numel(CellVarIndex)%look for all physical fields
113                        if NbDim(icell)>=2 % select 2D field
114                            if  isfield(Field,'Mesh') && ~isempty(Field.Mesh)&& ~isempty(VarType{icell}.coord_x) && ~isempty(VarType{icell}.coord_y)%case of unstructured data
115                                eval(['X=Field.' Field.ListVarName{VarType{icell}.coord_x} ';'])
116                                eval(['Y=Field.' Field.ListVarName{VarType{icell}.coord_y} ';'])
117                                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
118                                    (Y<(xy(1,2)+Field.Mesh/3) & Y>(xy(1,2)-Field.Mesh/3));%f
119                                ivec=find(flag_vec,1);% search the (first) selected vector index ivec
120                                hhh=findobj(haxes,'Tag','vector_marker');
121                                if ~isempty(ivec)
122                                    % mark the vectors with a circle in the absence of other operations
123                                    if ~test_object && ~test_edit_object && ~test_ruler
124                                        pointershape='arrow'; %mouse indicates  the detection of a vector
125                                        if isempty(hhh)
126                                            set(0,'CurrentFigure',currentfig)
127                                            set(currentfig,'CurrentAxes',haxes)
128                                            rectangle('Curvature',[1 1],...
129                                                'Position',[X(ivec)-Field.Mesh/2 Y(ivec)-Field.Mesh/2 Field.Mesh Field.Mesh],'EdgeColor','m',...
130                                                'LineStyle','-','Tag','vector_marker');
131                                        else
132                                            set(hhh,'Visible','on')
133                                            set(hhh,'Position',[X(ivec)-Field.Mesh/2 Y(ivec)-Field.Mesh/2 Field.Mesh Field.Mesh])
134                                        end
135                                    end
136                                    %display the field values
137                                    for ivar=1:numel(CellVarIndex{icell})
138                                        VarName=Field.ListVarName{CellVarIndex{icell}(ivar)};
139                                        eval(['VarVal=Field.' VarName '(ivec);'])
140                                        var_text=[VarName '=' num2str(VarVal,3) ','];
141                                        if isequal(ivar,VarType{icell}.coord_x)||isequal(ivar,VarType{icell}.coord_y)||isequal(ivar,VarType{icell}.coord_z)
142                                            text_displ_1=[text_displ_1 var_text];
143                                        elseif isequal(ivar,VarType{icell}.vector_x)||isequal(ivar,VarType{icell}.vector_y)||isequal(ivar,VarType{icell}.vector_z)
144                                            text_displ_3=[text_displ_3 var_text];
145                                        else
146                                            text_displ_4=[text_displ_4 var_text];
147                                        end
148                                    end
149                                else
150                                    if ~isempty(hhh)
151                                        set(hhh,'Visible','off')
152                                    end
153                                end
154                            elseif numel(VarType{icell}.coord) >=2 %structured coordinates
155                                yName=Field.ListVarName{VarType{icell}.coord(1)};
156                                xName=Field.ListVarName{VarType{icell}.coord(2)};
157                                 eval(['y=Field.' yName ';'])
158                                 eval(['x=Field.' xName ';'])
159                                VarName=Field.ListVarName{CellVarIndex{icell}(1)};
160                                eval(['nxy=size(Field.' VarName ');']);
161                                MaxAY=max(y(1),y(end)); %#ok<COLND>
162                                MinAY=min(y(1),y(end)); %#ok<COLND>
163                                if (xy(1,1)>x(1))&(xy(1,1)<x(end))&(xy(1,2)<MaxAY)&(xy(1,2)>MinAY) %#ok<COLND>
164                                    indx0=1+round((nxy(2)-1)*(xy(1,1)-x(1))/(x(end)-x(1)));%#ok<COLND> % index x of pixel
165                                    indy0=1+round((nxy(1)-1)*(xy(1,2)-y(1))/(y(end)-y(1)));%#ok<COLND> % index y of pixel
166                                    if indx0>=1 & indx0<=nxy(2) & indy0>=1 & indy0<=nxy(1)
167                                        text_displ_2=['i='  num2str(indx0) ',j=' num2str(indy0) ','];
168                                        for ivar=1:numel(CellVarIndex{icell})
169                                            VarName=Field.ListVarName{CellVarIndex{icell}(ivar)};
170                                            eval(['VarVal=Field.' VarName '(indy0,indx0,:);'])
171                                            var_text=[VarName '=' num2str(VarVal) ','];
172                                            text_displ_2=[text_displ_2 var_text];
173                                        end
174                                    end
175                                end
176                            end
177                        end
178                    end
179              % display the current x,y coordinates in the absence of detected vector
180                    if isempty(ivec)
181                        if isempty(xName)
182                            xName='x';
183                            yName='y';
184                        end
185                        text_displ_1=[xName '=' num2str(xy(1,1),3) ', ' yName '=' num2str(xy(1,2),3) ','];
186                    end
187              %display the z coordinate if defined by the projection plane
188                    if isfield(Field,'PlaneCoord')
189%                             ZIndex=Field.ZIndex;
190                        if size(Field.PlaneCoord)>=[1 3]
191                            z=Field.PlaneCoord(1,3);
192                            if isfield(Field,'PlaneAngle')&&~isequal(Field.PlaneAngle,[0 0 0])
193                                om=norm(Field.PlaneAngle);%norm of rotation angle in radians
194                                OmAxis=Field.PlaneAngle/om; %unit vector marking the rotation axis
195                                cos_om=cos(pi*om/180);
196                                sin_om=sin(pi*om/180);
197                                coeff=OmAxis(3)*(1-cos_om);
198                                norm_plane(1)=OmAxis(1)*coeff+OmAxis(2)*sin_om;
199                                norm_plane(2)=OmAxis(2)*coeff-OmAxis(1)*sin_om;
200                                norm_plane(3)=OmAxis(3)*coeff+cos_om;
201                                Z0=norm_plane*Field.PlaneCoord'/norm_plane(3);
202                                z=Z0-norm_plane(1)*xy(1,1)/norm_plane(3)-norm_plane(2)*xy(1,2)/norm_plane(3);
203                            end
204                        end
205                    end
206                    if ~isempty(z)
207                        text_displ_1=[text_displ_1 ' z=' num2str(z,3)];
208                    end
209                    %coordinate transform if proj_coord differs from menu_coord A REVOIR
210%                     if isfield(Field,'CoordUnit')
211%                         mouse.CoordUnit=Field.CoordUnit;
212%                     end
213             % case of PIV correlation display
214                    if test_piv
215                        par=read_GUI(hhciv.Civ1);
216                        [dd,ind_pt]=min(abs(Field.X-xy(1,1))+abs(Field.Y-xy(1,2)));
217                        xround=Field.X(ind_pt);
218                        yround=Field.Y(ind_pt);
219                        par.Grid=[xround size(Field.A,1)-yround+1];
220                        % mark the correlation box with a rectangle
221                        par.ImageA=Field.A;
222                        par.ImageB=Field.B;
223                        [par.ImageHeight,par.ImageWidth]=size(par.ImageA);
224                        Param.Civ1=par;
225                        ibx2=floor((par.Bx-1)/2);
226                        iby2=floor((par.By-1)/2);
227                        isx2=floor((par.Searchx-1)/2);
228                        isy2=floor((par.Searchy-1)/2);
229                        shiftx=par.Shiftx;
230                        shifty=par.Shifty;     
231                        hhh=findobj(haxes,'Tag','PIV_box_marker');
232                        hhhh=findobj(haxes,'Tag','PIV_search_marker');
233                        if isempty(hhh)
234                            set(0,'CurrentFigure',currentfig)
235                            set(currentfig,'CurrentAxes',haxes)
236                            rectangle('Curvature',[0 0],...
237                                'Position',[xround-ibx2 yround-iby2 2*ibx2 2*iby2],'EdgeColor','m',...
238                                'LineStyle','-','Tag','PIV_box_marker');
239                            rectangle('Curvature',[0 0],...
240                                'Position',[xround-isx2+shiftx yround-isy2+shifty 2*isx2 2*isy2],'EdgeColor','m',...
241                                'LineStyle','- -','Tag','PIV_search_marker');
242                        else
243                            set(hhh,'Position',[xround-ibx2 yround-iby2 2*ibx2 2*iby2])
244                            set(hhhh,'Position',[xround-isx2+shiftx yround-isy2+shifty 2*isx2 2*isy2])
245                        end
246                        [Data,errormsg,result_conv]= civ_matlab(Param);
247                        if ~isempty(errormsg)
248                            text_displ_4=errormsg;
249                        else
250                            rangx(1)=-(isx2-ibx2)+shiftx;
251                            rangx(2)=isx2-ibx2+shiftx;
252                            rangy(1)=-(isy2-iby2)-shifty;
253                            rangy(2)=(isy2-iby2)-shifty;
254                            hcorr=[];
255                            if isfield(AxeData,'CurrentCorrImage')
256                                hcorr=AxeData.CurrentCorrImage;
257                                if ~ishandle(hcorr)
258                                    hcorr=[];
259                                end
260                            end
261                            if isempty(hcorr)
262                                corrfig=findobj(allchild(0),'tag','corrfig');
263                                if ~isempty(corrfig)
264                                    set(0,'CurrentFigure',corrfig(1))
265                                    AxeData.CurrentCorrImage=imagesc(rangx,-rangy,result_conv,[0 1]);
266                                    AxeData.CurrentVector=line([0 Data.Civ1_U],[0 Data.Civ1_V],'Tag','vector');
267                                    colorbar
268                                    set(haxes,'UserData',AxeData)
269                                    set(get(AxeData.CurrentCorrImage,'parent'),'YDir','normal')
270                                end
271                            else
272                                set(AxeData.CurrentCorrImage,'CData',result_conv)
273                                set(AxeData.CurrentCorrImage,'XData',rangx)
274                                set(AxeData.CurrentCorrImage,'YData',-rangy)
275                                set(AxeData.CurrentVector,'XData',[0 Data.Civ1_U],'YData',[0 Data.Civ1_V])
276                            end
277                        end
278                    end
279                end
280            end
281        end
282    end
283end
284if ~isempty(text_displ_1)
285set(handles.text_display,'String',[{text_displ_1};{text_displ_2};{text_displ_3};{text_displ_4}])
286else
287   set(handles.text_display,'String',get(handles.text_display,'UserData'))
288end
289% set(handles.text_display_1,'String',text_displ_1);
290% set(handles.text_display_2,'String',text_displ_2);
291% set(handles.text_display_3,'String',text_displ_3);
292% set(handles.text_display_4,'String',text_displ_4);
293
294%%%%%%%%%%%%%
295%% draw a zoom rectangle if no object creation is selected
296if test_zoom_draw
297   xy_rect=AxeData.CurrentOrigin;
298   if ~isempty(xy_rect)
299        rect(1)=min(xy(1,1),xy_rect(1));%origin rectangle, x coordinate
300        rect(2)=min(xy(1,2),xy_rect(2));%origin rectangle, y coordinate
301        rect(3)=abs(xy(1,1)-xy_rect(1));%rectangle width
302        rect(4)=abs(xy(1,2)-xy_rect(2));%rectangle height
303        if rect(3)>0 & rect(4)>0
304            if isfield(AxeData,'CurrentRectZoom')& ishandle(AxeData.CurrentRectZoom)
305                set(AxeData.CurrentRectZoom,'Position',rect);%update the rectangle position
306            else
307                AxeData.CurrentRectZoom=rectangle('Position',rect,'LineStyle',':','Tag','rect_zoom');
308                set(haxes,'UserData',AxeData)
309            end
310        end
311   end
312    pointershape='arrow';
313end
314
315%%%%%%%%%%%%%%%%%
316%% create or modify an object
317if ~isempty(huvmat) && test_object
318    PlotData=get(AxeData.CurrentObject,'UserData');
319    huvmat=findobj(allchild(0),'Name','uvmat');%find the uvmat interface handle
320    if ~isempty(huvmat)
321        UvData=get(huvmat,'UserData');
322        if ~isfield(PlotData,'IndexObj')
323             return
324        end
325        ObjectData=UvData.Object{PlotData.IndexObj};
326        XYData=AxeData.CurrentOrigin;
327        if isequal(AxeData.Drawing,'create') && isfield(AxeData,'CurrentOrigin') && ~isempty(AxeData.CurrentOrigin)
328           if strcmp(ObjectData.Type,'line')||strcmp(ObjectData.Type,'polyline')||strcmp(ObjectData.Type,'polygon')||strcmp(ObjectData.Type,'points')
329              ObjectData.Coord=[ObjectData.Coord ;xy(1,1:2)];
330             % ObjectData.Coord(end,:)=xy(1,:);
331           elseif strcmp(ObjectData.Type,'rectangle')||strcmp(ObjectData.Type,'ellipse')||strcmp(ObjectData.Type,'volume')
332              ObjectData.Coord(1,1)=(xy(1,1)+XYData(1))/2;%origin rectangle, x coordinate
333              ObjectData.Coord(1,2)=(xy(1,2)+XYData(2))/2;
334              ObjectData.RangeX=abs(xy(1,1)-XYData(1))/2;%rectangle width
335              ObjectData.RangeY=abs(xy(1,2)-XYData(2))/2;%rectangle height
336           elseif isequal(ObjectData.Type,'plane') %case of 'plane'
337                DX=(xy(1,1)-ObjectData.Coord(1,1));
338                DY=(xy(1,2)-ObjectData.Coord(1,2));
339                ObjectData.Phi=(angle(DX+i*DY))*180/pi;%rectangle widt
340                if isfield(ObjectData,'RangeX')
341                    XMax=sqrt(DX*DX+DY*DY);
342                    if XMax>max(ObjectData.RangeX)
343                        ObjectData.RangeX=[min(ObjectData.RangeX) XMax];
344                    end
345                end
346           end
347            plot_object(ObjectData,[],AxeData.CurrentObject,'m');
348            pointershape='crosshair';
349        elseif  isequal(AxeData.Drawing,'translate')
350            DX=xy(1,1)-XYData(1);%translation from initial position
351            DY=xy(1,2)-XYData(2);
352            ObjectData.Coord(:,1)=ObjectData.Coord(:,1)+DX;
353            ObjectData.Coord(:,2)=ObjectData.Coord(:,2)+DY;
354            plot_object(ObjectData,[],AxeData.CurrentObject,'m');
355            pointershape='fleur';
356        elseif  isequal(AxeData.Drawing,'deform')
357            ind_move=AxeData.CurrentIndex;
358            ObjectData.Coord(ind_move,1)=xy(1,1);
359            ObjectData.Coord(ind_move,2)=xy(1,2);
360            plot_object(ObjectData,[],AxeData.CurrentObject,'m');
361            pointershape='circle';
362        end
363    end
364end   
365
366%% detect calibration points if the GUI geometry_calib is opened
367h_geometry_calib=findobj(allchild(0),'Name','geometry_calib'); %find the geomterty_calib GUI
368if ~test_zoom && ~isempty(h_geometry_calib)
369    pointershape='crosshair';%default for geometry_calib: ready to create new points
370    hh_geometry_calib=guidata(h_geometry_calib);
371    if  ~isempty(xy) && isfield(hh_geometry_calib,'ListCoord')
372        h_ListCoord=hh_geometry_calib.ListCoord; %findobj(h_geometry_calib,'Tag','ListCoord');
373        Coord=get(h_ListCoord,'String');
374        data=read_geometry_calib(Coord);%transform char cell to numbers
375        if size(data.Coord,2)>=5
376            XCoord=(data.Coord(:,4));
377            YCoord=(data.Coord(:,5));
378            xy=get(haxes,'CurrentPoint');%xy(1,1),xy(1,2): current x,y positions in axes coordinates
379            if ~isempty(xy)
380                xlim=get(haxes,'XLim');
381                ind_range_x=abs((xlim(2)-xlim(1))/50);
382                ylim=get(haxes,'YLim');
383                ind_range_y=abs((ylim(2)-ylim(1))/50);
384                ind_range=sqrt(ind_range_x*ind_range_y);
385                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
386                              (YCoord<xy(1,2)+ind_range) & (YCoord>xy(1,2)-ind_range),1);%find the first calibration point in the neighborhood of the mouse
387                if ~isempty(index_point)
388                    pointershape='arrow';% default pointer is an arrow
389                end
390                hh=findobj('Tag','calib_points');%look for handle of calibration points
391               if ~isempty(hh) && ~isempty(get(hh,'UserData')) && get(hh_geometry_calib.edit_append,'Value')
392                    index_point=get(hh,'UserData');
393                    XCoord(index_point)=xy(1,1);
394                    YCoord(index_point)=xy(1,2);
395                    set(hh,'XData',XCoord)
396                    set(hh,'YData',YCoord)
397               end
398                if ~isempty(index_point)
399                    set(h_ListCoord,'Value',index_point)%mrk the point on the GUI geometry_calib
400                    hhh=findobj('Tag','calib_marker');%look for handle of point marker (circle)
401                    if ~isempty(hhh)
402                        set(hhh,'Position',[XCoord(index_point)-ind_range/2 YCoord(index_point)-ind_range/2 ind_range ind_range])
403                    end
404                end
405            end
406        end
407    end
408end
409
410%% draw ruler
411if test_ruler && isfield(AxeData,'Drawing') && isequal(AxeData.Drawing,'ruler')
412    if isfield(AxeData,'RulerHandle')
413        pointershape='crosshair'; %give  the mouse pointer a cross shape
414        RulerCoord=[AxeData.RulerCoord ;xy(1,1:2)]; %coordinates defining the ruler segment
415        set(AxeData.RulerHandle,'XData',RulerCoord(:,1));% updtate the x coordinates for the ruler graphic object
416        set(AxeData.RulerHandle,'YData',RulerCoord(:,2));% updtate the y coordinates for the ruler graphic object
417    end
418end
419
420%% update the mouse pointer
421set(currentfig,'Pointer',pointershape);
Note: See TracBrowser for help on using the repository browser.