source: trunk/src/mouse_down.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.5 KB
Line 
1%'mouse_down': function activated when the mouse button is pressed on a figure (callback for 'WindowButtonDownFcn'
2%--------------------------------------------------------------
3% xy=mouse_down(hObject,eventdata)
4% activated by the command:
5% set(hObject,'WindowButtonDownFcn',{'mouse_down'}),
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 xy=mouse_down(hObject,eventdata)
25
26MouseAction='none'; %default
27huvmat=findobj(allchild(0),'tag','uvmat');%find the uvmat interface handle which controls theoption of  mouse action
28if isempty(huvmat)
29    return
30end
31hhuvmat=guidata(huvmat);%handles of elements in uvmat
32UvData=get(huvmat,'UserData');
33MouseAction='none'; %default
34testzoom=get(hhuvmat.zoom,'Value');% get the mouse action from the uvmat GUI: options:
35if isfield(UvData,'MouseAction')
36    MouseAction=UvData.MouseAction;% get the mouse action from the uvmat GUI: options:
37end
38
39test_create=~testzoom && (isequal(MouseAction,'create_object') || isequal(MouseAction,'create_mask'));
40%test_cal=get(handles.cal,'Value');
41test_cal=strcmp(MouseAction,'calib');
42test_ruler=strcmp(MouseAction,'ruler');
43test_edit=strcmp(MouseAction,'edit_object');
44test_edit_vect=strcmp(MouseAction,'edit_vect');
45xdisplay=[];%default
46ydisplay=[];%default
47AxeData=[];%default
48
49%edit an existing point or line if found
50hcurrentobject=gco;% current object handle (selected by the mouse)
51hcurrentfig=hObject;% current figure handle
52fig_tag=get(hcurrentfig,'Tag');
53tag_obj=get(gco,'Tag');
54xy=[];%default
55xy_fig=get(hcurrentfig,'CurrentPoint');% current point of the current figure (gcbo)
56hchild=get(hcurrentfig,'Children');%handles of all objects in the current figure
57haxes=[];
58% loop on all the objects in the current figure (selected by the last mouse click)
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 object child of the current figure
63        %if the mouse is over an axis, look at the data
64        if isequal(htype,'axes')
65            y_lim=get(hchild(ichild),'YLim');
66            x_lim=get(hchild(ichild),'XLim');
67            haxes=hchild(ichild);
68            xy=get(haxes,'CurrentPoint');%xy(1,1),xy(1,2): current x,y positions in axes coordinates
69            if xy(1,1)>x_lim(1) && xy(1,1)<x_lim(2) && xy(1,2)>y_lim(1) && xy(1,2)<y_lim(2)
70                AxeData=get(haxes,'UserData');% data attached to the axis
71                AxeData.CurrentOrigin=[xy(1,1) xy(1,2)];% The current point set by the mouse becomes the current origin
72                if ~isequal(tag_obj,'proj_object') & ~test_create
73                    x_mouse=xy(1,1);%default
74                    y_mouse=xy(1,2);%default
75                    u_mouse=[];
76                    v_mouse=[];
77                    w_mouse=[];
78                    A_mouse=[];
79                    c_text=[];
80                    f_text=[];
81                    ff_text=[];     
82                    ivec=[];   
83                    if isfield(AxeData,'X') & isfield(AxeData,'Y') & isfield(AxeData,'Mesh')% test on the existence of a vector field in the current axis
84                        flag_vec=(AxeData.X<(xy(1,1)+AxeData.Mesh/4) & AxeData.X>(xy(1,1)-AxeData.Mesh/4)) & ...%flagx=1 for the vectors with x position selected by the mouse
85                          (AxeData.Y<(xy(1,2)+AxeData.Mesh/4) & AxeData.Y>(xy(1,2)-AxeData.Mesh/4));%f
86                        ivec=find(flag_vec,1);% search the (first) selected vector index ivec
87                    end
88                end
89            else
90                haxes=[];%mouse out of axes
91            end
92        end
93    end
94end
95test2D=0;
96if isfield(AxeData,'NbDim')
97    if isequal(AxeData.NbDim,2)
98        test2D=1;
99    end
100end
101if ~test2D     %desable  object creation and vector editing if NbDim different from 2
102    test_create=0;
103    test_edit_vect=0;
104end
105%delete the current zoom rectangle
106if isfield(AxeData,'CurrentRectZoom') & ishandle(AxeData.CurrentRectZoom)
107    delete(AxeData.CurrentRectZoom)
108    AxeData.CurrentRectZoom=[];
109end   
110
111% zoom has first priority
112if testzoom %&& ~test_create && ~test_edit && ~test_edit_vect && exist('xy','var')
113     AxeData.Drawing='zoom'; %initiate drawing mode
114     AxeData.CurrentObject=[];%unselect objects
115     set(haxes,'UserData',AxeData);
116     return
117end
118if isempty(huvmat)
119    return
120end
121
122%ruler has second priority
123if test_ruler
124    UvData.RulerCoord(1,1)=xy(1,1);
125    UvData.RulerCoord(1,2)=xy(1,2);
126    UvData.RulerHandle=line([xy(1,1) xy(1,1)],[xy(1,2) xy(1,2)],'Color','m','Tag','ruler');
127    set(huvmat,'UserData',UvData)
128    AxeData.Drawing='ruler';
129    set(haxes,'UserData',AxeData);
130    return
131end
132
133%selection of an existing projection object
134if  test_edit && (isequal(tag_obj,'proj_object')||isequal(tag_obj,'DeformPoint'))
135    if ~(isfield(AxeData,'Drawing') && isequal(AxeData.Drawing,'create'))
136        userdata=get(hcurrentobject,'UserData');
137        if ishandle(userdata)%the selected line depends on a parent line
138            AxeData.CurrentObject=userdata;% the parent object becomes the current one
139        else
140            AxeData.CurrentObject=hcurrentobject;% the selected object becomes the current one
141        end
142        ObjectData=get(AxeData.CurrentObject,'UserData');
143        if test_edit & isfield(ObjectData,'IndexObj')
144            hother=findobj('Tag','proj_object','Type','line');%find all the proj objects
145            set(hother,'Color','b');%reset all the proj objects in 'blue' by default
146            set(hother,'Selected','off')
147            hother=findobj('Tag','proj_object','Type','rectangle');
148            set(hother,'EdgeColor','b');
149            set(hother,'Selected','off');
150            hother=findobj('Tag','proj_object','Type','image');
151            for iobj=1:length(hother)
152                   Acolor=get(hother(iobj),'CData');
153                   Acolor(:,:,1)=zeros(size(Acolor,1),size(Acolor,2));
154                   set(hother(iobj),'CData',Acolor);
155            end
156            hother=findobj('Tag','DeformPoint');
157            set(hother,'Color','b');
158            set(hother,'Selected','off')   
159            if isequal(get(AxeData.CurrentObject,'Type'),'line')
160                set(AxeData.CurrentObject,'Color','m'); %set the selected object to magenta color
161            elseif isequal(get(AxeData.CurrentObject,'Type'),'rectangle')
162                 set(AxeData.CurrentObject,'EdgeColor','m'); %set the selected object to magenta color
163            end
164            if isfield(ObjectData,'SubObject')& ishandle(ObjectData.SubObject)
165                for iobj=1:length(ObjectData.SubObject)
166                    hsub=ObjectData.SubObject(iobj);
167                    if isequal(get(hsub,'Type'),'rectangle')
168                        set(hsub,'EdgeColor','m'); %set the selected object to magenta color
169                    elseif isequal(get(hsub,'Type'),'image')
170                       Acolor=get(hsub,'CData');
171                       Acolor(:,:,1)=Acolor(:,:,3);
172                       set(hsub,'CData',Acolor);
173                    else
174                        set(hsub,'Color','m')
175                    end
176                end
177            end
178            if isequal(tag_obj,'DeformPoint')
179                 set(hcurrentobject,'Color','m'); %set the selected DeformPoint to magenta color
180            end
181            IndexObj=ObjectData.IndexObj;
182                    %indicate on the list of the GUI uvmat which object has been selected
183            if strcmp(get(hcurrentfig,'tag'),'uvmat') %if the uvmat graph has been selected, object projection is on the other frame view_field
184                set(hhuvmat.list_object_2,'Value',IndexObj);
185            else
186                set(hhuvmat.list_object_1,'Value',IndexObj);
187            end
188            testdeform=0;
189            set(gcbo,'Pointer','circle');
190            AxeData.Drawing='deform';
191            if isequal(tag_obj,'DeformPoint')       
192               if isfield(ObjectData,'DeformPoint')
193                   set(hcurrentobject,'Selected','on')
194                   for ipt=1:length(ObjectData.DeformPoint)
195                       if isequal(ObjectData.DeformPoint(ipt),hcurrentobject)
196                            AxeData.CurrentIndex=ipt;
197                            testdeform=1;
198                       end
199                   end
200               end
201            end
202            if testdeform==0
203                AxeData.Drawing='translate';
204                set(AxeData.CurrentObject,'Selected','on')
205                set(gcbo,'Pointer','fleur');
206            end
207        end
208    end
209end
210%  create new projection  object
211if  test_create && ~isempty(xy) && ~(isfield(AxeData,'Drawing')&& isequal(AxeData.Drawing,'create'))
212        ObjectData=read_set_object(UvData.sethandles);
213        ObjectData.Coord=[]; %reset previous object coordinates
214        ObjectData.Coord(1,1)=xy(1,1);
215        ObjectData.Coord(1,2)=xy(1,2);
216        ObjectData.Coord(1,3)=0;
217        if isfield(AxeData,'ObjectCoord') & size(AxeData.ObjectCoord,2)==3
218             ObjectData.Coord(1,3)=AxeData.ObjectCoord(1,3); %generaliser au cas avec angle
219        end
220        AxeData.CurrentObject=plot_object(ObjectData,[],haxes,'m');%draw the object and its handle becomes AxeData.CurrentObject
221        if isfield(UvData,'Object')
222            IndexObj=length(UvData.Object)+1;% add the object as index IndexObj on the list of the interface
223        else
224            IndexObj=2;
225        end 
226        UvData.Object{IndexObj}=ObjectData;
227        UvData.Object{IndexObj}.HandlesDisplay(1)=AxeData.CurrentObject;
228        set(huvmat,'UserData',UvData)
229        list_str=get(hhuvmat.list_object_1,'String');
230        list_str{IndexObj}=[num2str(IndexObj) '-' ObjectData.Style];
231        set(hhuvmat.list_object_1,'String',list_str)
232        list_str{end+1}='...';
233        set(hhuvmat.list_object_2,'String',list_str)
234        if strcmp(fig_tag,'view_field')%we are in view_field plot
235              set(hhuvmat.list_object_1,'Value',IndexObj)% the projection field will be plotted in uvmat frame
236        else%we are in uvmat plot
237            set(hhuvmat.list_object_2,'Value',IndexObj)
238        end
239        PlotData=get(AxeData.CurrentObject,'UserData');
240        PlotData.IndexObj=IndexObj;
241        set(AxeData.CurrentObject,'UserData',PlotData); %record the object index in the graph
242        AxeData.Drawing='create';
243end
244
245% create calibration points if the GUI geometry_calib is opened, if the main axes axes3 of uvmat has ben selected
246if ~testzoom && test_cal && ~isempty(haxes) && strcmp(get(haxes,'tag'),'axes3')
247    h_geometry_calib=findobj(allchild(0),'Name','geometry_calib'); %find the geomterty_calib GUI
248    hh_geometry_calib=guidata(h_geometry_calib);
249    h_ListCoord=hh_geometry_calib.ListCoord; %findobj(h_geometry_calib,'Tag','ListCoord');
250    h_edit_append=hh_geometry_calib.edit_append;%findobj(h_geometry_calib,'Tag','edit_append');
251    if isequal(get(h_edit_append,'Value'),1) && ~isempty(haxes)
252        coord_value=get(hhuvmat.transform_fct,'Value');% set uvmat to pixel coordinates, run it again if not
253        if ~(isequal(coord_value,1)||isequal(coord_value,3)); %active only with no transform or px (no phys)
254            set(hhuvmat.transform_fct,'Value',1)
255            uvmat('transform_fct_Callback',hObject,eventdata,hhuvmat); %file input with xml reading  in uvmat
256            set(hhuvmat.FixedLimits,'Value',0)% put FixedLimits option to 'off'
257            set(hhuvmat.FixedLimits,'BackgroundColor',[0.7 0.7 0.7])
258            return
259        end
260        Coord=get(h_ListCoord,'String');
261        data=read_geometry_calib(Coord);%transform char cell to numbers
262        xlim=get(haxes,'XLim');
263        ind_range_x=abs((xlim(2)-xlim(1))/50);
264        ylim=get(haxes,'YLim');
265        ind_range_y=abs((ylim(2)-ylim(1))/50);
266        ind_range=sqrt(ind_range_x*ind_range_y);
267        test_newpoint=1;
268        if size(data.Coord,2)>=5 %if calibration points already exist
269            XCoord=(data.Coord(:,4));
270            YCoord=(data.Coord(:,5));
271            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
272                          (YCoord<xy(1,2)+ind_range) & (YCoord>xy(1,2)-ind_range),1);%find the first calibration point in the neighborhood of the mouse
273            test_newpoint=isempty(index_point);%test for no existing calibration point near the mouse position
274        end
275        val=get(h_ListCoord,'Value');
276        %create a new calib point if we are not close to an existing one
277        if test_newpoint                 
278             strline=[ '    |    '  '    |    '  '    |    ' num2str(xy(1,1),4) '    |    ' num2str(xy(1,2),4)];
279           
280             if length(Coord)>=val
281                 Coord(val+1:length(Coord)+1)=Coord(val:length(Coord));% push the list forward beyond the current point
282             end
283             Coord{val}=strline;
284             set(h_ListCoord,'String',Coord)
285            % set(h_ListCoord,'Value',val+1)
286             data=read_geometry_calib(Coord);%transform char cell to numbers
287             XCoord=data.Coord(:,4);
288             YCoord=data.Coord(:,5);
289        end
290        hh=findobj('Tag','calib_points');%look for handle of calibration points           
291        if isempty(hh)
292            hh=line(XCoord,YCoord,'Color','m','Tag','calib_points','LineStyle','.','Marker','+');
293        else
294            set(hh,'XData',XCoord)
295            set(hh,'YData',YCoord)
296        end
297        set(hh,'UserData',val)% flag the points to edit mode
298        hhh=findobj('Tag','calib_marker');%look for handle of point marker (circle)
299        if ~isempty(hhh)
300            set(hhh,'Position',[xy(1,1)-ind_range/2 xy(1,2)-ind_range/2 ind_range ind_range])
301        else
302            rectangle('Curvature',[1 1],...
303                  'Position',[xy(1,1)-ind_range/2 xy(1,2)-ind_range/2 ind_range ind_range],'EdgeColor','m',...
304                  'LineStyle','-','Tag','calib_marker');
305           % line([xy(1,1) xy(1,1)],[xy(1,2) xy(1,2)],'Color','m','Tag','calib_marker','LineStyle','.','Marker','o','MarkerSize',ind_range);
306        end
307        AxeData.Drawing='calibration';
308    end
309end
310
311% edit vectors
312if test_edit_vect & ~isempty(ivec)
313    if ~(isfield(AxeData,'FF')&& ~isempty(AxeData.FF))
314        AxeData.FF=zeros(size(AxeData.X));
315    end
316    if isequal(AxeData.FF(ivec),0)
317        AxeData.FF(ivec)=100; %mark vector #ivec as false
318    else
319        AxeData.FF(ivec)=0;
320    end
321    PlotParam=read_plot_param(hhuvmat);
322    [PlotType,ScalOut]= plot_field(AxeData,haxes,PlotParam,1);
323end   
324
325
326set(haxes,'UserData',AxeData);
327
328%------------------------------------------------------
329function update_plot(AxeData,haxes)
330%--------------------------------------------
331
332
333% %determine the axes of action of the set_edit interface
334% % haxes= findobj(huvmat,'Tag','axes3'); %main plotting axes as default
335% % AxeData=get(haxes,'UserData')
336% %For vector field representation
337% PlotHandles.auto_xy=findobj(huvmat,'Tag','auto_xy');
338% PlotHandles.VecScale=findobj(huvmat,'Tag','VecScale');
339% PlotHandles.AutoVec=findobj(huvmat,'Tag','AutoVec');
340% PlotHandles.checkyellow=findobj(huvmat,'Tag','checkyellow');
341% PlotHandles.checkblack=findobj(huvmat,'Tag','checkblack');
342% PlotHandles.col_vec=findobj(huvmat,'Tag','col_vec');
343% PlotHandles.colcode1=findobj(huvmat,'Tag','colcode1');
344% PlotHandles.colcode2=findobj(huvmat,'Tag','colcode2');
345% PlotHandles.vec_col_bar=findobj(huvmat,'Tag','vec_col_bar');
346% PlotHandles.slider1=findobj(huvmat,'Tag','slider1');
347% PlotHandles.slider2=findobj(huvmat,'Tag','slider2');
348% PlotHandles.max_vec=findobj(huvmat,'Tag','max_vec');
349% PlotHandles.min_vec=findobj(huvmat,'Tag','min_vec');
350% PlotHandles.AutoVecColor=findobj(huvmat,'Tag','AutoVecColor');
351% PlotHandles.decimate4=findobj(huvmat,'Tag','decimate4');
352%
353% %vectors
354% Vectors.VecScale=str2num(get(PlotHandles.VecScale,'String'));
355% Vectors.AutoVec=get(PlotHandles.AutoVec,'Value');%automatic vector length
356% Vectors.checkyellow=get(PlotHandles.checkyellow,'Value');
357% Vectors.checkblack=get(PlotHandles.checkblack,'Value');
358% Vectors.decimate4=get(PlotHandles.decimate4,'Value');% =1; for reducing the nbre of vectors
359% menu_col=get(PlotHandles.col_vec,'String');
360% menu_val=get(PlotHandles.col_vec,'Value');
361% Vectors.CName=menu_col{menu_val}; %'ima_cor','black','white',...
362% Vectors.colcode1=str2num(get(PlotHandles.colcode1,'String'));% first threshold for rgb, first value for'continuous'
363% Vectors.colcode2=str2num(get(PlotHandles.colcode2,'String'));% second threshold for rgb, last value (saturation) for 'continuous'
364% Vectors.option=get(PlotHandles.vec_col_bar,'Value'); % =1 (64 colors), =0 (3 colors)
365% Vectors.min=get(PlotHandles.slider1,'Min');
366% Vectors.max=get(PlotHandles.slider1,'Max');
367% Vectors.auto=get(PlotHandles.AutoVecColor,'Value');% =1; thresholds scaling relative to min and max, =0 fixed thresholds
368% PlotParam.Vectors=Vectors;
369
Note: See TracBrowser for help on using the repository browser.