Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Geometrical Problem

Subject: Geometrical Problem

From: James Ramm

Date: 28 Jun, 2010 14:51:10

Message: 1 of 1

Hi, I have a function which plots two surfaces at different elevations and 'closes' the gaps between their edges using patch, to create a closed 3D surface. (see the function Surf3D).
I now realise a problem in that my algorithm for finding the boundary edges and plotting the faces is not entirely correct - specifically south facing faces are not plotted. Logically, the algorithm here should be the reverse of that for the north faces (which works fine), and it is....however it does not work. I wondered if anyone could spot the problem.

This is the code, running it will produce the faulty image:

[Xn,Yn,Ztop] = peaks(100);
Zbot=Ztop-5;
% Get a subset of the area (originally created with ginput command)
x=[-1.0023 -0.2281 0.8088 1.8594 1.7627 0.6982 -0.7120];
y=[-0.7807 -1.5351 -1.9737 -1.0263 0.7982 1.9386 1.5000];
% replace all values outside the subset with nan
isinpolygon=inpolygon(Xn,Yn,x,y);
newZtop=Ztop; newZtop(find(isinpolygon==0))=nan;
newZbot=Zbot; newZbot(find(isinpolygon==0))=nan;
R=newZtop;

%plot the two surfaces
scrsz = get(0,'ScreenSize');
figure('Position',[scrsz(3)/4 scrsz(4)/4 scrsz(3)/2 scrsz(4)/2],'Renderer','opengl'); surf(Xn,Yn,newZtop,R); hold on; surf(Xn,Yn,newZbot,R);
[I,J]=size(Ztop);

%Now follows the alogrithm to get the vertices of boundary faces and plot them.

for i=1:I
    for j=1:J
        if isnan(R(i,j))
        else
            if ~isnan(R(i+1,j)) && isnan(R(i,j-1))%west
                xt1=Xn(i,j); xt2=Xn(i+1,j); xb1=Xn(i,j); xb2=Xn(i+1,j);
                yt1=Yn(i,j); yt2=Yn(i+1,j); yb1=Yn(i,j); yb2=Yn(i+1,j);
                zt1=newZtop(i,j); zt2=newZtop(i+1,j); zb1=newZbot(i,j); zb2=newZbot(i+1,j);
                patch([xt1 xt2 xb2 xb1],[yt1 yt2 yb2 yb1],[zt1 zt2 zb2 zb1],R(i,j))
            end
           if ~isnan(R(i-1,j)) && isnan(R(i,j-1)) %west
                xt1=Xn(i,j); xt2=Xn(i-1,j); xb1=Xn(i,j); xb2=Xn(i-1,j);
                yt1=Yn(i,j); yt2=Yn(i-1,j); yb1=Yn(i,j); yb2=Yn(i-1,j);
                zt1=newZtop(i,j); zt2=newZtop(i-1,j); zb1=newZbot(i,j); zb2=newZbot(i-1,j);
                patch([xt1 xt2 xb2 xb1],[yt1 yt2 yb2 yb1],[zt1 zt2 zb2 zb1],R(i,j))
            end
            if ~isnan(R(i,j+1)) && isnan(R(i+1,j)) %south - this causes the problems
                 xt3=Xn(i,j); xt4=Xn(i,j+1); xb3=Xn(i,j); xb4=Xn(i,j+1);
                yt3=Yn(i,j); yt4=Yn(i,j+1); yb3=Yn(i,j); yb4=Yn(i,j+1);
                zt3=newZtop(i,j); zt4=newZtop(i,j+1); zb3=newZbot(i,j); zb4=newZbot(i,j+1);
                patch([xt3 xt4 xb4 xb3],[yt3 yt4 yb4 yb3],[zt3 zt4 zb4 zb3],R(i,j))
            end
            if ~isnan(R(i,j-1)) && isnan(R(i+1,j)) %south - causes problems
                 xt3=Xn(i,j); xt4=Xn(i,j-1); xb3=Xn(i,j); xb4=Xn(i,j-1);
                yt3=Yn(i,j); yt4=Yn(i,j-1); yb3=Yn(i,j); yb4=Yn(i,j-1);
                zt3=newZtop(i,j); zt4=newZtop(i,j-1); zb3=newZbot(i,j); zb4=newZbot(i,j-1);
                patch([xt3 xt4 xb4 xb3],[yt3 yt4 yb4 yb3],[zt3 zt4 zb4 zb3],R(i,j))
            end
            if ~isnan(R(i+1,j)) && isnan(R(i,j+1))%east
                xt1=Xn(i,j); xt2=Xn(i+1,j); xb1=Xn(i,j); xb2=Xn(i+1,j);
                yt1=Yn(i,j); yt2=Yn(i+1,j); yb1=Yn(i,j); yb2=Yn(i+1,j);
                zt1=newZtop(i,j); zt2=newZtop(i+1,j); zb1=newZbot(i,j); zb2=newZbot(i+1,j);
                patch([xt1 xt2 xb2 xb1],[yt1 yt2 yb2 yb1],[zt1 zt2 zb2 zb1],R(i,j-1))
            end
            if ~isnan(R(i-1,j)) && isnan(R(i,j+1))%east
                xt1=Xn(i,j); xt2=Xn(i-1,j); xb1=Xn(i,j); xb2=Xn(i-1,j);
                yt1=Yn(i,j); yt2=Yn(i-1,j); yb1=Yn(i,j); yb2=Yn(i-1,j);
                zt1=newZtop(i,j); zt2=newZtop(i-1,j); zb1=newZbot(i,j); zb2=newZbot(i-1,j);
                patch([xt1 xt2 xb2 xb1],[yt1 yt2 yb2 yb1],[zt1 zt2 zb2 zb1],R(i,j-1))
            end
            if ~isnan(R(i,j+1))&& isnan(R(i-1,j)) %north
                 xt3=Xn(i,j); xt4=Xn(i,j+1); xb3=Xn(i,j); xb4=Xn(i,j+1);
                yt3=Yn(i,j); yt4=Yn(i,j+1); yb3=Yn(i,j); yb4=Yn(i,j+1);
                zt3=newZtop(i,j); zt4=newZtop(i,j+1); zb3=newZtop(i,j); zb4=newZtop(i,j+1);
                patch([xt3 xt4 xb4 xb3],[yt3 yt4 yb4 yb3],[zt3 zt4 zb4 zb3],R(i,j))
            end
            if ~isnan(R(i,j-1))&& isnan(R(i-1,j)) %north
                 xt3=Xn(i,j); xt4=Xn(i,j-1); xb3=Xn(i,j); xb4=Xn(i,j-1);
                yt3=Yn(i,j); yt4=Yn(i,j-1); yb3=Yn(i,j); yb4=Yn(i,j-1);
                zt3=newZtop(i,j); zt4=newZtop(i,j-1); zb3=newZbot(i,j); zb4=newZbot(i,j-1);
                patch([xt3 xt4 xb4 xb3],[yt3 yt4 yb4 yb3],[zt3 zt4 zb4 zb3],R(i,j))
            end
        end
    end
end
set(findobj(gca,'type','surface'),'EdgeColor','none');
set(findobj(gca,'type','patch'),'EdgeColor','none');
view(350,40)

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us