I have following data from which I need to create coon's patch.
I need to code in Matlab
Matrix of polygon vertices given below
P0u = [0,0,0; 0.2357,0.2357,0.3333; 1.1785,0.2357,0.3333; 1.4142,0,0];
P0w = [1.4142,0,0; 1.1785,0.2357,0.3333; 1.1785,1.1785,0.3333; 1.4142,1.4142,0];
P1u = [1.4142,1.4142,0; 1.1785,1.1785,0.3333; 0.2357,1.1785,0.3333; 0,1.4142,0];
P1w = [0,1.4142,0; 0.2357,1.1785,0.3333; 0.2357,0.2357,0.3333; 0,0,0];
What I have tried:
I have tried below Matlab code but it is not giving appropriate results.
clc
clear
close all
%% Defining input parameters
% Defining individual point on polygon
A1 = [0,0,0];
A2 = [0.2357,0.2357,0.3333];
A3 = [1.1785,0.2357,0.3333];
A4 = [1.4142,0,0];
B1 = [1.4142,0,0];
B2 = [1.1785,0.2357,0.3333];
B3 = [1.1785,1.1785,0.3333];
B4 = [1.4142,1.4142,0];
C1 = [1.4142,1.4142,0];
C2 = [1.1785,1.1785,0.3333];
C3 = [0.2357,1.1785,0.3333];
C4 = [0,1.4142,0];
D1 = [0,1.4142,0];
D2 = [0.2357,1.1785,0.3333];
D3 = [0.2357,0.2357,0.3333];
D4 = [0,0,0];
%% Plot polygon
P0u = [A1;A2;A3;A4]; % Creating matrix for polygon vertices
P0w = [B1;B2;B3;B4]; % Creating matrix for polygon vertices
P1u = [C1;C2;C3;C4]; % Creating matrix for polygon vertices
P1w = [D1;D2;D3;D4]; % Creating matrix for polygon vertices
[r,s] = size(P0u); % getting size of matrix in terms of rows and columns
[r,s] = size(P0w); % getting size of matrix in terms of rows and columns
[r,s] = size(P1u); % getting size of matrix in terms of rows and columns
[r,s] = size(P1w); % getting size of matrix in terms of rows and columns
n = r-1; % n+1 represents number of vertices of the polygon
np = 20; % represents number of equi-distance points on the bezier curve
t = linspace(0,1,np);
hold all
grid on
view(45,45)
figure(1)
xlabel('X-Axis')
ylabel('Y-Axis')
zlabel('Z-Axis')
for k = 1:n
plot3([P0u(k,1),P0u(k+1,1)], [P0u(k,2),P0u(k+1,2)], [P0u(k,3),P0u(k+1,3)], 'r', 'LineWidth', 1);
plot3([P0w(k,1),P0w(k+1,1)], [P0w(k,2),P0w(k+1,2)], [P0w(k,3),P0w(k+1,3)], 'r', 'LineWidth', 1);
plot3([P1u(k,1),P1u(k+1,1)], [P1u(k,2),P1u(k+1,2)], [P1u(k,3),P1u(k+1,3)], 'r', 'LineWidth', 1);
plot3([P1w(k,1),P1w(k+1,1)], [P1w(k,2),P1w(k+1,2)], [P1w(k,3),P1w(k+1,3)], 'r', 'LineWidth', 1);
end
%% Generate the points on the bezier curve
for j = 1:np
P1 = [0,0,0];
P2 = [0,0,0];
P3 = [0,0,0];
P4 = [0,0,0];
for i = 0:n
M(i+1) = (factorial(n)/(factorial(i)*factorial(n-i)))*((t(j))^i)*((1-t(j))^(n-i));
P1 = P1 + P0u(i+1,:).*M(i+1);
P2 = P2 + P0w(i+1,:).*M(i+1);
P3 = P3 + P1u(i+1,:).*M(i+1);
P4 = P4 + P1w(i+1,:).*M(i+1);
end
Q1(j,:) = P1;
Q2(j,:) = P2;
Q3(j,:) = P3;
Q4(j,:) = P4;
end
%% Plot the bezier curve from the obtained points
for l = 1:np-1
plot3([Q1(l,1),Q1(l+1,1)],[Q1(l,2),Q1(l+1,2)], [Q1(l,3),Q1(l+1,3)], 'b', 'LineWidth', 1);
plot3([Q2(l,1),Q2(l+1,1)],[Q2(l,2),Q2(l+1,2)], [Q2(l,3),Q2(l+1,3)], 'b', 'LineWidth', 1);
plot3([Q3(l,1),Q3(l+1,1)],[Q3(l,2),Q3(l+1,2)], [Q3(l,3),Q3(l+1,3)], 'b', 'LineWidth', 1);
plot3([Q4(l,1),Q4(l+1,1)],[Q4(l,2),Q4(l+1,2)], [Q4(l,3),Q4(l+1,3)], 'b', 'LineWidth', 1);
end
%% Bilinear surface
npu = 10; % Number of points in u-direction
npw = 10; % Number of points in w-direction
u = linspace(0,1,npu);
w = linspace(0,1,npw);
for i = 1:npu
for j = 1:npw
Q = (((1-u(i))^3*A1)+(A2*3*u(i)*(1-u(i))^2)+(A3*3*u(i)^2*(1-u(i)))+(A4*u(i)^3))*(1-w(j)) + ...
(((1-u(i))^3*C1)+(C2*3*u(i)*(1-u(i))^2)+(C3*3*u(i)^2*(1-u(i)))+(C4*u(i)^3))*(w(j)) + ...
(((1-w(j))^3*B1)+(B2*3*w(j)*(1-w(j))^2)+(B3*3*w(j)^2*(1-w(j)))+(B4*w(j)^3))*(1-u(i))+...
(((1-w(j))^3*D1)+(D2*3*w(j)*(1-w(j))^2)+(D3*3*w(j)^2*(1-w(j)))+(D4*w(j)^3))*(u(i)) - ...
(((1-u(i)*(1-w(j))* A1))) - (((1-u(i)*(w(j))* C1))) - (((1-w(j)*(u(i))*D1))) - (((w(j)*(u(i))* B4)));
Qx(i,j) = Q(1);
Qy(i,j) = Q(2);
Qz(i,j) = Q(3);
end
end
surf(Qx,Qy,Qz)
xlabel('X-Axis')
ylabel('Y-Axis')
zlabel('Z-Axis')