Home > matGeom > meshes3d > surfToMesh.m

surfToMesh

PURPOSE ^

Convert surface grids into face-vertex mesh.

SYNOPSIS ^

function varargout = surfToMesh(x, y, varargin)

DESCRIPTION ^

 Convert surface grids into face-vertex mesh.

   [V, F] = surfToMesh(X, Y)
   [V, F] = surfToMesh(X, Y, Z)
   Converts the surface grid given by two or three coordinate arrays into
   a face-vertex quad mesh.

   Example
     % transform a surface into a mesh
     [X, Y] = meshgrid(-2:.2:2, -2:.2:2);                                
     Z = X .* exp(-X.^2 - Y.^2);
     [V, F] = surfToMesh(X, Y, Z);
     figure;
     drawMesh(V, F); view(3);

     % Transform surface of a cylinder as a mesh
     [x, y, z] = cylinder(5*ones(1, 10));
     [v, f] = surfToMesh(x, y, z, 'xPeriodic', true);
     figure;
     drawMesh(v, f);
     view(3); axis equal;

   See also
     meshes3d, meshgrid, drawMesh, torusMesh, sphereMesh

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function varargout = surfToMesh(x, y, varargin)
0002 % Convert surface grids into face-vertex mesh.
0003 %
0004 %   [V, F] = surfToMesh(X, Y)
0005 %   [V, F] = surfToMesh(X, Y, Z)
0006 %   Converts the surface grid given by two or three coordinate arrays into
0007 %   a face-vertex quad mesh.
0008 %
0009 %   Example
0010 %     % transform a surface into a mesh
0011 %     [X, Y] = meshgrid(-2:.2:2, -2:.2:2);
0012 %     Z = X .* exp(-X.^2 - Y.^2);
0013 %     [V, F] = surfToMesh(X, Y, Z);
0014 %     figure;
0015 %     drawMesh(V, F); view(3);
0016 %
0017 %     % Transform surface of a cylinder as a mesh
0018 %     [x, y, z] = cylinder(5*ones(1, 10));
0019 %     [v, f] = surfToMesh(x, y, z, 'xPeriodic', true);
0020 %     figure;
0021 %     drawMesh(v, f);
0022 %     view(3); axis equal;
0023 %
0024 %   See also
0025 %     meshes3d, meshgrid, drawMesh, torusMesh, sphereMesh
0026 
0027 % ------
0028 % Author: David Legland
0029 % e-mail: david.legland@inra.fr
0030 % Created: 2012-10-25,    using Matlab 7.9.0.529 (R2009b)
0031 % Copyright 2012 INRA - Cepia Software Platform.
0032 
0033 
0034 %% Parse inputs
0035 
0036 % check if z-value is present
0037 if ~isempty(varargin) && isnumeric(varargin{1})
0038     z = varargin{1};
0039     varargin(1) = [];
0040 end
0041 
0042 % default periodicities
0043 xPeriodic = false;
0044 yPeriodic = false;
0045 
0046 % parse input options
0047 while length(varargin) > 1
0048     paramName = lower(varargin{1});
0049     switch paramName
0050         case 'xperiodic'
0051             xPeriodic = varargin{2};
0052         case 'yperiodic'
0053             yPeriodic = varargin{2};
0054         otherwise
0055             error(['Unknown parameter name: ' paramName]);
0056     end
0057 
0058     varargin(1:2) = [];
0059 end
0060 
0061 
0062 %% Compute vertex indices
0063 
0064 % size along each direction (arrays are (y,x)-indexed)
0065 n1 = size(x, 1);
0066 n2 = size(x, 2);
0067 
0068 % in case of periodicity, the last vertex of the grid is drop (it is
0069 % assumed to be the same as the first one)
0070 if xPeriodic
0071     n2 = n2 - 1;
0072 end
0073 if yPeriodic
0074     n1 = n1 - 1;
0075 end
0076 
0077 % new size of vertex grid
0078 dim = [n1 n2];
0079 nv = n1 * n2;
0080 
0081 
0082 %% Create vertex array
0083 
0084 % eventually remove boundary vertices
0085 x = x(1:n1, 1:n2);
0086 y = y(1:n1, 1:n2);
0087 
0088 % create vertex array
0089 if ~exist('z', 'var')
0090     vertices = [x(:) y(:)];
0091 else
0092     z = z(1:n1, 1:n2);
0093     vertices = [x(:) y(:) z(:)];
0094 end
0095 
0096 
0097 %% Create face array
0098 
0099 % vertex indices in grid
0100 inds = reshape(1:nv, dim);
0101 if xPeriodic
0102     inds = inds(:, [1:end 1]);
0103 end
0104 if yPeriodic
0105     inds = inds([1:end 1], :);
0106 end
0107 
0108 % vertex indices for each face
0109 v1 = inds(1:end-1, 1:end-1);
0110 v2 = inds(1:end-1, 2:end);
0111 v3 = inds(2:end, 2:end);
0112 v4 = inds(2:end, 1:end-1);
0113 
0114 % concatenate indices
0115 faces = [v1(:) v2(:) v3(:) v4(:)];
0116 
0117 
0118 %% format output
0119 varargout = formatMeshOutput(nargout, vertices, faces);

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