[37] | 1 | function P = click(N,varargin) ; |
---|
| 2 | % CLICK Click a point |
---|
| 3 | % P=CLICK() let the user click a point in the current figure and |
---|
| 4 | % returns its coordinates in P. The user can abort the operation |
---|
| 5 | % by pressing any key, in which case the empty matrix is returned. |
---|
| 6 | % |
---|
| 7 | % P=CLICK(N) lets the user select N points in a row. The user can |
---|
| 8 | % stop inserting points by pressing any key, in which case the |
---|
| 9 | % partial list is returned. |
---|
| 10 | % |
---|
| 11 | % CLICK(V,N,'Opt',val,...) accepts the following options: |
---|
| 12 | % |
---|
| 13 | % 'PlotMarker' [0] |
---|
| 14 | % Put a marker as points are selected. The markers are |
---|
| 15 | % deleted on exiting the function. |
---|
| 16 | % |
---|
| 17 | % See also CLICKPOINT(). |
---|
| 18 | |
---|
| 19 | % AUTORIGHTS |
---|
| 20 | % Copyright (C) 2006 Andrea Vedaldi |
---|
| 21 | % |
---|
| 22 | % This file is part of VLUtil. |
---|
| 23 | % |
---|
| 24 | % VLUtil is free software; you can redistribute it and/or modify |
---|
| 25 | % it under the terms of the GNU General Public License as published by |
---|
| 26 | % the Free Software Foundation; either version 2, or (at your option) |
---|
| 27 | % any later version. |
---|
| 28 | % |
---|
| 29 | % This program is distributed in the hope that it will be useful, |
---|
| 30 | % but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 31 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 32 | % GNU General Public License for more details. |
---|
| 33 | % |
---|
| 34 | % You should have received a copy of the GNU General Public License |
---|
| 35 | % along with this program; if not, write to the Free Software Foundation, |
---|
| 36 | % Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 37 | |
---|
| 38 | plot_marker = 0 ; |
---|
| 39 | for k=1:2:length(varargin) |
---|
| 40 | switch lower(varargin{k}) |
---|
| 41 | case 'plotmarker' |
---|
| 42 | plot_marker = varargin{k+1} ; |
---|
| 43 | otherwise |
---|
| 44 | error(['Uknown option ''', varargin{k}, '''.']) ; |
---|
| 45 | end |
---|
| 46 | end |
---|
| 47 | |
---|
| 48 | if nargin < 1 |
---|
| 49 | N=1; |
---|
| 50 | end |
---|
| 51 | |
---|
| 52 | % -------------------------------------------------------------------- |
---|
| 53 | % Do job |
---|
| 54 | % -------------------------------------------------------------------- |
---|
| 55 | |
---|
| 56 | fig = gcf ; |
---|
| 57 | |
---|
| 58 | is_hold = ishold ; |
---|
| 59 | hold on ; |
---|
| 60 | |
---|
| 61 | bhandler = get(fig,'WindowButtonDownFcn') ; |
---|
| 62 | khandler = get(fig,'KeyPressFcn') ; |
---|
| 63 | pointer = get(fig,'Pointer') ; |
---|
| 64 | |
---|
| 65 | set(fig,'WindowButtonDownFcn',@click_handler) ; |
---|
| 66 | set(fig,'KeyPressFcn',@key_handler) ; |
---|
| 67 | set(fig,'Pointer','crosshair') ; |
---|
| 68 | |
---|
| 69 | P=[] ; |
---|
| 70 | h=[] ; |
---|
| 71 | data.exit=0; |
---|
| 72 | guidata(fig,data) ; |
---|
| 73 | while size(P,2) < N |
---|
| 74 | uiwait(fig) ; |
---|
| 75 | data = guidata(fig) ; |
---|
| 76 | if(data.exit) |
---|
| 77 | break ; |
---|
| 78 | end |
---|
| 79 | P = [P data.P] ; |
---|
| 80 | if( plot_marker ) |
---|
| 81 | h=[h plot(data.P(1),data.P(2),'rx')] ; |
---|
| 82 | end |
---|
| 83 | end |
---|
| 84 | |
---|
| 85 | if ~is_hold |
---|
| 86 | hold off ; |
---|
| 87 | end |
---|
| 88 | |
---|
| 89 | if( plot_marker ) |
---|
| 90 | pause(.1); |
---|
| 91 | delete(h) ; |
---|
| 92 | end |
---|
| 93 | |
---|
| 94 | set(fig,'WindowButtonDownFcn',bhandler) ; |
---|
| 95 | set(fig,'KeyPressFcn',khandler) ; |
---|
| 96 | set(fig,'Pointer',pointer) ; |
---|
| 97 | |
---|
| 98 | % ==================================================================== |
---|
| 99 | function click_handler(obj,event) |
---|
| 100 | % -------------------------------------------------------------------- |
---|
| 101 | data = guidata(gcbo) ; |
---|
| 102 | |
---|
| 103 | P = get(gca, 'CurrentPoint') ; |
---|
| 104 | P = [P(1,1); P(1,2)] ; |
---|
| 105 | |
---|
| 106 | data.P = P ; |
---|
| 107 | guidata(obj,data) ; |
---|
| 108 | uiresume(gcbo) ; |
---|
| 109 | |
---|
| 110 | % ==================================================================== |
---|
| 111 | function key_handler(obj,event) |
---|
| 112 | % -------------------------------------------------------------------- |
---|
| 113 | data = guidata(gcbo) ; |
---|
| 114 | data.exit = 1 ; |
---|
| 115 | guidata(obj,data) ; |
---|
| 116 | uiresume(gcbo) ; |
---|