Home > matGeom > meshes3d > drawPolyhedron.m

drawPolyhedron

PURPOSE ^

DRAWPOLYHEDRON Draw polyhedron defined by vertices and faces.

SYNOPSIS ^

function varargout = drawPolyhedron(nodes, faces, varargin)

DESCRIPTION ^

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

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

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 % Author: David Legland
0025 % E-mail: david.legland@inrae.fr
0026 % Created: 2005-02-10
0027 % Copyright 2005-2024 INRA - TPV URPOI - BIA IMASTE
0028 
0029 %% Initialisations
0030 
0031 
0032 % process input arguments
0033 switch length(varargin)
0034     case 0 
0035         % default color is red
0036         varargin = {'facecolor', [1 0 0]};
0037     case 1
0038         % use argument as color for faces
0039         varargin = {'facecolor', varargin{1}};
0040     otherwise
0041         % otherwise do nothing
0042 end
0043 
0044 % overwrites on current figure
0045 hold on;
0046 
0047 % if nodes are 2D points, add a z=0 coordinate
0048 if size(nodes, 2) == 2
0049     nodes(1,3) = 0;
0050 end
0051 
0052 
0053 %% main loop : for each face
0054 
0055 if iscell(faces)
0056     % array FACES is a cell array
0057     h = zeros(length(faces(:)), 1);
0058 
0059     for f = 1:length(faces(:))
0060         % get nodes of the cell
0061         face = faces{f};
0062 
0063         if sum(isnan(face))~=0
0064             % Special processing in case of multiple polygonal face.
0065             % each polygonal loop is separated by a NaN.
0066             
0067             % find indices of loops breaks
0068             inds = find(isnan(face));
0069             
0070             % replace NaNs by index of first vertex of each polygon
0071             face(inds(2:end))   = face(inds(1:end-1)+1);
0072             face(inds(1))       = face(1);
0073             face(length(face)+1)= face(inds(end)+1);            
0074         end
0075         
0076         % draw current face
0077         cnodes  = nodes(face, :);
0078         h(f)    = patch(cnodes(:, 1), cnodes(:, 2), cnodes(:, 3), [1 0 0]);
0079     end
0080 
0081 else
0082     % array FACES is a NC*NV indices array, with NV : number of vertices of
0083     % each face, and NC number of faces
0084     h = zeros(size(faces, 1), 1);
0085     for f = 1:size(faces, 1)
0086         % get nodes of the cell
0087         cnodes = nodes(faces(f,:)', :);
0088         h(f) = patch(cnodes(:, 1), cnodes(:, 2), cnodes(:, 3), [1 0 0]);
0089     end
0090 end
0091 
0092 % set up drawing options
0093 if ~isempty(varargin)
0094     set(h, varargin{:});
0095 end
0096 
0097 % format output parameters
0098 if nargout > 0
0099     varargout = {h};
0100 end

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