%% export points to inp

samples = {'out_out_CD','out_out_MD','in_in_CD','in_in_MD'};
degree = {'0','45','90','180'};

load_folder_layers = 'Results\layer\transfer\align_0dg_rest\';

save_folder =  'Results\export\';
if ~exist(save_folder,'dir')
    mkdir(save_folder)
end

% if you want to run the measure separately you can use the for loop
%for i = 1
    sample = samples{i};
    disp(['Load Data ',sample])
    load([load_folder_layers,sample,'_aligned'])
    
    disp('Export mesh ...')
    f = parfeval(@elements_mesh,1,aligned_inner_dg0);
    ele = fetchOutputs(f);

    sz0 = size(aligned_inner_dg0);
    %0 dg
    no = [reshape(permute(aligned_inner_dg0(:,:,1,:),[2,1,3,4]),[sz0(1)*sz0(2)*sz0(4),1]), ...
          reshape(permute(aligned_inner_dg0(:,:,2,:),[2,1,3,4]),[sz0(1)*sz0(2)*sz0(4),1]), ...
          reshape(permute(aligned_inner_dg0(:,:,3,:),[2,1,3,4]),[sz0(1)*sz0(2)*sz0(4),1])];
    no = [(1:size(no,1))',no];
    
    disp('Write mesh inner 0 ...')
    parfeval(@write_mesh,0,no,ele,[save_folder,'inner_',sample,'_0']);
    
    no = [reshape(permute(aligned_outer_dg0(:,:,1,:),[2,1,3,4]),[sz0(1)*sz0(2)*sz0(4),1]), ...
          reshape(permute(aligned_outer_dg0(:,:,2,:),[2,1,3,4]),[sz0(1)*sz0(2)*sz0(4),1]), ...
          reshape(permute(aligned_outer_dg0(:,:,3,:),[2,1,3,4]),[sz0(1)*sz0(2)*sz0(4),1])];
    no = [(1:size(no,1))',no];

    disp('Write mesh outer 0 ...')
    parfeval(@write_mesh,0,no,ele,[save_folder,'outer_',sample,'_0']);
    
    % 45
    no = [reshape(permute(aligned_inner_dg45(:,:,1,:),[2,1,3,4]),[sz0(1)*sz0(2)*sz0(4),1]), ...
          reshape(permute(aligned_inner_dg45(:,:,2,:),[2,1,3,4]),[sz0(1)*sz0(2)*sz0(4),1]), ...
          reshape(permute(aligned_inner_dg45(:,:,3,:),[2,1,3,4]),[sz0(1)*sz0(2)*sz0(4),1])];
    no = [(1:size(no,1))',no];

    disp('Write mesh inner 45 ...')
    parfeval(@write_mesh,0,no,ele,[save_folder,'inner_',sample,'_45']);
    
    no = [reshape(permute(aligned_outer_dg45(:,:,1,:),[2,1,3,4]),[sz0(1)*sz0(2)*sz0(4),1]), ...
          reshape(permute(aligned_outer_dg45(:,:,2,:),[2,1,3,4]),[sz0(1)*sz0(2)*sz0(4),1]), ...
          reshape(permute(aligned_outer_dg45(:,:,3,:),[2,1,3,4]),[sz0(1)*sz0(2)*sz0(4),1])];
    no = [(1:size(no,1))',no];

    disp('Write mesh outer 45 ...')
    parfeval(@write_mesh,0,no,ele,[save_folder,'outer_',sample,'_45']);
    
    % 90
    no = [reshape(permute(aligned_inner_dg90(:,:,1,:),[2,1,3,4]),[sz0(1)*sz0(2)*sz0(4),1]), ...
          reshape(permute(aligned_inner_dg90(:,:,2,:),[2,1,3,4]),[sz0(1)*sz0(2)*sz0(4),1]), ...
          reshape(permute(aligned_inner_dg90(:,:,3,:),[2,1,3,4]),[sz0(1)*sz0(2)*sz0(4),1])];
    no = [(1:size(no,1))',no];

    disp('Write mesh inner 90 ...')
    parfeval(@write_mesh,0,no,ele,[save_folder,'inner_',sample,'_90']);
    
    no = [reshape(permute(aligned_outer_dg90(:,:,1,:),[2,1,3,4]),[sz0(1)*sz0(2)*sz0(4),1]), ...
          reshape(permute(aligned_outer_dg90(:,:,2,:),[2,1,3,4]),[sz0(1)*sz0(2)*sz0(4),1]), ...
          reshape(permute(aligned_outer_dg90(:,:,3,:),[2,1,3,4]),[sz0(1)*sz0(2)*sz0(4),1])];
    no = [(1:size(no,1))',no];

    disp('Write mesh outer 90 ...')
    parfeval(@write_mesh,0,no,ele,[save_folder,'outer_',sample,'_90']);
    
    % 180
    no = [reshape(permute(aligned_inner_dg180(:,:,1,:),[2,1,3,4]),[sz0(1)*sz0(2)*sz0(4),1]), ...
          reshape(permute(aligned_inner_dg180(:,:,2,:),[2,1,3,4]),[sz0(1)*sz0(2)*sz0(4),1]), ...
          reshape(permute(aligned_inner_dg180(:,:,3,:),[2,1,3,4]),[sz0(1)*sz0(2)*sz0(4),1])];
    no = [(1:size(no,1))',no];

    disp('Write mesh inner 180 ...')
    parfeval(@write_mesh,0,no,ele,[save_folder,'inner_',sample,'_180']);
    
    no = [reshape(permute(aligned_outer_dg180(:,:,1,:),[2,1,3,4]),[sz0(1)*sz0(2)*sz0(4),1]), ...
          reshape(permute(aligned_outer_dg180(:,:,2,:),[2,1,3,4]),[sz0(1)*sz0(2)*sz0(4),1]), ...
          reshape(permute(aligned_outer_dg180(:,:,3,:),[2,1,3,4]),[sz0(1)*sz0(2)*sz0(4),1])];
    no = [(1:size(no,1))',no];

    disp('Write mesh outer 180 s...')
    parfeval(@write_mesh,0,no,ele,[save_folder,'outer_',sample,'_180']);
    
%end