function [shadow_percentage] = sblock(p1,p2,w,d)
clear objects
clear functions
error(nargchk(3, 4, nargin))
if nargin == 3
d = w;
end
if nargin == 4
l=d;
end
t = 0:.25:1;
len_t = length(t);
syms u x_var y_var z_var;
c1 = 0.05;
k = -1.00099;
D = 25;
surface_area = 536.15;
pf = [0 0 1/(2*c1)];
zmax = c1*((D/2)^2)/...
(1+sqrt(1-(1+k)*c1^2*((D/2)^2)));
surface = c1*(x_var^2+y_var^2)/(1+sqrt(1-(1+k)*c1^2*(x_var^2+y_var^2)))-z_var;
h1 = pf(3)-zmax;
h2 = pf(3);
r1 = D/2;
r2 = h2*r1/h1;
z_0 = pf(3);
c2 = r2/(pf(3));
height_check = c1*(p1(1)^2+p1(2)^2)/(1+sqrt(1-(1+k)*c1^2*(p1(1)^2+p1(2)^2)));
if height_check > p1(3)
slope1 = p2-p1;
if (abs(slope1(1))<=1e-6 && abs(slope1(2))<=1e-6)
vert = 1;
x = p2(1);
y = p2(2);
z = p1(3) + u*slope1(3);
else
x = p1(1)+u*slope1(1);
y = p1(2)+u*slope1(2);
z = p1(3)+u*slope1(3);
end
prime_surface = subs(surface,x_var,x);
prime_surface = subs(prime_surface,y_var,y);
prime_surface = subs(prime_surface,z_var,z);
uu = eval(solve(prime_surface,u));
prime_point1 = [subs(x,u,uu(1)) subs(y,u,uu(1)) subs(z,u,uu(1))];
prime_point2 = [subs(x,u,uu(2)) subs(y,u,uu(2)) subs(z,u,uu(2))];
if (sqrt(prime_point1(1)^2+prime_point1(2)^2)-D/2<1e-6)
p1 = prime_point1;
else
p1 = prime_point2;
end
else
slope1 = p2-p1;
if (abs(slope1(1))<=1e-6 && abs(slope1(2))<=1e-6)
vert = 1;
end
p1= p1;
end
fprintf('prime inter\n')
if (p1(1) == 0 && p1(2)==0 && p2(1)==0 && p2(2) ==0)
if nargin == 4
fprintf('DO NOT ALIGN RECTANGULAR BEAMS WITH PRIMARY SURFACE VERTEX AND PRIME FOCUS\n');
shadow_percentage = sprintf('Invalid');
return
end
if p2(3) >= pf(3)
shadow_percentage = 100;
return
end
slope = (pf(3)-p2(3))/(pf(1)-(p2(1)+d/2));
b = pf(3);
z = slope*u+b;
prime_surface = subs(surface,x_var,x);
prime_surface = subs(prime_surface,y_var,0);
prime_surface = subs(prime_surface,z_var,z);
x = eval(solve(prime_surface,x_var));
point1 = x(1);
point2 = x(2);
if (point1<=D/2)
intersect_x = point1;
elseif (point2<=D/2)
intersect_x = point2;
else
shadow_percentage = 100;
return
end
xs=intersect_x*(0:.1:10);
shadow_area =0;
for i = 1:1:length(xs)-1
shadow_area_fraction = pi*(xs(i+1)^2-xs(i)^2);
shadow_area = shadow_area+shadow_area_fraction;
end
shadow_percentage = shadow_area/surface_area;
return
end
height_check = -sqrt((p2(1)^2+p2(2)^2)/c2^2)+z_0;
if height_check < p2(3)
slope1 = p2-p1;
if (abs(slope1(1))<=1e-6 && abs(slope1(2))<=1e-6)
vert = 1;
x = p2(1);
y = p2(2);
z = p1(3) + u*slope1(3);
else
x = p1(1)+u*slope1(1);
y = p1(2)+u*slope1(2);
z = p1(3)+u*slope1(3);
end
cone_surface = (x^2+y^2)/c2^2-(z-z_0)^2;
uu = eval(solve(cone_surface,u));
cone_point1 = [subs(x,u,uu(1)) subs(y,u,uu(1)) subs(z,u,uu(1))];
cone_point2 = [subs(x,u,uu(2)) subs(y,u,uu(2)) subs(z,u,uu(2))];
if (sqrt(cone_point1(1)^2+cone_point1(2)^2)-D/2<1e-6)
p2_act = cone_point1;
else
p2_act = cone_point2;
end
else
slope1 = p2-p1;
if (abs(slope1(1))<=1e-6 && abs(slope1(2))<=1e-6)
vert = 1;
end
p2_act = p2;
end
fprintf('ray cone\n')
if exist('vert','var')==0
beam_slope = p2_act-p1;
else
beam_slope = [0 0 p2_act(3)-p1(3)];
end
for i=1:1:len_t
beam_center(i,:) = [p1(1)+beam_slope(1)*t(i), p1(2)+beam_slope(2)*t(i), p1(3)+beam_slope(3)*t(i)];
slope = beam_center(i,:)-pf;
x=pf(1)+u*slope(1);
y=pf(2)+u*slope(2);
z=pf(3)+u*slope(3);
prime_surface = subs(surface,x_var,x);
prime_surface = subs(prime_surface,y_var,y);
prime_surface = subs(prime_surface,z_var,z);
uu = eval(solve(prime_surface,u));
if length(uu)==2
surf_point1 = subs(z,u,uu(1));
surf_point2 = subs(z,u,uu(2));
if (norm(surf_point1,2)<norm(surf_point2,2))
shadow_center(i,:) = [subs(x,u,uu(1)) subs(y,u,uu(1)) surf_point1];
else
shadow_center(i,:) = [subs(x,u,uu(2)) subs(y,u,uu(2)) surf_point2];
end
else
surf_point1 = subs(z,u,uu(1));
shadow_center(i,:) = [subs(x,u,uu(1)) subs(y,u,uu(1)) surf_point1];
end
end
fprintf('center line\n')
L = norm(p2_act-p1,2);
if exist('vert','var')
T = [0 0 1; 0 1 0; -1 0 0; ];
else
l_x = (p2_act(1)-p1(1))/L;
m_x = (p2_act(2)-p1(2))/L;
n_x = (p2_act(3)-p1(3))/L;
D = (l_x^2+m_x^2)^0.5;
l_y = -m_x/D;
m_y = l_x/D;
n_y = 0;
l_z = -l_x*n_x/D;
m_z = -m_x*n_x/D;
n_z = D;
T = [l_x m_x n_x; l_y m_y n_y; l_z m_z n_z];
end
if nargin == 3
r = d/2;
phi = 0:pi/30:(2*pi-pi/30);
slope_check =0;
len_2 = ceil(length(t)/2);
fprintf('phi iter begin\n')
for j = len_2
for i = 1:1:length(phi)
x = t(j)*L;
y = r*sin(phi(i));
z = r*cos(phi(i));
p3 = [x y z];
check = T'*p3'+p1';
element_edge= T'*p3'+p1';
slope3 = element_edge'-pf;
x1=pf(1)+u*slope3(1);
y1=pf(2)+u*slope3(2);
z1=pf(3)+u*slope3(3);
shadow = c1*(x1^2+y1^2)/(1+sqrt(1-(1+k)*c1^2*(x1^2+y1^2)))-z1;
uu1 = eval(solve(shadow,u));
if length(uu1) == 2
shadow1 = [subs(x1,u,uu1(1)) subs(y1,u,uu1(1)) subs(z1,u,uu1(1))];
shadow2 = [subs(x1,u,uu1(2)) subs(y1,u,uu1(2)) subs(z1,u,uu1(2))];
if (shadow1(3)<shadow2(3))
shadow_point1 = shadow1;
elseif (shadow2(3)<shadow1(3))
shadow_point1 = shadow2;
end
else
shadow1 = [subs(x1,u,uu1(1)) subs(y1,u,uu1(1)) subs(z1,u,uu1(1))];
shadow_point1 = shadow1;
end
if j == len_2
slope_check1 = (shadow_center(1,2)-shadow_point1(2))/(shadow_center(1,1)-shadow_point1(1));
slope_check2 = (shadow_center(1,2)-shadow_center(2,2))/(shadow_center(1,1)-shadow_center(2,1));
slope_diff = abs(slope_check1-slope_check2);
store(i,:) = [slope_diff phi(i)];
if slope_diff>slope_check
slope_check = slope_diff;
phi2 = phi(i);
shadow_edge1(j,:) = shadow_point1;
end
end
end
end
fprintf('phi iter done\n')
for i=1:1:len_t
if i~=len_2
x = t(i)*L;
y = r*sin(phi2);
z = r*cos(phi2);
p3 = [x y z];
element_edge= T'*p3'+p1';
slope3 = element_edge'-pf;
x1=pf(1)+u*slope3(1);
y1=pf(2)+u*slope3(2);
z1=pf(3)+u*slope3(3);
shadow = c1*(x1^2+y1^2)/(1+sqrt(1-(1+k)*c1^2*(x1^2+y1^2)))-z1;
uu1 = eval(solve(shadow,u));
if length(uu1) == 2
shadow1 = [subs(x1,u,uu1(1)) subs(y1,u,uu1(1)) subs(z1,u,uu1(1))];
shadow2 = [subs(x1,u,uu1(2)) subs(y1,u,uu1(2)) subs(z1,u,uu1(2))];
if (shadow1(3)<shadow2(3))
shadow_point1 = shadow1;
elseif (shadow2(3)<shadow1(3))
shadow_point1 = shadow2;
end
else
shadow1 = [subs(x1,u,uu1(1)) subs(y1,u,uu1(1)) subs(z1,u,uu1(1))];
shadow_point1 = shadow1;
end
shadow_edge1(i,:) = shadow_point1;
end
end
[~, locs] = findpeaks(store(:,1));
if phi(locs(1)) == phi2
phi2 = phi(locs(2));
else
phi2 = phi(locs(1));
end
fprintf('side done\n')
for i=1:1:len_t
x = t(i)*L;
y = r*sin(phi2);
z = r*cos(phi2);
p3 = [x y z];
element_edge= T'*p3'+p1';
slope3 = element_edge'-pf;
x1=pf(1)+u*slope3(1);
y1=pf(2)+u*slope3(2);
z1=pf(3)+u*slope3(3);
shadow = c1*(x1^2+y1^2)/(1+sqrt(1-(1+k)*c1^2*(x1^2+y1^2)))-z1;
uu1 = eval(solve(shadow,u));
if length(uu1) == 2
shadow1 = [subs(x1,u,uu1(1)) subs(y1,u,uu1(1)) subs(z1,u,uu1(1))];
shadow2 = [subs(x1,u,uu1(2)) subs(y1,u,uu1(2)) subs(z1,u,uu1(2))];
if (shadow1(3)<shadow2(3))
shadow_point1 = shadow1;
elseif (shadow2(3)<shadow1(3))
shadow_point1 = shadow2;
end
else
shadow1 = [subs(x1,u,uu1(1)) subs(y1,u,uu1(1)) subs(z1,u,uu1(1))];
shadow_point1 = shadow1;
end
shadow_edge2(i,:) = shadow_point1;
end
end
fprintf('side done\n')
if nargin == 4
len_2 = ceil(length(t)/2);
x = t(len_2)*L;
w = w/2;
l = l/2;
loc_corner1 = [x w l];
loc_corner2 = [x -w l];
glo_corner1 = T'*loc_corner1'+p1';
glo_corner2 = T'*loc_corner2'+p1';
glo_corners = [glo_corner1'; glo_corner2'];
for i = 1:1:2
slope3 = glo_corners(i,:)-pf;
x1=pf(1)+u*slope3(1);
y1=pf(2)+u*slope3(2);
z1=pf(3)+u*slope3(3);
shadow = c1*(x1^2+y1^2)/(1+sqrt(1-(1+k)*c1^2*(x1^2+y1^2)))-z1;
uu1 = eval(solve(shadow,u));
if length(uu1) == 2
shadow1 = [subs(x1,u,uu1(1)) subs(y1,u,uu1(1)) subs(z1,u,uu1(1))];
shadow2 = [subs(x1,u,uu1(2)) subs(y1,u,uu1(2)) subs(z1,u,uu1(2))];
if (shadow1(3)<shadow2(3))
shadow_point1 = shadow1;
elseif (shadow2(3)<shadow1(3))
shadow_point1 = shadow2;
end
else
shadow1 = [subs(x1,u,uu1(1)) subs(y1,u,uu1(1)) subs(z1,u,uu1(1))];
shadow_point1 = shadow1;
end
shadow_edge(i,:) = shadow_point1;
end
slope_check1 = (shadow_center(1,2)-shadow_edge(1,2))/(shadow_center(1,1)-shadow_edge(1,1));
slope_check2 = (shadow_center(1,2)-shadow_edge(2,2))/(shadow_center(1,1)-shadow_edge(2,1));
slope_check3 = (shadow_center(1,2)-shadow_center(len_2,2))/(shadow_center(1,1)-shadow_center(len_2,1));
slope_diff1 = abs(slope_check1-slope_check3);
slope_diff2 = abs(slope_check2-slope_check3);
check_inline1 = abs(abs(glo_corner1(1))-abs(glo_corner2(1)));
check_inline2 = abs(abs(glo_corner1(2))-abs(glo_corner2(2)));
check_inline = check_inline1+check_inline2;
if (slope_diff1>slope_diff2 || (check_inline<1e-10))
shadow_edge1(2,:) = shadow_edge(1,:);
for i=1:1:len_t
x = t(i)*L;
loc_corner1 = [x w l];
loc_corner2 = [x -w l];
loc_corner3 = [x -w -l];
loc_corner4 = [x w -l];
glo_corner1 = T'*loc_corner1'+p1';
glo_corner2 = T'*loc_corner2'+p1';
glo_corner3 = T'*loc_corner3'+p1';
glo_corner4 = T'*loc_corner4'+p1';
if (((check_inline)<1e-10) && i~=len_t)
slope1 = glo_corner4'-pf;
slope3 = glo_corner3'-pf;
elseif ((check_inline<1e-10) && i==len_t)
slope1 = glo_corner1'-pf;
slope3 = glo_corner2'-pf;
elseif (check_inline>1e-10)
slope1 = glo_corner1'-pf;
slope3 = glo_corner3'-pf;
end
x1=pf(1)+u*slope1(1);
y1=pf(2)+u*slope1(2);
z1=pf(3)+u*slope1(3);
x3=pf(1)+u*slope3(1);
y3=pf(2)+u*slope3(2);
z3=pf(3)+u*slope3(3);
shadow_1 = c1*(x1^2+y1^2)/(1+sqrt(1-(1+k)*c1^2*(x1^2+y1^2)))-z1;
shadow_3 = c1*(x3^2+y3^2)/(1+sqrt(1-(1+k)*c1^2*(x3^2+y3^2)))-z3;
uu1 = eval(solve(shadow_1,u));
uu3 = eval(solve(shadow_3,u));
if length(uu1) == 2
shadow1 = [subs(x1,u,uu1(1)) subs(y1,u,uu1(1)) subs(z1,u,uu1(1))];
shadow2 = [subs(x1,u,uu1(2)) subs(y1,u,uu1(2)) subs(z1,u,uu1(2))];
if (shadow1(3)<shadow2(3))
shadow_point1 = shadow1;
elseif (shadow2(3)<shadow1(3))
shadow_point1 = shadow2;
end
else
shadow1 = [subs(x1,u,uu1(1)) subs(y1,u,uu1(1)) subs(z1,u,uu1(1))];
shadow_point1 = shadow1;
end
if length(uu3) == 2
shadow1 = [subs(x3,u,uu3(1)) subs(y3,u,uu3(1)) subs(z3,u,uu3(1))];
shadow2 = [subs(x3,u,uu3(2)) subs(y3,u,uu3(2)) subs(z3,u,uu3(2))];
if (shadow1(3)<shadow2(3))
shadow_point3 = shadow1;
elseif (shadow2(3)<shadow1(3))
shadow_point3 = shadow2;
end
else
shadow1 = [subs(x3,u,uu3(1)) subs(y3,u,uu3(1)) subs(z3,u,uu3(1))];
shadow_point3 = shadow1;
end
shadow_edge1(i,:) = shadow_point1;
shadow_edge2(i,:) = shadow_point3;
end
else
for i=1:1:len_t
x = t(i)*L;
loc_corner2 = [x -w l];
glo_corner2 = T'*loc_corner2'+p1';
loc_corner4 = [x w -l];
glo_corner4 = T'*loc_corner4'+p1';
slope2 = glo_corner2'-pf;
slope4 = glo_corner4'-pf;
x2=pf(1)+u*slope2(1);
y2=pf(2)+u*slope2(2);
z2=pf(3)+u*slope2(3);
x4=pf(1)+u*slope4(1);
y4=pf(2)+u*slope4(2);
z4=pf(3)+u*slope4(3);
shadow_2 = c1*(x2^2+y2^2)/(1+sqrt(1-(1+k)*c1^2*(x2^2+y2^2)))-z2;
shadow_4 = c1*(x4^2+y4^2)/(1+sqrt(1-(1+k)*c1^2*(x4^2+y4^2)))-z4;
uu2 = eval(solve(shadow_2,u));
uu4 = eval(solve(shadow_4,u));
if length(uu2) == 2
shadow1 = [subs(x2,u,uu2(1)) subs(y2,u,uu2(1)) subs(z2,u,uu2(1))];
shadow2 = [subs(x2,u,uu2(2)) subs(y2,u,uu2(2)) subs(z2,u,uu2(2))];
if (shadow1(3)<shadow2(3))
shadow_point2 = shadow1;
elseif (shadow2(3)<shadow1(3))
shadow_point2 = shadow2;
end
else
shadow1 = [subs(x2,u,uu2(1)) subs(y2,u,uu2(1)) subs(z2,u,uu2(1))];
shadow_point2 = shadow1;
end
if length(uu4) == 2
shadow1 = [subs(x4,u,uu4(1)) subs(y4,u,uu4(1)) subs(z4,u,uu4(1))];
shadow2 = [subs(x4,u,uu4(2)) subs(y4,u,uu4(2)) subs(z4,u,uu4(2))];
if (shadow1(3)<shadow2(3))
shadow_point4 = shadow1;
elseif (shadow2(3)<shadow1(3))
shadow_point4 = shadow2;
end
else
shadow1 = [subs(x4,u,uu4(1)) subs(y4,u,uu4(1)) subs(z4,u,uu4(1))];
shadow_point4 = shadow1;
end
shadow_edge1(i,:) = shadow_point2;
shadow_edge2(i,:) = shadow_point4;
end
end
end
shadow_area = 0;
for i=1:1:len_t-1
avg_width1 = sqrt(dot(shadow_edge1(i,1:3)-shadow_edge2(i,:),shadow_edge1(i,1:3)-shadow_edge2(i,:)));
avg_width2 = sqrt(dot(shadow_edge1(i+1,1:3)-shadow_edge2(i+1,:),shadow_edge1(i+1,1:3)-shadow_edge2(i+1,:)));
avg_length1 = sqrt(dot(shadow_edge1(i,1:3)-shadow_edge1(i+1,1:3),shadow_edge1(i,1:3)-shadow_edge1(i+1,1:3)));
avg_length2 = sqrt(dot(shadow_edge2(i,:)-shadow_edge2(i+1,:),shadow_edge2(i,:)-shadow_edge2(i+1,:)));
avg_width = (avg_width1+avg_width2)/2;
avg_length = (avg_length1+avg_length2)/2;
area(i) = avg_width*avg_length;
shadow_area = shadow_area+area(i);
end
shadow_percentage = 100*(shadow_area / surface_area);
end