CLIPMESHVERTICES Clip vertices of a surfacic mesh and remove outer faces. [V2, F2] = clipMeshVertices(V, F, B) Clip a mesh represented by vertex array V and face array F, with the box represented by B. The result is the set of vertices contained in the box, and a new set of faces corresponding to original faces with all vertices within the box. [V2, F2] = clipMeshVertices(..., 'shape', 'sphere') Specify the shape. Default is 'box'. But it is also possible to use 'sphere' or 'plane'. [V2, F2] = clipMeshVertices(..., 'inside', false) removes the inner faces instead of the outer faces. [V2, F2] = clipMeshVertices(..., 'trimMesh', TF) Also specifies if the isolated vertices need to be removed (TF=true) or not (TF=false). Default is false. Example [v, f] = createSoccerBall; f = triangulateFaces(f); box = [0 2 -1 2 -.5 2]; [v2, f2] = clipMeshVertices(v, f, box, 'inside', false); figure('color','w'); view(3); axis equal drawMesh(v, f, 'faceColor', 'none', 'faceAlpha', .2); drawBox3d(box) drawMesh(v2, f2, 'faceAlpha', .7); See also meshes3d, clipPoints3d
0001 function varargout = clipMeshVertices(v, f, b, varargin) 0002 %CLIPMESHVERTICES Clip vertices of a surfacic mesh and remove outer faces. 0003 % 0004 % [V2, F2] = clipMeshVertices(V, F, B) 0005 % Clip a mesh represented by vertex array V and face array F, with the 0006 % box represented by B. The result is the set of vertices contained in 0007 % the box, and a new set of faces corresponding to original faces with 0008 % all vertices within the box. 0009 % 0010 % [V2, F2] = clipMeshVertices(..., 'shape', 'sphere') Specify the shape. 0011 % Default is 'box'. But it is also possible to use 'sphere' or 'plane'. 0012 % 0013 % [V2, F2] = clipMeshVertices(..., 'inside', false) removes the inner 0014 % faces instead of the outer faces. 0015 % 0016 % [V2, F2] = clipMeshVertices(..., 'trimMesh', TF) 0017 % Also specifies if the isolated vertices need to be removed (TF=true) or 0018 % not (TF=false). Default is false. 0019 % 0020 % Example 0021 % [v, f] = createSoccerBall; 0022 % f = triangulateFaces(f); 0023 % box = [0 2 -1 2 -.5 2]; 0024 % [v2, f2] = clipMeshVertices(v, f, box, 'inside', false); 0025 % figure('color','w'); view(3); axis equal 0026 % drawMesh(v, f, 'faceColor', 'none', 'faceAlpha', .2); 0027 % drawBox3d(box) 0028 % drawMesh(v2, f2, 'faceAlpha', .7); 0029 % 0030 % See also 0031 % meshes3d, clipPoints3d 0032 % 0033 0034 % ------ 0035 % Author: David Legland, oqilipo 0036 % E-mail: david.legland@inrae.fr 0037 % Created: 2011-04-07, using Matlab 7.9.0.529 (R2009b) 0038 % Copyright 2011-2024 INRA - Cepia Software Platform 0039 0040 % if input is given as a structure, parse fields 0041 if isstruct(v) 0042 if nargin > 2 0043 varargin = [b, varargin]; 0044 end 0045 b = f; 0046 f = v.faces; 0047 v = v.vertices; 0048 end 0049 0050 parser = inputParser; 0051 validStrings = {'box', 'sphere', 'plane'}; 0052 addParameter(parser, 'shape', 'box', @(x) any(validatestring(x, validStrings))); 0053 addParameter(parser, 'inside', true, @islogical); 0054 addParameter(parser, 'trimMesh', false, @islogical); 0055 parse(parser, varargin{:}); 0056 0057 % clip the vertices 0058 [v2, indVertices] = clipPoints3d(v, b,... 0059 'shape', parser.Results.shape, 'inside', parser.Results.inside); 0060 0061 % create index array for face indices relabeling 0062 refInds = zeros(size(indVertices)); 0063 for i = 1:length(indVertices) 0064 refInds(indVertices(i)) = i; 0065 end 0066 0067 % select the faces with all vertices within the box 0068 if isnumeric(f) 0069 % Faces given as numeric array 0070 indFaces = sum(~ismember(f, indVertices), 2) == 0; 0071 f2 = refInds(f(indFaces, :)); 0072 0073 elseif iscell(f) 0074 % Faces given as cell array 0075 nFaces = length(f); 0076 indFaces = false(nFaces, 1); 0077 for i = 1:nFaces 0078 indFaces(i) = sum(~ismember(f{i}, indVertices), 2) == 0; 0079 end 0080 f2 = f(indFaces, :); 0081 0082 % re-label indices of face vertices (keeping horizontal index array) 0083 for i = 1:length(f2) 0084 f2{i} = refInds(f2{i})'; 0085 end 0086 end 0087 0088 if parser.Results.trimMesh 0089 [v2, f2] = trimMesh(v2, f2); 0090 end 0091 0092 varargout = formatMeshOutput(nargout, v2, f2);