移动光源的表面强度

时间:2015-08-05 10:27:16

标签: matlab light

我正在尝试编写一个模拟,将光源移动到物体表面上,每一步都会测量反射光强度回到光源。

然后绘制表面每个点的强度。随着每一步添加到第一个图。从而建立了表面地形图。

如果你能提出一种整理的方法,那就太麻烦了。

x = -10:0.25:10;
y = -10:0.25:10;
xlength=length(x);
ylength=length(y);
ymin=min(y);
ymax=max(y);
xmin=min(x);
xmax=max(x);
D=zeros(xlength);


[X,Y] = meshgrid(x,y);
z = 10-(X.^2)-(Y.^2)+5*sin(X);
rnd=rand(xlength);

c=randi(xlength);
d=randi(xlength); 
s=1;
t=0;


for i=1:ylength
    t=t+1;
    for j=1:xlength
        if s==c
            if t==d 
                D(s,t)=z(s,t);
            elseif t==d+1
                D(s,t)=z(s,t);
            elseif t==d+2
                D(s,t)=z(s,t);
            elseif t==d+3
                D(s,t)=z(s,t);
            elseif t==d+4
                D(s,t)=z(s,t);
            elseif t==d+5
                D(s,t)=z(s,t);
            else
                D(s,t)=0;
            end
        elseif s==c+1
            if t==d
                D(s,t)=z(s,t);
            elseif t==d+1
                D(s,t)=z(s,t);
            elseif t==d+2
                D(s,t)=z(s,t);
            elseif t==d+3
                D(s,t)=z(s,t);
            elseif t==d+4
                D(s,t)=z(s,t);
            elseif t==d+5
                D(s,t)=z(s,t);
            else
                D(s,t)=0;
            end
        elseif s==c+2
            if t==d
                D(s,t)=z(s,t);
            elseif t==d+1
                D(s,t)=z(s,t);
            elseif t==d+2
                D(s,t)=z(s,t);
            elseif t==d+3
                D(s,t)=z(s,t);
            elseif t==d+4
                D(s,t)=z(s,t);
            elseif t==d+5
                D(s,t)=z(s,t);
            else
                D(s,t)=0;
            end
        elseif s==c+3
            if t==d
                D(s,t)=z(s,t);
            elseif t==d+1
                D(s,t)=z(s,t);
            elseif t==d+2
                D(s,t)=z(s,t);
            elseif t==d+3
                D(s,t)=z(s,t);
            elseif t==d+4
                D(s,t)=z(s,t);
            elseif t==d+5
                D(s,t)=z(s,t);
            else
                D(s,t)=0;
            end
        elseif s==(c+4)
            if t==d
                D(s,t)=z(s,t);
            elseif t==d+1
                D(s,t)=z(s,t);
            elseif t==d+2
                D(s,t)=z(s,t);
            elseif t==d+3
                D(s,t)=z(s,t);
            elseif t==d+4
                D(s,t)=z(s,t);
            elseif t==d+5
                D(s,t)=z(s,t);
             else
                D(s,t)=0;
            end
        else
            D(s,t)=0;
        end
        s=s+1;
    end
    s=1;
end       

z1=z -(D/20);
z2=z1-z;

s=0;

figure
surf(X,Y,z)
axis([xmin xmax ymin ymax])

xlabel('X')
ylabel('Y')
zlabel('Z')
    for i=-10:2.5:10
        hold on

        light('position',[i,0,50])
        surf(X,Y,z,'EdgeColor', 'none')
        axis([xmin xmax ymin ymax])

        drawnow

        pause (1)
        delete(findall(gcf,'Type','light'))

        hold off
    end

这是我所拥有的。

1 个答案:

答案 0 :(得分:0)

完全符合您要求的代码:(感谢@Hoki帮助您更简化代码)

clear;clc
x = -10:0.25:10;
y = -10:0.25:10;
xlength=length(x);
ylength=length(y);
ymin=min(y);
ymax=max(y);
xmin=min(x);
xmax=max(x);
[X,Y] = meshgrid(x,y);
z = 10-(X.^2)-(Y.^2)+5*sin(X);

%% plot!

figure
surf(X,Y,z,'EdgeColor', 'none','lineStyle','none') % taken out the linestyle because it looks cooler
axis([xmin xmax ymin ymax])

xlabel('X')
ylabel('Y')
zlabel('Z')
% Added repetitions and decreased step size, plus making it go forward and backward.
repetitions=4;
for jj=1:repetitions

   hl = light('position',[-10,0,50]) ;     %// create the light source
   for i=-10:2.5:10
       set(hl,'position',[i,0,50]) ;       %// set the new light position
       drawnow                             %// flush graphic pipeline
       pause (0.1)                         %// let human user see something
   end
   delete(hl)   %// delete the light source
end

这给出了以下gif:

http://i.imgur.com/42ffYll.gifv

关于代码的其余部分......

请记住

if t==d
   D(s,t)=z(s,t);
elseif t==d+1
   D(s,t)=z(s,t);
elseif t==d+2
   D(s,t)=z(s,t);
elseif t==d+3
   D(s,t)=z(s,t);
elseif t==d+4
   D(s,t)=z(s,t);
elseif t==d+5
   D(s,t)=z(s,t);
else
   D(s,t)=0;
end

与字面意思相同:

if (t>=d && t<=d+5)
   D(s,t)=z(s,t);
else
    D(s,t)=0;
end

这可以帮助您减少代码。 还有矢量化。

你可以做到

D(s,(t>=d && t<=d+5))=z(s,(t>=d && t<=d+5)));

对于s等所有值等等。希望这能让你前进;)

相关问题