source: trunk/src/mouse_up.m @ 156

Last change on this file since 156 was 156, checked in by sommeria, 13 years ago

many bug repairs and corrections for mouse action
create_grid: option for black marjkers for grid detection

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