source: trunk/src/mouse_up.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: 17.1 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%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
9%  Copyright Joel Sommeria, 2008, LEGI / CNRS-UJF-INPG, sommeria@coriolis-legi.org.
10%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
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%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
23
24function mouse_up(hObject,eventdata,handles)
25MouseAction='none'; %default
26zoomstate=0;%default
27if ~exist('handles','var')
28   handles=get(gcbo,'UserData');
29end
30huvmat=findobj(allchild(0),'Name','uvmat');%find the uvmat interface handle
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
39currentfig=hObject;
40currentaxes=gca; %store the current axes handle
41AxeData=get(currentaxes,'UserData');
42
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};   
52    ObjectData.enable_plot=1;
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
64        if isequal(ObjectData.Style,'line')||isequal(ObjectData.Style,'polyline')||...
65                isequal(ObjectData.Style,'polygon')||isequal(ObjectData.Style,'points')
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
93    if isequal(ObjectData.Style,'rectangle')||isequal(ObjectData.Style,'ellipse')
94        NbDefPoint=1; 
95    elseif isequal(ObjectData.Style,'line')|| isequal(ObjectData.Style,'plane');
96        NbDefPoint=2;
97    else
98         NbDefPoint=3;
99    end
100   
101    %show object coordinates in the GUI set_object
102    h_set_object=findobj(allchild(0),'Name','set_object');
103    h_XObject=findobj(h_set_object,'Tag','XObject');
104    h_YObject=findobj(h_set_object,'Tag','YObject');
105    h_ZObject=findobj(h_set_object,'Tag','ZObject');
106    set(h_XObject,'String',num2str(ObjectData.Coord(:,1),4));
107    set(h_YObject,'String',num2str(ObjectData.Coord(:,2),4));
108    set(h_ZObject,'String',num2str(ObjectData.Coord(:,3),4));
109    if NbDefPoint<=2 || isequal(get(currentfig,'SelectionType'),'alt') ||...
110              isequal(AxeData.Drawing,'translate') || isequal(AxeData.Drawing,'deform');%stop drawing
111        AxeData.CurrentOrigin=[]; %suppress the current origin
112       if isequal(ObjectData.Style,'line') && size(ObjectData.Coord,1)<=1
113           AxeData.Drawing='off';
114           set(currentaxes,'UserData',AxeData);
115            return % line needs at leqst two points
116       end
117       if  ~isempty(ObjectData)
118             testmask=0;
119             hmask=findobj(huvmat,'Tag','makemask');
120             if ~isempty(hmask)
121                testmask=get(hmask,'Value');
122             end
123             if testmask
124                 PlotHandles=[];%do not project data on the object during mask creation
125             else
126                 PlotHandles=get_plot_handles(handles);%get the handles of the graphic objects setting the plotting parameters
127             end
128            AxeData.hset_object=set_object(ObjectData,PlotHandles);% call the set_object interface ,*
129            UvData.Object{IndexObj}=update_obj(UvData,IndexObj,ObjectData,PlotHandles);
130            %ObjectData=update_obj(UvData,IndexObj,ObjectData,PlotHandles);
131            if  isfield(UvData.Object{IndexObj},'PlotParam')
132                write_plot_param(PlotHandles,UvData.Object{IndexObj}.PlotParam); %update the display of plotting parameters for the current object
133            end             
134%             set(hhuvmat.create,'Value',0);% set to 'off' the button for object creation
135%             set(hhuvmat.create,'BackgroundColor',[0 1 0]);% paint the creation button in green
136            set(hhuvmat.edit,'BackgroundColor',[1 1 0]);% paint the edit text in yellow
137            set(hhuvmat.edit,'Value',1);%
138            set(hhuvmat.edit,'Enable','on');%
139            set(hhuvmat.MenuEditObject,'Enable','on');%
140            set(hhuvmat.MenuEdit,'Enable','on');%
141%             set(hhuvmat.MenuObject,'Enable','on');%
142            UvData.MouseAction='edit_object'; % set the edit button to 'on'
143        end
144    else
145       AxeData.CurrentOrigin=[xy(1,1) xy(1,2)]; %the current point becomes the new current origin
146       test_drawing=1;%allow continuation of drawing object
147       UvData.Object{IndexObj}=ObjectData;
148    end
149    hother=findobj('Tag','deformpoint');%find all the deformpoints
150    set(hother,'Color','b');%reset all the deformpoints in 'blue'
151else
152    test_drawing=0;
153end
154
155%creation of a new zoom plot
156test_replot=0;
157if isequal(get(currentfig,'SelectionType'),'normal');%if left button has been pressed
158%         FigData=get(currentfig,'UserData');
159        hparentfig=currentfig;
160        %open or update a new zoom figure if a rectangle has been drawn
161        if ishandle(currentaxes);
162            if isfield(AxeData,'CurrentRectZoom') & ishandle(AxeData.CurrentRectZoom)
163                PosRect=get(AxeData.CurrentRectZoom,'Position');
164                if isfield(AxeData,'CurrentVec') & ishandle(AxeData.CurrentVec)
165                    delete(AxeData.CurrentVec)
166                end
167                hfig2=figure;%create new figure
168                set(hfig2,'name','zoom')
169                set(hfig2,'Units','normalized')
170                set(hfig2,'Position',[0.2 0.33 0.6 0.6]);
171                map=colormap(currentaxes);
172                colormap(map);%transmit the current colormap to the zoom fig
173                get(handles.RootFile,'String')
174                set(hfig2,'Position',[0.2 0.33 0.6 0.6]);
175                if test_replot==0
176                    set(hfig2,'Unit','normalized')
177                    set(hfig2,'KeyPressFcn',{@keyboard_callback,handles})%set keyboard action function
178                    set(hfig2,'WindowButtonMotionFcn',{@mouse_motion,handles})%set mouse action function
179                    set(hfig2,'WindowButtonDownFcn',{@mouse_down})%set mouse click action function
180                    set(hfig2,'WindowButtonUpFcn',{@mouse_up,handles}) 
181                    set(hfig2,'DeleteFcn',{@close_fig,AxeData.CurrentRectZoom,'zoom'})
182                    set(hfig2,'UserData',AxeData.CurrentRectZoom)% record the parent object (zoom rectangle) in the new fig
183                    AxeData.ZoomAxes=copyobj(currentaxes,hfig2); %copy the current graph axes to the zoom figure
184                    figure(hfig2)
185                    set(AxeData.ZoomAxes,'Position',[0.1300    0.1100    0.7750    0.8150])% standard axes position on a figure
186                    hcol=findobj(hparentfig,'Tag','Colorbar'); %look for colorbar axes
187                    if ~isempty(hcol)             
188                        hcol_new=colorbar;
189                        YTick=get(hcol,'YTick');
190                        YTicklabel=get(hcol,'Yticklabel'); 
191                        colbarlim=get(hcol,'YLim');
192                        newcolbarlim=get(hcol_new,'YLim');
193                        scale_bar=(newcolbarlim(2)-newcolbarlim(1))/(colbarlim(2)-colbarlim(1));               
194                        YTick_rescaled=newcolbarlim(1)+scale_bar*(YTick-colbarlim(1));
195                        set(hcol_new,'YTick',YTick_rescaled);
196                        set(hcol_new,'Yticklabel',YTicklabel);
197                    end
198                end
199                if ishandle(AxeData.ZoomAxes)
200                    hnew_rect=findobj(AxeData.ZoomAxes,'Tag','rect_zoom');
201                    if ~isempty(hnew_rect)
202                        delete(hnew_rect);
203                        ChildAxeData=get(AxeData.ZoomAxes,'UserData');
204                        ChildAxeData.CurrentRectZoom=[]; % no rect zoom in the new window
205                        ChildAxeData.Drawing='off';
206                        ChildAxeData.ParentRect=AxeData.CurrentRectZoom;%set the rectangle as a 'parent' associated to the new axes
207                        set(AxeData.ZoomAxes,'UserData',ChildAxeData);%update the AxeData of the new axes
208                        set(AxeData.ZoomAxes,'Xlim',[PosRect(1) PosRect(1)+PosRect(3)])
209                        set(AxeData.ZoomAxes,'Ylim',[PosRect(2) PosRect(2)+PosRect(4)])
210                    end
211                end
212            end
213        end
214end
215%zoom in if no new figure is created
216if zoomstate
217     if  isequal(get(currentfig,'SelectionType'),'normal');%if left button has been pressed
218        %zoom(2)% zoom in by a factor of 2
219        alpha=0.5; %zoom factor (zoom in by a factor 2)
220        xlim=get(currentaxes,'XLim');
221        xlim_new(1)=(1+alpha)*xlim(1)/2+(1-alpha)*xlim(2)/2;
222        xlim_new(2)=(1-alpha)*xlim(1)/2+(1+alpha)*xlim(2)/2;
223        set(currentaxes,'XLim',xlim_new)
224        ylim=get(currentaxes,'YLim');
225        ylim_new(1)=(1+alpha)*ylim(1)/2+(1-alpha)*ylim(2)/2;
226        ylim_new(2)=(1-alpha)*ylim(1)/2+(1+alpha)*ylim(2)/2;
227        set(currentaxes,'YLim',ylim_new)
228        if isfield(AxeData,'ParentRect')% update the position of the parent rectangle represneting the field
229            hparentrect=AxeData.ParentRect;
230            xlim=get(currentaxes,'XLim');
231            ylim=get(currentaxes,'YLim');
232            rect([1 2])=[xlim(1) ylim(1)];
233            rect([3 4])=[xlim(2)-xlim(1) ylim(2)-ylim(1)];
234            set(hparentrect,'Position',rect)
235        end
236
237     elseif isequal(get(currentfig,'SelectionType'),'alt'); %if right button has been pressed
238            %zoom(0.5)% zoom out by a factor of 2
239            alpha=2; %zoom factor (zoom out by a factor 2)
240            xlim=get(currentaxes,'XLim');
241            xlim_new(1)=(1+alpha)*xlim(1)/2+(1-alpha)*xlim(2)/2;
242            xlim_new(2)=(1-alpha)*xlim(1)/2+(1+alpha)*xlim(2)/2;
243            ylim=get(currentaxes,'YLim');
244            ylim_new(1)=(1+alpha)*ylim(1)/2+(1-alpha)*ylim(2)/2;
245            ylim_new(2)=(1-alpha)*ylim(1)/2+(1+alpha)*ylim(2)/2;
246            set(currentaxes,'XLim',xlim_new)
247            set(currentaxes,'YLim',ylim_new)
248            %test whther zoom out is operating (to inactivate AxedAta
249            if ~isfield(AxeData,'CurrentXLim')| ~isequal(xlim,AxeData.CurrentXLim)
250                AxeData.CurrentXLim=xlim;%
251            end
252            if isfield(AxeData,'ParentRect')% update the position of the parent rectangle represneting the field
253                hparentrect=AxeData.ParentRect;
254                xlim=get(currentaxes,'XLim');
255                ylim=get(currentaxes,'YLim');
256                rect([1 2])=[xlim(1) ylim(1)];
257                rect([3 4])=[xlim(2)-xlim(1) ylim(2)-ylim(1)];
258                set(hparentrect,'Position',rect)
259            end
260      end
261end
262
263% editing calibration point
264if ~zoomstate strcmp(MouseAction,'calib')
265    h_geometry_calib=findobj(allchild(0),'Name','geometry_calib'); %find the geomterty_calib GUI
266    hh_geometry_calib=guidata(h_geometry_calib);
267    edit_test=get(hh_geometry_calib.edit_append,'Value');
268    hh=findobj(currentaxes,'Tag','calib_points');%look for handle of calibration points           
269    if ~isempty(hh) && edit_test
270        index_point=get(hh,'UserData');
271        set(hh,'UserData',[])%remove edit mode
272        h_ListCoord=hh_geometry_calib.ListCoord; %handles of the coordinate list
273        Coord=get(h_ListCoord,'String');
274%         val=get(h_ListCoord,'Value');
275        coord_str=Coord{index_point}; %current line (string)
276        k=findstr('|',coord_str);%find separator indices on the string
277        xy=get(currentaxes,'CurrentPoint');%xy(1,1),xy(1,2): current x,y positions in axes coordinates
278        if numel(k)>=3
279            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
280        else
281            coord_str=[ '    |    '  '    |    '  '    |    ' num2str(xy(1,1),4) '    |    ' num2str(xy(1,2),4)];
282        end
283        Coord{index_point}=coord_str;       
284        set(h_ListCoord,'String',Coord)
285        data=read_geometry_calib(Coord);%transform char cell to numbers
286        set(hh,'XData',data.Coord(:,4))
287        set(hh,'YData',data.Coord(:,5))
288    end
289end
290
291% finalising ruler
292if strcmp(MouseAction,'ruler')
293    UvData.MouseAction='none';
294    UvData=rmfield(UvData,'RulerHandle');
295     xy=get(currentaxes,'CurrentPoint');
296    RulerCoord=[UvData.RulerCoord ;xy(1,1:2)];
297    set(huvmat,'UserData',UvData)
298    RulerCoord=diff(RulerCoord,1);
299    RulerCoord=RulerCoord(1)+i*RulerCoord(2);
300    distance=abs(RulerCoord);
301    azimuth=(180/pi)*angle(RulerCoord);
302    msgbox_uvmat('RULER','',['length: ' num2str(distance,3) ',  angle(degrees): ' num2str(azimuth,3)])
303    hruler=findobj(currentaxes,'Tag','ruler');
304    delete(hruler)
305    AxeData.Drawing='off';%stop current drawing a
306end
307
308
309%display the data of the current object selected with the mouse right click
310if isequal(get(currentfig,'SelectionType'),'alt') && ~zoomstate && (~isfield(AxeData,'Drawing')||~isequal(AxeData.Drawing,'create'))
311    hother=findobj('Tag','proj_object');%find all the proj objects
312    nbselect=0;
313    %test the existence of selected objects:
314    for iproj=1:length(hother);
315        iselect=isequal(get(hother(iproj),'Selected'),'on');%reset all the proj objects in 'blue' by default
316        nbselect=nbselect+iselect;
317    end
318    hother=findobj('Tag','proj_object','Type','line');%find all the proj objects
319    set(hother,'Color','b');%reset all the proj objects in 'blue' by default
320    set(hother,'Selected','off')
321    hother=findobj('Tag','proj_object','Type','rectangle');
322    set(hother,'EdgeColor','b');
323    set(hother,'Selected','off')
324    hother=findobj('Tag','proj_object','Type','patch');
325    set(hother,'FaceColor','b');   
326    if isequal(get(gco,'Type'),'image')
327        currentobj=get(gco,'parent');%parent axes of the image
328    else
329        currentobj=gco;%default
330    end
331    if ((nbselect==0) && isequal(get(currentobj,'Type'),'axes')) || isequal(currentobj,huvmat)
332        global CurData
333        CurData=get(currentobj,'UserData');
334        %plot_text(CurData)
335        %get_field([],CurData);
336        evalin('base','global CurData')%make CurData global in the workspace
337        objtype=get(currentobj,'Type');
338        display(['UserData of ' objtype ':'])
339        evalin('base','CurData') %display CurData in the workspace
340        commandwindow
341    end
342end
343if test_drawing==0
344        AxeData.Drawing='off';%stop current drawing action
345end
346set(currentaxes,'UserData',AxeData);
347if ~isempty(huvmat)
348    set(huvmat,'UserData',UvData);
349end
350
351   
352
Note: See TracBrowser for help on using the repository browser.