ISPOINTINMESH Check if a point is inside a 3D mesh. B = isPointInMesh(PT, V, F) Check if the point PT (given as a 1-by-3 array) is inside the mesh defined by the vertices V and the face array F. The result is a boolean. If PT is a N-by-3 point array, the result is a N-by-1 array of logical. Example [v, f] = torusMesh([50 50 50 30 10 30 45]); [x, y, z] = meshgrid(5:5:100, 5:5:100, 5:5:100); res = false(size(x)); res(:) = isPointInMesh([x(:) y(:) z(:)], v, f); figure; plot3(x(res), y(res), z(res), 'b.'); axis equal; Algorithm: The method computes the intersection with a ray starting from the point(s) and with a random orientation. Some errors are possible if rays crosses the mesh between two or three faces. See also meshes3d, intersectLineMesh3d
0001 function res = isPointInMesh(point, v, f, varargin) 0002 %ISPOINTINMESH Check if a point is inside a 3D mesh. 0003 % 0004 % B = isPointInMesh(PT, V, F) 0005 % Check if the point PT (given as a 1-by-3 array) is inside the mesh 0006 % defined by the vertices V and the face array F. The result is a 0007 % boolean. 0008 % 0009 % If PT is a N-by-3 point array, the result is a N-by-1 array of logical. 0010 % 0011 % Example 0012 % [v, f] = torusMesh([50 50 50 30 10 30 45]); 0013 % [x, y, z] = meshgrid(5:5:100, 5:5:100, 5:5:100); 0014 % res = false(size(x)); 0015 % res(:) = isPointInMesh([x(:) y(:) z(:)], v, f); 0016 % figure; plot3(x(res), y(res), z(res), 'b.'); axis equal; 0017 % 0018 % Algorithm: 0019 % The method computes the intersection with a ray starting from the 0020 % point(s) and with a random orientation. Some errors are possible if 0021 % rays crosses the mesh between two or three faces. 0022 % 0023 % See also 0024 % meshes3d, intersectLineMesh3d 0025 0026 % ------ 0027 % Author: David Legland 0028 % e-mail: david.legland@inra.fr 0029 % Created: 2018-01-26, using Matlab 9.3.0.713579 (R2017b) 0030 % Copyright 2018 INRA - Cepia Software Platform. 0031 0032 % choose a random vector 0033 vect = rand(1, 3); 0034 0035 % initialize array for result 0036 np = size(point, 1); 0037 res = false(np, 1); 0038 0039 % iterate over the various points 0040 for i = 1:np 0041 % disp(i); 0042 line = createLine3d(point(i,:), vect); 0043 0044 [inters, pos] = intersectLineMesh3d(line, v, f); %#ok<ASGLU> 0045 0046 res(i) = mod(sum(pos > 0), 2) > 0; 0047 end