Newer
Older
Nicola Vigano
committed
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
function bl = gtDefFwdProjGvdm(grain, ref_sel, gv, fedpars, parameters, det_ind, verbose)
if (~exist('det_ind', 'var'))
det_ind = 1;
end
if (~exist('verbose', 'var'))
verbose = true;
end
o = GtConditionalOutput(verbose);
o.fprintf('Forward projection (%s):\n', mfilename)
nbl = numel(ref_sel);
uinds = gv.used_ind;
nv = numel(uinds);
ones_bl = ones(nbl, 1);
detgeo = parameters.detgeo(det_ind);
labgeo = parameters.labgeo;
samgeo = parameters.samgeo;
g = gtMathsRod2OriMat(grain.R_vector);
pls_orig = grain.allblobs.plorig(ref_sel, :);
if (strcmpi(fedpars.defmethod, 'rod_rightstretch'))
% We bring the plane normals back to the status of pl_cry
pls_orig = gtVectorLab2Cryst(pls_orig, g);
end
sinths = grain.allblobs.sintheta(ref_sel);
ominds = grain.allblobs.omind(ref_sel);
omegas = grain.allblobs.omega(ref_sel);
% The plane normals need to be brought in the Lab reference where the
% beam direction and rotation axis are defined.
% Use the Sample -> Lab orientation transformation assuming omega=0;
% (vector length preserved for free vectors)
pls_orig = gtGeoSam2Lab(pls_orig, eye(3), labgeo, samgeo, true, false);
if (isfield(fedpars, 'detector') ...
&& isfield(fedpars.detector, 'psf') ...
&& ~isempty(fedpars.detector(det_ind).psf))
psf = fedpars.detector(det_ind).psf;
if (~iscell(psf))
psf = { psf };
end
if (numel(psf) == 1)
psf = psf(ones_bl);
end
else
psf = {};
end
or_uvw = grain.allblobs.detector(det_ind).uvw(ref_sel, :);
if (isfield(fedpars, 'detector') ...
&& isfield(fedpars.detector, 'apply_uv_shift') ...
&& ~isempty(fedpars.detector(det_ind).apply_uv_shift) ...
&& fedpars.detector(det_ind).apply_uv_shift)
uvw_shifts = [or_uvw(:, 1:2), round(or_uvw(:, 3))]';
else
uvw_shifts = round(or_uvw)';
end
num_oversampling = size(gv.d, 3);
gvpow = gv.pow(1, uinds);
bl = gtFwdSimBlobDefinition('blob', nbl);
for ii_ss = 1:num_oversampling
gvcs = gv.cs(:, uinds, ii_ss);
gvd = gv.d(:, uinds, ii_ss);
% Deformation tensor (relative to reference state) from its components
defT = gtFedDefTensorFromComps(gvd, fedpars.dcomps, fedpars.defmethod, 0);
%%% Computation of indices
o.fprintf(' - Super sampling %03d/%03d:\n * Computing indices and bbsizes: ', ...
ii_ss, num_oversampling)
t = tic();
uvw = cell(nbl, 1);
uvw_min = zeros(nbl, 3);
uvw_max = zeros(nbl, 3);
for ii_b = 1:nbl
num_chars = o.fprintf('%03d/%03d', ii_b, nbl);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Calculate new detector coordinates
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% New deformed plane normals and relative elongations (relative to
% reference state)
pl_orig = pls_orig(ii_b, :)'; % ! use plcry and not plsam to keep omega order below!
[pl_samd, drel] = gtStrainPlaneNormals(pl_orig, defT); % unit column vectors
% New sin(theta)
sinth = sinths(ii_b) ./ drel;
% Predict omega angles: 4 for each plane normal
[om, pllab, ~, rot_l2s] = gtFedPredictOmegaMultiple(pl_samd, ...
sinth, labgeo.beamdir', labgeo.rotdir', labgeo.rotcomp, ominds(ii_b));
% Delete those where no reflection occurs
if any(isnan(om));
inds_bad = find(isnan(om));
gvd(:, inds_bad(1:min(10, numel(inds_bad))))
error('gtFedFwdProjExact:bad_R_vectors', ...
'No diffraction from some elements after deformation (%d over %d).', ...
numel(inds_bad), numel(om))
end
% Diffraction vector
dvec_lab = gtFedPredictDiffVecMultiple(pllab, labgeo.beamdir');
rot_s2l = permute(rot_l2s, [2 1 3]);
gvcs_lab = gtGeoSam2Lab(gvcs', rot_s2l, labgeo, samgeo, false);
Nicola Vigano
committed
uvw_bl = gtFedPredictUVWMultiple([], dvec_lab, gvcs_lab', ...
detgeo.detrefpos', detgeo.detnorm', detgeo.Qdet, ...
[detgeo.detrefu, detgeo.detrefv]', om, labgeo.omstep);
uvw_bl(3, :) = gtGrainAnglesTabularFix360deg(uvw_bl(3, :), ...
omegas(ii_b) * labgeo.omstep, parameters);
Nicola Vigano
committed
% Transforming into offsets from
uvw_bl = uvw_bl - uvw_shifts(:, ii_b * ones(1, nv));
uvw{ii_b} = uvw_bl;
uvw_min(ii_b, :) = min(uvw{ii_b}, [], 2)';
uvw_max(ii_b, :) = max(uvw{ii_b}, [], 2)';
o.fprintf(repmat('\b', [1, num_chars]));
end
o.fprintf('Done in %g s\n', toc(t));
o.fprintf(' * Computing max BBox size and feasibilities:\n')
psf_size = zeros(nbl, 2);
if (~isempty(psf))
for ii_b = 1:nbl
psf_size(ii_b, :) = (size(psf{ii_b}) - 1) / 2;
end
end
if (isfield(fedpars, 'detector'))
blob_size_add = fedpars.detector(det_ind).blobsizeadd;
else
blob_size_add = fedpars.blobsizeadd(det_ind, :);
end
blob_size_add = blob_size_add + [max(psf_size, [], 1), 0];
Nicola Vigano
committed
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
blob_uv_size = max(abs(min(uvw_min(:, 1:2), [], 1)), abs(max(uvw_max(:, 1:2), [], 1)));
blob_uv_size = 2 * (ceil(blob_uv_size) + blob_size_add(1:2)) + 1;
blob_orig_uv_shift = (blob_uv_size - 1) / 2 + 1;
blob_w_sizes = ceil(uvw_max(:, 3)) - floor(uvw_min(:, 3)) + blob_size_add(3) + 1;
blob_orig_w_shifts = 1 - floor(uvw_min(:, 3));
o.fprintf(' Computed Blob UV size: [%d, %d]\n', blob_uv_size);
o.fprintf(' Computed Blob W sizes: [%s]\n', sprintf(' %d', blob_w_sizes));
o.fprintf(' Blob size add: [%d, %d, %d] (psf: [%d, %d])\n', ...
blob_size_add, max(psf_size));
tmp_bl = gtFwdSimBlobDefinition('blob', nbl);
%%% Blob projection
o.fprintf(' * Projecting volumes: ')
t = tic();
for ii_b = 1:nbl
num_chars = o.fprintf('%03d/%03d', ii_b, nbl);
blob_orig_uvw_shift = [blob_orig_uv_shift, blob_orig_w_shifts(ii_b)]';
blob_uvw_size = [blob_uv_size blob_w_sizes(ii_b)];
% Detector coordinates U,V in blob
uvw_bl = uvw{ii_b} + blob_orig_uvw_shift(:, ones(1, nv));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Sum intensities
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Distribute value over 8 pixels
% This way of rounding is correct also for round values when mu=mod...
% is used above:
ul = floor(uvw_bl); % lower pixel indices
uh = ul + 1; % higher pixel indices
% uvw indices of 8 nearest neighbours (3xnx8)
ucbl8 = [ ul, ...
[ul([1,2],:); uh(3,:)], ...
[ul(1,:); uh(2,:); ul(3,:)], ...
[ul(1,:); uh([2,3],:)], ...
[uh(1,:); ul([2,3],:)], ...
[uh(1,:); ul(2,:); uh(3,:)], ...
[uh([1,2],:); ul(3,:)], ...
uh ];
% uvw indices of 8 nearest neighbours (nx3)
ucbl8 = ucbl8';
% Modulus of uvw position
mu = mod(uvw_bl, 1);
mu1 = mu(1, :);
mu2 = mu(2, :);
mu3 = mu(3, :);
mu12 = mu1 .* mu2;
mu13 = mu1 .* mu3;
mu23 = mu2 .* mu3;
mu123 = mu12 .* mu3;
ints = zeros(1, numel(gvpow) * 8, fedpars.bltype);
ints( 1 : nv) = gvpow.*(-mu1 - mu2 - mu3 + mu12 + mu13 + mu23 - mu123 + 1);
ints( nv+1 : 2*nv) = gvpow.*(mu3 - mu13 - mu23 + mu123);
ints(2*nv+1 : 3*nv) = gvpow.*(mu2 - mu12 - mu23 + mu123);
ints(3*nv+1 : 4*nv) = gvpow.*(mu23 - mu123);
ints(4*nv+1 : 5*nv) = gvpow.*(mu1 - mu12 - mu13 + mu123);
ints(5*nv+1 : 6*nv) = gvpow.*(mu13 - mu123);
ints(6*nv+1 : 7*nv) = gvpow.*(mu12 - mu123);
ints(7*nv+1 : 8*nv) = gvpow.*mu123;
Nicola Vigano
committed
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
344
345
346
347
348
349
% Accumulate all intensities in the blob voxel-wise
% 'uvw' needs to be nx3; 'ints' is now 1xnx8
try
tmp_bl(ii_b).intm = accumarray(ucbl8, ints, blob_uvw_size);
catch mexc
disp(' ')
disp('ERROR')
disp(['The error is probably caused by the projected intensities' ...
' falling outside the blob volume. Try increasing the blob' ...
' volume padding: fedpars.detector(det_ind).blobsizeadd'])
disp('Blob ID:')
disp(ii_b)
disp('Blob size:')
disp(blob_uvw_size)
disp('Min projected U,V,W coordinates:')
disp(min(uvw_bl,[],2)')
disp('Max projected U,V,W coordinates:')
disp(max(uvw_bl,[],2)')
new_exc = GtFedExceptionFwdProj(mexc, det_ind, [0 0 0]);
throw(new_exc)
end
tmp_bl(ii_b).bbsize = blob_uvw_size;
im_low_lims = uvw_shifts(:, ii_b) - blob_orig_uvw_shift + 1;
tmp_bl(ii_b).bbuim = [im_low_lims(1), im_low_lims(1) + blob_uvw_size(1) - 1];
tmp_bl(ii_b).bbvim = [im_low_lims(2), im_low_lims(2) + blob_uvw_size(2) - 1];
tmp_bl(ii_b).bbwim = [im_low_lims(3), im_low_lims(3) + blob_uvw_size(3) - 1];
o.fprintf(repmat('\b', [1, num_chars]));
end
o.fprintf('Done in %g s\n', toc(t));
if (num_oversampling == 1 || ii_ss == 1)
bl = tmp_bl;
else
o.fprintf(' * Merging super-sampling blobs: ')
t = tic();
tmp_bl_bbs = cat(1, tmp_bl(:).bbuim, tmp_bl(:).bbvim, tmp_bl(:).bbwim);
tmp_bl_bbs = reshape(tmp_bl_bbs, nbl, 3, 2);
bl_bbs = cat(1, bl(:).bbuim, bl(:).bbvim, bl(:).bbwim);
bl_bbs = reshape(bl_bbs, nbl, 3, 2);
should_reallocate = any(any(tmp_bl_bbs ~= bl_bbs, 2), 3);
container_blobs_bb = cat(3, ...
min(bl_bbs(:, :, 1), tmp_bl_bbs(:, :, 1)), ...
max(bl_bbs(:, :, 2), tmp_bl_bbs(:, :, 2)) ...
);
container_blobs_size = container_blobs_bb(:, :, 2) - container_blobs_bb(:, :, 1) + 1;
shift_bls = bl_bbs(:, :, 1) - container_blobs_bb(:, :, 1);
shift_tmp_bls = tmp_bl_bbs(:, :, 1) - container_blobs_bb(:, :, 1);
for ii_b = 1:nbl
if (should_reallocate(ii_b))
new_bl = gtFwdSimBlobDefinition('blob');
new_bl.intm = zeros(container_blobs_size(ii_b, :), fedpars.bltype);
new_bl.intm = gtPlaceSubVolume(new_bl.intm, bl(ii_b).intm, shift_bls(ii_b, :));
new_bl.intm = gtPlaceSubVolume(new_bl.intm, tmp_bl(ii_b).intm, shift_tmp_bls(ii_b, :), [], 'summed');
new_bl.bbsize = container_blobs_size(ii_b, :);
new_bl.bbuim = reshape(container_blobs_bb(ii_b, 1, :), 1, []);
new_bl.bbvim = reshape(container_blobs_bb(ii_b, 2, :), 1, []);
new_bl.bbwim = reshape(container_blobs_bb(ii_b, 3, :), 1, []);
bl(ii_b) = new_bl;
else
bl(ii_b).intm = bl(ii_b).intm + tmp_bl(ii_b).intm;
end
end
o.fprintf('Done in %g s\n', toc(t));
end
end
if (num_oversampling > 1)
o.fprintf(' - Renormalizing to initial intensity:');
t = tic();
for ii_b = 1:nbl
bl(ii_b).int = bl(ii_b).int / num_oversampling;
o.fprintf(repmat('\b', [1, num_chars]));
end
o.fprintf('Done in %g s\n', toc(t));
end
if (~isempty(psf))
o.fprintf(' - Applying PSF:');
t = tic();
for ii_b = 1:nbl
num_chars = o.fprintf('%03d/%03d', ii_b, nbl);
bl(ii_b).int = convn(bl(ii_b).int, psf{ii_b}, 'same');
o.fprintf(repmat('\b', [1, num_chars]));
end
o.fprintf('Done in %g s\n', toc(t));
end
o.fprintf(' - Computing boundaries of measured blobs inside blob bounding box..')
t = tic();
for ii_b = 1:nbl
uproj = sum(sum(abs(bl(ii_b).intm), 2), 3);
bl(ii_b).mbbu = [find(uproj, 1, 'first'), find(uproj, 1, 'last')] + bl(ii_b).bbuim(1) - 1;
vproj = sum(sum(abs(bl(ii_b).intm), 1), 3);
bl(ii_b).mbbv = [find(vproj, 1, 'first'), find(vproj, 1, 'last')] + bl(ii_b).bbvim(1) - 1;
wproj = sum(sum(abs(bl(ii_b).intm), 1), 2);
bl(ii_b).mbbw = [find(wproj, 1, 'first'), find(wproj, 1, 'last')] + bl(ii_b).bbwim(1) - 1;
bl(ii_b).mbbsize = [bl(ii_b).mbbu(2) - bl(ii_b).mbbu(1) + 1, ...
bl(ii_b).mbbv(2) - bl(ii_b).mbbv(1) + 1, ...
bl(ii_b).mbbw(2) - bl(ii_b).mbbw(1) + 1 ];
end
o.fprintf('\b\b: Done in %g s\n', toc(t));
end