source: trunk/src/mouse_up.m @ 146

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

bugs corrected for projection objects

File size: 17.6 KB
RevLine 
[11]1%'mouse_up': function  activated when the mouse button is released
[89]2%------------------------------------------------------------------------
[71]3% function mouse_up(hObject,eventdata,handles)
[11]4% activated by the command:
5% set(hObject,'WindowButtonUpFcn',{'mouse_up'}),
6% where hObject is the handle of the figure
[89]7
8%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
[11]9%  Copyright Joel Sommeria, 2008, LEGI / CNRS-UJF-INPG, sommeria@coriolis-legi.org.
[89]10%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
[11]11%     This file is part of the toolbox UVMAT.
12%
13%     UVMAT is free software; you can redistribute it and/or modify
14%     it under the terms of the GNU General Public License as published by
15%     the Free Software Foundation; either version 2 of the License, or
16%     (at your option) any later version.
17%
18%     UVMAT is distributed in the hope that it will be useful,
19%     but WITHOUT ANY WARRANTY; without even the implied warranty of
20%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21%     GNU General Public License (file UVMAT/COPYING.txt) for more details.
[89]22%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
[11]23
[71]24function mouse_up(hObject,eventdata,handles)
[11]25MouseAction='none'; %default
26zoomstate=0;%default
27if ~exist('handles','var')
28   handles=get(gcbo,'UserData');
29end
[102]30huvmat=findobj(allchild(0),'tag','uvmat');%find the uvmat interface handle
[11]31if ~isempty(huvmat)
32    hhuvmat=guidata(huvmat);
33    UvData=get(huvmat,'UserData');
34    if isfield(UvData,'MouseAction')
35        MouseAction=UvData.MouseAction;% set the mouse action (edit, create objects...)
36    end
37    zoomstate=get(hhuvmat.zoom,'Value');
38end
[71]39currentfig=hObject;
[11]40currentaxes=gca; %store the current axes handle
[71]41AxeData=get(currentaxes,'UserData');
42
[11]43test_drawing=0;%default
44
45%finalize the fabrication or the translation/deformation of an object and plot the corresponding projected field
46if ~isempty(huvmat) & isfield(AxeData,'Drawing') & ~isequal(AxeData.Drawing,'off') & isfield(AxeData,'CurrentObject')...
47           & ishandle(AxeData.CurrentObject)
48    xy=get(currentaxes,'CurrentPoint');%xy(1,1),xy(1,2): current x,y positions in axes coordinates
49    PlotData=get(AxeData.CurrentObject,'UserData');%get data attached to the current projection object 
50    IndexObj=PlotData.IndexObj;
51    ObjectData=UvData.Object{IndexObj};   
[71]52    ObjectData.enable_plot=1;
[11]53    if isequal(AxeData.Drawing,'translate')
54        XYData=AxeData.CurrentOrigin;
55        DX=xy(1,1)-XYData(1);%translation from initial position
56        DY=xy(1,2)-XYData(2);
57        ObjectData.Coord(:,1)=ObjectData.Coord(:,1)+DX;
58        ObjectData.Coord(:,2)=ObjectData.Coord(:,2)+DY;
59    elseif isequal(AxeData.Drawing,'deform')
60        ind_move=AxeData.CurrentIndex;
61        ObjectData.Coord(ind_move,1)=xy(1,1);
62        ObjectData.Coord(ind_move,2)=xy(1,2);
63    else   %creating object
[102]64        if strcmp(ObjectData.Style,'line')||strcmp(ObjectData.Style,'polyline')||...
65                strcmp(ObjectData.Style,'polygon')||strcmp(ObjectData.Style,'points')
[11]66            if isfield(AxeData,'ObjectCoord') && size(AxeData.ObjectCoord,2)==3
67              xy(1,3)=AxeData.ObjectCoord(1,3); % z coordinate of the mouse: to generalise ...
68            else
69                 xy(1,3)=0; % z coordinate set to 0 by default
70            end
71            if ~isequal(ObjectData.Coord,xy(1,:))
72                ObjectData.Coord=[ObjectData.Coord ;xy(1,:)];% append the coordiantes marked by the mouse to the eobject
73            end
74        elseif isequal(ObjectData.Style,'rectangle')||isequal(ObjectData.Style,'ellipse')||isequal(ObjectData.Style,'volume')
75            XYData=AxeData.CurrentOrigin;
76            ObjectData.Coord(1,1)=(xy(1,1)+XYData(1))/2;%origin rectangle, x coordinate
77            ObjectData.Coord(1,2)=(xy(1,2)+XYData(2))/2;
78            ObjectData.RangeX=abs(xy(1,1)-XYData(1))/2;%rectangle width
79            ObjectData.RangeY=abs(xy(1,2)-XYData(2))/2;%rectangle height
80        elseif isequal(ObjectData.Style,'plane') %case of 'plane'
81            DX=(xy(1,1)-ObjectData.Coord(1,1));
82            DY=(xy(1,2)-ObjectData.Coord(1,2));
83            ObjectData.Phi=(angle(DX+i*DY))*180/pi;%rectangle widt
84            if isfield(ObjectData,'RangeX')
85                XMax=sqrt(DX*DX+DY*DY);
86                if XMax>max(ObjectData.RangeX)
87                    ObjectData.RangeX=[min(ObjectData.RangeX) XMax];
88                end
89            end
90        end
91    end
92    %set(AxeData.CurrentObject,'UserData',ObjectData); %update the object properties
[102]93    if strcmp(ObjectData.Style,'rectangle')||strcmp(ObjectData.Style,'ellipse')
[11]94        NbDefPoint=1; 
[102]95    elseif strcmp(ObjectData.Style,'line')|| strcmp(ObjectData.Style,'plane');
[11]96        NbDefPoint=2;
97    else
98         NbDefPoint=3;
99    end
100   
101    %show object coordinates in the GUI set_object
[102]102    h_set_object=findobj(allchild(0),'Tag','set_object');
103    hh_set_object=guidata(h_set_object);
104    set(hh_set_object.XObject,'String',num2str(ObjectData.Coord(:,1),4));
105    set(hh_set_object.YObject,'String',num2str(ObjectData.Coord(:,2),4));
106    set(hh_set_object.ZObject,'String',num2str(ObjectData.Coord(:,3),4));
[11]107    if NbDefPoint<=2 || isequal(get(currentfig,'SelectionType'),'alt') ||...
[102]108              strcmp(AxeData.Drawing,'translate') || strcmp(AxeData.Drawing,'deform');%stop drawing
[11]109        AxeData.CurrentOrigin=[]; %suppress the current origin
110       if isequal(ObjectData.Style,'line') && size(ObjectData.Coord,1)<=1
111           AxeData.Drawing='off';
112           set(currentaxes,'UserData',AxeData);
113            return % line needs at leqst two points
114       end
115       if  ~isempty(ObjectData)
116             testmask=0;
117             hmask=findobj(huvmat,'Tag','makemask');
118             if ~isempty(hmask)
119                testmask=get(hmask,'Value');
120             end
121             if testmask
122                 PlotHandles=[];%do not project data on the object during mask creation
123             else
124                 PlotHandles=get_plot_handles(handles);%get the handles of the graphic objects setting the plotting parameters
125             end
126            UvData.Object{IndexObj}=update_obj(UvData,IndexObj,ObjectData,PlotHandles);
127            if  isfield(UvData.Object{IndexObj},'PlotParam')
128                write_plot_param(PlotHandles,UvData.Object{IndexObj}.PlotParam); %update the display of plotting parameters for the current object
[102]129            end   
[11]130            set(hhuvmat.edit,'BackgroundColor',[1 1 0]);% paint the edit text in yellow
131            set(hhuvmat.edit,'Value',1);%
132            set(hhuvmat.edit,'Enable','on');%
[67]133            set(hhuvmat.MenuEditObject,'Enable','on');%
[11]134            set(hhuvmat.MenuEdit,'Enable','on');%
135            UvData.MouseAction='edit_object'; % set the edit button to 'on'
136        end
137    else
138       AxeData.CurrentOrigin=[xy(1,1) xy(1,2)]; %the current point becomes the new current origin
139       test_drawing=1;%allow continuation of drawing object
140       UvData.Object{IndexObj}=ObjectData;
141    end
142    hother=findobj('Tag','deformpoint');%find all the deformpoints
143    set(hother,'Color','b');%reset all the deformpoints in 'blue'
144else
145    test_drawing=0;
146end
147%creation of a new zoom plot
148test_replot=0;
149if isequal(get(currentfig,'SelectionType'),'normal');%if left button has been pressed
150%         FigData=get(currentfig,'UserData');
151        hparentfig=currentfig;
152        %open or update a new zoom figure if a rectangle has been drawn
153        if ishandle(currentaxes);
154            if isfield(AxeData,'CurrentRectZoom') & ishandle(AxeData.CurrentRectZoom)
155                PosRect=get(AxeData.CurrentRectZoom,'Position');
156                if isfield(AxeData,'CurrentVec') & ishandle(AxeData.CurrentVec)
157                    delete(AxeData.CurrentVec)
158                end
[61]159                hfig2=figure;%create new figure
160                set(hfig2,'name','zoom')
161                set(hfig2,'Units','normalized')
[11]162                set(hfig2,'Position',[0.2 0.33 0.6 0.6]);
[61]163                map=colormap(currentaxes);
164                colormap(map);%transmit the current colormap to the zoom fig
165                get(handles.RootFile,'String')
166                set(hfig2,'Position',[0.2 0.33 0.6 0.6]);
[11]167                if test_replot==0
168                    set(hfig2,'Unit','normalized')
169                    set(hfig2,'KeyPressFcn',{@keyboard_callback,handles})%set keyboard action function
170                    set(hfig2,'WindowButtonMotionFcn',{@mouse_motion,handles})%set mouse action function
171                    set(hfig2,'WindowButtonDownFcn',{@mouse_down})%set mouse click action function
172                    set(hfig2,'WindowButtonUpFcn',{@mouse_up,handles}) 
173                    set(hfig2,'DeleteFcn',{@close_fig,AxeData.CurrentRectZoom,'zoom'})
174                    set(hfig2,'UserData',AxeData.CurrentRectZoom)% record the parent object (zoom rectangle) in the new fig
175                    AxeData.ZoomAxes=copyobj(currentaxes,hfig2); %copy the current graph axes to the zoom figure
176                    figure(hfig2)
177                    set(AxeData.ZoomAxes,'Position',[0.1300    0.1100    0.7750    0.8150])% standard axes position on a figure
178                    hcol=findobj(hparentfig,'Tag','Colorbar'); %look for colorbar axes
179                    if ~isempty(hcol)             
180                        hcol_new=colorbar;
181                        YTick=get(hcol,'YTick');
182                        YTicklabel=get(hcol,'Yticklabel'); 
183                        colbarlim=get(hcol,'YLim');
184                        newcolbarlim=get(hcol_new,'YLim');
185                        scale_bar=(newcolbarlim(2)-newcolbarlim(1))/(colbarlim(2)-colbarlim(1));               
186                        YTick_rescaled=newcolbarlim(1)+scale_bar*(YTick-colbarlim(1));
187                        set(hcol_new,'YTick',YTick_rescaled);
188                        set(hcol_new,'Yticklabel',YTicklabel);
189                    end
190                end
191                if ishandle(AxeData.ZoomAxes)
192                    hnew_rect=findobj(AxeData.ZoomAxes,'Tag','rect_zoom');
193                    if ~isempty(hnew_rect)
194                        delete(hnew_rect);
195                        ChildAxeData=get(AxeData.ZoomAxes,'UserData');
196                        ChildAxeData.CurrentRectZoom=[]; % no rect zoom in the new window
197                        ChildAxeData.Drawing='off';
198                        ChildAxeData.ParentRect=AxeData.CurrentRectZoom;%set the rectangle as a 'parent' associated to the new axes
199                        set(AxeData.ZoomAxes,'UserData',ChildAxeData);%update the AxeData of the new axes
200                        set(AxeData.ZoomAxes,'Xlim',[PosRect(1) PosRect(1)+PosRect(3)])
201                        set(AxeData.ZoomAxes,'Ylim',[PosRect(2) PosRect(2)+PosRect(4)])
202                    end
203                end
204            end
205        end
206end
[82]207%zoom in by a factor 2 if no new figure is created
[11]208if zoomstate
[82]209    xy=get(currentaxes,'CurrentPoint');%xy(1,1),xy(1,2): current x,y positions in axes coordinates
[11]210     if  isequal(get(currentfig,'SelectionType'),'normal');%if left button has been pressed
211        xlim=get(currentaxes,'XLim');
[82]212        xlim_new(2)=0.5*xy(1,1)+0.5*xlim(2);
213        xlim_new(1)=0.5*xy(1,1)+0.5*xlim(1);
[11]214        set(currentaxes,'XLim',xlim_new)
215        ylim=get(currentaxes,'YLim');
[82]216        ylim_new(2)=0.5*xy(1,2)+0.5*ylim(2);
217        ylim_new(1)=0.5*xy(1,2)+0.5*ylim(1);
[11]218        set(currentaxes,'YLim',ylim_new)
219        if isfield(AxeData,'ParentRect')% update the position of the parent rectangle represneting the field
220            hparentrect=AxeData.ParentRect;
221            xlim=get(currentaxes,'XLim');
222            ylim=get(currentaxes,'YLim');
223            rect([1 2])=[xlim(1) ylim(1)];
224            rect([3 4])=[xlim(2)-xlim(1) ylim(2)-ylim(1)];
225            set(hparentrect,'Position',rect)
226        end
[82]227     %zoom out by a factor of 2 out when the right mouse button has been used
[11]228     elseif isequal(get(currentfig,'SelectionType'),'alt'); %if right button has been pressed
[102]229%             alpha=2; %zoom factor (zoom out by a factor 2)
[11]230            xlim=get(currentaxes,'XLim');
[82]231            xlim_new(1)=2*xlim(1)-xy(1,1);
232            xlim_new(2)=2*xlim(2)-xy(1,1);
233%             xlim_new(1)=(1+alpha)*xlim(1)/2+(1-alpha)*xlim(2)/2;
234%             xlim_new(2)=(1-alpha)*xlim(1)/2+(1+alpha)*xlim(2)/2;
[11]235            ylim=get(currentaxes,'YLim');
[82]236            ylim_new(1)=2*ylim(1)-xy(1,2);
237            ylim_new(2)=2*ylim(2)-xy(1,2);
238%             ylim_new(1)=(1+alpha)*ylim(1)/2+(1-alpha)*ylim(2)/2;
239%             ylim_new(2)=(1-alpha)*ylim(1)/2+(1+alpha)*ylim(2)/2;
240            if isfield(AxeData,'RangeX') && isfield(AxeData,'RangeY')
241                xlim_new(1)=max(AxeData.RangeX(1),xlim_new(1));
242                xlim_new(2)=min(AxeData.RangeX(2),xlim_new(2));
243                ylim_new(1)=max(AxeData.RangeY(1),ylim_new(1));
244                ylim_new(2)=min(AxeData.RangeY(2),ylim_new(2));
245                if isequal(xlim_new,AxeData.RangeX) && isequal(ylim_new,AxeData.RangeY)
246                    set(hhuvmat.zoom,'Value',0)
247                    set(hhuvmat.zoom,'BackgroundColor',[0.7 0.7 0.7])
248                    set(hhuvmat.FixedLimits,'Value',0)
249                    set(hhuvmat.FixedLimits,'BackgroundColor',[0.7 0.7 0.7])
250                end
251            end
[11]252            set(currentaxes,'XLim',xlim_new)
253            set(currentaxes,'YLim',ylim_new)
254            %test whther zoom out is operating (to inactivate AxedAta
[82]255            if ~isfield(AxeData,'CurrentXLim')|| ~isequal(xlim,AxeData.CurrentXLim)
[11]256                AxeData.CurrentXLim=xlim;%
257            end
258            if isfield(AxeData,'ParentRect')% update the position of the parent rectangle represneting the field
259                hparentrect=AxeData.ParentRect;
260                xlim=get(currentaxes,'XLim');
261                ylim=get(currentaxes,'YLim');
262                rect([1 2])=[xlim(1) ylim(1)];
263                rect([3 4])=[xlim(2)-xlim(1) ylim(2)-ylim(1)];
264                set(hparentrect,'Position',rect)
265            end
266      end
267end
[67]268
269% editing calibration point
[81]270if ~zoomstate && strcmp(MouseAction,'calib')
[78]271    h_geometry_calib=findobj(allchild(0),'Name','geometry_calib'); %find the geomterty_calib GUI
272    hh_geometry_calib=guidata(h_geometry_calib);
273    edit_test=get(hh_geometry_calib.edit_append,'Value');
274    hh=findobj(currentaxes,'Tag','calib_points');%look for handle of calibration points           
275    if ~isempty(hh) && edit_test
276        index_point=get(hh,'UserData');
[67]277        set(hh,'UserData',[])%remove edit mode
[71]278        h_ListCoord=hh_geometry_calib.ListCoord; %handles of the coordinate list
279        Coord=get(h_ListCoord,'String');
[78]280%         val=get(h_ListCoord,'Value');
281        coord_str=Coord{index_point}; %current line (string)
[71]282        k=findstr('|',coord_str);%find separator indices on the string
283        xy=get(currentaxes,'CurrentPoint');%xy(1,1),xy(1,2): current x,y positions in axes coordinates
284        if numel(k)>=3
285            coord_str=[coord_str(1:k(3)-1) '|    ' num2str(xy(1,1),4) '    |    ' num2str(xy(1,2),4)]; %update the pixel information while preserving phys coord
286        else
287            coord_str=[ '    |    '  '    |    '  '    |    ' num2str(xy(1,1),4) '    |    ' num2str(xy(1,2),4)];
288        end
[78]289        Coord{index_point}=coord_str;       
[71]290        set(h_ListCoord,'String',Coord)
[78]291        data=read_geometry_calib(Coord);%transform char cell to numbers
292        set(hh,'XData',data.Coord(:,4))
293        set(hh,'YData',data.Coord(:,5))
[71]294    end
[67]295end
296
297% finalising ruler
298if strcmp(MouseAction,'ruler')
[60]299    UvData.MouseAction='none';
[61]300    UvData=rmfield(UvData,'RulerHandle');
[60]301     xy=get(currentaxes,'CurrentPoint');
[61]302    RulerCoord=[UvData.RulerCoord ;xy(1,1:2)];
[60]303    set(huvmat,'UserData',UvData)
[61]304    RulerCoord=diff(RulerCoord,1);
[60]305    RulerCoord=RulerCoord(1)+i*RulerCoord(2);
[61]306    distance=abs(RulerCoord);
307    azimuth=(180/pi)*angle(RulerCoord);
[60]308    msgbox_uvmat('RULER','',['length: ' num2str(distance,3) ',  angle(degrees): ' num2str(azimuth,3)])
[61]309    hruler=findobj(currentaxes,'Tag','ruler');
310    delete(hruler)
[71]311    AxeData.Drawing='off';%stop current drawing a
[60]312end
[11]313
[60]314
[11]315%display the data of the current object selected with the mouse right click
316if isequal(get(currentfig,'SelectionType'),'alt') && ~zoomstate && (~isfield(AxeData,'Drawing')||~isequal(AxeData.Drawing,'create'))
317    hother=findobj('Tag','proj_object');%find all the proj objects
318    nbselect=0;
319    %test the existence of selected objects:
320    for iproj=1:length(hother);
321        iselect=isequal(get(hother(iproj),'Selected'),'on');%reset all the proj objects in 'blue' by default
322        nbselect=nbselect+iselect;
323    end
324    hother=findobj('Tag','proj_object','Type','line');%find all the proj objects
325    set(hother,'Color','b');%reset all the proj objects in 'blue' by default
326    set(hother,'Selected','off')
327    hother=findobj('Tag','proj_object','Type','rectangle');
328    set(hother,'EdgeColor','b');
329    set(hother,'Selected','off')
330    hother=findobj('Tag','proj_object','Type','patch');
331    set(hother,'FaceColor','b');   
332    if isequal(get(gco,'Type'),'image')
333        currentobj=get(gco,'parent');%parent axes of the image
334    else
335        currentobj=gco;%default
336    end
337    if ((nbselect==0) && isequal(get(currentobj,'Type'),'axes')) || isequal(currentobj,huvmat)
[102]338        currentfig=get(currentobj,'parent');
339        figname=get(currentfig,'name');
340        eval(['global Data_' figname])
341        eval(['Data_' figname '=get(currentobj,''UserData'')']);
342        evalin('base',['global Data_' figname])%make CurData global in the workspace
[11]343        objtype=get(currentobj,'Type');
344        display(['UserData of ' objtype ':'])
[102]345        evalin('base',['Data_' figname]) %display CurData in the workspace
[82]346        commandwindow %brings the Matlab command window to the front
[11]347    end
348end
349if test_drawing==0
350        AxeData.Drawing='off';%stop current drawing action
351end
352set(currentaxes,'UserData',AxeData);
353if ~isempty(huvmat)
354    set(huvmat,'UserData',UvData);
355end
356
357   
358
Note: See TracBrowser for help on using the repository browser.