% The script generates an isosurface and srores it in STL file
% External functions used: stlwrite, mesh2tri and smoothpatch
% Alexander Teplukhin and Dmitri Babikov, 19/08/14

l = 128;                                    % Dimention L
m = 128;                                    % Dimention M
n = 128;                                    % Dimention N
vmax = 787.380;                             % Isoenergy Vmax
ifile = 'pes.dat';                          % Input filename
ofile = 'pes.stl';                          % Output filename

pot = importdata(ifile);                    % Load data
pot = reshape(pot,l,m,n);                   % Reshape to 3D arrray
pot(:,:,n) = 10000;                         % Set last layer to large energy to get closed figure
fv = isosurface(pot,vmax);                  % Generate isosurface
fv = smoothpatch(fv,1,10);                  % Add extra smoothness
fv.vertices(:,3) = fv.vertices(:,3) * 2;    % Stretch along N direction
stlwrite(ofile,fv,'facecolor',[0,0,31]);    % Save isosurface to the STL file
