source: trunk/src/mouse_motion.m @ 527

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

various cleaning

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