Home > matGeom > meshes3d > concatenateMeshes.m

concatenateMeshes

PURPOSE ^

CONCATENATEMESHES Concatenate multiple meshes.

SYNOPSIS ^

function varargout = concatenateMeshes(varargin)

DESCRIPTION ^

 CONCATENATEMESHES Concatenate multiple meshes.

   [V,F] = concatenateMeshes(V1,F1,V2,F2, ...)
   Returns one mesh represented by vertices V and faces F by concatenating
   the meshes defined by V1, V2, ... and F1, F2, ...

   [V,F] = concatenateMeshes(MESH1, MESH2, ...)
   where MESH1, MESH2, ... are structs or struct arrays with the fields  
   vertices and faces

   See also
     splitMesh

 ---------
 Authors: oqilipo (parsing), Alec Jacobson (loop)
 Created: 2017-09-12
 Copyright 2017

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function varargout = concatenateMeshes(varargin)
0002 % CONCATENATEMESHES Concatenate multiple meshes.
0003 %
0004 %   [V,F] = concatenateMeshes(V1,F1,V2,F2, ...)
0005 %   Returns one mesh represented by vertices V and faces F by concatenating
0006 %   the meshes defined by V1, V2, ... and F1, F2, ...
0007 %
0008 %   [V,F] = concatenateMeshes(MESH1, MESH2, ...)
0009 %   where MESH1, MESH2, ... are structs or struct arrays with the fields
0010 %   vertices and faces
0011 %
0012 %   See also
0013 %     splitMesh
0014 %
0015 % ---------
0016 % Authors: oqilipo (parsing), Alec Jacobson (loop)
0017 % Created: 2017-09-12
0018 % Copyright 2017
0019 
0020 %% parsing inputs
0021 assert(~isempty(varargin))
0022 
0023 if isstruct(varargin{1})
0024     VF_fields = {'vertices','faces'};
0025     
0026     errorStructFields=['If the first input argument is a struct '...
0027         'with the fields vertices and faces the additonal ' ...
0028         'arguments must have the same format'];
0029     % Check, if all input arguments are structs
0030     assert(all(cellfun(@isstruct, varargin)), errorStructFields)
0031     % Check, if all structs contain the two fields vertices and faces
0032     assert(all(cellfun(@(x) all(ismember(fieldnames(x), ...
0033         VF_fields)), varargin)), errorStructFields)
0034     
0035     if length(varargin)==1
0036         errorArgAndStructLength = ['If the input is only one struct ' ...
0037             'it has to contain more than one mesh.'];
0038         assert(length(varargin{1})>1, ...
0039             errorArgAndStructLength)
0040     end
0041     
0042     % Order of the fields: vertices, faces
0043     varargin = cellfun(@(x) orderfields(x, VF_fields),varargin, 'UniformOutput',0);
0044     
0045     % Convert the structs into one cell array
0046     varargin = ...
0047         cellfun(@struct2cell, varargin, 'UniformOutput', false);
0048     varargin = cellfun(@squeeze, varargin, 'UniformOutput',0);
0049     varargin = reshape([varargin{:}],[],1)';
0050 end
0051 
0052 NoA = length(varargin);
0053 assert(mod(NoA,2)==0);
0054 
0055 cellfun(@(x) validateattributes(x, {'numeric'},...
0056     {'size',[NaN,3],'finite'}), varargin(1:2:end))
0057 cellfun(@(x) validateattributes(x, {'numeric'},...
0058     {'integer'}), varargin(2:2:end))
0059 % Check if all faces have the same number of columns
0060 errorFacesRows='The faces of all meshes must have the same number of columns';
0061 assert(numel(unique(cellfun(@(x) size(x,2), varargin(2:2:end))))==1, errorFacesRows)
0062 
0063 
0064 %% loop
0065 v=[];
0066 f=[];
0067 for m = 1:NoA/2
0068     vm = varargin{2*m-1};
0069     fm = varargin{2*m};
0070     f = [f; fm+size(v,1)]; %#ok<AGROW>
0071     v = [v; vm]; %#ok<AGROW>
0072 end
0073 
0074 
0075 %% parsing outputs
0076 [v, f] = trimMesh(v, f);
0077 
0078 switch nargout
0079     case 1
0080         mesh.vertices=v;
0081         mesh.faces=f;
0082         varargout{1}=mesh;
0083     case 2
0084         varargout{1}=v;
0085         varargout{2}=f;
0086 end
0087

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