我已经标记出了网格中想要细化的表面和面所在的四面体(所在行索引和值都已经提取出来),但是应用loop细化是对部分网格进行细化,我该怎么修改程序才可以实现细化网格和未细化网格统一在一个矩阵中(即索引和三角形相对性,现在是细化的和没细化的是分开的),并且将统一后的面矩阵所有的面在合并成网格的四面体格式。
loop细化matlab代码
function [newVertices, newFaces] = linearSubdivision(vertices, faces)
% Linear subdivision for triangle meshes
% 代码的大致思想和之前LOOP细分是一样的,但是代码显然简单了不少,因为线性细分的规则是只需要在两点之间线性插值其中点,因此所用的代码简单非常多,相信不难理解
% 运行成功,可以自行控制网格加密的程度,下一步是进行局部网格加密
% 输入:vertices:所有顶点
% faces:所有面
% Dimensions:
% vertices: 3xnVertices
% faces: 3xnFaces
% 线性细分四面体
% example:
% vertices = [10 10 10; -100 10 -10; -100 -10 10; 10 -10 -10]';
% faces = [1 2 3; 1 3 4; 1 4 2; 4 3 2]';
%
% figure(2);
% subplot(1,4,1);
% plotMesh(vertices, faces);
% for i=2:4
% subplot(1,4,i);
% [vertices, faces] = linearSubdivision(vertices, faces);
% plotMesh(vertices, faces);
% end
global edgeVertex;
global newIndexOfVertices;
newFaces = [];
newVertices = vertices;
nVertices = size(vertices,2);
nFaces = size(faces,2);
edgeVertex= zeros(nVertices, nVertices);
newIndexOfVertices = nVertices;
% ------------------------------------------------------------------------ %
% create a matrix of edge-vertices and a new triangulation (newFaces).
%
% * edgeVertex(x,y): index of the new vertex between (x,y)
%
% 0riginal vertices: va, vb, vc.
% New vertices: vp, vq, vr.
%
% vb vb
% / \ / \
% / \ vp--vq
% / \ / \ / \
% va ----- vc -> va-- vr --vc
%
for i=1:nFaces
[vaIndex, vbIndex, vcIndex] = deal(faces(1,i), faces(2,i), faces(3,i));
vpIndex = addEdgeVertex(vaIndex, vbIndex);
vqIndex = addEdgeVertex(vbIndex, vcIndex);
vrIndex = addEdgeVertex(vaIndex, vcIndex);
fourFaces = [vaIndex,vpIndex,vrIndex; vpIndex,vbIndex,vqIndex; vrIndex,vqIndex,vcIndex; vrIndex,vpIndex,vqIndex]';
newFaces = [newFaces, fourFaces];
end;
% ------------------------------------------------------------------------ %
% positions of the new vertices 新顶点的位置
for v1=1:nVertices-1
for v2=v1:nVertices
vNIndex = edgeVertex(v1,v2);
if (vNIndex~=0)
newVertices(:,vNIndex) = 1/2*(vertices(:,v1)+vertices(:,v2));
end;
end;
end;
end
% ---------------------------------------------------------------------------- %
function vNIndex = addEdgeVertex(v1Index, v2Index)
global edgeVertex;
global newIndexOfVertices;
if (v1Index>v2Index) % setting: v1 <= v2 设置: v1 <= v2
vTmp = v1Index;
v1Index = v2Index;
v2Index = vTmp;
end;
if (edgeVertex(v1Index, v2Index)==0) % new vertex 新顶点
newIndexOfVertices = newIndexOfVertices+1;
edgeVertex(v1Index, v2Index) = newIndexOfVertices;
end;
vNIndex = edgeVertex(v1Index, v2Index);
return;
end
function plotMesh(vertices, faces)
hold on;
trimesh(faces', vertices(1,:), vertices(2,:), vertices(3,:));
colormap hot;
axis tight;
axis square;
axis off;
view(3);
end