source: trunk/src/mouse_up.m @ 429

Last change on this file since 429 was 429, checked in by sommeria, 12 years ago

improvements in object drawing and projection on points

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