-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalc_intensity.m
306 lines (264 loc) · 11.6 KB
/
Calc_intensity.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
function [Intensity,PhaseOneEl,NraysOneEl,Int_bone_long, Int_bone_shear,...
Pl, PS, NL, NS, polx,poly,polz, ks1,ks2,ks3, kl1,kl2,kl3] = Calc_intensity(ray, object)
% Calc_intensity given ray (struct containing a number of rays) this
% function will calculate the intensiy, phase, nrays in each point of the
% grid
% velx,vely,velz are the component (x,y,z) of the average of the
% polarization direction
% The velocity will be calculate later
[ xmin, xmax, ymin,ymax, zmin, zmax, Nx, Ny, Nz, dx, dy,...
dz, xx, yy, zz, xxb,yyb, zzb ] = Define_table();
Intensity=zeros(Nx,Ny,Nz); %Intensity in soft
PhaseOneEl=zeros(Nx,Ny,Nz); %Phase in soft
NraysOneEl=zeros(Nx,Ny,Nz);%Number of rays in soft in each cube
poly=zeros(Nx,Ny,Nz); %POLArization direction x
polx=zeros(Nx,Ny,Nz); %POLArization direction y
polz=zeros(Nx,Ny,Nz); %POLArization direction z
Int_bone_long=zeros(Nx,Ny,Nz); %Intensity in bone due to long
Pl=zeros(Nx,Ny,Nz); %Phase in bone due to long
NL=zeros(Nx,Ny,Nz); %Number of rays in each cube long
Int_bone_shear=zeros(Nx,Ny,Nz);%Intensity in bone due to shear
PS=zeros(Nx,Ny,Nz);%Phase in bone due to shear
NS=zeros(Nx,Ny,Nz);%Number of rays in each cube shear
% full approach
ks1=zeros(Nx,Ny,Nz); %direction ray
ks2=zeros(Nx,Ny,Nz); %direction ray
ks3=zeros(Nx,Ny,Nz); %direction ray
kl1=zeros(Nx,Ny,Nz); %direction ray
kl2=zeros(Nx,Ny,Nz); %direction ray
kl3=zeros(Nx,Ny,Nz); %direction ray
% vec_mat={ray.actual_object}';
% svid=~cellfun(@isempty, vec_mat);
% ray(svid)=[];
[~,sizeR]=size(ray);
if (sizeR>0)
for ri=1: sizeR
if ray(ri).start(1)<xmin
% if ray(ri).end(1)>xmin
if ray(ri).Vray(1)>0
lambda_x=(xxb-ray(ri).start(1))/ray(ri).Vray(1); % all positive elements
else % Vray(1)<=0, ray cannot reach table region
lambda_x=inf;
end
if ray(ri).end(1)<xmin % ray cannot reach the table
lambda_x=inf;
end
elseif ray(ri).start(1)>xmax
if ray(ri).Vray(1)<0
lambda_x=(xxb-ray(ri).start(1))/ray(ri).Vray(1);
% all positive elements, BUT: decreasing sequence
lambda_x=lambda_x(end:-1:1); % reverse
else % Vray(1)>=0, ray cannot reach table region
lambda_x=inf;
end
if ray(ri).end(1)>xmax % ray cannot reach the table
lambda_x=inf;
end
else % xmin<= startpoint(1)<= xmax, start inside table region
if ray(ri).Vray(1)>0
lambda_x=(xxb-ray(ri).start(1))/ray(ri).Vray(1); % may contain pos and neg values
lambda_x=lambda_x(lambda_x>=0); % no neg values
lambda_x=[0,lambda_x]; % add lambda=0, doubles will be removed later on
elseif ray(ri).Vray(1)<0
lambda_x=(xxb-ray(ri).start(1))/ray(ri).Vray(1); % may contain pos and neg values
lambda_x=lambda_x(lambda_x>=0); % no neg values, BUT decreasing sequence
lambda_x=lambda_x(end:-1:1); % reverse
lambda_x=[0,lambda_x]; % add lambda=0, doubles will be removed later
else % Vray(1) ==0
lambda_x=0;
end;
end;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if lambda_x(1)<inf % ray may pass through table region
% Generate lambda_y, the series of lambdas such that
% startpoint+lambda_y*Vray crosses a y-boundary between two cubes
if ray(ri).start(2)<ymin
if ray(ri).Vray(2)>0
lambda_y=(yyb-ray(ri).start(2))/ray(ri).Vray(2); % all positive elements
else % Vray(2)<=0, ray cannot reach table region
lambda_y=inf;
end
elseif ray(ri).start(2)>ymax
if ray(ri).Vray(2)<0
lambda_y=(yyb-ray(ri).start(2))/ray(ri).Vray(2);
% all positive elements, BUT decreasing sequence
lambda_y=lambda_y(end:-1:1); %reverse
else % Vray(2)>=0, ray cannot reach table region
lambda_y=inf;
end
else % ymin<= startpoint(2)<= ymax, start inside table region
if ray(ri).Vray(2)>0
lambda_y=(yyb-ray(ri).start(2))/ray(ri).Vray(2); % may contain pos and neg values
lambda_y=lambda_y(lambda_y>=0); % no neg values
lambda_y=[0,lambda_y]; % add lambda=0, doubles will be removed later on
elseif ray(ri).Vray(2)<0
lambda_y=(yyb-ray(ri).start(2))/ray(ri).Vray(2); % may contain pos and neg values
lambda_y=lambda_y(lambda_y>=0); % no neg values, BUT decreasing sequence
lambda_y=lambda_y(end:-1:1); % reverse
lambda_y=[0,lambda_y];% add lambda=0, doubles will be removed later
else % Vray(2) ==0
lambda_y=0;
end;
end;
else
lambda_y=inf; % lambda_x(1)=inf, so ray does not pass through table region anyway
end;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if lambda_x(1)<inf && lambda_y(1)<inf % ray may pass through table region
% Generate lambda_z, the seies of lambdas such that
% startpoint+lambda_z*Vray crosses a z-boundary between two cubes
if ray(ri).start(3)<zmin
if ray(ri).Vray(3)>0
lambda_z=(zzb-ray(ri).start(3))/ray(ri).Vray(3); % all positive elements
else % Vray(3)<=0, ray cannot reach table region
lambda_z=inf;
end
elseif ray(ri).start(3)>zmax
if ray(ri).Vray(3)<0
lambda_z=(zzb-ray(ri).start(3))/ray(ri).Vray(3);
% all positive elements, BUT decreasing sequence
lambda_z=lambda_z(end:-1:1); % reverse
else % Vray(2)>=0, ray cannot reach table region
lambda_z=inf;
end
else % zmin<= startpoint(3)<= zmax, start inside table region
if ray(ri).Vray(3)>0
lambda_z=(zzb-ray(ri).start(3))/ray(ri).Vray(3); % may contain pos and neg values
lambda_z=lambda_z(lambda_z>=0); % no neg values
lambda_z=[0,lambda_z]; % add lambda=0, doubles will be removed later on
elseif ray(ri).Vray(3)<0
lambda_z=(zzb-ray(ri).start(3))/ray(ri).Vray(3); % may contain pos and neg values
lambda_z=lambda_z(lambda_z>=0); % no neg values, BUT decreasing sequence
lambda_z=lambda_z(end:-1:1); % reverse
lambda_z=[0,lambda_z]; %add lambda=0, doubles will be removed later
else % Vray(3) ==0
lambda_z=0;
end;
end;
else
lambda_z=inf; % lambda_x(1)=inf or lambda_y(1)=inf,
% so ray does not pass through table region anyway
end;
% now process the three lambda sequences
Min_lambda = max([lambda_x(1),lambda_y(1),lambda_z(1)]);
Max_lambda = min([lambda_x(end),lambda_y(end),lambda_z(end)]);
% part of the ray between Min_lambda and Max_lambda is
% in the table region;
if Min_lambda<Max_lambda %ray passes through table region
%P_in=ray(ri).start+Min_lambda*ray(ri).Vray;
%P_out=ray(ri).start+Max_lambda*ray(ri).Vray;
lambda_x=lambda_x(Min_lambda<=lambda_x);
lambda_x_restr=lambda_x(lambda_x<=Max_lambda);
lambda_y=lambda_y(Min_lambda<=lambda_y);
lambda_y_restr=lambda_y(lambda_y<=Max_lambda);
lambda_z=lambda_z(Min_lambda<=lambda_z);
lambda_z_restr=lambda_z(lambda_z<=Max_lambda);
lambda_interesting=unique(sort([lambda_x_restr,lambda_y_restr,lambda_z_restr]));
for n=1:length(lambda_interesting)-1
lambda_1= lambda_interesting(n);
lambda_2= lambda_interesting(n+1);
lambda_12=(lambda_1+lambda_2)/2;
P_in_cube=ray(ri).start+lambda_1*ray(ri).Vray;
P_out_cube=ray(ri).start+lambda_2*ray(ri).Vray;
%ray(ri).start+lambda_12*ray(ri).Vray;
ind=floor((ray(ri).start+lambda_12*ray(ri).Vray-[xmin;ymin;zmin])./[dx;dy;dz])+1;
%
% Possible condition: I can reduce it, to explain
%
% if ray(ri).actual_object==2
% ffjdsjfkewf
% end
if ray(ri).direction == 1 %ray is going from left to right
if P_out_cube(1) > ray(ri).end(1)% ray ends in the cube
lambda_2=(ray(ri).end- ray(ri).start)'/ray(ri).Vray';
lambda_12=(lambda_1+lambda_2)/2;
end
else %ray is going from right to left
if P_out_cube(1) < ray(ri).end(1)% ray ends in the cube
lambda_2=(ray(ri).end- ray(ri).start)'/ray(ri).Vray';
lambda_12=(lambda_1+lambda_2)/2;
end
end;
%CONDITION NECESSARY to avoid that the rays ending in the
% middle of the table region will be considered to go through
% and intersect the cubes
if ((ray(ri).direction==1 && P_in_cube(1)<= ray(ri).end(1) ) || ...
(ray(ri).direction==-1 && P_in_cube(1)>= ray(ri).end(1)) )
% find the correct k and alpha
if object(ray(ri).actual_object).kind==0 %soft tissue
alpha= object(ray(ri).actual_object).attenuation;
kw=object(ray(ri).actual_object).k;
Intensity(ind(1),ind(2),ind(3))=Intensity(ind(1),ind(2),ind(3))+ ...
(ray(ri).I0*(exp(-2*alpha*lambda_12)))* ((abs(lambda_1-lambda_2))/(dx*dy*dz));
PhaseOneEl(ind(1),ind(2),ind(3))=PhaseOneEl(ind(1),ind(2),ind(3))+kw*lambda_12;
PhaseOneEl(ind(1),ind(2),ind(3))=PhaseOneEl(ind(1),ind(2),ind(3))+ray(ri).phase_initial;
NraysOneEl(ind(1),ind(2),ind(3))=NraysOneEl(ind(1),ind(2),ind(3))+1;
else if ray(ri).shear==0 %long
alpha= object(ray(ri).actual_object).attenuationl;
kw=object(ray(ri).actual_object).kl;
% treat bone here
Int_bone_long(ind(1),ind(2),ind(3))=Int_bone_long(ind(1),ind(2),ind(3))+...
(ray(ri).I0*(exp(-2*alpha*lambda_12)))* ((abs(lambda_1-lambda_2))/(dx*dy*dz));
Pl(ind(1),ind(2),ind(3))=Pl(ind(1),ind(2),ind(3))+kw*lambda_12;
Pl(ind(1),ind(2),ind(3))=Pl(ind(1),ind(2),ind(3))+ray(ri).phase_initial;
NL(ind(1),ind(2),ind(3))=NL(ind(1),ind(2),ind(3))+1;
else
alpha= object(ray(ri).actual_object).attenuations;
kw=object(ray(ri).actual_object).ks;
% treat bone here
Int_bone_shear(ind(1),ind(2),ind(3))=Int_bone_shear(ind(1),ind(2),ind(3))+...
(ray(ri).I0*(exp(-2*alpha*lambda_12)))* ((abs(lambda_1-lambda_2))/(dx*dy*dz));
PS(ind(1),ind(2),ind(3))=PS(ind(1),ind(2),ind(3))+kw*lambda_12;
PS(ind(1),ind(2),ind(3))=PS(ind(1),ind(2),ind(3))+ray(ri).phase_initial;
NS(ind(1),ind(2),ind(3))=NS(ind(1),ind(2),ind(3))+1;
polx(ind(1),ind(2),ind(3))=polx(ind(1),ind(2),ind(3))+ray(ri).polarization(1);% sum of the pol dir x
poly(ind(1),ind(2),ind(3))=poly(ind(1),ind(2),ind(3))+ray(ri).polarization(2);% sum of the pol dir y
polz(ind(1),ind(2),ind(3))=polz(ind(1),ind(2),ind(3))+ray(ri).polarization(3);% sum of the pol dir z
ks1(ind(1),ind(2),ind(3))= ks1(ind(1),ind(2),ind(3))+ray(ri).Vray(1);% sum of the dir x
ks2(ind(1),ind(2),ind(3))=ks2(ind(1),ind(2),ind(3))+ray(ri).Vray(2);% sum of the dir y
ks3(ind(1),ind(2),ind(3))=ks3(ind(1),ind(2),ind(3))+ray(ri).Vray(3);% sum of the dir z
end
end
end
end;
end
end
end
polx=polx./max(NS,1); %calculation of the average
poly=poly./max(NS,1);
polz=polz./max(NS,1);
velx2=polx.^2;
vely2=poly.^2;
velz2=polz.^2;
vnorm=velx2+vely2+velz2;
vnorm(vnorm==0)=1;
vnorm=sqrt(vnorm);
polx=polx./(vnorm); %normalization
poly=poly./(vnorm);
polz=polz./(vnorm);
%Vray long
kl1=kl1./max(NL,1); %calculation of the average
kl2=kl2./max(NL,1);
kl3=kl3./max(NL,1);
klx2=kl1.^2;
kly2=kl2.^2;
klz2=kl3.^2;
vnorm=klx2+kly2+klz2;
vnorm(vnorm==0)=1;
vnorm=sqrt(vnorm);
kl1=kl1./(vnorm); %normalization
kl2=kl2./(vnorm);
kl3=kl3./(vnorm);
%Vray shear
ks1=ks1./max(NS,1); %calculation of the average
ks2=ks2./max(NS,1);
ks3=ks3./max(NS,1);
ksx2=ks1.^2;
ksy2=ks2.^2;
ksz2=ks3.^2;
vnorm=ksx2+ksy2+ksz2;
vnorm(vnorm==0)=1;
vnorm=sqrt(vnorm);
ks1=ks1./(vnorm); %normalization
ks2=ks2./(vnorm);
ks3=ks3./(vnorm);