Generate mesh and refine mesh of triangles

*爱你&永不变心* 提交于 2021-02-08 10:24:07

问题


I need to find a way to mesh triangles then to refine using refine. My vertices of my original triangles are stored inside a matrix of size nb_points * 2. My faces are stored in a nb_faces * 3 matrix. The data for each face is stored in a nb_face * 1 matrix. The meshing is done by diving the area using the middles of the segments of the triangles.

Example : Origin :

vertices = [0 1 ; 
            2 3 ; 
            4 1 ;
            4 5];
faces = [1 2 3;
         2 3 4];
data = [1 2]; 

Picture of the figure

Result expected after meshing :

vertices = [0 1; 
            2 3; 
            4 1;
            4 5;
            1 2;
            3 2;
            2 1;
            3 4;
            4 3];
faces = [1 5 7;
         2 5 6;
         5 6 7;
         7 6 3;
         2 6 8;
         6 8 9;
         6 9 3;
         8 4 9];
data = [1 1 1 1 2 2 2 2];

Picture of the new figure

I am displaying using :

FV.Vertices = vertices; 
FV.Faces = faces; 
FV.FaceVertexCData = data;
figure; hold on; axis equal; grid on;
patch(FV,'FaceColor','flat');

Precision : I do not want to use the following functions which gives way too many vertices and faces :

  • generateMesh()
  • refinemesh()

The data are temperatures since this is a simulation of heat transfer.


回答1:


With a for loop it can be done quite easily, here is one solution:

% Dummy data
vertices = [0 1 ; 
            2 3 ; 
            4 1 ;
            4 5];
faces = [1 2 3;
         2 3 4];
data = [1 2]; 

% Number of vertices
vnum = size(vertices,1);

% new faces empty vector
nfaces = [];

% triangular shift
tshift = [2,-1,-1].';

% Run the algorithm
for ii = 1:size(faces,1)
    % For each triangle get the 3 pairs of vertices
    nsk = [faces(ii,1), faces(ii,2);faces(ii,2), faces(ii,3);faces(ii,3), faces(ii,1)];
    % Compute the center of each pair of vertices
    cmiddle = (vertices(nsk(:,1),:)+vertices(nsk(:,2),:))/2
    % Compute the new faces
    nfaces = [nfaces;[nsk(:,1),vnum+(ii*3-3+[1:3].'),vnum+(ii*3-3+[1:3].')+tshift];[(vnum+(ii*3-2)):(vnum+(ii*3))]]
    % Add the new vertices
    vertices = [vertices;cmiddle];
end

% Delete the duplicate vertices
[vertices,~,ind] = unique(vertices,'rows');
faces = ind(nfaces);

% Plot
figure; hold on; axis equal; grid on;
patch('Faces',faces,'Vertices',vertices,'FaceVertexCData',kron(data,ones(1,4)).','FaceColor','flat')
colorbar

If you find a way to generate the nsk vector without for loop you could even get rid of the loop. This code will only work on triangle, but it can be adapted if needed.

Result:

You can repeat the operation:



来源:https://stackoverflow.com/questions/59382625/generate-mesh-and-refine-mesh-of-triangles

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!