-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrectrange.m
146 lines (126 loc) · 5.66 KB
/
rectrange.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
function [Xrange,Yrange]=rectrange(xgrid,ygrid,f,varargin);
%RectRange Coordinate range of a selected rectangle in figure
%
% Warning! Do not use this function upon your final prepared/optimized
% figure, because it will manipulate Axes Aspect Ratio.
%
% [Xrange,Yrange]=rectrange(xgrid,ygrid,f) lets you select a rectangle in
% the current figure using the mouse. The figure has to be generated based
% on a rectangle grid (X, Y) which is usually produced by using function
% MESHGRID.Then the coordinate ranges of the selected rectangle (Not its
% position.Thus, its different from function GETRECT) based on grid
% (xgrid,ygrid) will be derived.This would be useful when you have a map
% generated based on grids of longitudes latitudes and want to know
% longitude/latitude range of a certain region.However, this function won't
% work when the map is produced by using M_Map toolbox due to projection
% issues.
%
% Inputs:
% xgrid,ygrid - The rectangle grid on which the figure bases. Usually, they
% are generated by [xgrid,ygrid]=meshgrid(xgv,ygv). See
% meshgrid.m.
% f - The handle of the figure. For exmaple, f = figure;
% 'precise' - Default output mode. The precise coordinate ranges will be
% obtained no matter there are intervals between grid points.
% 'nearest' - Other than 'presice', the coordinate ranges of your
% selected rectangle in x- and y-direction will be derived
% from 4 grid points close to each vertex of the rectangle.
%
% Outputs:
% Xrange - The range of selected rectangle in x-direction.
% Yrange - The range of selected rectangle in y-direction.
%
% For example,
% [X,Y,Z] = peaks;
% f=figure;
% contourf(X,Y,Z,'linestyle','none');
% axis equal
% [rx,ry]=rectrange(X,Y,f);
%
% One can test this function by uncommenting lines 106, 107, 125-128, 133-
% 134 and 145.The rectangle you selected and its two vertices will be drawn in
% your figure.
%
% Author:
% https://github.com/chouj
% 2018/07/29
if nargin<3
error('Not Enough Inputs. At least 3 inputs');
end
if nargin>4
error('Too Many Inputs. No more than 4 inputs please');
end
p = inputParser;
p.addRequired('xgrid',@(x)validateattributes(x,{'numeric'},{'2d'},'rectrange','x',1));
p.addRequired('ygrid',@(x)validateattributes(x,{'numeric'},{'2d'},'rectrange','y',2));
p.addRequired('f',@ishandle);
defaultoutput = 'precise';
p.addOptional('output',defaultoutput,@(x)any(validatestring(x,{'precise','nearest'})));
p.parse(xgrid,ygrid,f,varargin{:});
axis normal
set(f,'Unit','normalized');
Pa=get(f.CurrentAxes,'Position');
set(f.CurrentAxes,'Position',[0 0 1 1]);
disp('Draw a rectangular region of interest (ROI) in figure by using the mouse !')
k = waitforbuttonpress;
finalRect = rbbox;
if finalRect(1,1)<0&finalRect(1,2)<0&finalRect(1,1)+finalRect(1,3)<=1&finalRect(1,2)+finalRect(1,4)<=1
finalRect(1,3)=finalRect(1,1)+finalRect(1,3);
finalRect(1,4)=finalRect(1,2)+finalRect(1,4);
finalRect(1,2)=0;
finalRect(1,1)=0;
elseif finalRect(1,1)>=0&finalRect(1,2)<0&finalRect(1,1)+finalRect(1,3)<=1&finalRect(1,2)+finalRect(1,4)<=1
finalRect(1,4)=finalRect(1,2)+finalRect(1,4);
finalRect(1,2)=0;
elseif finalRect(1,1)<0&finalRect(1,2)>=0&finalRect(1,1)+finalRect(1,3)<=1&finalRect(1,2)+finalRect(1,4)<=1
finalRect(1,3)=finalRect(1,1)+finalRect(1,3);
finalRect(1,1)=0;
elseif finalRect(1,1)+finalRect(1,3)>1&finalRect(1,2)>=0&finalRect(1,2)+finalRect(1,4)<=1
finalRect(1,3)=1-finalRect(1,1);
elseif finalRect(1,1)+finalRect(1,3)>1&finalRect(1,2)<0
finalRect(1,3)=1-finalRect(1,1);
finalRect(1,4)=finalRect(1,2)+finalRect(1,4);
finalRect(1,2)=0;
elseif finalRect(1,2)+finalRect(1,4)>1&finalRect(1,1)>=0&finalRect(1,1)+finalRect(1,3)<=1
finalRect(1,4)=1-finalRect(1,2);
elseif finalRect(1,2)+finalRect(1,4)>1&finalRect(1,1)<0
finalRect(1,4)=1-finalRect(1,2);
finalRect(1,3)=finalRect(1,1)+finalRect(1,3);
finalRect(1,1)=0;
elseif finalRect(1,2)+finalRect(1,4)>1&finalRect(1,1)+finalRect(1,3)>1
finalRect(1,3)=1-finalRect(1,1);
finalRect(1,4)=1-finalRect(1,2);
end
%hold on
%annotation('rectangle',finalRect);
r=size(p.Results.xgrid,1);c=size(p.Results.ygrid,2);
cindex=round(c*finalRect(1));
cp=(max(p.Results.xgrid(:))-min(p.Results.xgrid(:)))*finalRect(1)+min(p.Results.xgrid(:));
cp2=(max(p.Results.xgrid(:))-min(p.Results.xgrid(:)))*finalRect(1)+min(p.Results.xgrid(:))+(max(p.Results.xgrid(:))-min(p.Results.xgrid(:)))*finalRect(3);
if cindex==0
cindex=1;
end
rindex=round(r*finalRect(2));
if rindex==0
rindex=1;
end
rp=(max(p.Results.ygrid(:))-min(p.Results.ygrid(:)))*finalRect(2)+min(p.Results.ygrid(:));
rp2=(max(p.Results.ygrid(:))-min(p.Results.ygrid(:)))*finalRect(2)+min(p.Results.ygrid(:))+(max(p.Results.ygrid(:))-min(p.Results.ygrid(:)))*finalRect(4);
%plot(cp,rp,'ro')
%text(cp,rp,['(',num2str(cp),',',num2str(rp),')'],'color','r');
%plot(cp2,rp2,'ro')
%plot(p.Results.xgrid(rindex,cindex),p.Results.ygrid(rindex,cindex),'r+')
rlength=round(c*finalRect(3));
clength=round(r*finalRect(4));
%plot(p.Results.xgrid(rindex+clength,cindex+rlength),p.Results.ygrid(rindex+clength,cindex+rlength),'r+')
%text(p.Results.xgrid(rindex+clength,cindex+rlength),p.Results.ygrid(rindex+clength,cindex+rlength),['(',num2str(p.Results.xgrid(rindex+clength,cindex+rlength)),',',num2str(p.Results.ygrid(rindex+clength,cindex+rlength)),')'],'color','b');
if strcmp(p.Results.output,'nearest')==1
Xrange=[p.Results.xgrid(rindex,cindex) p.Results.xgrid(rindex+clength,cindex+rlength)];
Yrange=[p.Results.ygrid(rindex,cindex) p.Results.ygrid(rindex+clength,cindex+rlength)];
else
Xrange=[cp cp2];
Yrange=[rp rp2];
end
%set(f.CurrentAxes,'Position',Pa);
%hold off
end