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.

mesh2idtf(varargin)
function meshmodel = mesh2idtf(varargin)
% MESH2IDTF Create a multilayer mesh structure for use with save_idtf.
%
% Create a meshmodel structure for use with save_idtf.
%
% This is used to create a multilayer model object with transparency. This
% 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
% (mesh2idtf) simply makes a multilayer mesh structure for use with
% save_idtf (the modified save_idtf, rewritten by Francis Esmonde-White in April 2011).
%
% Usage:
%     meshmodel = mesh2idtf(varargin);
%
% 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




% if ~exist('modelname', 'var') || ~ischar(modelname) || length(modelname)<1
%     error('The model name must be specified as a character string');
% end

meshmodel.name = 'MODEL'; % Apparently the model name needs to be model for IDTFConverter.exe to function.

if numel(varargin) < 6
    error('mesh2idtf: \n You must specify at least one mesh \n You must have layer properties specified in sets of 6 inputs.\n They should be in the order:\n layername, resourcename, faces, vertices, face_vertex_data (mesh node colors), opacity',1);
end

i=0;

while numel(varargin)>0
    if mod(numel(varargin),6)
        error('mesh2idtf: \n You must have layer properties specified in sets of 6 inputs.\n They should be in the order:\n layername, resourcename, faces, vertices, face_vertex_data (mesh node colors), opacity',1);
    end
    
    i=i+1; % increment the layer count
    
    % % Extract the mesh model properties from varargin.
    % % They should be in the order:
    %       layername, resourcename, faces, vertices, face_vertex_data (mesh node colors), opacity
    
    % % 1
    layername = varargin{1};
    varargin(1) = [];
    if ~exist('layername', 'var') || ~ischar(layername) || length(layername)<1
        error('The layer name must be specified as a character string');
    end
    
    meshmodel.mesh(i).layername = layername;
    
    % % 2
    resourcename = varargin{1}; % 2
    varargin(1) = [];
    if ~exist('resourcename', 'var') || ~ischar(resourcename) || length(resourcename)<1
        error('The resource name must be specified as a character string');
    end
    
    meshmodel.mesh(i).resourcename = resourcename;
    
    % % 3
    faces = varargin{1}; % 3
    varargin(1) = [];
    if ~exist('faces', 'var') || size(faces,1)<2 || size(faces,2)~=3
        error('The faces array must be specified as a [n x 3] array of vertex indices');
    end
    
    meshmodel.mesh(i).f = faces;
    
    % % 4
    vertices = varargin{1}; % 4
    varargin(1) = [];
    if ~exist('vertices', 'var') || size(vertices,1)<2 || size(vertices,2)~=3
        error('The vertices array must be specified as a [m x 3] array of vertex [X, Y, Z] positions');
    end
    
    meshmodel.mesh(i).v = vertices;
    
    % % 5
    face_vertex_data = varargin{1}; % 5
    varargin(1) = [];
    if ~exist('face_vertex_data', 'var') || size(face_vertex_data,1)~=size(vertices,1)
        error('The face_vertex_data vertex color array must be specified as a [m x p] array of vertex colors');
    end
    
    meshmodel.mesh(i).colors = face_vertex_data;
    
    % % 6
    opacity = varargin{1}; % 6
    varargin(1) = [];
    if ~exist('opacity', 'var') || ~isnumeric(opacity) || length(opacity)~=1
        error('The opacity must be specified as a floating point number for each layer (range 0-1, where 1 is opaque, and 0 is completely transparent.)');
    end
    
    meshmodel.mesh(i).opacity = opacity;
    
end

Contact us