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) ot 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) ot 0018 % not (TF=false). Default is false. 0019 % 0020 % 0021 % Example 0022 % [v, f] = createSoccerBall; 0023 % f = triangulateFaces(f); 0024 % box = [0 2 -1 2 -.5 2]; 0025 % [v2, f2] = clipMeshVertices(v, f, box, 'inside', false); 0026 % figure('color','w'); view(3); axis equal 0027 % drawMesh(v, f, 'faceColor', 'none', 'faceAlpha', .2); 0028 % drawBox3d(box) 0029 % drawMesh(v2, f2, 'faceAlpha', .7); 0030 % 0031 % See also 0032 % meshes3d, clipPoints3d 0033 % 0034 0035 % ------ 0036 % Author: David Legland, oqilipo 0037 % e-mail: david.legland@inra.fr 0038 % Created: 2011-04-07, using Matlab 7.9.0.529 (R2009b) 0039 % Copyright 2011 INRA - Cepia Software Platform. 0040 0041 % if input is given as a structure, parse fields 0042 if isstruct(v) 0043 if nargin > 2 0044 varargin = [b, varargin]; 0045 end 0046 b = f; 0047 f = v.faces; 0048 v = v.vertices; 0049 end 0050 0051 parser = inputParser; 0052 validStrings = {'box', 'sphere', 'plane'}; 0053 addParameter(parser, 'shape', 'box', @(x) any(validatestring(x, validStrings))); 0054 addParameter(parser, 'inside', true, @islogical); 0055 addParameter(parser, 'trimMesh', false, @islogical); 0056 parse(parser, varargin{:}); 0057 0058 % clip the vertices 0059 [v2, indVertices] = clipPoints3d(v, b,... 0060 'shape', parser.Results.shape, 'inside', parser.Results.inside); 0061 0062 % create index array for face indices relabeling 0063 refInds = zeros(size(indVertices)); 0064 for i = 1:length(indVertices) 0065 refInds(indVertices(i)) = i; 0066 end 0067 0068 % select the faces with all vertices within the box 0069 if isnumeric(f) 0070 % Faces given as numeric array 0071 indFaces = sum(~ismember(f, indVertices), 2) == 0; 0072 f2 = refInds(f(indFaces, :)); 0073 0074 elseif iscell(f) 0075 % Faces given as cell array 0076 nFaces = length(f); 0077 indFaces = false(nFaces, 1); 0078 for i = 1:nFaces 0079 indFaces(i) = sum(~ismember(f{i}, indVertices), 2) == 0; 0080 end 0081 f2 = f(indFaces, :); 0082 0083 % re-label indices of face vertices (keeping horizontal index array) 0084 for i = 1:length(f2) 0085 f2{i} = refInds(f2{i})'; 0086 end 0087 end 0088 0089 if parser.Results.trimMesh 0090 [v2, f2] = trimMesh(v2, f2); 0091 end 0092 0093 varargout = formatMeshOutput(nargout, v2, f2);