source: trunk/src/mouse_motion.m @ 104

Last change on this file since 104 was 104, checked in by gostiaux, 14 years ago

Same as previous revision (file was not saved before commit, sorry...)

File size: 16.7 KB
Line 
1%'mouse_motion': permanently called by mouse motion over a figure (Callback for 'WindowButtonMotionFcn' of the figure)
2%-----------------------------------------------------------------------
3%
4% function mouse_motion(hObject,eventdata,handles)
5% activated by the command:
6% set(hObject,'WindowButtonMotionFcn',{'mouse_motion',handles})
7% where hObject is the handle of the figure
8%
9%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
10%  Copyright Joel Sommeria, 2008, LEGI / CNRS-UJF-INPG, sommeria@coriolis-legi.org.
11%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
12%     This file is part of the toolbox UVMAT.
13%
14%     UVMAT is free software; you can redistribute it and/or modify
15%     it under the terms of the GNU General Public License as published by
16%     the Free Software Foundation; either version 2 of the License, or
17%     (at your option) any later version.
18%
19%     UVMAT is distributed in the hope that it will be useful,
20%     but WITHOUT ANY WARRANTY; without even the implied warranty of
21%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22%     GNU General Public License (file UVMAT/COPYING.txt) for more details.
23%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
24
25function mouse_motion(hObject,eventdata,handles)
26
27if ~exist('handles','var')
28    return
29end
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            mouse.X=xy(1,1);
74            mouse.Y=xy(1,2);
75            u_mouse=[];
76            v_mouse=[];
77            w_mouse=[];
78            A_mouse=[];
79            c_text=[];
80            f_text=[];
81            ff_text=[];     
82            ivec=[];
83            AxeData=get(haxes,'UserData');% data attached to the axis
84            if isfield(AxeData,'Drawing')&& ~isempty(AxeData.Drawing)
85                test_draw=~isequal(AxeData.Drawing,'off');
86            end
87            test_zoom_draw=test_draw && isequal(AxeData.Drawing,'zoom')&& isfield(AxeData,'CurrentOrigin') && isequal(get(gcf,'SelectionType'),'normal');
88            test_object=test_draw && isfield(AxeData,'CurrentObject') && ~isempty(AxeData.CurrentObject) && ishandle(AxeData.CurrentObject);       
89             if ~test_edit && ~test_zoom_draw && ~test_ruler
90                 pointershape='crosshair';%set pointer with cross shape (default when mouse is over an axis)
91             end
92            if isfield(AxeData,'X') && isfield(AxeData,'Y') && isfield(AxeData,'Mesh')% test on the existence of a vector field in the current axis
93                if ~isempty(AxeData.Mesh)
94                    flag_vec=(AxeData.X<(xy(1,1)+AxeData.Mesh/3) & AxeData.X>(xy(1,1)-AxeData.Mesh/3)) & ...%flagx=1 for the vectors with x position selected by the mouse
95                          (AxeData.Y<(xy(1,2)+AxeData.Mesh/3) & AxeData.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 % mark the vectors with a circle in the absence of other operations
100                            if  ~test_create && ~test_edit && ~test_ruler
101                                pointershape='arrow'; %mouse indicates  the detection of a vector
102                                if isempty(hhh)
103                                     hstack=findobj(allchild(0),'Type','figure');%current stack order of figures in matlab
104                                    axes(haxes)
105                                    rectangle('Curvature',[1 1],...
106                      'Position',[AxeData.X(ivec)-AxeData.Mesh/2 AxeData.Y(ivec)-AxeData.Mesh/2 AxeData.Mesh AxeData.Mesh],'EdgeColor','m',...
107                      'LineStyle','-','Tag','vector_marker');
108                                    set(0,'Children',hstack);%put back the initial figure stack after plot creation
109                                else
110                                    set(hhh,'Visible','on')
111                                    set(hhh,'Position',[AxeData.X(ivec)-AxeData.Mesh/2 AxeData.Y(ivec)-AxeData.Mesh/2 AxeData.Mesh AxeData.Mesh])
112                                end
113                            end
114                        end
115                        mouse.X=AxeData.X(ivec);
116                        mouse.Y=AxeData.Y(ivec);
117                        u_mouse=AxeData.U(ivec);%displacement
118                        v_mouse=AxeData.V(ivec);
119                        w_mouse=0; %default
120                        if isfield(AxeData,'W') & length(AxeData.W)>=ivec
121                            w_text=[',  w=' num2str(AxeData.W(ivec),3)];
122                        else
123                            w_text='';
124                        end
125                        if ~isfield(AxeData,'CName')
126                            AxeData.CName='C';%REVOIR
127                        end
128                        c_text=[', ' AxeData.CName '=' num2str(AxeData.C(ivec),3)];
129                        if isfield(AxeData,'F')&length(AxeData.F)>=ivec
130                            f_text=[',  f=' num2str(AxeData.F(ivec),3)];
131                        else
132                            f_text='';
133                        end
134                        if isfield(AxeData,'FF')&length(AxeData.FF)>=ivec
135                            ff_text=[',  ff=' num2str(AxeData.FF(ivec),3)];
136                        else
137                            ff_text='';
138                        end
139                    else
140                        if ~isempty(hhh)
141                            set(hhh,'Visible','off')
142                        end
143                    end
144                end
145            end
146            if isfield(AxeData,'Z')
147                mouse.Z=AxeData.Z; %generaliser au cas avec angle
148            end
149            if isfield(AxeData,'ObjectCoord') & size(AxeData.ObjectCoord,2)==3
150                mouse.Z=AxeData.ObjectCoord(1,3); %generaliser au cas avec angle
151            end
152            testscal= isfield(AxeData,'A')& isfield(AxeData,'AX')& isfield(AxeData,'AY');%test the existence of an image (or scalar represented by an image)
153               if testscal
154                   testscal=~isempty(AxeData.A)&~isempty(AxeData.AX)& ~isempty(AxeData.AY);
155               end
156            if testscal%test the existence of an image (or scalar represented by an image)
157                nxy=size(AxeData.A);
158                MaxAY=max(AxeData.AY(1),AxeData.AY(end));
159                MinAY=min(AxeData.AY(1),AxeData.AY(end));
160                if (xy(1,1)>AxeData.AX(1))&(xy(1,1)<AxeData.AX(end))&(xy(1,2)<MaxAY)&(xy(1,2)>MinAY)
161                    indx0=1+round((nxy(2)-1)*(xy(1,1)-AxeData.AX(1))/(AxeData.AX(end)-AxeData.AX(1)));% index x of pixel
162                    indy0=1+round((nxy(1)-1)*(xy(1,2)-AxeData.AY(1))/(AxeData.AY(end)-AxeData.AY(1)));% index y of pixel
163                    if indx0>=1 & indx0<=nxy(2) & indy0>=1 & indy0<=nxy(1)
164                        A_mouse=AxeData.A(indy0,indx0,:);
165                    end
166                end
167            end
168            %coordinate transform if proj_coord differs from menu_coord
169            if isfield(AxeData,'CoordType')
170                  mouse.CoordType=AxeData.CoordType;
171            end
172            if isfield(AxeData,'CoordUnit')
173                  mouse.CoordUnit=AxeData.CoordUnit;
174            end
175            if isfield(mouse,'CoordType')
176                if isequal(mouse.CoordType,'px')
177                    mouse.CoordUnit='px';
178                end
179            else
180                mouse.CoordUnit='';%default     
181            end     
182            text_displ_1=['x=' num2str(mouse.X,4) ',y=' num2str(mouse.Y,4)];
183            if isfield(mouse,'Z')&~isempty(mouse.Z)
184                text_displ_1=[text_displ_1 ',z=' num2str(mouse.Z,3)];
185            end
186            if isfield(mouse,'CoordUnit')
187                 text_displ_1=[text_displ_1 ' ' mouse.CoordUnit];
188            end
189            if ~isempty(ivec)
190                text_displ_4=['vec#=' num2str(ivec)];
191            end
192            if ~isempty(u_mouse)
193                text_displ_3=['u=' num2str(u_mouse,3) ',v=' num2str(v_mouse,3) w_text ];
194                if  isfield(mouse,'CoordUnit')
195                    if isequal(mouse.CoordUnit,'px')
196                        text_displ_3=[text_displ_3 '  ' mouse.CoordUnit];
197                    elseif isfield(AxeData,'TimeUnit')
198                        text_displ_3=[text_displ_3 '  ' mouse.CoordUnit '/' AxeData.TimeUnit];
199                    end
200                end
201                text_displ_4=[text_displ_4 c_text f_text ff_text];
202            end
203           
204            if ~isempty(A_mouse)
205                text_displ_2=['A=' num2str(double(A_mouse)) ',i='  num2str(indx0) ',j=' num2str(indy0)];
206            end
207        elseif isequal(htype,'uicontrol') && isequal(get(hchild(ichild),'Visible'),'on')&& ~isequal(get(hchild(ichild),'Style'),'frame')
208            text_displ_1=get(hchild(ichild),'Tag');
209        end
210    end
211end
212set(handles.text_display_1,'String',text_displ_1);
213set(handles.text_display_2,'String',text_displ_2);
214set(handles.text_display_3,'String',text_displ_3);
215set(handles.text_display_4,'String',text_displ_4);
216% if ~test_draw
217%     return
218% end
219% At this stage  if no drawing  operation is done
220
221
222%%%%%%%%%%%%%
223%draw a zoom rectangle if no object creation is selected
224if test_zoom_draw
225   xy_rect=AxeData.CurrentOrigin;
226   if ~isempty(xy_rect)
227        rect(1)=min(xy(1,1),xy_rect(1));%origin rectangle, x coordinate
228        rect(2)=min(xy(1,2),xy_rect(2));%origin rectangle, y coordinate
229        rect(3)=abs(xy(1,1)-xy_rect(1));%rectangle width
230        rect(4)=abs(xy(1,2)-xy_rect(2));%rectangle height
231        if rect(3)>0 & rect(4)>0
232            if isfield(AxeData,'CurrentRectZoom')& ishandle(AxeData.CurrentRectZoom)
233                set(AxeData.CurrentRectZoom,'Position',rect);%update the rectangle position
234            else
235                AxeData.CurrentRectZoom=rectangle('Position',rect,'LineStyle',':','Tag','rect_zoom');
236                set(haxes,'UserData',AxeData)
237            end
238        end
239   end
240    pointershape='arrow';
241end
242
243%%%%%%%%%%%%%%%%%
244%create or modify an object
245
246if ~isempty(huvmat) && test_object
247    PlotData=get(AxeData.CurrentObject,'UserData');
248    huvmat=findobj(allchild(0),'Name','uvmat');%find the uvmat interface handle
249    if ~isempty(huvmat)
250        UvData=get(huvmat,'UserData');
251        if ~isfield(PlotData,'IndexObj')
252             return
253        end
254        ObjectData=UvData.Object{PlotData.IndexObj};
255        XYData=AxeData.CurrentOrigin;
256        if isequal(AxeData.Drawing,'create') && isfield(AxeData,'CurrentOrigin') && ~isempty(AxeData.CurrentOrigin)
257           if isequal(ObjectData.Style,'line')|isequal(ObjectData.Style,'polyline')|isequal(ObjectData.Style,'polygon')|isequal(ObjectData.Style,'points')
258              xy(1,3)=0;
259              ObjectData.Coord=[ObjectData.Coord ;xy(1,:)];
260             % ObjectData.Coord(end,:)=xy(1,:);
261           elseif isequal(ObjectData.Style,'rectangle')|isequal(ObjectData.Style,'ellipse')|isequal(ObjectData.Style,'volume')
262              ObjectData.Coord(1,1)=(xy(1,1)+XYData(1))/2;%origin rectangle, x coordinate
263              ObjectData.Coord(1,2)=(xy(1,2)+XYData(2))/2;
264              ObjectData.RangeX=abs(xy(1,1)-XYData(1))/2;%rectangle width
265              ObjectData.RangeY=abs(xy(1,2)-XYData(2))/2;%rectangle height
266           elseif isequal(ObjectData.Style,'plane') %case of 'plane'
267                DX=(xy(1,1)-ObjectData.Coord(1,1));
268                DY=(xy(1,2)-ObjectData.Coord(1,2));
269                ObjectData.Phi=(angle(DX+i*DY))*180/pi;%rectangle widt
270                if isfield(ObjectData,'RangeX')
271                    XMax=sqrt(DX*DX+DY*DY);
272                    if XMax>max(ObjectData.RangeX)
273                        ObjectData.RangeX=[min(ObjectData.RangeX) XMax];
274                    end
275                end
276           end
277            plot_object(ObjectData,[],AxeData.CurrentObject,'m');
278            pointershape='crosshair';
279        elseif  isequal(AxeData.Drawing,'translate')
280            DX=xy(1,1)-XYData(1);%translation from initial position
281            DY=xy(1,2)-XYData(2);
282            ObjectData.Coord(:,1)=ObjectData.Coord(:,1)+DX;
283            ObjectData.Coord(:,2)=ObjectData.Coord(:,2)+DY;
284            plot_object(ObjectData,[],AxeData.CurrentObject,'m');
285            pointershape='fleur';
286        elseif  isequal(AxeData.Drawing,'deform')
287            ind_move=AxeData.CurrentIndex;
288            ObjectData.Coord(ind_move,1)=xy(1,1);
289            ObjectData.Coord(ind_move,2)=xy(1,2);
290            plot_object(ObjectData,[],AxeData.CurrentObject,'m');
291            pointershape='circle';
292        end
293    end
294end   
295
296% detect calibration points if the GUI geometry_calib is opened
297h_geometry_calib=findobj(allchild(0),'Name','geometry_calib'); %find the geomterty_calib GUI
298if ~test_zoom && ~isempty(h_geometry_calib)
299    pointershape='crosshair';%default for geometry_calib: ready to create new points
300    hh_geometry_calib=guidata(h_geometry_calib);
301    if  ~isempty(xy)
302        h_ListCoord=hh_geometry_calib.ListCoord; %findobj(h_geometry_calib,'Tag','ListCoord');
303        Coord=get(h_ListCoord,'String');
304        data=read_geometry_calib(Coord);%transform char cell to numbers
305        if size(data.Coord,2)>=5
306            XCoord=(data.Coord(:,4));
307            YCoord=(data.Coord(:,5));
308            xy=get(haxes,'CurrentPoint');%xy(1,1),xy(1,2): current x,y positions in axes coordinates
309            if ~isempty(xy)
310                xlim=get(haxes,'XLim');
311                ind_range_x=abs((xlim(2)-xlim(1))/50);
312                ylim=get(haxes,'YLim');
313                ind_range_y=abs((ylim(2)-ylim(1))/50);
314                ind_range=sqrt(ind_range_x*ind_range_y);
315                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
316                              (YCoord<xy(1,2)+ind_range) & (YCoord>xy(1,2)-ind_range),1);%find the first calibration point in the neighborhood of the mouse
317                if ~isempty(index_point)
318                    pointershape='arrow';% default pointer is an arrow
319                end
320                hh=findobj('Tag','calib_points');%look for handle of calibration points
321               if ~isempty(hh) && ~isempty(get(hh,'UserData')) && get(hh_geometry_calib.edit_append,'Value')
322                    index_point=get(hh,'UserData');
323                    XCoord(index_point)=xy(1,1);
324                    YCoord(index_point)=xy(1,2);
325                    set(hh,'XData',XCoord)
326                    set(hh,'YData',YCoord)
327               end
328                if ~isempty(index_point)
329                    set(h_ListCoord,'Value',index_point)%mrk the point on the GUI geometry_calib
330                    hhh=findobj('Tag','calib_marker');%look for handle of point marker (circle)
331                    if ~isempty(hhh)
332                        set(hhh,'Position',[XCoord(index_point)-ind_range/2 YCoord(index_point)-ind_range/2 ind_range ind_range])
333                    end
334                end
335            end
336        end
337    end
338end
339
340%draw ruler
341if test_ruler && isequal(AxeData.Drawing,'ruler')
342           if isfield(UvData,'RulerHandle')
343               pointershape='crosshair';
344                RulerCoord=[UvData.RulerCoord ;xy(1,1:2)];
345                set(UvData.RulerHandle,'XData',RulerCoord(:,1));
346                set(UvData.RulerHandle,'YData',RulerCoord(:,2));
347           end
348end
349set(currentfig,'Pointer',pointershape);
Note: See TracBrowser for help on using the repository browser.