function build_ply(outfile,xyz,rgb)
%outfile is the binary ply'filename which you want to make;
%xyz is the N*3 matrix for point cloud's coordinate data;
%rgb is the color for every point respectively.
if(size(xyz,1)~=size(rgb,1))
error('Input xyz and rgb arguments should have same number of rows');
end
if(size(xyz,2)~=3 | size(rgb,2)~=3)
error('Input xyz aand rgb arguments should have 3 columns');
end
pt3d(:,1) = xyz(:,1);
pt3d(:,2) = xyz(:,2);
pt3d(:,3) = xyz(:,3);
pt3d=pt3d';
RGB(:,1) = uint8(rgb(:,1));
RGB(:,2) = uint8(rgb(:,2));
RGB(:,3) = uint8(rgb(:,3));
RGB=RGB';
dataChunk = [reshape(typecast(reshape(single(pt3d),1,[]),'uint8'),3*4,[]); RGB];
size(dataChunk,2)
file = writePLYhead(outfile, size(dataChunk,2));
fwrite(file, dataChunk,'uint8');
fclose(file);
function file = writePLYhead(PLYfilename, pointCount,faceCount)
%%
if ~exist('faceCount','var')
faceCount = 0;
end
%%
file = fopen(PLYfilename,'w');
fprintf (file, 'ply\n');
%fprintf (file, 'format ascii 1.0\n');
fprintf (file, 'format binary_little_endian 1.0\n');
fprintf (file, 'element vertex %d\n', pointCount);
fprintf (file, 'property float x\n');
fprintf (file, 'property float y\n');
fprintf (file, 'property float z\n');
fprintf (file, 'property uchar red\n');
fprintf (file, 'property uchar green\n');
fprintf (file, 'property uchar blue\n');
fprintf (file, 'element face %d\n',faceCount);
fprintf (file, 'property list uchar int vertex_indices\n');
fprintf (file, 'end_header\n');
return