%-------------------------- % @Author: Jingqiao Hu % @Date: 2021-11-01 15:24:36 % @LastEditTime: 2022-01-03 21:55:46 % c_vedges: the edges of each voronoi cell, [x1,y1,x2,y2] * n-rows % c_polys: the nodes of each voronoi cell, [x1,y1,x2,y2] * n-rows % c_nodes % nfaces_local: nfaces in each cvt %-------------------------- % read file version function [c_nodes, c_faces, c_vedges, c_polys] = generate_mesh_fromCVT(ncvt, path0, delta) cc_nodes = cell(ncvt, 1); c_faces = cell(ncvt, 1); c_vedges = cell(ncvt, 1); c_polys = cell(ncvt, 1); polyvec = []; figure; for i = 40 : ncvt [vor_edges, pgon] = read_cvt(i-1, path0); c_vedges{i} = vor_edges; c_polys{i} = pgon; % [n,2] polyvec = [polyvec; polyshape(pgon)]; [~, ~, tri_nodes, tri_faces] = read_cdt(i-1, path0, delta); c_nodes{i} = tri_nodes; c_faces{i} = tri_faces; plot(polyshape(pgon),'FaceColor','white','EdgeColor','k','LineWidth', 2, 'FaceAlpha',0); hold on; end axis equal; axis off % figure; h = plot(polyvec,'FaceColor','white','EdgeColor','black'); % h.LineWidth = 1.5; end function [pnts_num, faces_num, nodes, faces] = read_cdt(i, path0, delta) datapath = [path0+'cdt'+num2str(i)]; fileID = fopen(datapath, 'r'); info = fscanf(fileID, '%f'); pnts_num = info(1); faces_num = info(2); nodes = reshape(info(3 : 3+pnts_num*2-1), 2, [])'; % all nodes coodinates of cdt % faces = reshape(info(3+pnts_num*2 : end), 3, [])'; % faces of cdt, formed of node-nidx % faces = faces + 1; % faces begins with 0 fclose(fileID); dt = delaunayTriangulation(nodes); % figure; clf; triplot(dt,'Color','black'); faces = dt.ConnectivityList; % if i == 39 triplot(dt,'LineStyle','--','Color','k','LineWidth', 1); hold on; % end faces = checkValid_trimesh(nodes, faces, delta); faces_num = size(faces, 1); end function [edges, pgon] = read_cvt(i, path0) datapath = [path0+'vcell'+num2str(i)]; fileID = fopen(datapath, 'r'); info = fscanf(fileID, '%f'); pnts_num = info(1); edges = reshape(info(2 : end), 4, [])'; % [x1,y1,x2,y2] fclose(fileID); pgon = edges(:, 1:2); end % reorder the edges to [end to end] function new_edges = reorder_edges(edges) new_edges = edges; edges(1,:) = []; for i = 1 : size(new_edges, 1)-1 nj = new_edges(i, 2); [r,c] = find(edges==nj); if c == 2 new_edges(i+1, :) = fliplr(edges(r,:)); else new_edges(i+1, :) = edges(r,:); end edges(r,:) = []; end end function [nodes_new, faces_new] = remove_repeated_nodes(nodes, faces) % remove the repeated nodes [nodes_new, ia, ic] = unique(nodes, 'rows'); % new=nodes(ia), nodes=new(ic) old_order = [1 : nidx]'; new_order = ic; faces_new = changem(faces,new_order,old_order); % faces = faces_new; % nodes = nodes_new; end