Home > matGeom > meshes3d > triangulateFaces.m

triangulateFaces

PURPOSE ^

TRIANGULATEFACES Convert face array to an array of triangular faces.

SYNOPSIS ^

function [tri, inds] = triangulateFaces(faces)

DESCRIPTION ^

TRIANGULATEFACES Convert face array to an array of triangular faces.

   TRI = triangulateFaces(FACES)
   Returns a 3-columns array of indices, based on the data stored in the
   argument FACES:
   - if FACES is a N-by-3 array, returns the same array
   - if FACES is a N-by-4 array, returns an array with 2*N rows and 3
       columns, splitting each square into 2 triangles (uses first and
       third vertex of each square as diagonal).
   - if FACES is a cell array, split each face into a set of triangles,
       and returns the union of all triangles. Faces are assumed to be
       convex.

   [TRI INDS] = triangulateFaces(FACES)
   Also returns original face index of each new triangular face. INDS has
   the same number of rows as TRI, and has values between 1 and the
   number of rows of the original FACES array.


   Example
     % create a basic shape
     [n e f] = createCubeOctahedron;
     % draw with plain faces
     figure;
     drawMesh(n, f);
     % draw as a triangulation
     tri = triangulateFaces(f);
     figure;
     patch('vertices', n, 'faces', tri, 'facecolor', 'r');

   See also
     meshes3d, drawMesh, mergeCoplanarFaces

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [tri, inds] = triangulateFaces(faces)
0002 %TRIANGULATEFACES Convert face array to an array of triangular faces.
0003 %
0004 %   TRI = triangulateFaces(FACES)
0005 %   Returns a 3-columns array of indices, based on the data stored in the
0006 %   argument FACES:
0007 %   - if FACES is a N-by-3 array, returns the same array
0008 %   - if FACES is a N-by-4 array, returns an array with 2*N rows and 3
0009 %       columns, splitting each square into 2 triangles (uses first and
0010 %       third vertex of each square as diagonal).
0011 %   - if FACES is a cell array, split each face into a set of triangles,
0012 %       and returns the union of all triangles. Faces are assumed to be
0013 %       convex.
0014 %
0015 %   [TRI INDS] = triangulateFaces(FACES)
0016 %   Also returns original face index of each new triangular face. INDS has
0017 %   the same number of rows as TRI, and has values between 1 and the
0018 %   number of rows of the original FACES array.
0019 %
0020 %
0021 %   Example
0022 %     % create a basic shape
0023 %     [n e f] = createCubeOctahedron;
0024 %     % draw with plain faces
0025 %     figure;
0026 %     drawMesh(n, f);
0027 %     % draw as a triangulation
0028 %     tri = triangulateFaces(f);
0029 %     figure;
0030 %     patch('vertices', n, 'faces', tri, 'facecolor', 'r');
0031 %
0032 %   See also
0033 %     meshes3d, drawMesh, mergeCoplanarFaces
0034 %
0035 
0036 % ------
0037 % Author: David Legland
0038 % e-mail: david.legland@inra.fr
0039 % Created: 2008-09-08,    using Matlab 7.4.0.287 (R2007a)
0040 % Copyright 2008 INRA - BIA PV Nantes - MIAJ Jouy-en-Josas.
0041 
0042 %% Tri mesh case: return original set of faces
0043 
0044 if isnumeric(faces) && size(faces, 2) == 3
0045     tri = faces;
0046     if nargout > 1
0047         inds = (1:size(faces, 1))';
0048     end
0049     return;
0050 end
0051 
0052 
0053 %% Square faces: split each square into 2 triangles
0054 
0055 if isnumeric(faces) && size(faces, 2) == 4
0056     nf = size(faces, 1);
0057     tri = zeros(nf * 2, 3);
0058     tri(1:2:end, :) = faces(:, [1 2 3]);
0059     tri(2:2:end, :) = faces(:, [1 3 4]);
0060     
0061     if nargout > 1
0062         inds = kron(1:size(faces, 1), ones(1,2))';
0063     end
0064     
0065     return;
0066 end
0067 
0068 
0069 %% Pentagonal faces (for dodecahedron...): split into 3 triangles
0070 
0071 if isnumeric(faces) && size(faces, 2) == 5
0072     nf = size(faces, 1);
0073     tri = zeros(nf * 3, 3);
0074     tri(1:3:end, :) = faces(:, [1 2 3]);
0075     tri(2:3:end, :) = faces(:, [1 3 4]);
0076     tri(3:3:end, :) = faces(:, [1 4 5]);
0077     
0078     if nargout > 1
0079         inds = kron(1:size(faces, 1), ones(1,2))';
0080     end
0081     
0082     return;
0083 end
0084 
0085 
0086 %% Faces as cell array
0087 
0088 % number of faces
0089 nf  = length(faces);
0090 
0091 % compute total number of triangles
0092 ni = zeros(nf, 1);
0093 for i = 1:nf
0094     % as many triangles as the number of vertices minus 1
0095     ni(i) = length(faces{i}) - 2;
0096 end
0097 nt = sum(ni);
0098 
0099 % allocate memory for triangle array
0100 tri = zeros(nt, 3);
0101 inds = zeros(nt, 1);
0102 
0103 % convert faces to triangles
0104 t = 1;
0105 for i = 1:nf
0106     face = faces{i};
0107     nv = length(face);
0108     v0 = face(1);
0109     for j = 3:nv
0110         tri(t, :) = [v0 face(j-1) face(j)];
0111         inds(t) = i;
0112         t = t + 1;
0113     end
0114 end
0115

Generated on Wed 16-Feb-2022 15:10:47 by m2html © 2003-2019