source: trunk/src/mouse_motion.m @ 309

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

many bugs corrected, cleaning of civ.m, resize of the GUI civ

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