ply_write.m 11.2 KB
Newer Older
Radim Tylecek's avatar
Radim Tylecek committed
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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
function ply_write ( Elements, Path, Format, Str )
%% PLY_WRITE writes 3D data as a PLY file.
%
%  Discussion:
%
%    PLY_WRITE(DATA,FILENAME) writes the structure DATA as a binary
%    PLY file.  Every field of DATA is interpreted as an element
%    and every subfield as an element property.  Each subfield of
%    property data must either be an array or a cell array of
%    arrays.  All property data in an element must have the same
%    length.
%
%    A common PLY data structure has the following fields:
%      DATA.vertex.x = x coordinates, [Nx1] real array
%      DATA.vertex.y = y coordinates, [Nx1] real array
%      DATA.vertex.z = z coordinates, [Nx1] real array
%
%      DATA.face.vertex_indices = vertex index lists,
%         an {Mx1} cell array where each cell holds a one-
%         dimesional array (of any length) of vertex indices.
%
%    Some other common data fields:
%
%      DATA.vertex.nx = x coordinate of normal, [Nx1] real array
%      DATA.vertex.ny = y coordinate of normal, [Nx1] real array
%      DATA.vertex.nz = z coordinate of normal, [Nx1] real array
%
%      DATA.edge.vertex1 = index to a vertex, [Px1] integer array
%      DATA.edge.vertex2 = second vertex index, [Px1] integer array
%
%    Many other fields and properties can be added.  The PLY format
%    is not limited to the naming in the examples above -- they are
%    only the conventional naming.
%
%    PLY_WRITE(DATA,FILENAME,FORMAT) write the PLY with a specified
%    data format, where FORMAT is
%      'ascii'                  ASCII text data
%      'binary_little_endian'   binary data, little endian
%      'binary_big_endian'      binary data, big endian (default)
%
%    PLY_WRITE(DATA,FILENAME,FORMAT,'double') or
%
%    PLY_WRITE(DATA,FILENAME,'double') write floating-point data as
%    double precision rather than in the default single precision.
%
%  Example:
%
%    % make a cube
%    clear Data;
%    Data.vertex.x = [0;0;0;0;1;1;1;1];
%    Data.vertex.y = [0;0;1;1;0;0;1;1];
%    Data.vertex.z = [0;1;1;0;0;1;1;0];
%    Data.face.vertex_indices = {[0,1,2,3],[7,6,5,4], ...
%         [0,4,5,1],[1,5,6,2],[2,6,7,3],[3,7,4,0]};
%    plywrite(Data,'cube.ply','ascii');
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    01 July 2016    Seth Billings   bug fix: 'ushort' / 'uint16' data type was
%                                    not included in the min/max value arrays; now
%                                    excluding 'ushort' from the integer data types 
%                                    (rather than fixing the min/max arrays), since
%                                    some programs such as MeshLab do not handle the 
%                                    'ushort' data type properly anyway
%
%    01 July 2016    Seth Billings   bug fixes for max data value of 'short' / 'int16' 
%                                    data type and for selection of output data type
%    01 March 2007
%
%  Author:
%
%    Pascal Getreuer 2004
%    Seth Billings 2016
%
  if ( nargin < 4 )

    Str = '';

    if ( nargin < 3 )
      Format = 'binary_big_endian';
    elseif ( strcmpi ( Format, 'double' ) )
      Str = 'double';
      Format = 'binary_big_endian';
    end

  end

  [ fid, Msg ] = fopen ( Path, 'wt' );

  if ( fid == -1 )
    error(Msg);
  end
%
%  Bug Fix: Seth Billings
%  - correcting max integer value for 'short' / 'int16' from 2^16-1 to 2^15-1
%  - excluding 'ushort' from integer types since MeshLab does not
%    read 'ushort' data type properly.
%
  PlyTypeNames = {'char','uchar','short','int','uint','float','double', ...
    'char8','uchar8','short16','int32','uint32','float32','double64'};
  FWriteTypeNames = {'schar','uchar','int16','int32','uint32','single','double'};
  MatlabTypeNames = {'int8','uint8','int16','int32','uint32','single','double'};
  PrintfTypeChar = {'%d','%u','%d','%d','%u','%-.6f','%-.14e'};
  IntegerDataMin =  [-128,   0, -2^15,  -2^31,   0];
  IntegerDataMax =  [ 127, 255,  2^15-1, 2^31-1, 2^32-1];
  numTypes = length(MatlabTypeNames);
  
  %PlyTypeNames = {'char','uchar','short','ushort','int','uint','float','double', ...
  %  'char8','uchar8','short16','ushort16','int32','uint32','float32','double64'};
  %FWriteTypeNames = {'schar','uchar','int16','uint16','int32','uint32','single','double'};
  %MatlabTypeNames = {'int8','uint8','int16','uint16','int32','uint32','single','double'};
  %PrintfTypeChar = {'%d','%u','%d','%u','%d','%u','%-.6f','%-.14e'};  
  %IntegerDataMin = [-128, 0, -2^15, -2^31, 0];
  %IntegerDataMax = [127, 255, 2^16-1, 2^31-1, 2^32-1];
%
%  Write the PLY header.
%
  fprintf(fid,'ply\nformat %s 1.0\ncomment created by MATLAB ply_write\n',Format);
  ElementNames = fieldnames(Elements);
  NumElements = length(ElementNames);
  Data = cell(NumElements,1);

  for i = 1 : NumElements

    eval(['tmp=isa(Elements.',ElementNames{i},',''struct'');']);

    if ( tmp )
      eval(['PropertyNames{i}=fieldnames(Elements.',ElementNames{i},');']);
    else
      PropertyNames{i} = [];
    end

    if ( ~isempty(PropertyNames{i}) )
      eval(['Data{i}{1}=Elements.',ElementNames{i},'.',PropertyNames{i}{1},';']);
      ElementCount(i) = prod(size(Data{i}{1}));
      Type{i} = zeros(length(PropertyNames{i}),1);
    else
      ElementCount(i) = 0;
    end

    fprintf(fid,'element %s %u\n',ElementNames{i},ElementCount(i));

    for j = 1 : length(PropertyNames{i})

      eval(['Data{i}{j}=Elements.',ElementNames{i},'.',PropertyNames{i}{j},';']);

      if ( ElementCount(i) ~= prod(size(Data{i}{j})) )
        fclose(fid);
        error('All property data in an element must have the same length.');
      end

      if ( iscell(Data{i}{j}) )
        Type{i}(j) = numTypes + 1;  % Seth Billings
        %Type{i}(j) = 9;
        Data{i}{j} = Data{i}{j}{1};
      end

      for k = 1 : length(MatlabTypeNames)
        if ( isa(Data{i}{j},MatlabTypeNames{k}) )
          Type{i}(j) = Type{i}(j) + k;
          break;
        end
      end

      if ( ~rem(Type{i}(j),numTypes+1) ) % Seth Billings
      %if ( ~rem(Type{i}(j),9) )
        fclose(fid);
        error('Unsupported data structure.');
      end
%
%  Try to convert float data to integer data
%
%  Array data.
%
      if ( Type{i}(j) <= numTypes)  % Seth Billings
      %if ( Type{i}(j) <= 8 )
        if any(strcmp({'single','double'},MatlabTypeNames{Type{i}(j)}))
          if ~any(floor(Data{i}{j}) ~= Data{i}{j})  % data is integer
            MinValue = min(min(Data{i}{j}));
            MaxValue = max(max(Data{i}{j}));

            % choose smallest possible integer data format            
            % Bug Fix: Seth Billings
            tmp = min(find(MinValue >= IntegerDataMin & MaxValue <= IntegerDataMax));
            %tmp = max(min(find(MinValue >= IntegerDataMin)),min(find(MaxValue <= IntegerDataMax)));

            if ~isempty(tmp)
              Type{i}(j) = tmp;
            end
          end
        end
      else        % cell array data
        eval(['Data{i}{j}=Elements.',ElementNames{i},'.',PropertyNames{i}{j},';']);
        tmp = 1;

        for k = 1:prod(size(Data{i}{j}))
          tmp = tmp & all(floor(Data{i}{j}{k}) == Data{i}{j}{k});
        end

        if tmp  % data is integer
          MinValue = inf;
          MaxValue = -inf;

          for k = 1:prod(size(Data{i}{j}))
            MinValue = min(MinValue,min(Data{i}{j}{k}));
            MaxValue = max(MaxValue,max(Data{i}{j}{k}));
          end
%
%  choose smallest possible integer data format
%  Bug Fix: Seth Billings
%
          tmp = min(find(MinValue >= IntegerDataMin & MaxValue <= IntegerDataMax));
          %tmp = max(min(find(MinValue >= IntegerDataMin)),min(find(MaxValue <= IntegerDataMax)));

          if ( ~ isempty ( tmp ) )
            Type{i}(j) = tmp + numTypes + 1;
            %Type{i}(j) = tmp + 9;
          end

        end
      end

      % convert double to single if specified
      if rem(Type{i}(j),numTypes+1) == numTypes & ~strcmpi(Str,'double') % Seth Billings
      %if rem(Type{i}(j),9) == 8 & ~strcmpi(Str,'double')
        Type{i}(j) = Type{i}(j) - 1;
      end

      if Type{i}(j) <= numTypes  % Seth Billings
      %if Type{i}(j) <= 8
        fprintf(fid,'property %s %s\n',PlyTypeNames{Type{i}(j)},PropertyNames{i}{j});
      else
        fprintf(fid,'property list uchar %s %s\n',PlyTypeNames{Type{i}(j)-(numTypes+1)},PropertyNames{i}{j}); % Seth Billings
        %fprintf(fid,'property list uchar %s %s\n',PlyTypeNames{Type{i}(j)-9},PropertyNames{i}{j});
      end
    end
  end

  fprintf(fid,'end_header\n');

  switch Format
    case 'ascii'
      Format = 0;
    case 'binary_little_endian'
      fclose(fid);
      fid = fopen(Path,'a','ieee-le');
      Format = 1;
    case 'binary_big_endian'
      fclose(fid);
      fid = fopen(Path,'a','ieee-be');
      Format = 2;
  end

  for i = 1 : NumElements
    if ~isempty(PropertyNames{i})
      if ~Format          % write ASCII data
        for k = 1:ElementCount(i)
          for j = 1:length(PropertyNames{i})
            if Type{i}(j) <= numTypes  % Seth Billings
            %if Type{i}(j) <= 8
              fprintf(fid,[PrintfTypeChar{Type{i}(j)},' '],Data{i}{j}(k));
            else
              fprintf(fid,'%u%s ',length(Data{i}{j}{k}),sprintf([' ',PrintfTypeChar{Type{i}(j)-(numTypes+1)}],Data{i}{j}{k})); % Seth Billings
              %fprintf(fid,'%u%s ',length(Data{i}{j}{k}),sprintf([' ',PrintfTypeChar{Type{i}(j)-9}],Data{i}{j}{k}));
            end
          end

          fprintf(fid,'\n');
        end
      else            % write binary data
        if all(Type{i} <= numTypes) & all(Type{i} == Type{i}(1))  % Seth Billings
        %if all(Type{i} <= 8) & all(Type{i} == Type{i}(1))
          % property data without list types (fast)
          tmp = zeros(length(PropertyNames{i}),ElementCount(i));

          for j = 1:length(PropertyNames{i})
            tmp(j,:) = Data{i}{j}(:)';
          end

          fwrite(fid,tmp,FWriteTypeNames{Type{i}(j)});
        elseif all(Type{i} > numTypes) % Seth Billings
        %elseif all(Type{i} > 8)
        % only list types
          Type{i} = Type{i} - (numTypes+1); % Seth Billings
          %Type{i} = Type{i} - 9;

          if length(PropertyNames{i}) == 1
           % only one list property
            tmp = FWriteTypeNames{Type{i}(1)};

            for k = 1:ElementCount(i)
              fwrite(fid,length(Data{i}{1}{k}),'uchar');
              fwrite(fid,Data{i}{1}{k},tmp);
            end
          else
           % multiple list properties
            for k = 1:ElementCount(i)
              for j = 1:length(PropertyNames{i})
                fwrite(fid,length(Data{i}{j}{k}),'uchar');
                fwrite(fid,Data{i}{j}{k},FWriteTypeNames{Type{i}(j)});
              end
            end
          end
        else
        % mixed type
          for k = 1:ElementCount(i)
            for j = 1:length(PropertyNames{i})
              if Type{i}(j) <= numTypes % Seth Billings
              %if Type{i}(j) <= 8
                fwrite(fid,Data{i}{j}(k),FWriteTypeNames{Type{i}(j)});
              else
                fwrite(fid,length(Data{i}{j}{k}),'uchar');
                fwrite(fid,Data{i}{j}{k},FWriteTypeNames{Type{i}(j)-(numTypes+1)}); % Seth Billings
                %fwrite(fid,Data{i}{j}{k},FWriteTypeNames{Type{i}(j)-9});
              end
            end
          end
        end
      end
    end
  end

  fclose ( fid );

  return
end