mesh_refine_tri6 - creates 6 smaller triangles from a triangle mesh [ FV ] = mesh_refine_tri6( FV ) FV.vertices is vertices (Nx3 matrix) FV.faces is faces with indices into rows of V (Mx3 matrix) For each face of F, 4 new vertices are created at the triangle edge midpoints and the triangle centroid. Each face is divided into 6 faces and returned in FV.faces. B /+\ / + \ a/__X__\b Construct new triangles / +|+ \ [A,a,X], [A,X,c] / + | + \ [B,X,a], [B,b,X] /+___ | ___+\ [C,X,b], [C,c,X] A c C It is assumed that the vertices are listed in clockwise order in FV.faces (A,B,C above), as viewed from the outside in a RHS coordinate system. See also: mesh_refine, mesh_refine_tri4, sphere_tri, sphere_project
0001 function [ FV ] = mesh_refine_tri6(FV) 0002 0003 % mesh_refine_tri6 - creates 6 smaller triangles from a triangle mesh 0004 % 0005 % [ FV ] = mesh_refine_tri6( FV ) 0006 % 0007 % FV.vertices is vertices (Nx3 matrix) 0008 % FV.faces is faces with indices into rows of V (Mx3 matrix) 0009 % 0010 % For each face of F, 4 new vertices are created at the 0011 % triangle edge midpoints and the triangle centroid. Each 0012 % face is divided into 6 faces and returned in FV.faces. 0013 % 0014 % B 0015 % /+\ 0016 % / + \ 0017 % a/__X__\b Construct new triangles 0018 % / +|+ \ [A,a,X], [A,X,c] 0019 % / + | + \ [B,X,a], [B,b,X] 0020 % /+___ | ___+\ [C,X,b], [C,c,X] 0021 % A c C 0022 % 0023 % It is assumed that the vertices are listed in clockwise order in 0024 % FV.faces (A,B,C above), as viewed from the outside in a RHS coordinate 0025 % system. 0026 % 0027 % See also: mesh_refine, mesh_refine_tri4, 0028 % sphere_tri, sphere_project 0029 % 0030 0031 0032 % This can be done until some minimal distance (D) of the mean 0033 % distance between vertices of all triangles is achieved. If 0034 % no D argument is given, the function refines the mesh once. 0035 % 0036 0037 % $Revision: 1.1 $ $Date: 2004/11/12 01:32:35 $ 0038 0039 % Licence: GNU GPL, no implied or express warranties 0040 % History: 05/2002, Darren.Weber_at_radiology.ucsf.edu, created 0041 % 0042 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0043 0044 tic; fprintf('...refining mesh (tri6)...'); 0045 0046 % NOTE 0047 % The centroid is located one third of the way from each vertex to 0048 % the midpoint of the opposite side. Each median divides the triangle 0049 % into two equal areas; all the medians together divide it into six 0050 % equal parts, and the lines from the median point to the vertices 0051 % divide the whole into three equivalent triangles. 0052 0053 % Each input triangle with vertices labelled [A,B,C] as shown 0054 % below will be turned into six new triangles: 0055 % 0056 % Make new midpoints 0057 % a = (A+B)/2 0058 % b = (B+C)/2 0059 % c = (C+A)/2 0060 % 0061 % Make triangle centroid 0062 % X = b + ( (A - b) ./3 ); 0063 % 0064 % B 0065 % /+\ 0066 % / + \ 0067 % a/__X__\b Construct new triangles 0068 % / +|+ \ [A,a,X], [A,X,c] 0069 % / + | + \ [B,X,a], [B,b,X] 0070 % /+___ | ___+\ [C,X,b], [C,c,X] 0071 % A c C 0072 % 0073 0074 0075 0076 0077 % Initialise a new vertices and faces matrix 0078 Nvert = size(FV.vertices,1); 0079 Nface = size(FV.faces,1); 0080 V2 = zeros(Nface*4,3); 0081 F2 = zeros(Nface*6,3); 0082 0083 for f=1:Nface, 0084 0085 % Get the triangle vertex indices 0086 NA = FV.faces(f,1); 0087 NB = FV.faces(f,2); 0088 NC = FV.faces(f,3); 0089 0090 % Get the triangle vertex coordinates 0091 A = FV.vertices(NA,:); 0092 B = FV.vertices(NB,:); 0093 C = FV.vertices(NC,:); 0094 0095 % Now find the midpoint between all vertices 0096 a = (A + B) ./ 2; 0097 b = (B + C) ./ 2; 0098 c = (C + A) ./ 2; 0099 0100 % Now find the centroid length of the medians 0101 X = b + ( (A - b) ./3 ); 0102 %Bc = c + ( (B - c) ./3 ); % Bc = X 0103 %Cc = a + ( (C - a) ./3 ); % Cc = X 0104 0105 % Store the midpoint and the centroid vertices, 0106 % checking if vertex already exists 0107 [FV, Na] = mesh_find_vertex(FV,a); 0108 [FV, Nb] = mesh_find_vertex(FV,b); 0109 [FV, Nc] = mesh_find_vertex(FV,c); 0110 [FV, NX] = mesh_find_vertex(FV,X); 0111 0112 % Create new faces with centroid 0113 F2(f*6-5,:) = [NA, Na, NX ]; 0114 F2(f*6-4,:) = [NA, NX, Nc ]; 0115 F2(f*6-3,:) = [NB, NX, Na ]; 0116 F2(f*6-2,:) = [NB, Nb, NX ]; 0117 F2(f*6-1,:) = [NC, NX, Nb ]; 0118 F2(f*6-0,:) = [NC, Nc, NX ]; 0119 0120 0121 %figure; patch('vertices',[A;B;C],'faces',[1 2 3],'facecolor',[.7 .7 .7]); hold on; 0122 %plot3(A(1),A(2),A(3),'ro'); 0123 %plot3(b(1),b(2),b(3),'ro'); 0124 %plot3(Ac(1),Ac(2),Ac(3),'bo') 0125 %if isequal(r,2), return; end 0126 0127 end 0128 0129 % Replace the faces matrix 0130 FV.faces = F2; 0131 0132 0133 t=toc; fprintf('done (%5.2f sec)\n',t); 0134 0135 return 0136 0137 0138 % Find the length of each median 0139 %A2bLength = sqrt ( sum( (A - b).^2, 2 ) ); 0140 %B2cLength = sqrt ( sum( (B - c).^2, 2 ) ); 0141 %C2aLength = sqrt ( sum( (C - a).^2, 2 ) ); 0142 0143 0144 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0145 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0146 function [FV, N] = mesh_find_vertex(FV,vertex) 0147 0148 Vn = size(FV.vertices,1); 0149 Va = repmat(vertex,Vn,1); 0150 Vexist = find( FV.vertices(:,1) == Va(:,1) & ... 0151 FV.vertices(:,2) == Va(:,2) & ... 0152 FV.vertices(:,3) == Va(:,3) ); 0153 if Vexist, 0154 if size(Vexist) == [1,1], 0155 N = Vexist; 0156 else, 0157 msg = sprintf('replicated vertices'); 0158 error(msg); 0159 end 0160 else 0161 FV.vertices(end+1,:) = vertex; 0162 N = size(FV.vertices,1); 0163 end 0164 0165 return