Skip to content

Instantly share code, notes, and snippets.

@j6k4m8
Last active August 29, 2015 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save j6k4m8/fce465ce15189cf2c5f1 to your computer and use it in GitHub Desktop.
Save j6k4m8/fce465ce15189cf2c5f1 to your computer and use it in GitHub Desktop.
RAMONVolume to .obj conversion
function RAMON2obj(matrix_or_ramon, id, show)
%% TO_OBJ Convert an annotation to a 3D .obj file
% For use with CAJAL (https://github.com/openconnectome/cajal)
% Pass an anno'd RAMONVolume or its data
% and this will export a file called
% `export-{id}.obj` that contains a mesh
% of the selected anno, where {id} is the
% id that was exported.
% TO_OBJ(matrix, id) Convert annotation #id from matrix.data to .obj
% TO_OBJ(matrix, id, 1) SLOW! Convert as above, but show first
if nargin < 3
show = 0;
end
if isa(matrix_or_ramon, 'RAMONVolume')
mat = matrix_or_ramon.data;
else
mat = matrix_or_ramon;
end
p = patch(isosurface(mat ~= id, 0));
n = isonormals(mat, p);
if show
p.FaceColor = 'red';
p.EdgeColor = 'none';
daspect([1,1,1])
view(3); axis tight
camlight
lighting gouraud
end
vs = p.Vertices;
fs = p.Faces;
f = fopen(['export-' int2str(id) '.obj'], 'w');
fprintf(f, ['# Generated by MATLAB RAMON2obj' ...
'\n# on ' date '. Script by @j6k4m8.\n']);
for ii = 1:length(vs)
fprintf(f, 'v %d %d %d\n', vs(ii,:));
end
for ii = 1:length(fs)
fprintf(f, 'f %d %d %d\n', fs(ii,:));
end
fclose(f);
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment