geom2am.m 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. function geom2am(Geometry,AmiraMeshFile)
  2. % geom2am(Geometry,AmiraMeshFile) converts a geometry structure to an
  3. % AmiraMesh (.am) file. The Geometry structure has the form:
  4. %
  5. % Geometry =
  6. % start: [3x1 double/single]
  7. % voxel_size: [3x1 double/single]
  8. % data: [MxNxQ double/single]
  9. %
  10. % AmiraMeshFile = name of resulting AmiraMesh file
  11. %
  12. % Ryan T Flynn 23 August 2007
  13. % check validity of inputs
  14. if ~isa(Geometry,'struct')
  15. error('Geometry must be a structure.');
  16. else % ensure that all of the required fields are present
  17. if ~isfield(Geometry,'start')
  18. error('Geometry must have a ''start'' field.');
  19. elseif prod(size(Geometry.start)) ~= 3 | ~isnumeric(Geometry.start)
  20. error('Geometry.start must be a 3-element vector.');
  21. end
  22. if ~isfield(Geometry,'voxel_size')
  23. error('Geometry must have a ''voxel_size'' field.');
  24. elseif prod(size(Geometry.start)) ~= 3 | ~isnumeric(Geometry.start)
  25. error('Geometry.voxel_size field must be a 3-element vector.');
  26. end
  27. if ~isfield(Geometry,'data')
  28. error('Geometry must have a ''data'' field.');
  29. elseif ~isnumeric(Geometry.start)
  30. error('Geometry.data field must be a numeric array.');
  31. end
  32. end
  33. if ~isa(AmiraMeshFile,'char')
  34. error('AmiraMeshFile must be a character array.');
  35. end
  36. Xcount = size(Geometry.data,1);
  37. Ycount = size(Geometry.data,2);
  38. Zcount = size(Geometry.data,3);
  39. inc = Geometry.voxel_size;
  40. % bounding box coordinate vectors
  41. boxstart = Geometry.start;
  42. boxend = boxstart + [inc(1)*(Xcount-1) inc(2)*(Ycount-1) inc(3)*(Zcount-1)];
  43. % conversion table for data types
  44. % 6 allowed types for Amira:
  45. % byte, short, ushort, int32, float, double
  46. % Figure out what the data type is
  47. for k=1:6
  48. if isa(Geometry.data,'int8')
  49. MatlabDataType = 'int8';
  50. AmiraDataType = 'byte';
  51. elseif isa(Geometry.data,'int16')
  52. MatlabDataType = 'int16';
  53. AmiraDataType = 'short';
  54. elseif isa(Geometry.data,'uint16')
  55. MatlabDataType = 'uint16';
  56. AmiraDataType = 'ushort';
  57. elseif isa(Geometry.data,'int32')
  58. MatlabDataType = 'int32';
  59. AmiraDataType = 'int32';
  60. elseif isa(Geometry.data,'float')
  61. MatlabDataType = 'float';
  62. AmiraDataType = 'float';
  63. elseif isa(Geometry.data,'double')
  64. MatlabDataType = 'double';
  65. AmiraDataType = 'double';
  66. else
  67. error('Geometry.data data type is not supported by Amira.');
  68. end
  69. end
  70. % open the AmiraMesh file for writing text
  71. fid = fopen(AmiraMeshFile,'w');
  72. fprintf(fid,'# AmiraMesh 3D BINARY 2.0\n\n');
  73. fprintf(fid,['# CreationDate: ' datestr(now,'ddd mmm') datestr(now,' dd HH:MM:SS yyyy') '\n\n']);
  74. fprintf(fid,'define Lattice %g %g %g\n\n',Xcount,Ycount,Zcount);
  75. fprintf(fid,'Parameters {\n');
  76. fprintf(fid,' Content "%gx%gx%g %s, uniform coordinates",',Xcount,Ycount,Zcount,AmiraDataType);
  77. fprintf(fid,' BoundingBox %g %g %g %g %g %g,\n',boxstart(1),boxend(1),boxstart(2),boxend(2),boxstart(3),boxend(3));
  78. fprintf(fid,' CoordType "uniform"\n');
  79. fprintf(fid,'}\n\n');
  80. fprintf(fid,'Lattice { %s Data } @1\n\n',AmiraDataType);
  81. fprintf(fid,'# Data section follows\n');
  82. fprintf(fid,'@1\n');
  83. fclose(fid); % close the text part of the file
  84. % write the data in big endian format
  85. fid = fopen(AmiraMeshFile,'ab','ieee-be');
  86. fwrite(fid,Geometry.data,MatlabDataType);
  87. fclose(fid);