Connecting the 3d faces of a polygon together

7 views (last 30 days)
Hi everyone,
I have two faces of a polygon in 3d, meaning that I have drawn the opposite faces of the 3d figure with nothing in between. The values for the x,y, and z coordinates for each face have been stored into x-coor, y-coor, and z-coor arrays for each face respectively.
For instance, face 1 has all x-coors stored in array x, face 2 has x-coors stored in arrayx2, face 1 has y-coors stored in array y, etc.
Any ideas on how to connect the corresponding points from each face together to form a full solid? Thanks.

Accepted Answer

Walter Roberson
Walter Roberson on 26 Jun 2015
http://www.mathworks.com/help/matlab/ref/patch.html and scan down to "Specifying Patch Object Shapes" and the second group of code, the one starting with
% The Vertices property contains the coordinates of each
and you can use the method shown there. So in your case, the Vertices would be
verts = [x(:), y(:), z(:); x2(:), y2(:), z2(:)];
and your faces would start
numvert = length(x);
faces(1,:) = 1:numvert; %draw first polygon
faces(2,:) = numvert+1:2*numvert; %draw second polygon
and then
vL = (1 : numvert - 1) .';
faces(2+vL,1:4) = [vL, vL + 1, numvert + 1 + vL, numvert + vL];
faces(2+numvert,1:4) = [numvert, 1, 2*numvert, numvert+1];
This creates a whole bunch of 4-sided polygons, connecting each point to the next point, then over to the corresponding next point, then back to the corresponding point, with an implicit close back to the original point. You did not explicitly ask for such polygons, but if you do not use triangles or quads then you will not get a "solid" like you asked for.
  2 Comments
Abel Matser
Abel Matser on 24 Sep 2020
Edited: Abel Matser on 24 Sep 2020
Dear Walter, I am looking to a similar solution, but the code you provided did not directly work for me. Matlab R2019a throws two errors. I'm using the following code:
% Example with data
shape = polyshape([0 0 1 1 0.5],[1 0 0 1 1.5]);
x = shape.Vertices(:,1);
y = shape.Vertices(:,2);
num_x = numel(x);
ub = 0.7;
lb = 0.1;
z = lb * ones(num_x,1);
x2 = x;
y2 = y;
z2 = ub * ones(num_x,1);
verts = [x(:), y(:), z(:); x2(:), y2(:), z2(:)];
numvert = length(x);
% Comment to create picture
faces(1,:) = 1:numvert; %draw first polygon
faces(2,:) = numvert+1:2*numvert; %draw second polygon
vL = (1 : numvert - 1) .';
faces(2+vL,1:4) = [vL, vL + 1, numvert + 1 + vL, numvert + vL];
faces(2+numvert,1:4) = [numvert, 1, 2*numvert, numvert+1];
% Uncomment to create picture
% faces = faces(3:end,:);
figure
ax = axes('XLim',[-3 3],'YLim',[-3 3],'ZLim',[-2 2]);
view(3)
grid on
alpha = 0.5;
p = patch('Faces',faces,'Vertices',verts,'FaceColor','red')
I'm trying to create an extrusion of a house shape in this example, which uses 5 points. The first error is caused because my shape has 5 points instead of 4.
% Unable to perform assignment because the size of the left side is 1-by-4
% and the size of the right side is 1-by-5.
% Error in polyshape2solid (line 74)
% faces(1,:) = 1:numvert; %draw first polygon
The second error is when I leave the declaration of faces(1:2,:) empty
% Error using patch
% Value must be of numeric type and greater than 1
Lastly, I deleted the first two rows faces(1:2,:). This results in an incomplete patch, see the attached picture.
I created my own topic in case this is overlooked: link

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!