Last active
August 29, 2015 14:26
-
-
Save j6k4m8/fce465ce15189cf2c5f1 to your computer and use it in GitHub Desktop.
RAMONVolume to .obj conversion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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