source: trunk/src/mouse_motion.m @ 71

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

civ3D updated: introduction of image size
imadoc2struct: reding of image size from the xml file
set_object, view_field and related functions: improvement of projection object editing
mouse: possibility of adjusting the calibrations points with the mouse

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