Home > matGeom > meshes3d > fillMeshFaces.m

fillMeshFaces

PURPOSE ^

FILLMESHFACES Fill the faces of a mesh with the specified colors.

SYNOPSIS ^

function varargout = fillMeshFaces(varargin)

DESCRIPTION ^

FILLMESHFACES 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 %FILLMESHFACES 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 % Created: 2020-04-16, using Matlab 9.7.0.1247435 (R2019b) Update 2
0037 % Copyright 2020-2024 INRAE - BIA Research Unit - BIBS Platform (Nantes)
0038 
0039 %% Parse input arguments
0040 
0041 % extract first argument
0042 var1 = varargin{1};
0043 varargin(1) = [];
0044 
0045 % Check if first input argument is an axes handle
0046 if isAxisHandle(var1)
0047     ax = var1;
0048     var1 = varargin{1};
0049     varargin(1) = [];
0050 else
0051     ax = gca;
0052 end
0053 
0054 % Check if the input is a mesh structure
0055 if isstruct(var1)
0056     % extract data to display
0057     vertices = var1.vertices;
0058     faces = var1.faces;
0059 else
0060     % assumes input is given with vertices+faces arrays
0061     vertices = var1;
0062     faces = varargin{1};
0063     varargin(1) = [];
0064 end
0065 
0066 % next argument is face color
0067 colors = varargin{1};
0068 varargin(1) = [];
0069 
0070 % adapt the face color key value depending on the size of the "color" input
0071 % argument
0072 faceColorMode = 'interp';
0073 if size(colors, 1) == size(faces, 1)
0074     faceColorMode = 'flat';
0075 end
0076 
0077 % array FACES is a NF-by-NV indices array, with NV number of vertices of
0078 % each face, and NF number of faces
0079 h = patch('Parent', ax, ...
0080     'vertices', vertices, 'faces', faces, 'FaceVertexCData', colors, ...
0081     'FaceColor', faceColorMode, varargin{:});
0082 
0083 
0084 %% Process output arguments
0085 
0086 % format output parameters
0087 if nargout > 0
0088     varargout = {h};
0089 end

Generated on Thu 21-Nov-2024 11:30:22 by m2html © 2003-2022