Code covered by the BSD License  

Highlights from
Generate U3D files from STL models for making multilayer 3D PDF figures

image thumbnail

Generate U3D files from STL models for making multilayer 3D PDF figures

by

 

11 May 2011 (Updated )

Conversion of meshes into U3D files for making multilayer 3D PDF figures with transparency.

idtf2u3d(idtf_filename, u3d_filename)
function [] = idtf2u3d(idtf_filename, u3d_filename)
% IDTF2U3D Convert a multilayer mesh saved as an idtf file into U3D format.
%
% Create a U3D file from an IDTF file.
%
% This is used to create a U3D file for a multilayer model object with
% transparency. This U3D model can in turn be inserted into a PDF file using
% either Adobe Acrobat Pro or with latex using pdflatex and the movie15
% package. This function (idtf2u3d) simply makes a multilayer mesh U3D file
% from mesh2idtf and save_idtf (the modified save_idtf, rewritten by
% Francis Esmonde-White in April 2011).
%
% As with the code by Alexandre Gramfort, this function requires components
% from the Universal 3D Sample Software project. See:
% (http://sourceforge.net/projects/u3d/)
%
% These components include binary .exe and .dll files from
% http://sourceforge.net/projects/u3d/ in order to convert the
% .idtf files into .u3d files. Unfortunately the Mathworks no longer allows
% the binary files to be included in packages on the Mathworks File
% Exchange. Instead, it requires the IDTFConverter binaries to be downloaded and
% installed. I have repackaged these files into a small download (2 MB)
% available at my personal web site. To access these files, go to
% http://www.esmonde-white.com/home/diversions/matlab-program-for-making-3d-pdf-figures/win32_3dpdf.zip
% The binaries on my web site are from U3D_A_061228_5.zip (Gold Update 1.2)
% and were downloaded on May 11, 2011. I have included a matlab file to
% download and install these files directly. 
%
% Usage:
%     idtf2u3d(idtf_filename, u3d_filename);
%
% Example:
%     [bone.v, bone.f, bone.n, bone.c, bone.stltitle] = stlread('33091_bone_outer_002.stl'); %load the meshes
%     [skin.v, skin.f, skin.n, skin.c, skin.stltitle] = stlread('33091_WholeArm_002.stl');
% 
%     [bone.v, bone.f]=patchslim(bone.v, bone.f); % shrink the meshes a bit
%     [skin.v, skin.f]=patchslim(skin.v, skin.f);
% 
%     bone.c = repmat(hex2dec('D3')/255,[size(bone.v,1),3]); % Color the meshes... I'm using RGB hex colors (HTML tables are easy to find)
%     skin.c = repmat(hex2dec(['F4';'A4';'60'])'/255,[size(skin.v,1),1]);
% 
%     show_model(skin,'FaceColor',skin.c(1,:),'LineStyle','none','FaceAlpha',0.3); % this is a command I created to plot 3D meshes with less typing.
%     hold on;
%     show_model(bone,'LineStyle','none','FaceColor',repmat(hex2dec('D3')/255,[1,3]))
%     axis equal
%
%       % Turn the meshes into a multilayer mesh structure
%     meshmodel1 = mesh2idtf(...
%         'CorticalBone', 'CorticalBoneMesh', bone.f, bone.v, bone.c, 0.95, ... % bone layer, mostly opaque (opacity of 0.95)
%         'Skin', 'SkinMesh', skin.f, skin.v, skin.c, 0.35 ); % skin layer, mostly transparent (opacity of 0.35)
%
%     save_idtf('test2layer.idtf', meshmodel1) % Save the intermediate data file 
%
%     idtf2u3d('test2layer.idtf', 'test2layer.u3d') % create the U3D file.
%
% The properties for each layer should be specified in the following order:
% 	layername (Layer name in the PDF structure), resourcename, faces, vertices, face_vertex_data (mesh node colors), opacity
%
% See also stlread, patchslim, show_model, mesh2idtf, save_idtf, idtf2u3d.
%
% Written by Francis Esmonde-White, May 2011
% Based on 'Matlab mesh to PDF with 3D interactive object', written by Alexandre Gramfort on 2009-04-21.
% The original code is at http://www.mathworks.com/matlabcentral/fileexchange/25383-matlab-mesh-to-pdf-with-3d-interactive-object



idtf_filename  = fullfile(pwd,[idtf_filename]);
[idtfpathstr, idtfname, idtfext] = fileparts(idtf_filename);
if ~strcmp(idtfext, '.idtf')
    error('The idtf file name must end in .idtf');
end

% idtffile = tempname;
u3d_filename  = fullfile(pwd,[u3d_filename]);
[u3dpathstr, u3dname, u3dext] = fileparts(u3d_filename);
if ~strcmp(u3dext, '.u3d')
    error('The u3d file name must end in .u3d');
end

% save_idtf(idtffile,points,faces,face_vertex_data);
% strfind(idtf_filename,'.idtf');
copyfile(idtf_filename,fullfile(idtfpathstr,[idtfname,'_bak.idtf']));

mfiledir = fileparts(mfilename('fullpath'));

IDTFcmd = 'IDTFConverter';
if isunix
    error('This code is only written for windows at the moment, sorry!')
else % windows
    IDTFcmd = [IDTFcmd,'.exe'];
end



% current_dir = pwd; % skip this change directory business. Just call the command using
% the relative pathname instead.

try
    cmd = sprintf([IDTFcmd,' -input "%s" -output "%s"'],idtf_filename,u3d_filename);
    if ispc
        cmd = [mfiledir,'\win32\',cmd];
    end
    if ~exist([mfiledir,'\win32\',IDTFcmd],'file')
        disp('IDTFConverter.exe needed, downloading automatically.');
        installGoogleArchive();
    end
    
    disp(cmd);
    [status,result] = dos(cmd);
    disp(result);
catch
    rethrow(lasterror)
%     cd(current_dir)
end

Contact us