-
Notifications
You must be signed in to change notification settings - Fork 28
/
palm_hemisplit.m
155 lines (143 loc) · 4.72 KB
/
palm_hemisplit.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
function palm_hemisplit(varargin)
% Split back bh into lh and rh for files that have previously
% been merged with palm_hemimerge.
% Output file names have prefix "lh" and "rh".
%
% palm_hemisplit [-v #vtx] [-f #fac] <files>
%
% Wildcards are accepted.
%
% -v <nV> : Specify the number of vertices for the left hemisphere.
% -f <nF> : Specify the number of faces for the left hemisphere.
%
% For surfaces (meshes) and curvature files, both -v and -f are necessary.
% For dpv or dpx, use -v for the same purpose. For dpf, use -f.
% _____________________________________
% Anderson M. Winkler
% FMRIB / University of Oxford
% Jun/2016
% http://brainder.org
% List of files (with wildcards)
a = 1;
j = 1;
nVL = [];
nFL = [];
while a <= nargin
if a < nargin && strcmpi(varargin{a},'-v')
nVL = varargin{a+1};
if ischar(nVL)
nVL = str2double(nVL);
end
a = a + 2;
elseif a < nargin && strcmpi(varargin{a},'-f')
nFL = varargin{a+1};
if ischar(nFL)
nFL = str2double(nFL);
end
a = a + 2;
else
F = dir(varargin{a});
for f = numel(F):-1:1
if F(f).name(1) == '.'
F(f) = [];
else
Dlist{j} = F(f).folder;
Flist{j} = F(f).name;
j = j + 1;
end
end
a = a + 1;
end
end
Dlist = flipud(Dlist');
Flist = flipud(Flist');
% For each input file.
for f = 1:numel(Flist)
fprintf('Working on: %s\n',fullfile(Dlist{f},Flist{f}));
B = palm_miscread(fullfile(Dlist{f},Flist{f}));
L = B; R = B;
switch B.readwith
case {'load','csvread','fs_load_mgh'}
nVB = size(B.data,1);
if isempty(nVL)
nVL = nVB/2;
end
L.data = B.data(1:nVL,:,:,:);
R.data = B.data(nVL+1:end,:,:,:);
case 'dpxread'
nXB = size(B.data,1);
if strcmpi(Flist{f}(end-2:end),'dpf')
if isempty(nFL)
nXL = nXB/2;
else
nXL = nFL;
end
else
if isempty(nVL)
nXL = nXB/2;
else
nXL = nVL;
end
end
L.data = B.data(1:nXL,:,:,:);
R.data = B.data(nXL+1:end,:,:,:);
L.extra.crd = B.extra.crd(1:nXL,:,:,:);
R.extra.crd = B.extra.crd(nXL+1:end,:,:,:);
L.extra.idx = (0:size(L.data,1)-1)';
R.extra.idx = (0:size(R.data,1)-1)';
case {'srfread','fs_read_surf'}
nVB = size(B.data.vtx,1);
nFB = size(B.data.fac,1);
if xor(isempty(nVL),isempty(nFL))
warning(...
['With surfaces (meshes), either both "-v" and "-f" must be supplied, or neither.\n'...
'Skipping: %s.'],Flist{f});
else
if isempty(nVL) && isempty(nFL)
nVL = nVB/2;
nFL = nFB/2;
end
L.data.vtx = B.data.vtx(1:nVL,:);
R.data.vtx = B.data.vtx(nVL+1:end,:);
L.data.fac = B.data.fac(1:nFL,:);
R.data.fac = B.data.fac(nFL+1:end,:)-nVL;
end
case 'fs_read_curv'
nVB = size(B.data,1);
nFB = B.extra.fnum;
if xor(isempty(nVL),isempty(nFL))
warning(...
['With curvatures, either both "-v" and "-f" must be supplied, or neither.\n'...
'Skipping: %s.'],Flist{f});
else
if isempty(nVL) && isempty(nFL)
nVL = nVB/2;
nFL = nFB/2;
end
L.data = B.data(1:nVL,:,:,:);
R.data = B.data(nVL+1:end,:,:,:);
L.extra.fnum = nFL;
R.extra.fnum = nFB-nFL;
end
otherwise
warning('Cannot deal with files read with %s. Skipping: %s\n',B.readwith,Flist{f});
end
% Adjust filenames and save
filename = Flist{f};
if any(strcmpi(filename(end-3:end),{'.mgh','.mgz'}))
filename = filename(1:end-4);
end
if strcmpi(Flist{f}(1:2),'bh')
L.filename = filename;
L.filename(1:2) = 'lh';
L.filename = fullfile(Dlist{f},L.filename);
R.filename = filename;
R.filename(1:2) = 'rh';
R.filename = fullfile(Dlist{f},R.filename);
else
L.filename = fullfile(Dlist{f},strcat('lh_',filename));
R.filename = fullfile(Dlist{f},strcat('rh_',filename));
end
palm_miscwrite(L);
palm_miscwrite(R);
end