|
|
function plywrite(fn, shape, tex, tl) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
error(nargchk(4, 4, nargin)); |
|
|
|
|
|
[fid, message] = fopen(fn, 'w'); |
|
|
if fid < 0, error(['Cannot open the file ' fn '\n' message]); end |
|
|
|
|
|
nver = numel(shape)/3; |
|
|
nface = numel(tl)/3; |
|
|
|
|
|
|
|
|
fprintf(fid, 'ply\n'); |
|
|
fprintf(fid, 'format binary_little_endian 1.0\n'); |
|
|
fprintf(fid, 'comment Made from the 3D Morphable Face Model of the Univeristy of Basel, Switzerland.\n'); |
|
|
fprintf(fid, 'element vertex %d\n', nver); |
|
|
fprintf(fid, 'property float %s\n', 'x', 'y', 'z'); |
|
|
fprintf(fid, 'property uchar %s\n', 'red', 'green', 'blue'); |
|
|
fprintf(fid, 'element face %d\n', nface); |
|
|
fprintf(fid, 'property list uchar int vertex_indices\n'); |
|
|
fprintf(fid, 'end_header\n'); |
|
|
|
|
|
|
|
|
for i=1:nver |
|
|
count = fwrite(fid, single(shape(3*(i-1)+1:3*i)), 'float32'); |
|
|
if count ~= 3 |
|
|
error('Error writing %s: %d elements were written instead of %d', fn, count, 3); |
|
|
end |
|
|
count = fwrite(fid, uint8(tex(3*(i-1)+1:3*i)), 'uchar'); |
|
|
if count ~= 3 |
|
|
error('Error writing %s: %d elements were written instead of %d', fn, count, 3); |
|
|
end |
|
|
end |
|
|
|
|
|
|
|
|
tl = tl-1; |
|
|
new_tl = zeros(3, size(tl,1), 'int32'); |
|
|
new_tl(1,:) = tl(:,2)'; |
|
|
new_tl(2,:) = tl(:,1)'; |
|
|
new_tl(3,:) = tl(:,3)'; |
|
|
nver_per_face = uint8(3); |
|
|
for i=1:nface |
|
|
fwrite(fid, nver_per_face, 'uchar'); |
|
|
count = fwrite(fid, new_tl(:,i), 'int32'); |
|
|
if count ~= 3 |
|
|
error('Error writing |
|
|
end |
|
|
end |
|
|
|
|
|
fclose(fid); |
|
|
|
|
|
|
|
|
|