source: trunk/src/mouse_motion.m @ 78

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

debugging of geometry_calib and mouse operations
merge_proj: call to read_image replaced. Still problems to solve to merge images

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