-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmergeTileBuffer.m
171 lines (114 loc) · 3.65 KB
/
mergeTileBuffer.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
function mergeTileBuffer(f0,f1)
% mergeTileBuffer: merge the edge buffers two tiles with linear feathering
%
% mergeTileBuffer(f0,f1) where f0 and f1 are the file names or mat file
% handles of neighboring tiles.
%% first test if input args are either valid filenames or mat file handles
fprintf('Merging tile %s with %s\n', f0, f1)
if isstr(f0) % it's a string, might be a filename
if exist(f0,'file') % yup its a file
m0 = matfile(f0); % load it
else
error('File does not exist');
end
elseif isvalid(f0) % not a string, is it a valid file handle?
m0 = f0; % yes, so set m to f and get the filename for f
f0 = m0.Properties.Source;
else error('input arg must be a filename or valid matfile handle')
end
if isstr(f1) % it's a string, might be a filename
if exist(f1,'file') % yup its a file
m1 = matfile(f1); % load it
else
error('File does not exist');
end
elseif isvalid(f1) % not a string, is it a valid file handle?
m1 = f1; % yes, so set m to f and get the filename for f
f1 = m1.Properties.Source;
else error('input arg must be a filename or valid matfile handle')
end
% make sure writeable
m0.Properties.Writable = true;
m1.Properties.Writable = true;
%% crop tiles to buffer
c0 = m0.x >= min(m1.x) & m0.x <= max(m1.x);
r0 = m0.y >= min(m1.y) & m0.y <= max(m1.y);
c0 = [find(c0,1,'first'),find(c0,1,'last')];
r0 = [find(r0,1,'first'),find(r0,1,'last')];
if isempty(c0) || isempty(r0); error('no overlap betwene tiles'); end
c1 = m1.x >= min(m0.x) & m1.x <= max(m0.x);
r1 = m1.y >= min(m0.y) & m1.y <= max(m0.y);
c1 = [find(c1,1,'first'),find(c1,1,'last')];
r1 = [find(r1,1,'first'),find(r1,1,'last')];
%% Make weight array based on boundary being merged
info0=whos(m0,'z');
sz0=info0.size;
if c0(1) > 1; % merging on f0's right boundary
W0 = linspace(1,0,diff(c0)+1);
W0 = repmat(W0,diff(r0)+1,1);
m0.mergedRight=true;
m1.mergedLeft=true;
elseif c0(2) < sz0(2) % merging on f0's left boundary
W0 = linspace(0,1,diff(c0)+1);
W0 = repmat(W0,diff(r0)+1,1);
m0.mergedLeft=true;
m1.mergedRight=true;
elseif r0(2) < sz0(2) % merging on f0's top boundary
W0 = linspace(0,1,diff(r0)+1);
W0 = repmat(W0(:),1,diff(c0)+1);
m0.mergedTop=true;
m1.mergedBottom=true;
elseif r0(1) > 1 % merging on f0's bottom boundary
W0 = linspace(1,0,diff(r0)+1);
W0 = repmat(W0(:),1,diff(c0)+1);
m0.mergedBottom=true;
m1.mergedTop=true;
else
error('buffer region is same size as full grid')
end
%% merge z
z0= m0.z(r0(1):r0(2),c0(1):c0(2));
z1= m1.z(r1(1):r1(2),c1(1):c1(2));
z=(z0.*W0)+(z1.*(1-W0));
n=isnan(z0) & ~isnan(z1);
z(n)=z1(n);
n=~isnan(z0) & isnan(z1);
z(n)=z0(n);
m0.z(r0(1):r0(2),c0(1):c0(2))=z;
m1.z(r1(1):r1(2),c1(1):c1(2))=z;
clear z0 z1 z;
%% merge mt
mt0= m0.mt(r0(1):r0(2),c0(1):c0(2));
mt1= m1.mt(r1(1):r1(2),c1(1):c1(2));
mt = mt0 | mt1;
m0.mt(r0(1):r0(2),c0(1):c0(2))=mt;
m1.mt(r1(1):r1(2),c1(1):c1(2))=mt;
clear mt0 mt1 mt;
%% merge or
or0= m0.or(r0(1):r0(2),c0(1):c0(2));
or1= m1.or(r1(1):r1(2),c1(1):c1(2));
or0=single(or0);
or1=single(or1);
or=(or0.*W0)+(or1.*(1-W0));
n= or0 == 0 & or1 ~= 0;
or(n)=or1(n);
n= or0 ~= 0 & or1 == 0;
or(n)=or0(n);
or=int16(or);
m0.or(r0(1):r0(2),c0(1):c0(2))=or;
m1.or(r1(1):r1(2),c1(1):c1(2))=or;
clear or0 or1 or;
%% merge dy
dy0= m0.dy(r0(1):r0(2),c0(1):c0(2));
dy1= m1.dy(r1(1):r1(2),c1(1):c1(2));
dy0=single(dy0);
dy1=single(dy1);
dy=(dy0.*W0)+(dy1.*(1-W0));
n= dy0 == 0 & dy1 ~= 0;
dy(n)=dy1(n);
n= dy0 ~= 0 & dy1 == 0;
dy(n)=dy0(n);
dy=int16(dy);
m0.dy(r0(1):r0(2),c0(1):c0(2))=dy;
m1.dy(r1(1):r1(2),c1(1):c1(2))=dy;
clear dy0 dy1 dy;