%% Demo 3 - creating meshes from simple geometries

%% creating a simple brick-shaped surface

corner1=[0 0 0];        % a box is defined by the two diagonals
corner2=[40,30,20]
density=2;  %%<--- Hands-on suggestions: change this number and see the difference
maxvol=10;  %%<--- Hands-on suggestions: change this number and see the difference

[nbox,fbox,ebox]=meshabox(corner1,corner2,density,maxvol);

[nbox,fbox]=removeisolatednode(nbox,fbox);
plotmesh(nbox,fbox,'x>20');


%% creating a simple spherical surface
c0=[10 11 15];           % center of the sphere
radius=20;               % radius of the sphere                      
[nsph,fsph]=meshasphere(c0,radius,density,maxvol);
[nsph,fsph]=removeisolatednode(nsph,fsph);
figure;
plotmesh(nsph,fsph,'x>10');

%% combining the two surfaces

% first, we want to orient the two surfaces consistently
[nbox,fbox]=meshcheckrepair(nbox,fbox);
[nsph,fsph]=meshcheckrepair(nsph,fsph);

[n1,f1]=mergesurf(nbox,fbox,nsph,fsph);

%%%% alternatively, you can use surfboolean 
%% hands-on 
% [n1,f1]=surfboolean(nbox,fbox,'and',nsph,fsph(:,[2 1 3]));
% plotmesh(n1,f1)
% 
% [n1,f1]=surfboolean(nbox,fbox,'first',nsph,fsph(:,[2 1 3]));
% plotmesh(n1,f1)


%% Create a volumetric mesh, and define labels

seeds=[c0; corner2-1];     % this line defines two seeds, the 1st seeds for label 1, and 2nd seed for region 2
[ncomb,ecomb]=s2m(n1,f1,1,10,'tetgen',seeds);

%% Plot the final volumetric mesh

plotmesh(ncomb,ecomb)
figure;
plotmesh(ncomb,ecomb,'z<10')


