0001 function varargout = clipPoints3d(points, shape, varargin)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025 parser = inputParser;
0026 validStrings = {'box', 'sphere', 'plane'};
0027 addParameter(parser, 'shape', 'box', @(x) any(validatestring(x, validStrings)));
0028 addParameter(parser, 'inside', true, @islogical);
0029 parse(parser,varargin{:});
0030
0031 switch parser.Results.shape
0032 case 'box'
0033 LI = clipPointsByBox(points, shape);
0034 case 'plane'
0035 LI = clipPointsByPlane(points, shape);
0036 case 'sphere'
0037 LI = clipPointsBySphere(points, shape);
0038 end
0039
0040 if parser.Results.inside
0041
0042 ind = find(LI);
0043 else
0044
0045 ind = find(~LI);
0046 end
0047 points = points(ind, :);
0048
0049
0050 varargout{1} = points;
0051 if nargout == 2
0052 varargout{2} = ind;
0053 end
0054
0055 function LI = clipPointsByBox(points, box)
0056
0057 xmin = box(1);
0058 xmax = box(2);
0059 ymin = box(3);
0060 ymax = box(4);
0061 zmin = box(5);
0062 zmax = box(6);
0063
0064
0065 xOk = points(:,1) >= xmin & points(:,1) <= xmax;
0066 yOk = points(:,2) >= ymin & points(:,2) <= ymax;
0067 zOk = points(:,3) >= zmin & points(:,3) <= zmax;
0068
0069 LI = xOk & yOk & zOk;
0070 end
0071
0072 function LI = clipPointsByPlane(points, plane)
0073
0074 LI = isBelowPlane(points, plane);
0075 end
0076
0077 function LI = clipPointsBySphere(points, sphere)
0078
0079 LI = distancePoints3d(points, sphere(1:3)) <= sphere(4);
0080 end
0081
0082 end
0083