这在 MATLAB 中可能不是一件容易的事,但它是可能的。我将在此处概述一组步骤,以两个相交的圆柱体为例...
创建四面体网格:
第一步是为您的拉伸创建一个四面体网格。如果您要挤出的 2D 二进制图像是凸的并且没有孔,您可以使用 delaunayTriangulation 函数来执行此操作:
DT = delaunayTriangulation(P);
这里,P 包含挤压的“端盖”的坐标点(即管两端的面)。但是,在生成四面体网格时,delaunayTriangulation 不允许您指定 constrained edges,因此它最终会填充挤出中的孔或凹面。 可能在其他工具箱中有一些更好的网格生成替代品,例如 Partial Differential Equations Toolbox,但我无法访问它们,也无法谈论它们的适用性。
如果自动网格生成选项不起作用,您必须自己构建四面体网格并将该数据传递给 triangulation。这可能很棘手,但我将向您展示如何为圆柱体执行此操作的一些步骤,这可能会帮助您找出更多涉及的形状。下面,我们构建了一组坐标点P1 和一个M-by-4 矩阵T1,其中每一行包含对定义一个四面体的P1 行的索引:
% Create circle coordinates for the end caps:
N = 21;
theta = linspace(0, 2*pi, N).';
x = sin(theta(1:(end-1)));
y = cos(theta(1:(end-1)))+0.5;
z = ones(N-1, 1);
% Build tetrahedrons for first cylinder, aligned along the z axis:
P1 = [0 0.5 -1; ... % Center point of bottom face
x y -z; ... % Edge coordinates of bottom face
0 0.5 1; ... % Center point of top face
x y z]; % Edge coordinates of top face
cBottom = ones(N-1, 1); % Row indices for bottom center coordinate
cEdgeBottom1 = (2:N).'; % Row indices for bottom edge coordinates
cEdgeBottom2 = [3:N 2].'; % Shifted row indices for bottom edge coordinates
cTop = cBottom+N; % Row indices for top center coordinate
cEdgeTop1 = cEdgeBottom1+N; % Row indices for top edge coordinates
cEdgeTop2 = cEdgeBottom2+N; % Shifted row indices for top edge coordinates
% There are 3 tetrahedrons per radial slice of the cylinder: one that includes the
% bottom face and half of the side face (all generated simultaneously by the first row
% below), one that includes the other half of the side face (second row below), and one
% that includes the top face (third row below):
T1 = [cEdgeBottom1 cEdgeBottom2 cEdgeTop1 cBottom; ...
cEdgeBottom2 cEdgeTop1 cEdgeTop2 cBottom; ...
cEdgeTop1 cEdgeTop2 cTop cBottom];
TR1 = triangulation(T1, P1);
为了更好地可视化圆柱体是如何被分成四面体的,下面是一个分解视图的动画:
现在我们可以创建第二个圆柱体,偏移并旋转,使其与 x 轴对齐并与第一个相交:
% Build tetrahedrons for second cylinder:
P2 = [P1(:, 3) -P1(:, 2) P1(:, 1)];
T2 = T1;
TR2 = triangulation(T2, P2);
% Plot cylinders:
tetramesh(TR1, 'FaceColor', 'r', 'FaceAlpha', 0.6);
hold on;
tetramesh(TR2, 'FaceColor', 'g', 'FaceAlpha', 0.6);
axis equal;
xlabel('x');
ylabel('y');
zlabel('z');
这是visualize them的情节:
寻找交叉区域:
一旦我们有了体积的四面体表示,我们就可以generate a grid of points 覆盖相交区域并使用pointLocation 函数来确定两个圆柱体内的点:
nGrid = 101;
[X, Y, Z] = meshgrid(linspace(-1, 1, nGrid));
QP = [X(:) Y(:) Z(:)];
indexIntersect = (~isnan(pointLocation(TR1, QP))) & ...
(~isnan(pointLocation(TR2, QP)));
mask = double(reshape(indexIntersect, [nGrid nGrid nGrid]));
我们现在有体积数据mask,其中包含零和一,它们定义了交叉区域。您制作的网格越精细(通过调整nGrid),这将更准确地表示圆柱之间的真实相交区域。
生成 3D 表面:
您可能希望根据这些数据创建一个曲面,定义相交区域的边界。有几种方法可以做到这一点。一种是使用isosurface 生成表面,然后您可以使用featureEdges 对其进行可视化。例如:
[F, V] = isosurface(mask, 0.5);
TR = triangulation(F, V);
FE = featureEdges(TR, pi/6).';
xV = V(:, 1);
yV = V(:, 2);
zV = V(:, 3);
trisurf(TR, 'FaceColor', 'c', 'FaceAlpha', 0.8, 'EdgeColor', 'none');
axis equal;
xlabel('x');
ylabel('y');
zlabel('z');
hold on;
plot3(xV(FE), yV(FE), zV(FE), 'k');
以及由此产生的情节:
另一种选择是创建一个“体素化”的类似 Minecraft 的表面,正如我所说明的 here:
[X, Y, Z, C] = build_voxels(permute(mask, [2 1 3]));
hSurface = patch(X, Y, Z, 'c', ...
'AmbientStrength', 0.5, ...
'BackFaceLighting', 'unlit', ...
'EdgeColor', 'none', ...
'FaceLighting', 'flat');
axis equal;
view(-37.5, 30);
set(gca, 'XLim', [0 101], 'YLim', [25 75], 'ZLim', [0 102]);
xlabel('x');
ylabel('y');
zlabel('z');
grid on;
light('Position', get(gca, 'CameraPosition'), 'Style', 'local');
以及由此产生的情节: