Home > matGeom > meshes3d > fillMeshFaces.m

fillMeshFaces

PURPOSE ^

Fill the faces of a mesh with the specified colors.

SYNOPSIS ^

function varargout = fillMeshFaces(varargin)

DESCRIPTION ^

 Fill the faces of a mesh with the specified colors.

   fillMeshFaces(V, F, VERTEXCOLORS)
   Colorizes a mesh by filling faces with an array of values. The colors
   can be a NV-by-1 array of values, or a NV-by-3 array of values.
   Face filling uses 'interp' coloring mode.

   fillMeshFaces(V, F, FACECOLORS)
   Colorizes the mesh by specifying the value or the color associated to
   each face. Face filling uses 'flat' coloring mode.

   fillMeshFaces(..., PNAME, PVALUE)
   Specifies additional parameters that will be passed to the 'patch'
   function.

   Example
     % Colorize mesh based on z-coordinate of vertices.
     [v, f] = createIcosahedron;
     values = v(:,3);
     figure; axis equal; view(3);
     fillMeshFaces(v, f, values);

     % Colorize mesh using specific color for each face
     [v, f] = createIcosahedron;
     colors = jet(20);
     figure; axis equal; view(3);
     fillMeshFaces(v, f, colors);

   See also
     drawMesh

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function varargout = fillMeshFaces(varargin)
0002 % Fill the faces of a mesh with the specified colors.
0003 %
0004 %   fillMeshFaces(V, F, VERTEXCOLORS)
0005 %   Colorizes a mesh by filling faces with an array of values. The colors
0006 %   can be a NV-by-1 array of values, or a NV-by-3 array of values.
0007 %   Face filling uses 'interp' coloring mode.
0008 %
0009 %   fillMeshFaces(V, F, FACECOLORS)
0010 %   Colorizes the mesh by specifying the value or the color associated to
0011 %   each face. Face filling uses 'flat' coloring mode.
0012 %
0013 %   fillMeshFaces(..., PNAME, PVALUE)
0014 %   Specifies additional parameters that will be passed to the 'patch'
0015 %   function.
0016 %
0017 %   Example
0018 %     % Colorize mesh based on z-coordinate of vertices.
0019 %     [v, f] = createIcosahedron;
0020 %     values = v(:,3);
0021 %     figure; axis equal; view(3);
0022 %     fillMeshFaces(v, f, values);
0023 %
0024 %     % Colorize mesh using specific color for each face
0025 %     [v, f] = createIcosahedron;
0026 %     colors = jet(20);
0027 %     figure; axis equal; view(3);
0028 %     fillMeshFaces(v, f, colors);
0029 %
0030 %   See also
0031 %     drawMesh
0032  
0033 % ------
0034 % Author: David Legland
0035 % e-mail: david.legland@inrae.fr
0036 % INRAE - BIA Research Unit - BIBS Platform (Nantes)
0037 % Created: 2020-04-16,    using Matlab 9.7.0.1247435 (R2019b) Update 2
0038 % Copyright 2020 INRAE.
0039 
0040 %% Parse input arguments
0041 
0042 % extract first argument
0043 var1 = varargin{1};
0044 varargin(1) = [];
0045 
0046 % Check if first input argument is an axes handle
0047 if isAxisHandle(var1)
0048     ax = var1;
0049     var1 = varargin{1};
0050     varargin(1) = [];
0051 else
0052     ax = gca;
0053 end
0054 
0055 % Check if the input is a mesh structure
0056 if isstruct(var1)
0057     % extract data to display
0058     vertices = var1.vertices;
0059     faces = var1.faces;
0060 else
0061     % assumes input is given with vertices+faces arrays
0062     vertices = var1;
0063     faces = varargin{1};
0064     varargin(1) = [];
0065 end
0066 
0067 % next argument is face color
0068 colors = varargin{1};
0069 varargin(1) = [];
0070 
0071 % adapt the face color key value depending on the size of the "color" input
0072 % argument
0073 faceColorMode = 'interp';
0074 if size(colors, 1) == size(faces, 1)
0075     faceColorMode = 'flat';
0076 end
0077 
0078 % array FACES is a NF-by-NV indices array, with NV number of vertices of
0079 % each face, and NF number of faces
0080 h = patch('Parent', ax, ...
0081     'vertices', vertices, 'faces', faces, 'FaceVertexCData', colors, ...
0082     'FaceColor', faceColorMode, varargin{:});
0083 
0084 
0085 %% Process output arguments
0086 
0087 % format output parameters
0088 if nargout > 0
0089     varargout = {h};
0090 end

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