lin1175194301 2024-01-24 16:57 采纳率: 0%
浏览 3

实现网格细化,并且将细化后的面在合并成四面体

我已经标记出了网格中想要细化的表面和面所在的四面体(所在行索引和值都已经提取出来),但是应用loop细化是对部分网格进行细化,我该怎么修改程序才可以实现细化网格和未细化网格统一在一个矩阵中(即索引和三角形相对性,现在是细化的和没细化的是分开的),并且将统一后的面矩阵所有的面在合并成网格的四面体格式。

img

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

  • 写回答

1条回答 默认 最新

  • GISer Liu 2024-01-31 00:26
    关注

    该回答引用自GPT-3.5,由博主GIS_Liu编写:

    针对你的问题,你需要实现网格细化并将细化后的面与原始面统一在一个矩阵中,并将所有面合并成四面体网格的格式。下面是解决问题的思路和方案:

    解决思路:

    1. 网格细化修改:需要修改网格细化函数,使得细化后的面与原始面统一在一个矩阵中,而不是分开存储。
    2. 合并成四面体网格:根据细化后的面和原始面,构建四面体网格。

    解决方案:

    1. 网格细化修改:

    在网格细化函数 linearSubdivision 中,需要修改部分代码以将细化后的面与原始面统一存储在一个矩阵中。

    % 修改后的 linearSubdivision 函数
    function [newVertices, newFaces] = linearSubdivision(vertices, faces)
        global edgeVertex;
        global newIndexOfVertices;
        newFaces = faces; % 将新面的存储初始化为原始面
        newVertices = vertices;
        nVertices = size(vertices,2);
        nFaces = size(faces,2);
        edgeVertex = zeros(nVertices, nVertices);
        newIndexOfVertices = nVertices;
    
        % 细化原始面并添加新面
        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
    
        % 计算新顶点的位置
        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
    

    2. 合并成四面体网格:

    将细化后的面和原始面合并成四面体网格的格式。这一步需要根据细化后的面和原始面的顶点索引重新构建四面体网格。

    % 构建四面体网格
    function tetrahedralMesh = constructTetrahedralMesh(vertices, faces)
        tetrahedralMesh = [];
    
        % 对每个面进行处理,构建四面体网格
        for i = 1:size(faces, 2)
            face = faces(:, i);
            tetrahedra = [face(1), face(2), face(3), 0; face(1), face(2), 0, face(3); face(1), 0, face(2), face(3); 0, face(1), face(2), face(3)];
            tetrahedralMesh = [tetrahedralMesh, tetrahedra];
        end
    end
    

    总结:

    通过修改网格细化函数,使得细化后的面与原始面统一在一个矩阵中,并根据细化后的面和原始面构建四面体网格,可以实现网格的统一细化和合并。这样,你就能够将所有面合并成网格的四面体格式。

    如果该回答解决了您的问题,请采纳!如果没有,请详述问题所在!

    评论

报告相同问题?

问题事件

  • 创建了问题 1月24日

悬赏问题

  • ¥15 Opencv(C++)异常
  • ¥15 VScode上配置C语言环境
  • ¥15 汇编语言没有主程序吗?
  • ¥15 这个函数为什么会爆内存
  • ¥15 无法装系统,grub成了顽固拦路虎
  • ¥15 springboot aop 应用启动异常
  • ¥15 matlab有关债券凸性久期的代码
  • ¥15 lvgl v8.2定时器提前到来
  • ¥15 qtcp 发送数据时偶尔会遇到发送数据失败?用的MSVC编译器(标签-qt|关键词-tcp)
  • ¥15 cam_lidar_calibration报错