-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaw_getrect.m
303 lines (249 loc) · 8.39 KB
/
aw_getrect.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
function rect = aw_getrect(varargin)
% AW MODIFICATIONS: INCLUDE DIFFERENT LINE WIDTH
%
% GETRECT Select rectangle with mouse.
% RECT = GETRECT(FIG) lets you select a rectangle in the
% current axes of figure FIG using the mouse. Use the mouse to
% click and drag the desired rectangle. RECT is a four-element
% vector with the form [xmin ymin width height]. To constrain
% the rectangle to be a square, use a shift- or right-click to
% begin the drag.
%
% RECT = GETRECT(AX) lets you select a rectangle in the axes
% specified by the handle AX.
%
% Example
% --------
% imshow('moon.tif')
% rect = aw_getrect
%
% See also GETLINE, GETPTS.
% Callback syntaxes:
% aw_getrect('ButtonDown')
% aw_getrect('ButtonMotion')
% aw_getrect('ButtonUp')
% Copyright 1993-2003 The MathWorks, Inc.
% $Revision: 5.22.4.4 $ $Date: 2004/08/10 01:39:36 $
global GETRECT_FIG GETRECT_AX GETRECT_H1 GETRECT_H2
global GETRECT_PT1 GETRECT_TYPE
if ((nargin >= 1) && (ischar(varargin{1})))
% Callback invocation: 'ButtonDown', 'ButtonMotion', or 'ButtonUp'
feval(varargin{:});
return;
end
if (nargin < 1)
GETRECT_AX = gca;
GETRECT_FIG = ancestor(GETRECT_AX, 'figure');
else
if (~ishandle(varargin{1}))
CleanGlobals;
eid = 'Images:aw_getrect:expectedHandle';
error(eid, '%s', 'First argument is not a valid handle');
end
switch get(varargin{1}, 'Type')
case 'figure'
GETRECT_FIG = varargin{1};
GETRECT_AX = get(GETRECT_FIG, 'CurrentAxes');
if (isempty(GETRECT_AX))
GETRECT_AX = axes('Parent', GETRECT_FIG);
end
case 'axes'
GETRECT_AX = varargin{1};
GETRECT_FIG = ancestor(GETRECT_AX, 'figure');
otherwise
CleanGlobals;
eid = 'Images:aw_getrect:expectedFigureOrAxesHandle';
error(eid, '%s', 'First argument should be a figure or axes handle');
end
end
% Remember initial figure state
old_db = get(GETRECT_FIG, 'DoubleBuffer');
state = uisuspend(GETRECT_FIG);
% Set up initial callbacks for initial stage
% AW modified following line as unnecessary when the mouse initiates the
% action.
set(GETRECT_FIG, ...
'Pointer', 'crosshair', 'DoubleBuffer', 'on');
% set(GETRECT_FIG, ...
% 'Pointer', 'crosshair', ...
% 'WindowButtonDownFcn', 'aw_getrect(''ButtonDown'');', ...
% 'DoubleBuffer', 'on');
% Set axes limit modes to manual, so that the presence of lines used to
% draw the rectangles doesn't change the axes limits.
original_modes = get(GETRECT_AX, {'XLimMode', 'YLimMode', 'ZLimMode'});
set(GETRECT_AX,'XLimMode','manual', ...
'YLimMode','manual', ...
'ZLimMode','manual');
% Bring target figure forward
figure(GETRECT_FIG);
% Initialize the lines to be used for the drag
GETRECT_H1 = line('Parent', GETRECT_AX, ...
'XData', [0 0 0 0 0], ...
'YData', [0 0 0 0 0], ...
'Visible', 'off', ...
'Clipping', 'off', ...
'Color', 'w', ...
'LineStyle', '-', 'LineWidth' , 2);
GETRECT_H2 = line('Parent', GETRECT_AX, ...
'XData', [0 0 0 0 0], ...
'YData', [0 0 0 0 0], ...
'Visible', 'off', ...
'Clipping', 'off', ...
'Color', 'w', ...
'LineStyle', '-');
% AW added the following call to ButtonDown as the main function is now
% associated with WindowButtonDownFcn
ButtonDown;
% We're ready; wait for the user to do the drag
% Wrap the waitfor call in try-catch so
% that if the user Ctrl-C's we get a chance to
% clean up the figure.
errCatch = 0;
try
waitfor(GETRECT_H1, 'UserData', 'Completed');
catch
errCatch = 1;
end
% After the waitfor, if GETRECT_H1 is still valid
% and its UserData is 'Completed', then the user
% completed the drag. If not, the user interrupted
% the action somehow, perhaps by a Ctrl-C in the
% command window or by closing the figure.
if (errCatch == 1)
errStatus = 'trap';
elseif (~ishandle(GETRECT_H1) || ...
~strcmp(get(GETRECT_H1, 'UserData'), 'Completed'))
errStatus = 'unknown';
else
errStatus = 'ok';
x = get(GETRECT_H1, 'XData');
y = get(GETRECT_H1, 'YData');
end
% Delete the animation objects
if (ishandle(GETRECT_H1))
delete(GETRECT_H1);
end
if (ishandle(GETRECT_H2))
delete(GETRECT_H2);
end
% Restore the figure state
if (ishandle(GETRECT_FIG))
uirestore(state);
set(GETRECT_FIG, 'DoubleBuffer', old_db);
if ishandle(GETRECT_AX)
set(GETRECT_AX, {'XLimMode','YLimMode','ZLimMode'}, original_modes);
end
end
CleanGlobals;
% Depending on the error status, return the answer or generate
% an error message.
switch errStatus
case 'ok'
% Return the answer
xmin = min(x);
ymin = min(y);
rect = [xmin ymin max(x)-xmin max(y)-ymin];
case 'trap'
% An error was trapped during the waitfor
eid = 'Images:aw_getrect:interruptedMouseSelection';
error(eid, '%s', 'Interruption during mouse selection.');
case 'unknown'
% User did something to cause the rectangle drag to
% terminate abnormally. For example, we would get here
% if the user closed the figure in the drag.
eid = 'Images:aw_getrect:interruptedMouseSelection';
error(eid, '%s', 'Interruption during mouse selection.');
end
%--------------------------------------------------
% Subfunction ButtonDown
%--------------------------------------------------
function ButtonDown %#ok
global GETRECT_FIG GETRECT_AX GETRECT_H1 GETRECT_H2
global GETRECT_PT1 GETRECT_TYPE
set(GETRECT_FIG, 'Interruptible', 'off', ...
'BusyAction', 'cancel');
[x1, y1] = getcurpt(GETRECT_AX);
GETRECT_PT1 = [x1 y1];
GETRECT_TYPE = get(GETRECT_FIG, 'SelectionType');
x2 = x1;
y2 = y1;
xdata = [x1 x2 x2 x1 x1];
ydata = [y1 y1 y2 y2 y1];
set(GETRECT_H1, 'XData', xdata, ...
'YData', ydata, ...
'Visible', 'on');
set(GETRECT_H2, 'XData', xdata, ...
'YData', ydata, ...
'Visible', 'on');
% Let the motion functions take over.
set(GETRECT_FIG, 'WindowButtonMotionFcn', 'aw_getrect(''ButtonMotion'');', ...
'WindowButtonUpFcn', 'aw_getrect(''ButtonUp'');');
%-------------------------------------------------
% Subfunction ButtonMotion
%-------------------------------------------------
function ButtonMotion %#ok
global GETRECT_FIG GETRECT_AX GETRECT_H1 GETRECT_H2
global GETRECT_PT1 GETRECT_TYPE
[x2,y2] = getcurpt(GETRECT_AX);
x1 = GETRECT_PT1(1,1);
y1 = GETRECT_PT1(1,2);
xdata = [x1 x2 x2 x1 x1];
ydata = [y1 y1 y2 y2 y1];
if (~strcmp(GETRECT_TYPE, 'normal'))
[xdata, ydata] = Constrain(xdata, ydata);
end
set(GETRECT_H1, 'XData', xdata, ...
'YData', ydata);
set(GETRECT_H2, 'XData', xdata, ...
'YData', ydata);
%--------------------------------------------------
% Subfunction ButtonUp
%--------------------------------------------------
function ButtonUp %#ok
global GETRECT_FIG GETRECT_AX GETRECT_H1 GETRECT_H2
global GETRECT_PT1 GETRECT_TYPE
% Kill the motion function and discard pending events
set(GETRECT_FIG, 'WindowButtonMotionFcn', '', ...
'Interruptible', 'off');
% Set final line data
[x2,y2] = getcurpt(GETRECT_AX);
x1 = GETRECT_PT1(1,1);
y1 = GETRECT_PT1(1,2);
xdata = [x1 x2 x2 x1 x1];
ydata = [y1 y1 y2 y2 y1];
if (~strcmp(GETRECT_TYPE, 'normal'))
[xdata, ydata] = Constrain(xdata, ydata);
end
set(GETRECT_H1, 'XData', xdata, ...
'YData', ydata);
set(GETRECT_H2, 'XData', xdata, ...
'YData', ydata);
% Unblock execution of the main routine
set(GETRECT_H1, 'UserData', 'Completed');
%-----------------------------------------------
% Subfunction Constrain
%
% constrain rectangle to be a square in
% axes coordinates
%-----------------------------------------------
function [xdata_out, ydata_out] = Constrain(xdata, ydata)
x1 = xdata(1);
x2 = xdata(2);
y1 = ydata(1);
y2 = ydata(3);
ydis = abs(y2 - y1);
xdis = abs(x2 - x1);
if (ydis > xdis)
x2 = x1 + sign(x2 - x1) * ydis;
else
y2 = y1 + sign(y2 - y1) * xdis;
end
xdata_out = [x1 x2 x2 x1 x1];
ydata_out = [y1 y1 y2 y2 y1];
%---------------------------------------------------
% Subfunction CleanGlobals
%--------------------------------------------------
function CleanGlobals
% Clean up the global workspace
clear global GETRECT_FIG GETRECT_AX GETRECT_H1 GETRECT_H2
clear global GETRECT_PT1 GETRECT_TYPE