source: trunk/src/mouse_motion.m @ 106

Last change on this file since 106 was 105, checked in by sommeria, 14 years ago

Bug fix

File size: 17.3 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
30test_draw=0;
31test_create=0;%default
32test_object=0; %default
33test_edit=isfield(handles,'edit') && get(handles.edit,'Value');% edit test for mouse shap: an arrow
34test_zoom_draw=0; %default
35test_ruler=0;
36huvmat=findobj(allchild(0),'Name','uvmat');%find the uvmat interface handle
37hhuvmat=guidata(huvmat);
38test_zoom=get(hhuvmat.zoom,'Value');
39if ~isempty(huvmat)
40    UvData=get(huvmat,'UserData');
41    test_ruler=isfield(UvData,'MouseAction') && isequal(UvData.MouseAction,'ruler');
42end
43
44
45%find the current axe 'haxes' and display the current mouse position or uicontrol tag
46text_displ_1='';
47text_displ_2='';
48text_displ_3='';
49text_displ_4='';
50
51AxeData=[];%default
52mouse=[];
53xy=[];%default
54
55pointershape='arrow';% default pointer is an arrow
56currentfig=hObject;
57xy_fig=get(currentfig,'CurrentPoint');% current point of the current figure (gcbo)
58hchild=get(currentfig,'Children');%handles of all objects in the current figure
59
60% loop on all the objects in the current figure and detect whether the mouse is over a plot  axes
61haxes=[];
62for ichild=1:length(hchild)
63    obj_pos=get(hchild(ichild),'Position');
64    if numel(obj_pos)~=4% for some versions of matlab a uicontextmenu appears
65        continue
66    end%position of the object
67    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);
68        htype=get(hchild(ichild),'Type');%type of the crrent child
69        %if the mouse is over an axis, look at the data
70        if isequal(htype,'axes')
71            haxes=hchild(ichild);
72            xy=get(haxes,'CurrentPoint');%xy(1,1),xy(1,2): current x,y positions in axes coordinates
73            AxeData=get(haxes,'UserData');% data attached to the axis
74            if isfield(AxeData,'Drawing')&& ~isempty(AxeData.Drawing)
75                test_draw=~isequal(AxeData.Drawing,'off');
76            end
77            test_zoom_draw=test_draw && isequal(AxeData.Drawing,'zoom')&& isfield(AxeData,'CurrentOrigin') && isequal(get(gcf,'SelectionType'),'normal');
78            test_object=test_draw && isfield(AxeData,'CurrentObject') && ~isempty(AxeData.CurrentObject) && ishandle(AxeData.CurrentObject);
79            if ~test_edit && ~test_zoom_draw && ~test_ruler
80                pointershape='crosshair';%set pointer with cross shape (default when mouse is over an axis)
81            end
82            if isfield(AxeData,'ListVarName')
83                [CellVarIndex,NbDim,VarType]=find_field_indices(AxeData);
84                if isfield(AxeData,'Mesh') && ~isempty(AxeData.Mesh)
85                    text_displ_1='';
86                    text_displ_2='';
87                    text_displ_3='';
88                    text_displ_4='';
89                    for icell=1:numel(CellVarIndex)%look for all physical fields
90                        if NbDim(icell)==2 % select 2D field
91                            if ~isempty(VarType{icell}.coord_x) && ~isempty(VarType{icell}.coord_y)%case of unstructured data
92                                eval(['X=AxeData.' AxeData.ListVarName{VarType{icell}.coord_x} ';'])
93                                eval(['Y=AxeData.' AxeData.ListVarName{VarType{icell}.coord_y} ';'])
94                                flag_vec=(X<(xy(1,1)+AxeData.Mesh/3) & X>(xy(1,1)-AxeData.Mesh/3)) & ...%flagx=1 for the vectors with x position selected by the mouse
95                                    (Y<(xy(1,2)+AxeData.Mesh/3) & Y>(xy(1,2)-AxeData.Mesh/3));%f
96                                ivec=find(flag_vec,1);% search the (first) selected vector index ivec
97                                hhh=findobj(haxes,'Tag','vector_marker');
98                                if ~isempty(ivec)
99                                    if ~test_object && ~test_create && ~test_edit && ~test_ruler% mark the vectors with a circle in the absence of other operations
100                                        pointershape='arrow'; %mouse indicates  the detection of a vector
101                                        if isempty(hhh)
102                                            hstack=findobj(allchild(0),'Type','figure');%current stack order of figures in matlab
103                                            axes(haxes)
104                                            rectangle('Curvature',[1 1],...
105                                                'Position',[X(ivec)-AxeData.Mesh/2 Y(ivec)-AxeData.Mesh/2 AxeData.Mesh AxeData.Mesh],'EdgeColor','m',...
106                                                'LineStyle','-','Tag','vector_marker');
107                                            set(0,'Children',hstack);%put back the initial figure stack after plot creation
108                                        else
109                                            set(hhh,'Visible','on')
110                                            set(hhh,'Position',[X(ivec)-AxeData.Mesh/2 Y(ivec)-AxeData.Mesh/2 AxeData.Mesh AxeData.Mesh])
111                                        end
112                                    end
113                                    for ivar=1:numel(CellVarIndex{icell})
114                                        VarName=AxeData.ListVarName{CellVarIndex{icell}(ivar)};
115                                        eval(['VarVal=AxeData.' VarName '(ivec);'])
116                                        var_text=[VarName '=' num2str(VarVal,3) ','];
117                                        if isequal(ivar,VarType{icell}.coord_x)||isequal(ivar,VarType{icell}.coord_y)||isequal(ivar,VarType{icell}.coord_z)
118                                            text_displ_1=[text_displ_1 var_text];
119                                        elseif isequal(ivar,VarType{icell}.vector_x)||isequal(ivar,VarType{icell}.vector_y)||isequal(ivar,VarType{icell}.vector_z)
120                                            text_displ_3=[text_displ_3 var_text];
121                                        else
122                                            text_displ_4=[text_displ_4 var_text];
123                                        end
124                                    end
125                                else
126                                    if ~isempty(hhh)
127                                        set(hhh,'Visible','off')
128                                    end
129                                end
130                            elseif numel(VarType{icell}.coord) >=2 %structured coordinates
131                                eval(['y=AxeData.' AxeData.ListVarName{VarType{icell}.coord(1)} ';'])
132                                eval(['x=AxeData.' AxeData.ListVarName{VarType{icell}.coord(2)} ';'])
133                                nxy(1)=numel(y);
134                                nxy(2)=numel(x);
135                                MaxAY=max(y(1),y(end));
136                                MinAY=min(y(1),y(end));
137                                if (xy(1,1)>x(1))&(xy(1,1)<x(end))&(xy(1,2)<MaxAY)&(xy(1,2)>MinAY)
138                                    indx0=1+round((nxy(2)-1)*(xy(1,1)-x(1))/(x(end)-x(1)));% index x of pixel
139                                    indy0=1+round((nxy(1)-1)*(xy(1,2)-y(1))/(y(end)-y(1)));% index y of pixel
140                                    if indx0>=1 & indx0<=nxy(2) & indy0>=1 & indy0<=nxy(1)
141                                        text_displ_2=['i='  num2str(indx0) ',j=' num2str(indy0) ','];
142                                        for ivar=1:numel(CellVarIndex{icell})
143                                            VarName=AxeData.ListVarName{CellVarIndex{icell}(ivar)};
144                                            eval(['VarVal=AxeData.' VarName '(indy0,indx0);'])
145                                            var_text=[VarName '=' num2str(VarVal,3) ','];
146                                            text_displ_2=[text_displ_2 var_text];
147                                        end
148                                    end
149                                end
150                            end
151                        end
152                    end
153                end
154                if strcmp(text_displ_1,'')
155                    text_displ_1=['x=' num2str(xy(1,1)) ',y=' num2str(xy(1,2))];
156                end
157                if isfield(AxeData,'Z')
158                    text_displ_1=[text_displ_1 num2str(AxeData.Z)]; %generaliser au cas avec angle
159                end
160                if isfield(AxeData,'ObjectCoord') && size(AxeData.ObjectCoord,2)==3
161                    text_displ_1=[text_displ_1 num2str(AxeData.ObjectCoord(1,3))]; %generaliser au cas avec angle
162                end
163                %images
164                if strcmp(text_displ_2,'')&&isfield(AxeData,'A')&&isfield(AxeData,'AX')&&isfield(AxeData,'AY')
165                    y=AxeData.AY;
166                    x=AxeData.AX;
167                    nxy=size(AxeData.A);
168                    MaxAY=max(y(1),y(end));
169                    MinAY=min(y(1),y(end));
170                    if (xy(1,1)>x(1))&(xy(1,1)<x(end))&(xy(1,2)<MaxAY)&(xy(1,2)>MinAY)
171                        indx0=1+round((nxy(2)-1)*(xy(1,1)-x(1))/(x(end)-x(1)));% index x of pixel
172                        indy0=1+round((nxy(1)-1)*(xy(1,2)-y(1))/(y(end)-y(1)));% index y of pixel
173                        if indx0>=1 & indx0<=nxy(2) & indy0>=1 & indy0<=nxy(1)
174                            text_displ_2=['i='  num2str(indx0) ',j=' num2str(indy0) ',A=' num2str(AxeData.A(indy0,indx0,:))];
175                        end
176                    end
177                end
178                %coordinate transform if proj_coord differs from menu_coord A REVOIR
179                if isfield(AxeData,'CoordType')
180                    mouse.CoordType=AxeData.CoordType;
181                end
182                if isfield(AxeData,'CoordUnit')
183                    mouse.CoordUnit=AxeData.CoordUnit;
184                end
185                if isfield(mouse,'CoordType')
186                    if isequal(mouse.CoordType,'px')
187                        mouse.CoordUnit='px';
188                    end
189                else
190                    mouse.CoordUnit='';%default
191                end
192            end
193        end
194    end
195end
196set(handles.text_display_1,'String',text_displ_1);
197set(handles.text_display_2,'String',text_displ_2);
198set(handles.text_display_3,'String',text_displ_3);
199set(handles.text_display_4,'String',text_displ_4);
200
201%%%%%%%%%%%%%
202%draw a zoom rectangle if no object creation is selected
203if test_zoom_draw
204   xy_rect=AxeData.CurrentOrigin;
205   if ~isempty(xy_rect)
206        rect(1)=min(xy(1,1),xy_rect(1));%origin rectangle, x coordinate
207        rect(2)=min(xy(1,2),xy_rect(2));%origin rectangle, y coordinate
208        rect(3)=abs(xy(1,1)-xy_rect(1));%rectangle width
209        rect(4)=abs(xy(1,2)-xy_rect(2));%rectangle height
210        if rect(3)>0 & rect(4)>0
211            if isfield(AxeData,'CurrentRectZoom')& ishandle(AxeData.CurrentRectZoom)
212                set(AxeData.CurrentRectZoom,'Position',rect);%update the rectangle position
213            else
214                AxeData.CurrentRectZoom=rectangle('Position',rect,'LineStyle',':','Tag','rect_zoom');
215                set(haxes,'UserData',AxeData)
216            end
217        end
218   end
219    pointershape='arrow';
220end
221
222%%%%%%%%%%%%%%%%%
223%create or modify an object
224
225if ~isempty(huvmat) && test_object
226    PlotData=get(AxeData.CurrentObject,'UserData');
227    huvmat=findobj(allchild(0),'Name','uvmat');%find the uvmat interface handle
228    if ~isempty(huvmat)
229        UvData=get(huvmat,'UserData');
230        if ~isfield(PlotData,'IndexObj')
231             return
232        end
233        ObjectData=UvData.Object{PlotData.IndexObj};
234        XYData=AxeData.CurrentOrigin;
235        if isequal(AxeData.Drawing,'create') && isfield(AxeData,'CurrentOrigin') && ~isempty(AxeData.CurrentOrigin)
236           if isequal(ObjectData.Style,'line')|isequal(ObjectData.Style,'polyline')|isequal(ObjectData.Style,'polygon')|isequal(ObjectData.Style,'points')
237              xy(1,3)=0;
238              ObjectData.Coord=[ObjectData.Coord ;xy(1,:)];
239             % ObjectData.Coord(end,:)=xy(1,:);
240           elseif isequal(ObjectData.Style,'rectangle')|isequal(ObjectData.Style,'ellipse')|isequal(ObjectData.Style,'volume')
241              ObjectData.Coord(1,1)=(xy(1,1)+XYData(1))/2;%origin rectangle, x coordinate
242              ObjectData.Coord(1,2)=(xy(1,2)+XYData(2))/2;
243              ObjectData.RangeX=abs(xy(1,1)-XYData(1))/2;%rectangle width
244              ObjectData.RangeY=abs(xy(1,2)-XYData(2))/2;%rectangle height
245           elseif isequal(ObjectData.Style,'plane') %case of 'plane'
246                DX=(xy(1,1)-ObjectData.Coord(1,1));
247                DY=(xy(1,2)-ObjectData.Coord(1,2));
248                ObjectData.Phi=(angle(DX+i*DY))*180/pi;%rectangle widt
249                if isfield(ObjectData,'RangeX')
250                    XMax=sqrt(DX*DX+DY*DY);
251                    if XMax>max(ObjectData.RangeX)
252                        ObjectData.RangeX=[min(ObjectData.RangeX) XMax];
253                    end
254                end
255           end
256            plot_object(ObjectData,[],AxeData.CurrentObject,'m');
257            pointershape='crosshair';
258        elseif  isequal(AxeData.Drawing,'translate')
259            DX=xy(1,1)-XYData(1);%translation from initial position
260            DY=xy(1,2)-XYData(2);
261            ObjectData.Coord(:,1)=ObjectData.Coord(:,1)+DX;
262            ObjectData.Coord(:,2)=ObjectData.Coord(:,2)+DY;
263            plot_object(ObjectData,[],AxeData.CurrentObject,'m');
264            pointershape='fleur';
265        elseif  isequal(AxeData.Drawing,'deform')
266            ind_move=AxeData.CurrentIndex;
267            ObjectData.Coord(ind_move,1)=xy(1,1);
268            ObjectData.Coord(ind_move,2)=xy(1,2);
269            plot_object(ObjectData,[],AxeData.CurrentObject,'m');
270            pointershape='circle';
271        end
272    end
273end   
274
275% detect calibration points if the GUI geometry_calib is opened
276h_geometry_calib=findobj(allchild(0),'Name','geometry_calib'); %find the geomterty_calib GUI
277if ~test_zoom && ~isempty(h_geometry_calib)
278    pointershape='crosshair';%default for geometry_calib: ready to create new points
279    hh_geometry_calib=guidata(h_geometry_calib);
280    if  ~isempty(xy)
281        h_ListCoord=hh_geometry_calib.ListCoord; %findobj(h_geometry_calib,'Tag','ListCoord');
282        Coord=get(h_ListCoord,'String');
283        data=read_geometry_calib(Coord);%transform char cell to numbers
284        if size(data.Coord,2)>=5
285            XCoord=(data.Coord(:,4));
286            YCoord=(data.Coord(:,5));
287            xy=get(haxes,'CurrentPoint');%xy(1,1),xy(1,2): current x,y positions in axes coordinates
288            if ~isempty(xy)
289                xlim=get(haxes,'XLim');
290                ind_range_x=abs((xlim(2)-xlim(1))/50);
291                ylim=get(haxes,'YLim');
292                ind_range_y=abs((ylim(2)-ylim(1))/50);
293                ind_range=sqrt(ind_range_x*ind_range_y);
294                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
295                              (YCoord<xy(1,2)+ind_range) & (YCoord>xy(1,2)-ind_range),1);%find the first calibration point in the neighborhood of the mouse
296                if ~isempty(index_point)
297                    pointershape='arrow';% default pointer is an arrow
298                end
299                hh=findobj('Tag','calib_points');%look for handle of calibration points
300               if ~isempty(hh) && ~isempty(get(hh,'UserData')) && get(hh_geometry_calib.edit_append,'Value')
301                    index_point=get(hh,'UserData');
302                    XCoord(index_point)=xy(1,1);
303                    YCoord(index_point)=xy(1,2);
304                    set(hh,'XData',XCoord)
305                    set(hh,'YData',YCoord)
306               end
307                if ~isempty(index_point)
308                    set(h_ListCoord,'Value',index_point)%mrk the point on the GUI geometry_calib
309                    hhh=findobj('Tag','calib_marker');%look for handle of point marker (circle)
310                    if ~isempty(hhh)
311                        set(hhh,'Position',[XCoord(index_point)-ind_range/2 YCoord(index_point)-ind_range/2 ind_range ind_range])
312                    end
313                end
314            end
315        end
316    end
317end
318
319%draw ruler
320if test_ruler && isequal(AxeData.Drawing,'ruler')
321           if isfield(UvData,'RulerHandle')
322               pointershape='crosshair';
323                RulerCoord=[UvData.RulerCoord ;xy(1,1:2)];
324                set(UvData.RulerHandle,'XData',RulerCoord(:,1));
325                set(UvData.RulerHandle,'YData',RulerCoord(:,2));
326           end
327end
328set(currentfig,'Pointer',pointershape);
Note: See TracBrowser for help on using the repository browser.