二维点的德劳内三角剖分
R2026b使用 delaunayTriangulation 创建、查询和编辑 seamount 数据的德劳内三角剖分。海底山数据集包含用于定义海拔山曲面的 (x, y) 位置和对应的海拔 (z)。
加载 (x, y) 数据并进行三角剖分。
load seamount
DT = delaunayTriangulation(x,y)DT =
delaunayTriangulation with properties:
Points: [294×2 double]
ConnectivityList: [566×3 double]
Constraints: []
由于没有施加任何边约束,因此 Constraints 属性为空。Points 属性表示顶点坐标,ConnectivityList 属性表示三角形。这两个属性共同定义了三角剖分的矩阵数据。
delaunayTriangulation 类是矩阵数据的包装器,并且提供了一组补充方法。可以按照访问结构体字段的方式访问 delaunayTriangulation 中的属性。
访问顶点数据。
DT.Points;
访问连接数据。
DT.ConnectivityList;
访问 ConnectivityList 属性中的第一个三角形。
DT.ConnectivityList(1,:)
ans = 1×3
230 205 152
delaunayTriangulation 提供了一种对 ConnectivityList 属性矩阵进行索引的简单方法。
访问第一个三角形。
DT(1,:)
ans = 1×3
230 205 152
检查第一个三角形的第一个顶点。
DT(1,1)
ans = 230
检查三角剖分中的所有三角形。
DT(:,:);
对 delaunayTriangulation 输出 DT 进行索引,这与对 delaunay 的三角剖分数组输出进行索引类似。二者的区别是您可以对 DT 调用额外方法(例如 nearestNeighbor 和 pointLocation)。
使用 triplot 绘制 delaunayTriangulation。triplot 函数不是 delaunayTriangulation 方法,但它接受并可绘制 delaunayTriangulation。
triplot(DT) axis equal xlabel("Longitude") ylabel("Latitude") grid on

或者,可以使用 triplot(DT(:,:), DT.Points(:,1), DT.Points(:,2)); 获取相同的绘图。
使用 delaunayTriangulation 方法 convexHull 计算凸包并将其添加到绘图。由于您已经有一个德劳内三角剖分,因此可利用此方法来获得凸包,效率比使用 convhull 的完全计算方法更高。
hold on k = convexHull(DT); xHull = DT.Points(k,1); yHull = DT.Points(k,2); plot(xHull,yHull,"r",LineWidth=2) hold off

您可以按增量方式编辑 delaunayTriangulation,以添加或删除点。如果需要将点添加到现有的三角剖分,增量添加方法的速度要快于对增广点集进行完整的重新三角剖分。当要删除的点数量相对现有的点数量较小时,增量点删除的效率更高。
编辑三角剖分,删除上一次计算的凸包上的点。
figure plot(xHull,yHull,"r",LineWidth=2) axis equal xlabel("Longitude") ylabel("Latitude") grid on
凸包拓扑会重复开始和结束顶点。删除重复项。
k(end) = [];
现在删除凸包上的点。
DT.Points(k,:) = []
DT =
delaunayTriangulation with properties:
Points: [274×2 double]
ConnectivityList: [528×3 double]
Constraints: []
对新的三角剖分进行绘图。
hold on triplot(DT) hold off

紧靠凸包边界内侧有一个未删除的顶点。在图窗中使用放大工具可以看到,它确实处于凸包以内。您可以绘制顶点标签,以确定此顶点的索引,并从三角剖分中将其删除。或者,可以使用 nearestNeighbor 方法更轻松地确定索引。
该点靠近位置 (211.6, -48.15)。使用 nearestNeighbor 方法求最近的顶点。
vertexId = nearestNeighbor(DT,211.6,-48.15)
vertexId = 50
现在从三角剖分中删除该顶点。
DT.Points(vertexId,:) = []
DT =
delaunayTriangulation with properties:
Points: [273×2 double]
ConnectivityList: [525×3 double]
Constraints: []
对新的三角剖分进行绘图。
figure plot(xHull,yHull,"r",LineWidth=2) axis equal xlabel("Longitude") ylabel("Latitude") grid on hold on triplot(DT) hold off

将点添加到现有的三角剖分。添加 4 个点,在三角剖分的周围构成一个矩形。
Padditional = [210.9 -48.5; 211.6 -48.5; ...
211.6 -47.9; 210.9 -47.9];
DT.Points(end+(1:4),:) = PadditionalDT =
delaunayTriangulation with properties:
Points: [277×2 double]
ConnectivityList: [548×3 double]
Constraints: []
关闭所有现有图窗。
close all对新的三角剖分进行绘图。
figure plot(xHull,yHull,"r",LineWidth=2) axis equal xlabel("Longitude") ylabel("Latitude") grid on hold on triplot(DT) hold off

您可以编辑三角剖分中的点,将其移到新位置。编辑附加点集的第一个点(顶点 ID 274)。
DT.Points(274,:) = [211 -48.4];
关闭所有现有图窗。
close all对新的三角剖分进行绘图
figure plot(xHull,yHull,"r",LineWidth=2) axis equal xlabel("Longitude") ylabel("Latitude") grid on hold on triplot(DT) hold off

使用 triangulation 类的方法 vertexAttachments 求连接的三角形。由于连接到顶点的三角形数量是可变的,因此该方法将在元胞数组中返回连接的三角形 ID。您需要使用括号提取内容。
attTris = vertexAttachments(DT,274); hold on triplot(DT(attTris{:},:),DT.Points(:,1),DT.Points(:,2),"g") hold off

delaunayTriangulation 也可以用于对三维空间中的点进行三角剖分。生成的三角剖分由四面体构成。