Home > matGeom > meshes3d > drawMesh.m

drawMesh

PURPOSE ^

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

SYNOPSIS ^

function varargout = drawMesh(varargin)

DESCRIPTION ^

 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 % 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 %   INRA - TPV URPOI - BIA IMASTE
0051 %   created the 10/02/2005.
0052 %
0053 
0054 
0055 %% Parse input arguments
0056 
0057 % extract first argument
0058 var1 = varargin{1};
0059 varargin(1) = [];
0060 
0061 % Check if first input argument is an axes handle
0062 if isAxisHandle(var1)
0063     ax = var1;
0064     var1 = varargin{1};
0065     varargin(1) = [];
0066 else
0067     ax = gca;
0068 end
0069 
0070 % Check if the input is a mesh structure
0071 if isstruct(var1)
0072     % extract data to display
0073     vertices = var1.vertices;
0074     faces = var1.faces;
0075 else
0076     % assumes input is given with vertices+faces arrays
0077     vertices = var1;
0078     faces = varargin{1};
0079     varargin(1) = [];
0080 end
0081 
0082 % if vertices are 2D points, add a z=0 coordinate
0083 if size(vertices, 2) == 2
0084     vertices(1, 3) = 0;
0085 end
0086 
0087 
0088 %% Pre-processing for formatting display options
0089 
0090 % default color for drawing mesh
0091 faceColor = [1 0 0];
0092 
0093 % combine defualt face color with varargin
0094 if isempty(varargin)
0095     varargin = [{'FaceColor'}, faceColor];
0096 elseif length(varargin) == 1
0097     % if only one optional argument is provided, it is assumed to be color
0098     faceColor = varargin{1};
0099     varargin = [{'FaceColor'}, varargin];
0100 elseif length(varargin) > 1
0101     % check if FaceColor option is specified,
0102     % and if not use default face color
0103     indFC = strcmpi(varargin(1:2:end), 'FaceColor');
0104     if ~any(indFC)
0105         varargin = [{'FaceColor'}, {faceColor}, varargin];
0106     end
0107 end
0108 
0109 % check if simplified options are present
0110 indVC = find(strcmpi(varargin(1:2:end), 'VertexColor'));
0111 if ~isempty(indVC)
0112     vertexColor = varargin{indVC * 2};
0113     varargin([indVC*2-1 indVC*2]) = [];
0114     indFC = find(strcmpi(varargin(1:2:end), 'FaceColor'));
0115     if ~isempty(indFC)
0116         varargin([indFC*2-1 indFC*2]) = [];
0117     end
0118     varargin = [{'FaceVertexCData'}, {vertexColor}, {'FaceColor'}, {'interp'}, varargin];
0119 end
0120 
0121 
0122 %% Draw the mesh
0123 
0124 % overwrite on current figure
0125 hold(ax, 'on');
0126 
0127 % Use different processing depending on the type of faces
0128 if isnumeric(faces)
0129     % array FACES is a NF-by-NV indices array, with NF: number of faces,
0130     % and NV: number of vertices per face
0131     h = patch('Parent', ax, ...
0132         'vertices', vertices, 'faces', faces, ...
0133         varargin{:});
0134 
0135 elseif iscell(faces)
0136     % array FACES is a cell array. Need to draw each face as a single
0137     % patch.
0138     h = zeros(length(faces(:)), 1);
0139 
0140     for iFace = 1:length(faces(:))
0141         % get vertices of the cell
0142         face = faces{iFace};
0143 
0144         % Special processing in case of multiple polygonal face:
0145         % each polygonal loop is separated by a NaN.
0146         if sum(isnan(face)) ~= 0
0147             
0148             % find indices of loops breaks
0149             inds = find(isnan(face));
0150             
0151             % replace NaNs by index of first vertex of each polygon
0152             face(inds(2:end))   = face(inds(1:end-1)+1);
0153             face(inds(1))       = face(1);
0154             face(length(face)+1)= face(inds(end)+1);            
0155         end
0156         
0157         % draw current face
0158         cnodes = vertices(face, :);
0159         h(iFace) = patch(ax, cnodes(:, 1), cnodes(:, 2), cnodes(:, 3), faceColor);
0160     end
0161     
0162     % set up drawing options
0163     set(h, varargin{:});
0164 else
0165     error('MatGeom:drawMesh', 'Second argument must be a face array');
0166 end
0167 
0168 
0169 %% Process output arguments
0170 
0171 % format output parameters
0172 if nargout > 0
0173     varargout = {h};
0174 end

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