DRAWPOLYHEDRON Draw polyhedron defined by vertices and faces. drawPolyhedron(NODES, FACES) Draws the polyhedron defined by vertices NODES and the faces FACES. NODES is a NV-by-3 array containing coordinates of vertices, and FACES is either a NF-by3 or NF-by-4 array containing indices of vertices of the triangular or rectangular faces. FACES can also be a cell array, in the content of each cell is an array of indices to the nodes of the current face. Faces can have different number of vertices. H = drawPolyhedron(...); Also returns a handle to the created patche. Example: [n f] = createSoccerBall; drawPolyhedron(n, f); See also: polyhedra, drawMesh, drawPolygon --------- author : David Legland INRA - TPV URPOI - BIA IMASTE created the 10/02/2005.
0001 function varargout = drawPolyhedron(nodes, faces, varargin) 0002 %DRAWPOLYHEDRON Draw polyhedron defined by vertices and faces. 0003 % 0004 % drawPolyhedron(NODES, FACES) 0005 % Draws the polyhedron defined by vertices NODES and the faces FACES. 0006 % NODES is a NV-by-3 array containing coordinates of vertices, and FACES 0007 % is either a NF-by3 or NF-by-4 array containing indices of vertices of 0008 % the triangular or rectangular faces. 0009 % FACES can also be a cell array, in the content of each cell is an array 0010 % of indices to the nodes of the current face. Faces can have different 0011 % number of vertices. 0012 % 0013 % H = drawPolyhedron(...); 0014 % Also returns a handle to the created patche. 0015 % 0016 % Example: 0017 % [n f] = createSoccerBall; 0018 % drawPolyhedron(n, f); 0019 % 0020 % See also: 0021 % polyhedra, drawMesh, drawPolygon 0022 % 0023 % --------- 0024 % 0025 % author : David Legland 0026 % INRA - TPV URPOI - BIA IMASTE 0027 % created the 10/02/2005. 0028 % 0029 0030 % HISTORY 0031 % 07/11/2005 update doc. 0032 % 04/01/2007 typo 0033 % 18/01/2007 add support for 2D polyhedra ("nodes" is N-by-2 array), and 0034 % make 'cnodes' a list of points instead of a list of indices 0035 % 14/08/2007 add comment, add support for NaN in faces (complex polygons) 0036 % 14/09/2007 rename as drawPolyhedron 0037 % 16/10/2008 better support for colors 0038 % 27/07/2010 copy to 'drawMesh' 0039 0040 0041 %% Initialisations 0042 0043 0044 % process input arguments 0045 switch length(varargin) 0046 case 0 0047 % default color is red 0048 varargin = {'facecolor', [1 0 0]}; 0049 case 1 0050 % use argument as color for faces 0051 varargin = {'facecolor', varargin{1}}; 0052 otherwise 0053 % otherwise do nothing 0054 end 0055 0056 % overwrites on current figure 0057 hold on; 0058 0059 % if nodes are 2D points, add a z=0 coordinate 0060 if size(nodes, 2) == 2 0061 nodes(1,3) = 0; 0062 end 0063 0064 0065 %% main loop : for each face 0066 0067 if iscell(faces) 0068 % array FACES is a cell array 0069 h = zeros(length(faces(:)), 1); 0070 0071 for f = 1:length(faces(:)) 0072 % get nodes of the cell 0073 face = faces{f}; 0074 0075 if sum(isnan(face))~=0 0076 % Special processing in case of multiple polygonal face. 0077 % each polygonal loop is separated by a NaN. 0078 0079 % find indices of loops breaks 0080 inds = find(isnan(face)); 0081 0082 % replace NaNs by index of first vertex of each polygon 0083 face(inds(2:end)) = face(inds(1:end-1)+1); 0084 face(inds(1)) = face(1); 0085 face(length(face)+1)= face(inds(end)+1); 0086 end 0087 0088 % draw current face 0089 cnodes = nodes(face, :); 0090 h(f) = patch(cnodes(:, 1), cnodes(:, 2), cnodes(:, 3), [1 0 0]); 0091 end 0092 0093 else 0094 % array FACES is a NC*NV indices array, with NV : number of vertices of 0095 % each face, and NC number of faces 0096 h = zeros(size(faces, 1), 1); 0097 for f = 1:size(faces, 1) 0098 % get nodes of the cell 0099 cnodes = nodes(faces(f,:)', :); 0100 h(f) = patch(cnodes(:, 1), cnodes(:, 2), cnodes(:, 3), [1 0 0]); 0101 end 0102 end 0103 0104 % set up drawing options 0105 if ~isempty(varargin) 0106 set(h, varargin{:}); 0107 end 0108 0109 % format output parameters 0110 if nargout > 0 0111 varargout = {h}; 0112 end