MATLAB: How to create patches of quadrilaterals (4 vertices), 1 at a time and render them all at once

patch 3d visualization

Hi, I am using the patch command to create a 3D simulation of a cutting process. So, I want to create a 3D visualization of a block of material and track it as material gets taken out. I'm meshing the block and using a 2D matrix which specifies the instantaneous depth of each element in the mesh. Now i want to figure out the best way to patch for the corresponding 3D visulisation.
a=[ 0 0 0 0 0 0 0 -1 -1 -1 -1 -1 -1]; partmap=repmat(a,10,1);
So in this case, the material has been cut on the right side to a depth of 1 unit (-ve Z describes a cut of that value).
This is my current code to view this in 3D:
seenFirstCut = false;
firstCut = 0; %index of the first cut seen in the row
firstCutDepth = 1; %depth value of the 1st cut seen in the row
row = size(partmap,1);
column = size(partmap,2);
array = [];
for i=1:row %row
for j=1:column %column
val = partmap(i,j);
if (seenFirstCut == false)
%value of the current depth searched
firstCut = j;
firstCutDepth = val;
seenFirstCut = true;
elseif (val ~= firstCutDepth)
%patch

x_n = firstCut - 1;
x_o = (j-1);
y_n = row -(i - 1);
y_o = row -(i);
z_n = height/ms + partmap(i,j-1)/ms;
z_o = 0;
vert = [x_n y_o z_o; x_o y_o z_o; x_o y_n z_o; x_n y_o z_n; x_o y_o z_n; x_o y_n z_n; x_n y_n z_n; x_n y_n z_o];
fac = [1 8 7 4; 4 1 2 5; 5 6 7 4];
for m = 1: size(fac,1)
for b = 1: size(fac,2)
array = [array; vert(fac(m,b),:)];
end
end
% patch('Vertices',vert,'Faces',fac,'FaceVertexCData',0.2,'FaceColor','flat')

% view(3)

% axis equal vis3d

firstCut = j;
firstCutDepth = val;
elseif (j == column)
%patch
x_n = (firstCut - 1);
x_o = (j);
y_n = row - (i - 1);
y_o = row - (i);
z_n = height/ms + val/ms;
z_o = 0;
vert = [x_n y_o z_o; x_o y_o z_o; x_o y_n z_o; x_n y_o z_n; x_o y_o z_n; x_o y_n z_n; x_n y_n z_n; x_n y_n z_o];
fac = [1 8 7 4; 4 1 2 5; 5 4 7 6];
for m = 1: size(fac,1)
for b = 1: size(fac,2)
array = [array; vert(fac(m,b),:)];
end
end
% patch('Vertices',vert,'Faces',fac,'FaceVertexCData',0.2,'FaceColor','flat')
% view(3)
% axis equal vis3d
end
end
end
patch(array(1:16,1), array(1:16,2), array(1:16,3), 0.2 * ones(16,1));
view(3)
shading flat;
axis equal;
This gives a very weird 3D visual. I am basically looking at each row and patching rectangles of equal value and doing this iteratively. Is there a way to do this more efficiently? Is there a way to patch multilple quadrilaterals (given 4 vertices) with a single patch command where each quadrilateral is separate (last vertex of previous quadrilateral does not link to first vertex of next quadrilateral)?
Please let me know if I need to explain more. I know this is a little long-drawn. . . I'm hoping for some efficient solution!

Best Answer

You already have the basic mechanism, only commented out. Your patch() call with 'Faces' and 'Vertices' is a good mechanism. According to the documentation,
Faces
Each row in the faces array designates the connections for a single face, and the number of elements in that row that are not NaN defines the number of vertices for that face. Therefore, an m-by-n Faces array defines m faces with up to n vertices each.
Use the Vertices list to record the x, y, z of the unique vertices, and use the Faces matrix to list the order of the indices into the rows of Vertices that together form one face. There is an implicit last-connects-to-first for any one face but the faces are not linked unless you tell patch to link them.