gpt4 book ai didi

matlab - 如何在 MATLAB 中使用矩形放样以创建灵活的 3D 封闭管道?

转载 作者:行者123 更新时间:2023-12-02 20:01:08 25 4
gpt4 key购买 nike

我有一系列矩形,并且知道它们各自 4 个角的确切位置。我想绘制它们并通过它们中的每一个进行放样以创建类似矩形横截面的 3D 管道之类的东西。这些点也不应该限于沿着直轴。应对偏差应灵活。我还想将两端修补封闭。

我在你们的网站上看到了一个关于放样的类似问题,标题为“如何用椭圆放样以在 MATLAB 或 Python 中创建 3d 空心管?”。答案给我留下了深刻的印象,但遗憾的是对于椭圆和圆形。我试图让它与矩形一起工作,但无法弄清楚所需的逻辑。我还尝试将所有东西修补在一起,但这导致了锋利边缘的产生,这是我不想要的。我的代码看起来像这样:

A = importdata(filename);

[size_A, ~] = size(A.data);
axis vis3d;

for i=1:12:size_A-12

X1 = A.data(i+1); X2 = A.data(i+4); X3 = A.data (i+7); X4= A.data (i+10);
Y1 = A.data(i+2); Y2 = A.data(i+5); Y3 = A.data(i+8); Y4 = A.data(i+11);
Z1 = A.data(i+3); Z2 = A.data(i+6); Z3 = A.data(i+9); Z4 = A.data(i+12);

X= [X1;X2;X3;X4];
Y= [Y1;Y2;Y3;Y4];
Z= [Z1;Z2;Z3;Z4];


plot3(X, Y, Z)
patch(X, Y, Z, 'g'); %% for the particular planes


if(i>1) %% for the patching between two planes

A1= [ X1 X1 X2 X4; a1 X4 a2 X3; a2 a4 a3 a3; X2 a1 X3 a4];
B1= [ Y1 Y1 Y2 Y4; b1 Y4 b2 Y3; b2 b4 b3 b3; Y2 b1 Y3 b4];
C1= [ Z1 Z1 Z2 Z4; c1 Z4 c2 Z3; c2 c4 c3 c3; Z2 c1 Z3 c4];

plot3(A1, B1, C1)
patch(A1, B1, C1, 'g');

end

a1=X1; a2=X2; a3=X3; a4=X4;
b1=Y1; b2=Y2; b3=Y3; b4=Y4;
c1=Z1; c2=Z2; c3=Z3; c4=Z4;

figure(1)
grid on
axis equal
hold on
xlabel('x');
ylabel('y');
zlabel('z');

end

最终结果应该像一个具有矩形横截面的非常弯曲的管道。不应有任何尖角。

PS:MATLAB文件正在导入记事本.txt文档,其中将输入坐标,如下所示-

Number_of_sections= 7

X_coordinate= 60.00
Y_coordinate= 13.00
Z_coordinate= -7.50
X_coordinate= 60.00
Y_coordinate= -13.00
Z_coordinate= -7.50
X_coordinate= 60.00
Y_coordinate= -13.00
Z_coordinate= -12.50
X_coordinate= 60.00
Y_coordinate= 13.00
Z_coordinate= -12.50

X_coordinate= 95.00
Y_coordinate= 13.00
Z_coordinate= -7.50
X_coordinate= 95.00
Y_coordinate= -13.00
Z_coordinate= -7.50
X_coordinate= 95.00
Y_coordinate= -13.00
Z_coordinate= -12.50
X_coordinate= 95.00
Y_coordinate= 13.00
Z_coordinate= -12.50

X_coordinate= 95.50
Y_coordinate= 13.50
Z_coordinate= -7.50
X_coordinate= 95.50
Y_coordinate= -13.50
Z_coordinate= -7.50
X_coordinate= 95.50
Y_coordinate= -13.50
Z_coordinate= -12.50
X_coordinate= 95.50
Y_coordinate= 13.50
Z_coordinate= -12.50

X_coordinate= 96.00
Y_coordinate= 14.00
Z_coordinate= -7.50
X_coordinate= 96.00
Y_coordinate= -14.00
Z_coordinate= -7.50
X_coordinate= 96.00
Y_coordinate= -14.00
Z_coordinate= -12.50
X_coordinate= 96.00
Y_coordinate= 14.00
Z_coordinate= -12.50

X_coordinate= 96.50
Y_coordinate= 14.50
Z_coordinate= -7.50
X_coordinate= 96.50
Y_coordinate= -14.50
Z_coordinate= -7.50
X_coordinate= 96.50
Y_coordinate= -14.50
Z_coordinate= -12.50
X_coordinate= 96.50
Y_coordinate= 14.50
Z_coordinate= -12.50

X_coordinate= 97.00
Y_coordinate= 15.00
Z_coordinate= -7.50
X_coordinate= 97.00
Y_coordinate= -15.00
Z_coordinate= -7.50
X_coordinate= 97.00
Y_coordinate= -15.00
Z_coordinate= -12.50
X_coordinate= 97.00
Y_coordinate= 15.00
Z_coordinate= -12.50

X_coordinate= 99.00
Y_coordinate= 15.00
Z_coordinate= -7.50
X_coordinate= 99.00
Y_coordinate= -15.00
Z_coordinate= -7.50
X_coordinate= 99.00
Y_coordinate= -15.00
Z_coordinate= -12.50
X_coordinate= 99.00
Y_coordinate= 15.00
Z_coordinate= -12.50

X_coordinate= 99.250
Y_coordinate= 14.7500
Z_coordinate= -7.50
X_coordinate= 99.250
Y_coordinate= -15.2500
Z_coordinate= -7.50
X_coordinate= 99.250
Y_coordinate= -15.2500
Z_coordinate= -12.50
X_coordinate= 99.250
Y_coordinate= 14.7500
Z_coordinate= -12.50

X_coordinate= 99.50
Y_coordinate= 14.50
Z_coordinate= -7.50
X_coordinate= 99.50
Y_coordinate= -15.500
Z_coordinate= -7.50
X_coordinate= 99.50
Y_coordinate= -15.500
Z_coordinate= -12.50
X_coordinate= 99.50
Y_coordinate= 14.50
Z_coordinate= -12.50

X_coordinate= 100.0
Y_coordinate= 14.00
Z_coordinate= -7.50
X_coordinate= 100.0
Y_coordinate= -15.750
Z_coordinate= -7.50
X_coordinate= 100.0
Y_coordinate= -15.750
Z_coordinate= -12.50
X_coordinate= 100.0
Y_coordinate= 14.00
Z_coordinate= -12.50

X_coordinate= 110.0
Y_coordinate= 14.00
Z_coordinate= -7.50
X_coordinate= 110.0
Y_coordinate= -15.750
Z_coordinate= -7.50
X_coordinate= 110.0
Y_coordinate= -15.750
Z_coordinate= -12.50
X_coordinate= 110.0
Y_coordinate= 14.00
Z_coordinate= -12.50



X_coordinate= 110.0
Y_coordinate= 14.00
Z_coordinate= -7.50
X_coordinate= 110.0
Y_coordinate= -15.750
Z_coordinate= -7.50
X_coordinate= 120.0
Y_coordinate= -15.750
Z_coordinate= -12.50
X_coordinate= 120.0
Y_coordinate= 14.00
Z_coordinate= -12.50

X_coordinate= 110.0
Y_coordinate= 14.00
Z_coordinate= -5.50
X_coordinate= 110.0
Y_coordinate= -15.750
Z_coordinate= -5.50
X_coordinate= 120.0
Y_coordinate= -15.750
Z_coordinate= -7.50
X_coordinate= 120.0
Y_coordinate= 14.00
Z_coordinate= -7.50

X_coordinate= 80.0
Y_coordinate= 14.00
Z_coordinate= -5.50
X_coordinate= 80.0
Y_coordinate= -15.750
Z_coordinate= -5.50
X_coordinate= 80.0
Y_coordinate= -15.750
Z_coordinate= -7.50
X_coordinate= 80.0
Y_coordinate= 14.00
Z_coordinate= -7.50

X_coordinate= 70.0
Y_coordinate= 14.00
Z_coordinate= -5.50
X_coordinate= 70.0
Y_coordinate= -15.750
Z_coordinate= -5.50
X_coordinate= 80.0
Y_coordinate= -15.750
Z_coordinate= -5.50
X_coordinate= 80.0
Y_coordinate= 14.00
Z_coordinate= -5.50

X_coordinate= 70.0
Y_coordinate= 14.00
Z_coordinate= -2.50
X_coordinate= 70.0
Y_coordinate= -15.750
Z_coordinate= -2.50
X_coordinate= 80.0
Y_coordinate= -15.750
Z_coordinate= -2.50
X_coordinate= 80.0
Y_coordinate= 14.00
Z_coordinate= -2.50

Here is the image

所需的方向变化图像 2

更详细地表示所需方向变化的图像 3

最佳答案

通过数据文件中的点进行插值并不容易。我建议设计 B 样条曲线来在平面 1 和 2、平面 6 和 7 之间插入点。平面 2 和平面 6、平面 7 和平面 10 之间的空间看起来非常线性:

clear
filename = 'datafile.txt';
A = importdata(filename);

vertices = A.data(2:end);
vertices = reshape(vertices, 12, [])';

vx = vertices(:, 1:3:10);
vy = vertices(:,2:3:11);
vz = vertices(:,3:3:12);
figure; patch(vx',vy',vz',1); axis equal;

enter image description here

没有简单的方法可以进行此类插值,因为您希望确保沿曲线至少具有 C1 连续性以避免出现任何尖锐边缘。 B 样条曲线在这里可能很有用,但如果您不熟悉它,则很难对其进行编程。幸运的是,我从事一个需要曲面和曲线拟合的项目,并且我手头有代码:

function [x,y,z] = bspline(u, ctrlp, k, knots)    
U = bspbasis(u, numel(ctrlp(:,1)), k, knots);

x=U*ctrlp(:,1);
y=U*ctrlp(:,2);
z=U*ctrlp(:,3);
end

function U = bspbasis(u, nctrlp, K, knots)
nu = numel(u);
umax = max(u);
index = 1:nctrlp;

% preallocating variables
U = zeros(nu,nctrlp);
N = zeros(nctrlp+1,K);

% Calculate the denominators for basis functions (k>2). -may be useful when
% the size of point data is substantial, so the calculation is not repeated.
d1 = zeros(nctrlp,K);
d2 = d1;
for m=2:K
d1(:,m) = knots(index+m-1) - knots(index);
d2(:,m) = knots(index+m) - knots(index+1);
end

knots = knots(:);
knots1 = knots(1:nctrlp+1);
knots2 = knots(2:nctrlp+2);
knotSize = size(knots1);
knotc1 = knots(nctrlp+1);
knotc2 = knots(nctrlp+2);

for ui = 1:nu
% k = 1
u_ = u(ui)*ones(knotSize);
NA = u_>=knots1 & u_<knots2;
if u(ui) == umax && knotc1 == umax && knotc2 == umax
NA(1:nctrlp+1) = 0;
NA(end-1,1) = 1;
end
N(:,1) = NA;
% k > 2
for k = 2:K
p1 = (u(ui) - knots(index)) ./ d1(:,k) .* N(index,k-1);
p1(isnan(p1)) = 0;
p2 = (knots(index+k) - u(ui)) ./ d2(:,k) .* N(index+1,k-1);
p2(isnan(p2)) = 0;
N(index,k) = p1 + p2;
end
U(ui,:)=N(1:end-1,k);
end
end

如果你想理解上面的代码,可以阅读B-spline Wikipedia 。 Youtube 上也有很多教程和互动工具如this one .


下面的代码将三阶 B 样条曲线拟合到四组角顶点中的每一个。

u = linspace(0,1,500);
k = 3;

for i = 1:4
ishift = (i-1) * 3 + 1;
p = vertices(:,ishift:ishift+2);

ctrlp = [p(1,:); [0 0 0]; p(2:3,:); p(5:6,:); zeros(2,3); p(7,:); p(8,:)];
ctrlp(2,:) = 5*ctrlp(3,:) - 4*ctrlp(4,:);
ctrlp(7,:) = 2*ctrlp(6,:) - ctrlp(5,:);
ctrlp(8,:) = 4*ctrlp(9,:) - 3*ctrlp(10,:);
knots = getknots(ctrlp, k);
[x_,y_,z_] = bspline(u, ctrlp, k, knots);
x(:,i) = [p(:,1); x_];
y(:,i) = [p(:,2); y_];
z(:,i) = [p(:,3); z_];

[~,I] = sort(x(:,i));
x(:,i) = x(I,i);
y(:,i) = y(I,i);
z(:,i) = z(I,i);
end
c = repmat(1:numel(x)/4,4,1)';
xx=[x;flipud(x(:,[2,3,4,1]))];
yy=[y;flipud(y(:,[2,3,4,1]))];
zz=[z;flipud(z(:,[2,3,4,1]))];
cc=[c;flipud(c)];
figure; patch(xx,yy,zz,cc);

结果是平滑的放样表面:

enter image description here


我想向您解释代码是如何工作的,但在我挣扎了一个小时后,我放弃了......相反,我在下面提供了一些关键点的摘要。

总而言之,使用以下符号:C1、C2、...、C10 是 B 样条曲线的控制点,V1、V2、...、V10 是用于计算 B 样条曲线的顶点样条曲线。下图显示了用于计算第一条 B 样条曲线的控制点和顶点。

enter image description here

  1. u 中的值数量确定最终曲线上的点数。
  2. 三阶 B 样条曲线就足够了。
  3. 曲线必须经过 V1、V2、...、V10。
  4. 要通过 V1 和 V10,B 样条曲线必须是钳位类型。因此,C1 = V1C10 = V10 .
  5. 要通过 V2,C2 必须位于通过 V2 和 V3 的直线上,并且 C3 必须等于 V2。因此,C2 - V2 = d*(V2-V3)C3 = V2 .
  6. 要通过 V3、V4 和 V5,必须满足以下条件:C3 = V2; C4 = V3; C5 = V5; C6 = V6 .
  7. 要通过 V6,C7 必须位于通过 V5 和 V6 的直线上,并且 C6 等于 V6。因此,C7-V6 = d*(V6-V5) .
  8. 要通过 V7,C8 必须位于通过 V7 和 V8 的直线上,并且 C9 必须等于 V7。因此,C8-V7 = d*(V7-V8); C9 = V7; .
  9. C10 = V10C9 = V7 ,曲线经过V8和V9。
  10. 最后,结可以是均匀的,也可以通过控制点之间的弦长来估计。

更新:getknots

getknots功能:

function knots = getknots(ctrlp, k)
d = sqrt(sum(diff(ctrlp).^2, 2));
ds = cumsum(d)./sum(d);
knots = [zeros(k,1); ds(k-1:end); ones(k,1)];
end

bspline 的多合一代码方法:

clear
filename = 'datafile.txt';
A = importdata(filename);

vertices = A.data(2:end);
vertices = reshape(vertices, 12, [])';

u = linspace(0,1,500);
k = 3;

for i = 1:4
ishift = (i-1) * 3 + 1;
p = vertices(:,ishift:ishift+2);

ctrlp = [p(1,:); [0 0 0]; p(2:3,:); p(5:6,:); zeros(2,3); p(7,:); p(8,:)];
ctrlp(2,:) = 5*ctrlp(3,:) - 4*ctrlp(4,:);
ctrlp(7,:) = 2*ctrlp(6,:) - ctrlp(5,:);
ctrlp(8,:) = 4*ctrlp(9,:) - 3*ctrlp(10,:);
knots = getknots(ctrlp, k);
[x_,y_,z_] = bspline(u, ctrlp, k, knots);
x(:,i) = [p(:,1); x_];
y(:,i) = [p(:,2); y_];
z(:,i) = [p(:,3); z_];

[~,I] = sort(x(:,i));
x(:,i) = x(I,i);
y(:,i) = y(I,i);
z(:,i) = z(I,i);
end
c = repmat(1:numel(x)/4,4,1)';
xx=[x;flipud(x(:,[2,3,4,1]))];
yy=[y;flipud(y(:,[2,3,4,1]))];
zz=[z;flipud(z(:,[2,3,4,1]))];
cc=[c;flipud(c)];
figure; patch(xx,yy,zz,cc);

function [x,y,z] = bspline(u, ctrlp, k, knots)
U = bspbasis(u, numel(ctrlp(:,1)), k, knots);

x=U*ctrlp(:,1);
y=U*ctrlp(:,2);
z=U*ctrlp(:,3);
end

function U = bspbasis(u, nctrlp, K, knots)
nu = numel(u);
umax = max(u);
index = 1:nctrlp;

% preallocating variables
U = zeros(nu,nctrlp);
N = zeros(nctrlp+1,K);

% Calculate the denominators for basis functions (k>2). -may be useful when
% the size of point data is substantial, so the calculation is not repeated.
d1 = zeros(nctrlp,K);
d2 = d1;
for m=2:K
d1(:,m) = knots(index+m-1) - knots(index);
d2(:,m) = knots(index+m) - knots(index+1);
end

knots = knots(:);
knots1 = knots(1:nctrlp+1);
knots2 = knots(2:nctrlp+2);
knotSize = size(knots1);
knotc1 = knots(nctrlp+1);
knotc2 = knots(nctrlp+2);

for ui = 1:nu
% k = 1
u_ = u(ui)*ones(knotSize);
NA = u_>=knots1 & u_<knots2;
if u(ui) == umax && knotc1 == umax && knotc2 == umax
NA(1:nctrlp+1) = 0;
NA(end-1,1) = 1;
end
N(:,1) = NA;
% k > 2
for k = 2:K
p1 = (u(ui) - knots(index)) ./ d1(:,k) .* N(index,k-1);
p1(isnan(p1)) = 0;
p2 = (knots(index+k) - u(ui)) ./ d2(:,k) .* N(index+1,k-1);
p2(isnan(p2)) = 0;
N(index,k) = p1 + p2;
end
U(ui,:)=N(1:end-1,k);
end
end

function knots = getknots(ctrlp, k)
d = sqrt(sum(diff(ctrlp).^2, 2));
ds = cumsum(d)./sum(d);
knots = [zeros(k,1); ds(k-1:end); ones(k,1)];
end

关于matlab - 如何在 MATLAB 中使用矩形放样以创建灵活的 3D 封闭管道?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55852401/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com