-
Notifications
You must be signed in to change notification settings - Fork 0
/
linearizeHamCycle3D.m
309 lines (280 loc) · 10.6 KB
/
linearizeHamCycle3D.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
301
302
303
304
305
306
307
308
309
%Traversing the image I using a Hamilton cycle generated from a precomputed minimum spanning tree
% use tree as input
function [zLT,zVisitOrder] = linearizeHamCycle3D(mstT, I, entryPix)
currPix = entryPix;
currCirc = [floor((currPix(1)+1)/2),floor((currPix(2)+1)/2),floor((currPix(3)+1)/2)];
lastCirc = [0 0 0];
circVisitCnt = zeros(size(mstT,1),size(mstT,2),size(mstT,3));
global pixOcc;%pixel occupancy map
global LT;
global visitOrder;
global pixId;
global nneighbors;
global neighborDirs;
LT = zeros(size(I,1)*size(I,2)*size(I,3),2);
visitOrder = zeros(size(I,1)*size(I,2)*size(I,3),3);
pixOcc = false(size(I,1),size(I,2),size(I,3),1);
pixId = 1;
nneighbors = 6;
neighborDirs = [
-1,0,0; % up
1,0,0; % down
0,1,0; %right
0,-1,0;%left
0,0,1;%back
0,0,-1%front
];
% dirSeq = genDirSeq(mstSet);
fromDir = [0,0,0];
toDir = fromDir;
stack = [currCirc];
while ~isempty(stack)
currCirc = stack(1,1:3);
numvisited = circVisitCnt(currCirc(1),currCirc(2),currCirc(3));
% remove first
stack(1,:) = [];
fromDir = toDir;
isCurrCircDone = false;
children = mstT(currCirc(1),currCirc(2),currCirc(3),:);
children = reshape(children, [nneighbors,1]);
childCnt = nnz(children);
if numvisited < childCnt % not all paths are visited
for i = 1:nneighbors
if children(i) > 0
% for the current channel==direction version
tryDir = neighborDirs(i,:);
tryPix = currPix + tryDir;
if pixOcc(tryPix(1),tryPix(2),tryPix(3))
continue;
end
if checkDirValid3D(tryDir, currPix, currCirc,...
lastCirc, children) %check which direction can go
nextCirc = currCirc + tryDir;
% if next pixel is not in circuit, we visit
% current pixel and skip all
trynextPix = currPix+tryDir;
inCirc = checkPixInCirc3D(trynextPix, currCirc);
if ~inCirc
currPix=visitPix3D(currPix,currCirc,[0,0,0],I);
lastCirc = currCirc;
isCurrCircDone = true;
currPix = trynextPix;
end
toDir = tryDir;
if pixId == 1
fromDir = toDir;
end
stack = [nextCirc; currCirc;stack];
circVisitCnt(currCirc(1),currCirc(2),currCirc(3)) =...
circVisitCnt(currCirc(1),currCirc(2),currCirc(3)) + 1;
break;
end
end
end
else % has been visited
% trace back
% fromDir = toDir; % use last todir
if childCnt == 0 % leaf node: flip direction
toDir = lastCirc - currCirc; %new todir
else
%none leaf: get dir to next node in stack
if~isempty(stack)
toDir = stack(1,:) - currCirc;
else
% we are done!
toDir = fromDir;
end
end
circVisitCnt(currCirc(1),currCirc(2),currCirc(3)) =...
circVisitCnt(currCirc(1),currCirc(2),currCirc(3)) + 1;
end
if isCurrCircDone
% currCirc = currCirc + toDir; % go to next circuit
lastCirc = currCirc;
end
while ~isCurrCircDone % finish circuit by circuit
if fromDir == toDir
% fill side
currPix= visitPix3D(currPix,currCirc,[0,0,0], I);
% currPix = currPix + fromDir;
currPix=visitPix3D(currPix,currCirc,fromDir, I);
nextPix = currPix + toDir;
else
% fill side and turn side
currPix=visitPix3D(currPix, currCirc,[0,0,0], I);
if fromDir == -toDir
currPix=visitPix3D(currPix,currCirc,fromDir,I);
%if directions are opposite->this is the turn from
%forward to backward
%Add one more perpendicular edge!
if fromDir(1) ~= 0 % y dir
for t = 1:2
if t == 1
dir = [0,1, 0];
else
dir = [0,-1, 0];
end
isInCirc = checkPixInCirc3D(currPix + dir, currCirc);
if isInCirc
% currPix = currPix + dir;
currPix=visitPix3D(currPix,currCirc,dir, I);
break;
end
end
elseif fromDir(2) ~= 0 % x direction
for t = 1:2
if t == 1
dir = [1,0,0];
else
dir = [-1,0,0];
end
isInCirc = checkPixInCirc3D(currPix + dir, currCirc);
if isInCirc
% currPix = currPix + dir;
currPix=visitPix3D(currPix,currCirc,dir,I);
break;
end
end
else % z direction
for t = 1:2
if t == 1
dir = [0,0,1];
else
dir = [0,0,-1];
end
isInCirc = checkPixInCirc3D(currPix + dir, currCirc);
if isInCirc
% currPix = currPix + dir;
currPix=visitPix3D(currPix,currCirc,dir,I);
break;
end
end
end
currPix=visitPix3D(currPix,currCirc,toDir,I);
nextPix = currPix + toDir;
else
% regular turn point
currPix= visitPix3D(currPix,currCirc,fromDir,I);
currPix= visitPix3D(currPix,currCirc,toDir,I);
nextPix = currPix + toDir;
end
end
% if next pixel is not in circuit
if pixId >= length(LT)
break;
end
inCirc = checkPixInCirc3D(nextPix, currCirc);
if ~inCirc
lastCirc = currCirc;
isCurrCircDone = true;
currPix = nextPix;
end
end
end
zLT = LT;
zVisitOrder = visitOrder;
end
% helper functions
function isInside = checkPixInCirc3D(pix, circ)
node = 2* circ - 1;
pixInCirc = zeros(8,3);
cnt = 1;
for z = 1:2
for y = 1:2
for x = 1:2
pixInCirc(cnt,:) = node + [y,x,z];
cnt = cnt + 1;
end
end
end
isInside = false;
for nn = 1:size(pixInCirc,1)
if pix == pixInCirc(nn,:)
isInside = true;
break;
end
end
end
function lastPix = visitPix3D(currPix, currCirc, dir, I)
global pixId
global LT;
global visitOrder;
global pixOcc;
modPix = currPix + dir;
if ~pixOcc(modPix(1),modPix(2),modPix(3))
if checkPixInCirc3D(modPix, currCirc)
LT(pixId,1) = I(modPix(1),modPix(2),modPix(3));
% distance to center
ctr = [size(pixOcc,1)/2, size(pixOcc,2)/2, size(pixOcc,3)/2];
LT(pixId,2) = norm(ctr - modPix)/norm(ctr);
visitOrder(pixId,:) = modPix;
pixId = pixId + 1;
pixOcc(modPix(1),modPix(2),modPix(3)) = true;
lastPix = modPix;
else
lastPix = currPix;
end
else
lastPix = currPix;
end
end
% test if 2D projected line segments between 3D line segs aa->bb and cc->dd
function isProjIntersect = projIntersect(aa, bb, cc, dd)
if max(aa(3),bb(3)) < min(cc(3),dd(3))
isProjIntersect = false;
return;
end
if max(aa(2),bb(2)) < min(cc(2),dd(2))
isProjIntersect = false;
return;
end
if max(aa(1),bb(1)) < min(cc(1),dd(1))
isProjIntersect = false;
return;
end
if max(cc(3),dd(3))<min(aa(3),bb(3))
isProjIntersect = false;
return;
end
if max(cc(2),dd(2))<min(aa(2),bb(2))
isProjIntersect = false;
return;
end
if max(cc(1),dd(1))<min(aa(1),bb(1))
isProjIntersect = false;
return;
end
val = cross(aa-bb, cc-dd);
isProjIntersect = true;
end
function isValid = checkDirValid3D(dir, currPix, currCirc, lastCirc, children)
isValid = true;
global neighborDirs;
global nneighbors;
ctrPos = [2*(currCirc(1)-1)+1.5, 2*(currCirc(2)-1)+1.5, 2*(currCirc(3)-1)+1.5];
children = reshape(children,[nneighbors,1]);
if nnz(children) == 1
isValid = true;
return;
else
for i = 1:nneighbors
if children(i) > 0
tdir = neighborDirs(i,:);
if dir(1)~=tdir(1) || dir(2)~=tdir(2) || dir(3)~=tdir(3)
if projIntersect(currPix, currPix+dir, ctrPos, ctrPos+tdir)
isValid = false;
return;
end
end
end
end
%check link to the last circ
tdir = lastCirc - currCirc;
if dir(1)~=tdir(1) || dir(2)~=tdir(2) || dir(3)~=tdir(3)
if projIntersect(currPix, currPix+dir, ctrPos, ctrPos+tdir)
isValid = false;
return;
end
end
end
end