Can Anyone help me understanding the following code?
    3 views (last 30 days)
  
       Show older comments
    
Hello there, I have the following code that related to the mesh split functionality. can someone help me understanding what the author exactly did in order to split the mesh components? i found the function here: https://www.mathworks.com/matlabcentral/fileexchange/27667-splitfv-split-a-mesh
function fvOut = splitFV( f, v )
%SPLITFV Splits faces and vertices into connected pieces
%   FVOUT = SPLITFV(F,V) separates disconnected pieces inside a patch defined by faces (F) and
%   vertices (V). FVOUT is a structure array with fields "faces" and "vertices". Each element of
%   this array indicates a separately connected patch.
%
%   FVOUT = SPLITFV(FV) takes in FV as a structure with fields "faces" and "vertices"
%   Copyright Sven Holcombe
%   $Date: 2010/05/19 $
%%Extract f and v
if nargin==1 && isstruct(f) && all(isfield(f,{'faces','vertices'}))
    v = f.vertices;
    f = f.faces;
elseif nargin==2
    % f and v are already defined
else
    error('splitFV:badArgs','splitFV takes a faces/vertices structure, or these fields passed individually')
end
%%Organise faces into connected fSets that share nodes
fSets = zeros(size(f,1),1,'uint32');
currentSet = 0;
while any(fSets==0)
    currentSet = currentSet + 1;
    fprintf('Connecting set #%d vertices...',currentSet);
    nextAvailFace = find(fSets==0,1,'first');
    openVertices = f(nextAvailFace,:);
    while ~isempty(openVertices)
        availFaceInds = find(fSets==0);
        [availFaceSub, ~] = find(ismember(f(availFaceInds,:), openVertices)); 
        fSets(availFaceInds(availFaceSub)) = currentSet;
        openVertices = f(availFaceInds(availFaceSub),:);
    end
    fprintf(' done! Set #%d has %d faces.\n',currentSet,nnz(fSets==currentSet));
end
numSets = currentSet;
%%Create separate faces/vertices structures for each fSet
fvOut = repmat(struct('faces',[],'vertices',[]),numSets,1);
for currentSet = 1:numSets
    setF = f(fSets==currentSet,:);
    [unqVertIds, ~, newVertIndices] = unique(setF);
    fvOut(currentSet).faces = reshape(newVertIndices,size(setF));
    fvOut(currentSet).vertices = v(unqVertIds,:);
end
2 Comments
  Sven
      
 on 6 Sep 2018
				Sana, you will need to be more specific - you haven't actually asked a question. Is there a particular part of the code that you don't understand? Do you already understand how faces and vertices are defined for a mesh? If not, you should start there.
Answers (0)
See Also
Categories
				Find more on Surface and Mesh Plots in Help Center and File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
