1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- function geom2am(Geometry,AmiraMeshFile)
- % geom2am(Geometry,AmiraMeshFile) converts a geometry structure to an
- % AmiraMesh (.am) file. The Geometry structure has the form:
- %
- % Geometry =
- % start: [3x1 double/single]
- % voxel_size: [3x1 double/single]
- % data: [MxNxQ double/single]
- %
- % AmiraMeshFile = name of resulting AmiraMesh file
- %
- % Ryan T Flynn 23 August 2007
- % check validity of inputs
- if ~isa(Geometry,'struct')
- error('Geometry must be a structure.');
- else % ensure that all of the required fields are present
- if ~isfield(Geometry,'start')
- error('Geometry must have a ''start'' field.');
- elseif prod(size(Geometry.start)) ~= 3 | ~isnumeric(Geometry.start)
- error('Geometry.start must be a 3-element vector.');
- end
- if ~isfield(Geometry,'voxel_size')
- error('Geometry must have a ''voxel_size'' field.');
- elseif prod(size(Geometry.start)) ~= 3 | ~isnumeric(Geometry.start)
- error('Geometry.voxel_size field must be a 3-element vector.');
- end
- if ~isfield(Geometry,'data')
- error('Geometry must have a ''data'' field.');
- elseif ~isnumeric(Geometry.start)
- error('Geometry.data field must be a numeric array.');
- end
- end
- if ~isa(AmiraMeshFile,'char')
- error('AmiraMeshFile must be a character array.');
- end
- Xcount = size(Geometry.data,1);
- Ycount = size(Geometry.data,2);
- Zcount = size(Geometry.data,3);
- inc = Geometry.voxel_size;
- % bounding box coordinate vectors
- boxstart = Geometry.start;
- boxend = boxstart + [inc(1)*(Xcount-1) inc(2)*(Ycount-1) inc(3)*(Zcount-1)];
- % conversion table for data types
- % 6 allowed types for Amira:
- % byte, short, ushort, int32, float, double
- % Figure out what the data type is
- for k=1:6
- if isa(Geometry.data,'int8')
- MatlabDataType = 'int8';
- AmiraDataType = 'byte';
- elseif isa(Geometry.data,'int16')
- MatlabDataType = 'int16';
- AmiraDataType = 'short';
- elseif isa(Geometry.data,'uint16')
- MatlabDataType = 'uint16';
- AmiraDataType = 'ushort';
- elseif isa(Geometry.data,'int32')
- MatlabDataType = 'int32';
- AmiraDataType = 'int32';
- elseif isa(Geometry.data,'float')
- MatlabDataType = 'float';
- AmiraDataType = 'float';
- elseif isa(Geometry.data,'double')
- MatlabDataType = 'double';
- AmiraDataType = 'double';
- else
- error('Geometry.data data type is not supported by Amira.');
- end
- end
- % open the AmiraMesh file for writing text
- fid = fopen(AmiraMeshFile,'w');
- fprintf(fid,'# AmiraMesh 3D BINARY 2.0\n\n');
- fprintf(fid,['# CreationDate: ' datestr(now,'ddd mmm') datestr(now,' dd HH:MM:SS yyyy') '\n\n']);
- fprintf(fid,'define Lattice %g %g %g\n\n',Xcount,Ycount,Zcount);
- fprintf(fid,'Parameters {\n');
- fprintf(fid,' Content "%gx%gx%g %s, uniform coordinates",',Xcount,Ycount,Zcount,AmiraDataType);
- fprintf(fid,' BoundingBox %g %g %g %g %g %g,\n',boxstart(1),boxend(1),boxstart(2),boxend(2),boxstart(3),boxend(3));
- fprintf(fid,' CoordType "uniform"\n');
- fprintf(fid,'}\n\n');
- fprintf(fid,'Lattice { %s Data } @1\n\n',AmiraDataType);
- fprintf(fid,'# Data section follows\n');
- fprintf(fid,'@1\n');
- fclose(fid); % close the text part of the file
- % write the data in big endian format
- fid = fopen(AmiraMeshFile,'ab','ieee-be');
- fwrite(fid,Geometry.data,MatlabDataType);
- fclose(fid);
|