source: trunk/src/mouse_motion.m @ 534

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

new conventions for find_field_cells .

File size: 22.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
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                    [CellInfo,NbDimArray]=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(CellInfo)%look for all physical fields
110                        if NbDimArray(icell)>=2 % select 2D field
111                            if  isfield(Field,'Mesh') && ~isempty(Field.Mesh)&& strcmp(CellInfo{icell}.CoordType,'scattered')%case of unstructured data
112                                X=Field.(Field.ListVarName{CellInfo{icell}.CoordIndex(end)});
113                                Y=Field.(Field.ListVarName{CellInfo{icell}.CoordIndex(end-1)});
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(CellInfo{icell}.VarIndex)
135                                        VarName=Field.ListVarName{CellInfo{icell}.VarIndex(ivar)};
136                                        VarVal=Field.(VarName)(ivec);
137                                        var_text=[VarName '=' num2str(VarVal,3) ','];
138                                        if isequal(ivar,CellInfo{icell}.CoordIndex(end))||isequal(ivar,CellInfo{icell}.CoordIndex(end-1))||isequal(ivar,CellInfo{icell}.CoordIndex(1))
139                                            text_displ_1=[text_displ_1 var_text];
140                                        elseif (isfield(CellInfo{icell},'VarIndex_vector_x') && isequal(ivar,CellInfo{icell}.VarIndex_vector_x))||isequal(ivar,CellInfo{icell}.VarIndex_vector_y)||...
141                                                (isfield(CellInfo{icell},'VarIndex_vector_z') && isequal(ivar,CellInfo{icell}.VarIndex_vector_z))
142                                            text_displ_3=[text_displ_3 var_text];
143                                        else
144                                            text_displ_4=[text_displ_4 var_text];
145                                        end
146                                    end
147                                else
148                                    if ~isempty(hhh)
149                                        set(hhh,'Visible','off')
150                                    end
151                                end
152                            elseif strcmp(CellInfo{icell}.CoordType,'grid') %structured coordinates
153                                yName=Field.ListVarName{CellInfo{icell}.CoordIndex(1)};
154                                xName=Field.ListVarName{CellInfo{icell}.CoordIndex(2)};
155                                y=Field.(yName);
156                                x=Field.(xName);
157                                VarName=Field.ListVarName{CellInfo{icell}.VarIndex(1)};
158                                nxy=size(Field.(VarName));
159                                MaxAY=max(y(1),y(end)); %#ok<COLND>
160                                MinAY=min(y(1),y(end)); %#ok<COLND>
161                                if (xy(1,1)>x(1))&(xy(1,1)<x(end))&(xy(1,2)<MaxAY)&(xy(1,2)>MinAY) %#ok<COLND>
162                                    indx0=1+round((nxy(2)-1)*(xy(1,1)-x(1))/(x(end)-x(1)));%#ok<COLND> % index x of pixel
163                                    indy0=1+round((nxy(1)-1)*(xy(1,2)-y(1))/(y(end)-y(1)));%#ok<COLND> % index y of pixel
164                                    if indx0>=1 & indx0<=nxy(2) & indy0>=1 & indy0<=nxy(1)
165                                        text_displ_2=['i='  num2str(indx0) ',j=' num2str(indy0) ','];
166                                        for ivar=1:numel(CellInfo{icell}.VarIndex)
167                                            VarName=Field.ListVarName{CellInfo{icell}.VarIndex(ivar)};
168                                            VarVal=Field.(VarName)(indy0,indx0,:);
169                                            var_text=[VarName '=' num2str(VarVal) ','];
170                                            text_displ_2=[text_displ_2 var_text];
171                                        end
172                                    end
173                                end
174                            end
175                        end
176                    end
177              % display the current x,y coordinates in the absence of detected vector
178                    if isempty(ivec)
179                        if isempty(xName)
180                            xName='x';
181                            yName='y';
182                        end
183                        text_displ_1=[xName '=' num2str(xy(1,1),3) ', ' yName '=' num2str(xy(1,2),3) ','];
184                    end
185              %display the z coordinate if defined by the projection plane
186                    if isfield(Field,'PlaneCoord')
187%                             ZIndex=Field.ZIndex;
188                        if size(Field.PlaneCoord)>=[1 3]
189                            z=Field.PlaneCoord(1,3);
190                            if isfield(Field,'PlaneAngle')&&~isequal(Field.PlaneAngle,[0 0 0])
191                                om=norm(Field.PlaneAngle);%norm of rotation angle in radians
192                                OmAxis=Field.PlaneAngle/om; %unit vector marking the rotation axis
193                                cos_om=cos(pi*om/180);
194                                sin_om=sin(pi*om/180);
195                                coeff=OmAxis(3)*(1-cos_om);
196                                norm_plane(1)=OmAxis(1)*coeff+OmAxis(2)*sin_om;
197                                norm_plane(2)=OmAxis(2)*coeff-OmAxis(1)*sin_om;
198                                norm_plane(3)=OmAxis(3)*coeff+cos_om;
199                                Z0=norm_plane*Field.PlaneCoord'/norm_plane(3);
200                                z=Z0-norm_plane(1)*xy(1,1)/norm_plane(3)-norm_plane(2)*xy(1,2)/norm_plane(3);
201                            end
202                        end
203                    end
204                    if ~isempty(z)
205                        text_displ_1=[text_displ_1 ' z=' num2str(z,3)];
206                    end
207               % case of PIV correlation display
208                    if test_piv
209                        par=read_GUI(hhciv.Civ1);
210                        [dd,ind_pt]=min(abs(Field.X-xy(1,1))+abs(Field.Y-xy(1,2)));
211                        xround=Field.X(ind_pt);
212                        yround=Field.Y(ind_pt);
213%                         par.Grid=[xround size(Field.A,1)-yround+1];
214                        par.Grid=[xround yround];
215                        % mark the correlation box with a rectangle
216                        par.ImageA=Field.A;
217                        par.ImageB=Field.B;
218                        par.ImageHeight=size(par.ImageA,1);
219                        par.ImageWidth=size(par.ImageA,2);
220                        Param.Civ1=par;
221                        ibx2=floor((par.CorrBoxSize(1)-1)/2);
222                        iby2=floor((par.CorrBoxSize(2)-1)/2);
223                        isx2=floor((par.SearchBoxSize(1)-1)/2);
224                        isy2=floor((par.SearchBoxSize(2)-1)/2);
225                        shiftx=par.SearchBoxShift(1);
226                        shifty=par.SearchBoxShift(2);     
227                        hhh=findobj(CurrentAxes,'Tag','PIV_box_marker');
228                        hhhh=findobj(CurrentAxes,'Tag','PIV_search_marker');
229                        if isempty(hhh)
230                            set(0,'CurrentFigure',CurrentFig)
231                            set(CurrentFig,'CurrentAxes',CurrentAxes)
232                            rectangle('Curvature',[0 0],...
233                                'Position',[xround-ibx2 yround-iby2 2*ibx2 2*iby2],'EdgeColor','m',...
234                                'LineStyle','-','Tag','PIV_box_marker');
235                            rectangle('Curvature',[0 0],...
236                                'Position',[xround-isx2+shiftx yround-isy2+shifty 2*isx2 2*isy2],'EdgeColor','m',...
237                                'LineStyle','- -','Tag','PIV_search_marker');
238                        else
239                            set(hhh,'Position',[xround-ibx2 yround-iby2 2*ibx2 2*iby2])
240                            set(hhhh,'Position',[xround-isx2+shiftx yround-isy2+shifty 2*isx2 2*isy2])
241                        end
242                        [Data,errormsg,result_conv]= civ_matlab(Param);
243                        if ~isempty(errormsg)
244                            text_displ_4=errormsg;
245                        else
246                            rangx(1)=-(isx2-ibx2)+shiftx;
247                            rangx(2)=isx2-ibx2+shiftx;
248                            rangy(1)=-(isy2-iby2)-shifty;
249                            rangy(2)=(isy2-iby2)-shifty;
250                            hcorr=[];
251                            if isfield(AxeData,'CurrentCorrImage')
252                                hcorr=AxeData.CurrentCorrImage;
253                                if ~ishandle(hcorr)
254                                    hcorr=[];
255                                end
256                            end
257                            if isempty(hcorr)
258                                corrfig=findobj(allchild(0),'tag','corrfig');
259                                if ~isempty(corrfig)
260                                    set(0,'CurrentFigure',corrfig(1))         
261                                    AxeData.CurrentCorrImage=imagesc(rangx,-rangy,result_conv,[0 1]);
262                                    AxeData.CurrentVector=line([0 Data.Civ1_U],[0 Data.Civ1_V],'Tag','vector');
263                                   AxeData.TitleHandle=title(num2str(par.Grid));
264                                    colorbar
265                                    set(CurrentAxes,'UserData',AxeData)
266                                    set(get(AxeData.CurrentCorrImage,'parent'),'YDir','normal')
267                                end
268                            else
269                                set(AxeData.CurrentCorrImage,'CData',result_conv)
270                                set(AxeData.CurrentCorrImage,'XData',rangx)
271                                set(AxeData.CurrentCorrImage,'YData',-rangy)
272                                set(AxeData.CurrentVector,'XData',[0 Data.Civ1_U],'YData',[0 Data.Civ1_V])
273                                set(AxeData.TitleHandle,'String',num2str(par.Grid))
274                            end
275                        end
276                    end
277                end
278            end
279        end
280    end
281end
282if ~isempty(text_displ_1)
283set(handles.text_display,'String',[{text_displ_1};{text_displ_2};{text_displ_3};{text_displ_4}])
284else
285   set(handles.text_display,'String',get(handles.text_display,'UserData'))
286end
287
288%%%%%%%%%%%%%
289%% draw a zoom rectangle if no object creation is selected
290if test_zoom_draw
291   xy_rect=AxeData.CurrentOrigin;
292   if ~isempty(xy_rect)
293        rect(1)=min(xy(1,1),xy_rect(1));%origin rectangle, x coordinate
294        rect(2)=min(xy(1,2),xy_rect(2));%origin rectangle, y coordinate
295        rect(3)=abs(xy(1,1)-xy_rect(1));%rectangle width
296        rect(4)=abs(xy(1,2)-xy_rect(2));%rectangle height
297        if rect(3)>0 & rect(4)>0
298            if isfield(AxeData,'CurrentRectZoom')& ishandle(AxeData.CurrentRectZoom)
299                set(AxeData.CurrentRectZoom,'Position',rect);%update the rectangle position
300            else
301                AxeData.CurrentRectZoom=rectangle('Position',rect,'LineStyle',':','Tag','rect_zoom');
302                set(CurrentAxes,'UserData',AxeData)
303            end
304        end
305   end
306    pointershape='arrow';
307end
308
309%%%%%%%%%%%%%%%%%
310%% create or modify an object
311if ~isempty(huvmat) && test_object
312    UvData=get(huvmat,'UserData');
313    PlotData=get(AxeData.CurrentObject,'UserData');
314    if ~isfield(PlotData,'IndexObj')
315        return
316    end
317    ObjectData=UvData.Object{PlotData.IndexObj};
318    ProjObject=[];% object (plane) whose projection is represented on the current axes
319    if isequal(hObject,huvmat)% if the mouse ifs over the GUI uvmat
320        ProjObject=UvData.Object{get(hhuvmat.ListObject_1,'Value')};
321    else
322        ProjObject=UvData.Object{get(hhuvmat.ListObject,'Value')};
323    end
324    XYData=AxeData.CurrentOrigin;
325    if isequal(AxeData.Drawing,'create') && isfield(AxeData,'CurrentOrigin') && ~isempty(AxeData.CurrentOrigin)
326        if strcmp(ObjectData.Type,'line')||strcmp(ObjectData.Type,'polyline')||strcmp(ObjectData.Type,'polygon')||strcmp(ObjectData.Type,'points')
327            ObjectData.Coord=[ObjectData.Coord ;xy(1,1:2)];
328            % ObjectData.Coord(end,:)=xy(1,:);
329        elseif strcmp(ObjectData.Type,'rectangle')||strcmp(ObjectData.Type,'ellipse')||strcmp(ObjectData.Type,'volume')
330            ObjectData.Coord(1,1)=(xy(1,1)+XYData(1))/2;%origin rectangle, x coordinate
331            ObjectData.Coord(1,2)=(xy(1,2)+XYData(2))/2;
332            ObjectData.RangeX=abs(xy(1,1)-XYData(1))/2;%rectangle width
333            ObjectData.RangeY=abs(xy(1,2)-XYData(2))/2;%rectangle height
334        elseif isequal(ObjectData.Type,'plane') %case of 'plane'
335            DX=(xy(1,1)-ObjectData.Coord(1,1));
336            DY=(xy(1,2)-ObjectData.Coord(1,2));
337            ObjectData.Phi=(angle(DX+i*DY))*180/pi;%rectangle widt
338            if isfield(ObjectData,'RangeX')
339                XMax=sqrt(DX*DX+DY*DY);
340                if XMax>max(ObjectData.RangeX)
341                    ObjectData.RangeX=[min(ObjectData.RangeX) XMax];
342                end
343            end
344        end
345        plot_object(ObjectData,ProjObject,AxeData.CurrentObject,'m');
346        pointershape='crosshair';
347    elseif  isequal(AxeData.Drawing,'translate')
348        DX=xy(1,1)-XYData(1);%translation from initial position
349        DY=xy(1,2)-XYData(2);
350        ObjectData.Coord(:,1)=ObjectData.Coord(:,1)+DX;
351        ObjectData.Coord(:,2)=ObjectData.Coord(:,2)+DY;
352        plot_object(ObjectData,ProjObject,AxeData.CurrentObject,'m');
353        pointershape='fleur';
354    elseif  isequal(AxeData.Drawing,'deform')
355        ind_move=AxeData.CurrentIndex;
356        ObjectData.Coord(ind_move,1)=xy(1,1);
357        ObjectData.Coord(ind_move,2)=xy(1,2);
358        plot_object(ObjectData,ProjObject,AxeData.CurrentObject,'m');
359        pointershape='circle';
360    end
361end
362
363%% detect calibration points if the GUI geometry_calib is opened
364h_geometry_calib=findobj(allchild(0),'Name','geometry_calib'); %find the geomterty_calib GUI
365if ~test_zoom && ~isempty(h_geometry_calib)
366    pointershape='crosshair';%default for geometry_calib: ready to create new points
367    hh_geometry_calib=guidata(h_geometry_calib);
368    if  ~isempty(xy) && isfield(hh_geometry_calib,'ListCoord')
369        h_ListCoord=hh_geometry_calib.ListCoord; %findobj(h_geometry_calib,'Tag','ListCoord');
370        Coord=get(h_ListCoord,'String');
371        data=read_geometry_calib(Coord);%transform char cell to numbers
372        if size(data.Coord,2)>=5
373            XCoord=(data.Coord(:,4));
374            YCoord=(data.Coord(:,5));
375            xy=get(CurrentAxes,'CurrentPoint');%xy(1,1),xy(1,2): current x,y positions in axes coordinates
376            if ~isempty(xy)
377                xlim=get(CurrentAxes,'XLim');
378                ind_range_x=abs((xlim(2)-xlim(1))/50);
379                ylim=get(CurrentAxes,'YLim');
380                ind_range_y=abs((ylim(2)-ylim(1))/50);
381                ind_range=sqrt(ind_range_x*ind_range_y);
382                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
383                              (YCoord<xy(1,2)+ind_range) & (YCoord>xy(1,2)-ind_range),1);%find the first calibration point in the neighborhood of the mouse
384                if ~isempty(index_point)
385                    pointershape='arrow';% default pointer is an arrow
386                end
387                hh=findobj('Tag','calib_points');%look for handle of calibration points
388               if ~isempty(hh) && ~isempty(get(hh,'UserData')) && get(hh_geometry_calib.edit_append,'Value')
389                    index_point=get(hh,'UserData');
390                    XCoord(index_point)=xy(1,1);
391                    YCoord(index_point)=xy(1,2);
392                    set(hh,'XData',XCoord)
393                    set(hh,'YData',YCoord)
394               end
395                if ~isempty(index_point)
396                    set(h_ListCoord,'Value',index_point)%mrk the point on the GUI geometry_calib
397                    hhh=findobj('Tag','calib_marker');%look for handle of point marker (circle)
398                    if ~isempty(hhh)
399                        set(hhh,'Position',[XCoord(index_point)-ind_range/2 YCoord(index_point)-ind_range/2 ind_range ind_range])
400                    end
401                end
402            end
403        end
404    end
405end
406
407%% draw ruler
408if test_ruler && isfield(AxeData,'Drawing') && isequal(AxeData.Drawing,'ruler')
409    if isfield(AxeData,'RulerHandle')
410        pointershape='crosshair'; %give  the mouse pointer a cross shape
411        RulerCoord=[AxeData.RulerCoord ;xy(1,1:2)]; %coordinates defining the ruler segment
412        set(AxeData.RulerHandle,'XData',RulerCoord(:,1));% updtate the x coordinates for the ruler graphic object
413        set(AxeData.RulerHandle,'YData',RulerCoord(:,2));% updtate the y coordinates for the ruler graphic object
414    end
415end
416
417%% update the mouse pointer
418set(CurrentFig,'Pointer',pointershape);
Note: See TracBrowser for help on using the repository browser.