How to extrude an image
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I want to extrude a 2D image as attached by a value of 3 and make it 3D. I can create a 3D image by providing vertex values as given below but I couldn't read this image and extrude.
%%Set up Coordinates
XY1 = [-1 0; -4 0; -4 5; -1 5; -1 4; -3 4; -3 1; -1 1];
fv1 = Extrude (XY1, -1, 1);
%XY2 = [1 0; 3 0; 3 4; 4 4; 4 5; 1 5; 1 4; 2 4; 2 1; 1 1];
%fv2 = Extrude (XY2, -1, 1);
%%Set up the figure
figure;
axis ([-2 2 -1.5 1.5]);
for i = 1:100
% Set up the camera parameters
yaw = i*(5*pi/180);
pitch = (pi/6);
Tz = 10 + 2*sin(yaw);
T = [0 0 -Tz]';
% Clear the axes
cla;
% Project the points into the camera
uv = ProjectPoints (fv1.vertices', yaw, pitch, T);
fv.Vertices = uv';
fv.faces = fv1.faces;
patch (fv, 'FaceColor', 'none');
% Project the points into the camera
% uv = ProjectPoints (fv2.vertices', yaw, pitch, T);
% fv.Vertices = uv';
% fv.faces = fv2.faces;
% patch (fv, 'FaceColor', 'none');
% Pause so you can see the result
%pause (0.2);
end
function fv = Extrude (XY, Z1, Z2)
% Compute a 3D extrusion of a 2D polygon
% XY is an n x 2 array of the polygon vertices
n = size(XY,1);
v1 = XY;
v1(:,3) = Z1;
v2 = XY;
v2(:,3) = Z2;
vertices = [v1; v2];
faces = NaN(n+2, n);
% Front and back faces
faces(1,:) = 1:n;
faces(2,:) = (n+1):(2*n);
for i = 1:n
if (i < n)
next_index = i+1;
else
next_index = 1;
end
faces(i+2,1:4) = [i, next_index, next_index+n, i+n];
end
fv.vertices = vertices;
fv.faces = faces;
0 Kommentare
Antworten (1)
Michael Barrow
am 27 Jul. 2018
If you make assumptions about the ordering of your points (say they are arranged in a clockwork order that they appear in the polygon) you could extrude the outline using the following example 'extrudeROI(x,y,d)' where d is the extrusion depth and x and y are the coordinates of your ordered point set:
function [pts,edges] = ROI2DtoPoly(x,y)
%ROI2DtoPoly converts an ordered set of points to a 2D polygon
% you can draw the set of points using the function [binaryMaskn1, x, y] = roipoly
pts = [x,y];
edges = [1:length(x);[2:length(x),1]]'; %the connectivity list of x and y should be ordered
end
function [pts,edges,tri] = extrudeROI(x,y,d)
%extrudeROI create an extruded 3D polygon of height d from an ROI drawn using: [binaryMaskn1, x, y] = roipoly
[bpts,bdg] = ROI2DtoPoly(x,y);
tpts = bpts;
tdg = bdg;
%add in third dimension
bpts3d = cat(2,tpts,zeros(length(x),1));
tpts3d = cat(2,bpts,ones(length(x),1)*d);
%create 3d points structure
pts = [bpts3d; tpts3d];
%fix the connectivity by offsetting one face's connectivity and adding side connector
tdg = tdg+length(x); %edges will be for points at the top
%define two triangulation edges, (v)ertical (d)iagonal [(b)ase is implied]
%vertical edges
sdgv = [1:length(x);(1:length(x))+length(x)]'; %connect the sides (extrude)
%diagonal edges
sdgd = [1:length(x);mod((1:length(x)),length(x))+length(x)+1]'; %construct connectivity list (diagonals, assumes closed poly)
%all edges
edges = [bdg; tdg; sdgv; sdgd]; %all edges, but not triangulation.
%triangulation
tri = [[sdgv,sdgd(:,2)];[sdgd,[2:length(x),1]']];%[lower triangles;upper triangles]
end
0 Kommentare
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!