Home > matGeom > meshes3d > drawMesh.m

drawMesh

PURPOSE ^

DRAWMESH Draw a 3D mesh defined by vertex and face arrays.

SYNOPSIS ^

function varargout = drawMesh(varargin)

DESCRIPTION ^

DRAWMESH Draw a 3D mesh defined by vertex and face arrays.

   drawMesh(VERTICES, FACES)
   Draws the 3D mesh defined by vertices VERTICES and the faces FACES. 
   vertices is a NV-by-3 array containing coordinates of vertices, and
   FACES is either a NF-by-3 or NF-by-4 array containing face vertex
   indices of the triangular or rectangular faces.
   FACES can also be a cell array, each cell containing an array of vertex
   indices for the corresponding face. In this case the faces may have
   variable number of vertices. 
   
   drawMesh(MESH)
   Specifies the mesh as a structure containing at least the fields
   'vertices' and 'faces', using the same conventions as above.

   drawMesh(..., COLOR)
   Use the specified color to render the mesh faces.

   drawMesh(..., NAME, VALUE)
   Use one or several pairs of parameter name/value to specify drawing
   options. Options are the same as the 'patch' function.

   drawMesh(AX,...) 
   Draw into the axis specified by AX instead of the current axis.


   H = drawMesh(...);
   Also returns a handle to the created patch.

   Example:
     % display a polyhedra with polygonal faces
     [v, f] = createSoccerBall;
     drawMesh(v, f);

     % Display a mesh representing a  torus, using uniform face color
     [v, f] = torusMesh;
     figure; hold on; axis equal; view(3);
     drawMesh(v, f, 'FaceColor', 'g')
     % paint the mesh according to vertex x-coordinate
     figure; hold on; axis equal; view(3);
     drawMesh(v, f, 'VertexColor', v(:,1), 'LineStyle', 'none');

   See also
     meshes3d, polyhedra, patch

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function varargout = drawMesh(varargin)
0002 %DRAWMESH Draw a 3D mesh defined by vertex and face arrays.
0003 %
0004 %   drawMesh(VERTICES, FACES)
0005 %   Draws the 3D mesh defined by vertices VERTICES and the faces FACES.
0006 %   vertices is a NV-by-3 array containing coordinates of vertices, and
0007 %   FACES is either a NF-by-3 or NF-by-4 array containing face vertex
0008 %   indices of the triangular or rectangular faces.
0009 %   FACES can also be a cell array, each cell containing an array of vertex
0010 %   indices for the corresponding face. In this case the faces may have
0011 %   variable number of vertices.
0012 %
0013 %   drawMesh(MESH)
0014 %   Specifies the mesh as a structure containing at least the fields
0015 %   'vertices' and 'faces', using the same conventions as above.
0016 %
0017 %   drawMesh(..., COLOR)
0018 %   Use the specified color to render the mesh faces.
0019 %
0020 %   drawMesh(..., NAME, VALUE)
0021 %   Use one or several pairs of parameter name/value to specify drawing
0022 %   options. Options are the same as the 'patch' function.
0023 %
0024 %   drawMesh(AX,...)
0025 %   Draw into the axis specified by AX instead of the current axis.
0026 %
0027 %
0028 %   H = drawMesh(...);
0029 %   Also returns a handle to the created patch.
0030 %
0031 %   Example:
0032 %     % display a polyhedra with polygonal faces
0033 %     [v, f] = createSoccerBall;
0034 %     drawMesh(v, f);
0035 %
0036 %     % Display a mesh representing a  torus, using uniform face color
0037 %     [v, f] = torusMesh;
0038 %     figure; hold on; axis equal; view(3);
0039 %     drawMesh(v, f, 'FaceColor', 'g')
0040 %     % paint the mesh according to vertex x-coordinate
0041 %     figure; hold on; axis equal; view(3);
0042 %     drawMesh(v, f, 'VertexColor', v(:,1), 'LineStyle', 'none');
0043 %
0044 %   See also
0045 %     meshes3d, polyhedra, patch
0046 %
0047 
0048 % ------
0049 % Author: David Legland
0050 % E-mail: david.legland@inrae.fr
0051 % Created: 2005-02-10
0052 % Copyright 2005-2024 INRA - TPV URPOI - BIA IMASTE
0053 
0054 %% Parse input arguments
0055 
0056 % extract first argument
0057 var1 = varargin{1};
0058 varargin(1) = [];
0059 
0060 % Check if first input argument is an axes handle
0061 if isAxisHandle(var1)
0062     ax = var1;
0063     var1 = varargin{1};
0064     varargin(1) = [];
0065 else
0066     ax = gca;
0067 end
0068 
0069 % Check if the input is a mesh structure
0070 if isstruct(var1)
0071     % extract data to display
0072     vertices = var1.vertices;
0073     faces = var1.faces;
0074 else
0075     % assumes input is given with vertices+faces arrays
0076     vertices = var1;
0077     faces = varargin{1};
0078     varargin(1) = [];
0079 end
0080 
0081 % if vertices are 2D points, add a z=0 coordinate
0082 if size(vertices, 2) == 2
0083     vertices(1, 3) = 0;
0084 end
0085 
0086 
0087 %% Pre-processing for formatting display options
0088 
0089 % default color for drawing mesh
0090 faceColor = [1 0 0];
0091 
0092 % combine default face color with varargin
0093 if isempty(varargin)
0094     varargin = [{'FaceColor'}, faceColor];
0095 elseif isscalar(varargin)
0096     % if only one optional argument is provided, it is assumed to be color
0097     faceColor = varargin{1};
0098     varargin = [{'FaceColor'}, varargin];
0099 elseif length(varargin) > 1
0100     % check if FaceColor option is specified,
0101     % and if not use default face color
0102     indFC = strcmpi(varargin(1:2:end), 'FaceColor');
0103     if ~any(indFC)
0104         varargin = [{'FaceColor'}, {faceColor}, varargin];
0105     end
0106 end
0107 
0108 % check if simplified options are present
0109 indVC = find(strcmpi(varargin(1:2:end), 'VertexColor'));
0110 if ~isempty(indVC)
0111     vertexColor = varargin{indVC * 2};
0112     varargin([indVC*2-1 indVC*2]) = [];
0113     indFC = find(strcmpi(varargin(1:2:end), 'FaceColor'));
0114     if ~isempty(indFC)
0115         varargin([indFC*2-1 indFC*2]) = [];
0116     end
0117     varargin = [{'FaceVertexCData'}, {vertexColor}, {'FaceColor'}, {'interp'}, varargin];
0118 end
0119 
0120 
0121 %% Draw the mesh
0122 
0123 % overwrite on current figure
0124 hold(ax, 'on');
0125 
0126 % Use different processing depending on the type of faces
0127 if isnumeric(faces)
0128     % array FACES is a NF-by-NV indices array, with NF: number of faces,
0129     % and NV: number of vertices per face
0130     h = patch('Parent', ax, ...
0131         'vertices', vertices, 'faces', faces, ...
0132         varargin{:});
0133 
0134 elseif iscell(faces)
0135     % array FACES is a cell array. Need to draw each face as a single
0136     % patch.
0137     h = zeros(length(faces(:)), 1);
0138 
0139     for iFace = 1:length(faces(:))
0140         % get vertices of the cell
0141         face = faces{iFace};
0142 
0143         % Special processing in case of multiple polygonal face:
0144         % each polygonal loop is separated by a NaN.
0145         if sum(isnan(face)) ~= 0
0146             
0147             % find indices of loops breaks
0148             inds = find(isnan(face));
0149             
0150             % replace NaNs by index of first vertex of each polygon
0151             face(inds(2:end))   = face(inds(1:end-1)+1);
0152             face(inds(1))       = face(1);
0153             face(length(face)+1)= face(inds(end)+1);            
0154         end
0155         
0156         % draw current face
0157         cnodes = vertices(face, :);
0158         h(iFace) = patch(ax, cnodes(:, 1), cnodes(:, 2), cnodes(:, 3), faceColor);
0159     end
0160     
0161     % set up drawing options
0162     set(h, varargin{:});
0163 else
0164     error('MatGeom:drawMesh', 'Second argument must be a face array');
0165 end
0166 
0167 
0168 %% Process output arguments
0169 
0170 % format output parameters
0171 if nargout > 0
0172     varargout = {h};
0173 end

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