-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontour2mesh.m
116 lines (99 loc) · 4.89 KB
/
contour2mesh.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
load('nucsContours4render');
for i=1:size(nucsContours4render,1)
Vs = [1,1,1];
nucsContours = nucsContours4render(i,:);
numOfSices = size(find(~cellfun(@isempty,nucsContours)),2);
% Running over all slices to
extract all vertices:
for j=1:numOfSices
% contour of j slice:
vSlice = nucsContours{j}{1};
% Adding the y axis:
vSlice(:,3) = j;
Vs = [Vs; vSlice];
end
Vs(1,:) = [];
Vs(:,4) = 1:size(Vs,1);
Fs = [1,1,1];
thisSlice = Vs(Vs(:,3)==1,:);
for j = 2:size(thisSlice,1)-1
Fs(end+1,:) = [thisSlice(1,4), thisSlice(j,4), thisSlice(j+1,4)];
end
thisSlice = Vs(Vs(:,3)==numOfSices,:);
for j = 2:size(thisSlice,1)-1
Fs(end+1,:) = [thisSlice(1,4), thisSlice(j,4), thisSlice(j+1,4)];
end
for j=1:numOfSices-1
thisSlice = Vs(Vs(:,3)==j,:);
nextSlice = Vs(Vs(:,3)==j+1,:);
% If the two contours have the same amount of points:
if size(thisSlice,1)==size(nextSlice,1)
for k=1:size(thisSlice,1)-1
Fs(end+1,:) = [thisSlice(k,4), nextSlice(k,4), nextSlice(k+1,4)];
Fs(end+1,:) = [thisSlice(k,4), thisSlice(k+1,4), nextSlice(k+1,4)];
end
Fs(end+1,:) = [thisSlice(k+1,4), nextSlice(k+1,4), nextSlice(1,4)];
Fs(end+1,:) = [thisSlice(k+1,4), thisSlice(1,4), nextSlice(1,4)];
% If the current contour has twice as many points than next:
elseif ((size(thisSlice,1)/2)==size(nextSlice,1) || (size(thisSlice,1))==size(nextSlice,1)/2)
if (size(thisSlice,1)/2)==size(nextSlice,1)
temp = thisSlice;
thisSlice = nextSlice;
nextSlice = temp;
end
for k=1:size(thisSlice,1)-1
Fs(end+1,:) = [thisSlice(k,4), nextSlice(2*k-1,4), nextSlice(2*k,4)];
Fs(end+1,:) = [thisSlice(k,4), nextSlice(2*k,4), nextSlice(2*k+1,4)];
Fs(end+1,:) = [thisSlice(k,4), thisSlice(k+1,4), nextSlice(2*k+1,4)];
end
Fs(end+1,:) = [thisSlice(k,4), nextSlice(2*k-1,4), nextSlice(2*k,4)];
Fs(end+1,:) = [thisSlice(k,4), nextSlice(2*k,4), nextSlice(1,4)];
Fs(end+1,:) = [thisSlice(k,4), thisSlice(1,4), nextSlice(1,4)];
elseif ((size(thisSlice,1)/4)==size(nextSlice,1) || (size(thisSlice,1)==size(nextSlice,1)/4))
if (size(thisSlice,1)/4)==size(nextSlice,1)
temp = thisSlice;
thisSlice = nextSlice;
nextSlice = temp;
end
for k=1:size(thisSlice,1)-1
Fs(end+1,:) = [thisSlice(k,4), nextSlice(3*k-2,4), nextSlice(3*k-1,4)];
Fs(end+1,:) = [thisSlice(k,4), nextSlice(3*k-1,4), nextSlice(3*k,4)];
Fs(end+1,:) = [thisSlice(k,4), nextSlice(3*k,4), nextSlice(3*k+1,4)];
Fs(end+1,:) = [thisSlice(k,4), thisSlice(k+1,4), nextSlice(3*k+1,4)];
end
Fs(end+1,:) = [thisSlice(k,4), nextSlice(3*k-2,4), nextSlice(3*k-1,4)];
Fs(end+1,:) = [thisSlice(k,4), nextSlice(3*k-1,4), nextSlice(3*k,4)];
Fs(end+1,:) = [thisSlice(k,4), nextSlice(3*k,4), nextSlice(1,4)];
Fs(end+1,:) = [thisSlice(k,4), thisSlice(1,4), nextSlice(1,4)];
elseif ((size(thisSlice,1)/8)==size(nextSlice,1) || (size(thisSlice,1)==size(nextSlice,1)/8))
if (size(thisSlice,1)/8)==size(nextSlice,1)
temp = thisSlice;
thisSlice = nextSlice;
nextSlice = temp;
end
for k=1:size(thisSlice,1)-1
Fs(end+1,:) = [thisSlice(k,4), nextSlice(4*k-3,4), nextSlice(4*k-2,4)];
Fs(end+1,:) = [thisSlice(k,4), nextSlice(4*k-2,4), nextSlice(4*k-1,4)];
Fs(end+1,:) = [thisSlice(k,4), nextSlice(4*k-1,4), nextSlice(4*k,4)];
Fs(end+1,:) = [thisSlice(k,4), nextSlice(4*k,4), nextSlice(4*k+1,4)];
Fs(end+1,:) = [thisSlice(k,4), thisSlice(k+1,4), nextSlice(4*k+1,4)];
end
Fs(end+1,:) = [thisSlice(k,4), nextSlice(4*k-3,4), nextSlice(4*k-2,4)];
Fs(end+1,:) = [thisSlice(k,4), nextSlice(4*k-2,4), nextSlice(4*k-1,4)];
Fs(end+1,:) = [thisSlice(k,4), nextSlice(4*k-1,4), nextSlice(4*k,4)];
Fs(end+1,:) = [thisSlice(k,4), nextSlice(4*k,4), nextSlice(1,4)];
Fs(end+1,:) = [thisSlice(k,4), thisSlice(1,4), nextSlice(1,4)];
else
[i, j, size(thisSlice,1), size(nextSlice,1)]
end
end
Fs(1,:) = [];
fileID = fopen(['nuc' num2str(i) '.obj'],'w');
for j=1:size(Vs,1)
fwrite(fileID,['v ' num2str((Vs(j,3)+xyzStart(i,3))*2.25) ' ' num2str(Vs(j,1)+xyzStart(i,1)) ' ' num2str(Vs(j,2)+xyzStart(i,2)) ' 1.0' char(10)]);
end
for j=1:size(Fs,1)
fwrite(fileID,['f ' num2str(Fs(j,1)) ' ' num2str(Fs(j,2)) ' ' num2str(Fs(j,3)) ' 1.0' char(10)]);
end
fclose(fileID);
end