Newer
Older
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
classdef Gt6DVolumeProjector < handle
properties
proj_size = [];
angular_deviation = 0;
angular_sampling = 1;
volume_geometries = {};
astra_volume_geometries = {};
geometries = {};
astra_geometries = {};
ss_coeffs = {};
ss_geometries = {};
astra_ss_geometries = {};
volume_ids = {};
sinogram_ids = {};
algo_fproj_ids = {};
algo_bproj_ids = {};
statistics = GtTasksStatistics();
end
properties (Constant)
messageNoGPU = 'This machine cannot be used to forward and back project';
end
methods (Access = public)
function self = Gt6DVolumeProjector(initialVolumes, detectorSize, varargin)
% self.checkGPU()
if (~exist('initialVolumes', 'var') || isempty(initialVolumes))
error('Gt6DVolumeProjector:wrong_argument', ...
'No initial volumes specified')
end
numVolumes = length(initialVolumes);
self.statistics.add_task('fproj_raw_sum', 'Raw FP time');
self.statistics.add_task('bproj_raw_sum', 'Raw BP time');
self.statistics.add_task('fproj_raw_ss_single', 'Raw SS FP time (single)');
self.statistics.add_task('bproj_raw_ss_single', 'Raw SS BP time (single)');
for ii = 2:numVolumes
self.testVolumeGeometry(initialVolumes{1}, initialVolumes{ii});
end
% volume geometry (x, y, z)
self.volume_geometries = cell(numVolumes, 1);
self.astra_volume_geometries = cell(numVolumes, 1);
volSize = [size(initialVolumes{1}, 1), ...
size(initialVolumes{1}, 2), size(initialVolumes{1}, 3)];
tempVolGeom = astra_create_vol_geom(volSize(1), volSize(2), volSize(3));
for n = 1:numVolumes
self.volume_geometries{n} = volSize;
self.astra_volume_geometries{n} = tempVolGeom;
end
self.volume_ids = astra_mex_data3d('create', '-vol', self.astra_volume_geometries, initialVolumes);
if (~exist('detectorSize', 'var'))
diagonal = round(norm(size(initialVolumes{1})));
self.proj_size = [diagonal diagonal];
else
self.proj_size = detectorSize;
end
self = parse_pv_pairs(self, varargin);
end
function delete(self)
if (~isempty(self.volume_ids))
astra_mex_data3d('delete', self.volume_ids{:});
end
if (~isempty(self.sinogram_ids))
astra_mex_data3d('delete', self.sinogram_ids{:});
end
if (~isempty(self.algo_fproj_ids))
astra_mex_data3d('delete', self.algo_fproj_ids{:});
end
if (~isempty(self.algo_bproj_ids))
astra_mex_data3d('delete', self.algo_bproj_ids{:});
end
end
function initProjectionGeometry(self)
% Detector position is defined, through a vector pointing to it's
% center
% Let's clean up previous geometries
if (~isempty(self.astra_geometries))
astra_mex_data3d('delete', self.astra_geometries{:});
self.astra_geometries = {};
astra_mex_data3d('delete', self.algo_fproj_ids{:});
self.algo_fproj_ids = {};
astra_mex_data3d('delete', self.algo_bproj_ids{:});
self.algo_bproj_ids = {};
end
geometry = self.geometries;
if (iscell(geometry))
num_geoms = numel(geometry);
for n = 1:num_geoms
self.astra_geometries{n} = astra_create_proj_geom(...
'parallel3d_vec', self.proj_size(2), self.proj_size(1), geometry{n});
end
self.sinogram_ids = astra_mex_data3d('create', '-proj3d', self.astra_geometries);
else
self.astra_geometries{end+1} = astra_create_proj_geom(...
'parallel3d_vec', self.proj_size(2), self.proj_size(1), geometry);
self.sinogram_ids = astra_mex_data3d('create', '-proj3d', self.astra_geometries);
end
% Let's preallocate the algorithms
cfg = astra_struct('FP3D_CUDA');
% cfg.option.DetectorSuperSampling = self.angular_sampling;
% cfg.option.AngularDeviation = self.angular_deviation;
for ii = 1:numel(self.sinogram_ids)
cfg.ProjectionDataId = self.sinogram_ids{ii};
cfg.VolumeDataId = self.volume_ids{ii};
% For square weights
% cfg.ProjectionKernel = 'sum_square_weights';
self.algo_fproj_ids{ii} = astra_mex_algorithm('create', cfg);
end
cfg = astra_struct('BP3D_CUDA');
% cfg.option.VoxelSuperSampling = self.angular_sampling;
% cfg.option.AngularDeviation = self.angular_deviation;
for ii = 1:numel(self.sinogram_ids)
cfg.ProjectionDataId = self.sinogram_ids{ii};
cfg.ReconstructionDataId = self.volume_ids{ii};
self.algo_bproj_ids{ii} = astra_mex_algorithm('create', cfg);
end
end
function initProjectionGeometrySS(self)
% Let's clean up previous geometries
if (~isempty(self.astra_ss_geometries))
astra_mex_data3d('delete', self.astra_ss_geometries{:});
self.astra_ss_geometries = {};
end
num_ss_geoms = numel(self.ss_geometries);
for g_ii = 1:num_ss_geoms
self.astra_ss_geometries{g_ii} = astra_create_proj_geom(...
'parallel3d_vec', self.proj_size(2), self.proj_size(1), self.ss_geometries{g_ii});
end
end
function printStats(self)
self.statistics.printStats()
end
function forwardWeights = getRowsSum(self)
numGeoms = numel(self.astra_volume_geometries);
ones_cell = arrayfun(@(z){1}, 1:numGeoms);
forwardWeights = self.projectVolume(ones_cell);
end
function backwardWeights = getColumnsSum(self)
backwardWeights = self.backprojectVolume(1);
end
end
methods (Access = protected)
function sinograms = projectVolume(self, volumes)
numGeoms = numel(self.astra_geometries);
if (numGeoms ~= numel(volumes))
error('Gt6DVolumeProjector:wrong_argument', ...
'Number of volumes mismatch with allocated buffers')
end
astra_mex_data3d('store', self.volume_ids, volumes);
self.statistics.tic('fproj_raw_sum');
% astra_mex_algorithm('iterate', self.algo_fproj_ids, 1, 0, true);
for ii = 1:numGeoms
astra_mex_algorithm('iterate', self.algo_fproj_ids{ii});
end
self.statistics.toc('fproj_raw_sum');
% sinograms = astra_mex_data3d('get', self.sinogram_ids);
sinograms = astra_mex_data3d('get_single', self.sinogram_ids);
end
function volumes = backprojectVolume(self, sinograms)
numGeoms = numel(self.astra_geometries);
if (numGeoms ~= numel(sinograms))
error('Gt6DVolumeProjector:backprojVol:wrong_argument', ...
'Sinograms should be either a volume, or a cell of volumes')
end
astra_mex_data3d('store', self.sinogram_ids, sinograms);
self.statistics.tic('bproj_raw_sum');
% astra_mex_algorithm('iterate', self.algo_bproj_ids, 1, 0, true);
for ii = 1:numGeoms
astra_mex_algorithm('iterate', self.algo_bproj_ids{ii});
end
self.statistics.toc('bproj_raw_sum');
% volumes = astra_mex_data3d('get', self.volume_ids);
volumes = astra_mex_data3d('get_single', self.volume_ids);
end
function ss_sinograms = projectVolumeSS(self, volumes)
num_ss_geoms = numel(self.ss_geometries);
ss_sinograms = cell(num_ss_geoms, 1);
for ii = 1:num_ss_geoms
ss_sinograms{ii} = self.projectSingleVolumeSS(volumes, ii);
end
end
function ss_sinogram = projectSingleVolumeSS(self, volumes, ii)
self.statistics.tic('fproj_raw_ss_single');
cfg = astra_struct('FP3D_CUDA');
% cfg.option.DetectorSuperSampling = self.angular_sampling;
% cfg.option.AngularDeviation = self.angular_deviation;
vol_size = self.volume_geometries{1};
vol = zeros(vol_size, class(volumes{1}));
coeffs = self.ss_coeffs(ii);
num_inerp = numel(coeffs.indx);
for c = 1:num_inerp
vol = vol + coeffs.coeff(c) * volumes{coeffs.indx(c)};
end
sino_id = astra_mex_data3d('create', '-proj3d', self.astra_ss_geometries{ii}, 0);
vol_id = astra_mex_data3d('create', '-vol', self.astra_volume_geometries{1}, vol);
cfg.ProjectionDataId = sino_id;
cfg.VolumeDataId = vol_id;
algo_id = astra_mex_algorithm('create', cfg);
astra_mex_algorithm('iterate', algo_id);
astra_mex_algorithm('delete', algo_id);
ss_sinogram = astra_mex_data3d('get_single', sino_id);
astra_mex_data3d('delete', sino_id, vol_id);
self.statistics.toc('fproj_raw_ss_single');
end
function volumes = backprojectVolumeSS(self, ss_sinograms)
num_ss_geoms = numel(self.ss_geometries);
num_vols = numel(self.volume_geometries);
volumes = cell(num_vols, 1);
for ii = 1:num_vols
volumes{ii} = zeros(self.volume_geometries{ii}, 'single');
end
for ii = 1:num_ss_geoms
ss_sino = ss_sinograms{ii};
vol = self.backprojectSingleVolumeSS(ss_sino, ii);
coeffs = self.ss_coeffs(ii);
num_inerp = numel(coeffs.indx);
for c = 1:num_inerp
volumes{coeffs.indx(c)} = volumes{coeffs.indx(c)} + coeffs.coeff(c) * vol;
end
end
end
function volume = backprojectSingleVolumeSS(self, ss_sinogram, ii)
self.statistics.tic('bproj_raw_ss_single');
cfg = astra_struct('BP3D_CUDA');
% cfg.option.VoxelSuperSampling = self.angular_sampling;
% cfg.option.AngularDeviation = self.angular_deviation;
vol_size = self.astra_volume_geometries{1};
sino_id = astra_mex_data3d('create', '-proj3d', self.astra_ss_geometries{ii}, ss_sinogram);
vol_id = astra_mex_data3d('create', '-vol', vol_size, 0);
cfg.ProjectionDataId = sino_id;
cfg.ReconstructionDataId = vol_id;
algo_id = astra_mex_algorithm('create', cfg);
astra_mex_algorithm('iterate', algo_id);
astra_mex_algorithm('delete', algo_id);
volume = astra_mex_data3d('get_single', vol_id);
astra_mex_data3d('delete', sino_id, vol_id);
self.statistics.toc('bproj_raw_ss_single');
end
function assignVol(self, newVols)
for ii = 1:length(newVols)
self.testVolumeGeometry(newVols{ii});
self.volume{ii} = newVols{ii};
end
end
function updateVol(self, diffVols)
for ii = 1:length(diffVols)
self.testVolumeGeometry(diffVols{ii});
self.volume{ii} = self.volume{ii} - diffVols{ii};
end
end
function checkGPU(self)
try
d = gpuDevice;
hasGPU = d.SupportsDouble;
catch mexc1
mexc = MException('ASTRA:no_GPU', self.messageNoGPU);
mexc = addCause(mexc, mexc1);
throw(mexc)
end
if (~hasGPU)
mexc = MException('ASTRA:no_GPU', self.messageNoGPU);
throw(mexc)
end
end
function testVolumeGeometry(~, referenceVol, newVol)
if (~isequal(size(referenceVol), size(newVol)))
mexc = MException('PROJECTOR:wrong_argument', ...
'Size mismatch between the old and the new volume');
throw(mexc)
end
end
end
end