Home > bioelectromagnetism > mesh_refine_tri4.m

mesh_refine_tri4

PURPOSE ^

mesh_refine_tri4 - creates 4 triangle from each triangle of a mesh

SYNOPSIS ^

function [ FV ] = mesh_refine_tri4(FV)

DESCRIPTION ^

 mesh_refine_tri4 - creates 4 triangle from each triangle of a mesh

 [ FV ] = mesh_refine_tri4( FV )

 FV.vertices   - mesh vertices (Nx3 matrix)
 FV.faces      - faces with indices into 3 rows
                 of FV.vertices (Mx3 matrix)
 
 For each face, 3 new vertices are created at the 
 triangle edge midpoints.  Each face is divided into 4
 faces and returned in FV.

        B
       /\
      /  \
    a/____\b       Construct new triangles
    /\    /\       [A,a,c]
   /  \  /  \      [a,B,b]
  /____\/____\     [c,b,C]
 A         c       C    [a,b,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, sphere_tri, sphere_project

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

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

Generated on Mon 15-Aug-2005 15:36:19 by m2html © 2003