%% The code is written by Behnaz Pirzamanbein, bepi@dtu.dk last version 2020.08.04
%% Demo
close all
clear

addpath(genpath('./Functions'))

%% Defaults
% if you know the path otherwise choose the path using the 
answer = questdlg('Write data path or browse to the data folder?', 'Data Path','Write','Browse','Browse');
switch answer
    case 'Write'
        Data_path = '..\..\..\HCP Anywhere\LINX FP08.001\Activity 7 - bending - visualizations and tiff stacks\mat_file\';
    case 'Browse'
        Data_path = uigetdir;
        Data_path = fullfile(Data_path,'\');
        fprintf('Data Path: %s \n', Data_path)
end

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


%% extract poitns for the pipeline
clc
flag_points = 1; % 1 if no point has been extracted and first time use of the code
flag_save = 0; % 0: to not save the results; 1: to save the results

answer = inputdlg('Contrast Enhansed value:','Contrast',[1 50],{'.7 1.5'}); % this is default based on the data scanned at DTU
ce = str2num(answer{1});


if flag_points
    for i = 1
        sample = samples{i};
        for j = 1
            dg = degree{j};

            %% load data
            load([Data_path,'Processed_',sample,'_',dg])

            %% use the pre_processing to extract the points
            PointSelection
        end
    end
end

clearvars -except Data_path samples degree layer_name

%% layer segmentation and investigation
clc
answer = questdlg('Do you want to enhance the contrast? ','Contrast:','No','Yes','No');

for i = 1:4
    sample = samples{i};
    for j = 1:4
        %% load data
        dg = degree{j};

        fprintf('Load Data %s %s \n',sample,dg)
        load([Data_path,'Processed_',sample,'_',dg])
        
        %% use the pre_processing to extract the points
        
        % detect the layers
        layer_name = {'outer','inner'};
        
        for l = 1:length(layer_name)
            flag_layer = layer_name{l};
            
            disp(['Layer Detection ',flag_layer])
            load(['Results\point\',flag_layer,'_points_',sample,'_',dg])
            
            Delta_LU = [3, 7];
            
            if any(strcmp(dg,'0') | strcmp(dg,'45'))
                range = 15:-1:-15;
            else
                switch flag_layer
                    case 'inner'
                        range = 30:-1:-35;
                    case 'outer'
                        range = 20:-1:-20;
                end
            end
            flag_save = 1;
            flag_fig = 0; % if you want to check the results
            LayerDetection
        end
        
         %% measure
        disp('Measure ...')
        flag_save = 1;
        PrepMeasure
        
         %% registration
        disp('Registration ...')
        flag_save = 1;
        PrepRegistration
        
    end
end

%% visualize the measure
for i = 1:4
    flag_save = 1;
    Measure
end
close all

%% apply the registration
for i = 1:4
    flag_save = 1;
    Registration
end
close all

%% apply the alignment and transformation
for i = 1:4
    flag_save = 1;
    flag_fig = 1;
    Transformation
end
close all

%% convert the meshes
for i = 1:4
    Export2inp
end
close all