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:
Extruding a 2d surface.

Subject: Extruding a 2d surface.

From: Vincent Chong

Date: 7 May, 2007 11:13:27

Message: 1 of 3

Hi all.. I am writing a function to convert a 2d shape into a 3d one.
If I have an existing 2d shape defined by 2 vectors (x and y), and I
want to extrude the shape by adding a fixed thickness in the z
direction, how do I go about doing that?

Subject: Extruding a 2d surface.

From: Vincent Chong

Date: 7 May, 2007 11:14:54

Message: 2 of 3

Sorry, I don't mean to double post, but I forgot to mention that I
have to use the fill3 command to draw the 3d shape.

Any help would be appreciated. Thanks.

 Vincent Chong wrote:
>
>
> Hi all.. I am writing a function to convert a 2d shape into a 3d
> one.
> If I have an existing 2d shape defined by 2 vectors (x and y), and
> I
> want to extrude the shape by adding a fixed thickness in the z
> direction, how do I go about doing that?

Subject: Extruding a 2d surface.

From: Eric Carlson

Date: 8 May, 2007 13:15:38

Message: 3 of 3

Hello Vincent,

It's not elegant, and I do not have time to look up formats for fill3,
but try (from my book Foundations of Engineering with MATLAB 7):


function main
%P is perimeter given in y-z (x=0) plane
% following routine and subfunctions extrudes in x direction
%Second argument of "extrude" is nx,and gives
% x-resolution=linspace(0,1,nx)
P=[0 0;1 0; 0 1]; %boundary triangle
fv=extrude(P,11); %resolution of 11 in the direction of extrusion
patch(fv, 'FaceVertexCData', ones(length(fv.vertices),1)*[1 .5 .5],...
      'FaceColor', 'interp');
axis off; axis equal
fv.vertices %just to let you see vertex values

function fv = extrude(P, nx)
% P is a column vector sequence of
% points for boundary ( [z,y]
% nx is resolution in x direction
% x == linspace(0,1,nx)
nP=length(P)+1;
fv = mesh_triangle(linspace(0,1,nx), ...
              linspace(0,1,nP ),...
              @(x,y) 0 );
z= reshape(ones(nx,1)*(P(:,1)'), [nx*(nP-1),1]);
z= [z;ones(nx,1)*z(1)];
y= reshape(ones(nx,1)*(P(:,2)'), [nx*(nP-1),1]);
y= [y;ones(nx,1)*y(1)];
fv.vertices = [fv.vertices(:,1) z y];
fv=vertex_match(fv);

function fv2 = vertex_match(fv2)
% % finds repeated vertices and combines them
% % input fv2 is a face vertex structure
% % output fv2 with faces and vertices updated
% % Getting rid of redundant vertices can enhance transformation performance
[fv2.vertices, m, n]=unique(fv2.vertices, 'rows');
fv2.faces=n(fv2.faces);


function fv = mesh_triangle(x, y, f)
%function to generate a data set for use with
% the patch command on a regular grid
% INPUT: x, y 1-D vectors partioning horizontal
% and vertical grids, respectively
% f is function of f(x,y)
% pass @(x,y) 0 for just a grid
% OUTPUT: fv a face-vertex structure
n = length(x); m=length(y);
kount = -1;
fv.faces((m-1)*(n-1)*2,:)=[0 0 0];
fv.vertices(m*n,:)=[0 0 0];
ind = 0;
for jy = 1:m
     for ix=1:n
        ind = ind+1;
        fv.vertices(ind,:)=[x(ix) y(jy) f(x(ix), y(jy))];
     end
end

for jy = 1:m-1
     for ix = 1:n-1
         kount=kount+2;
         ind = (jy-1)*n+ix;
         fv.faces(kount,:)=[ind ind+1 ind+n+1];
         fv.faces(kount+1,:)=[ind ind+n+1 ind+n];
     end
end

Tags for this Thread

No tags are associated with 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