-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdownsampleCloud_voxelGrid.m
36 lines (27 loc) · 1.32 KB
/
downsampleCloud_voxelGrid.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
function [PCL_downsampled,downsampleIndices,full3Didx] = downsampleCloud_voxelGrid(PCL,voxelSize)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Downsample a point cloud using a voxel grid %
% %
% Inputs: %
% PCL - point cloud given as a MATLAB pointCloud object %
% voxelSize - length of cubic voxel side (scalar) %
% %
% Outputs: %
% PCL_downsampled - downsampled point cloud given as a %
% MATLAB pointCloud object %
% downsampleIndices - indices of remaining points %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Get point cloud x,y,z coordinates
xyz = PCL.Location;
%% Compute voxel grid dimensions
minXYZ = min(xyz);
maxXYZ = max(xyz);
M_XYZ = ceil((maxXYZ - minXYZ)/voxelSize);
%% Find indices of the voxel containing each point
xyzInds = floor(bsxfun(@minus,xyz,minXYZ)/voxelSize) + 1;
%% Convert indices triplets into linear indices such that each voxel is represented by a single integer instead of three
ind = sub2ind(M_XYZ,xyzInds(:,1),xyzInds(:,2),xyzInds(:,3));
%% Pseudo-randomly select a single point in each voxel
[~,downsampleIndices,full3Didx] = unique(ind);
PCL_downsampled = select(PCL,downsampleIndices);
return