clear all
%close all
clc

%% Data Settings
     NameOfTheFile  = 'modelyMATLAB.xlsb';
     NameOfTheSheet = 'Zapastie';
NumberOfTimeSamples = 8;

 switch (NameOfTheSheet)
     case ('Usta')
         CoorX = 120:-1:-81;
         CoorY = linspace(-100,0,640);
     case ('Ukazovak')
         CoorX = 120:-1:-81;
         CoorY = linspace(430,530,640);
     case ('Zapastie')
         CoorX = 120:-1:-231;
         CoorY = linspace(430,530,640);
 end
%% Plotting
 Data = xlsread(NameOfTheFile,NameOfTheSheet);
 Data_index = 0;
 Model_index = 0;
 
 %getting model from raw data
 for i = 1:1:length(Data)
     if (Data(i,1) > 0 )
         Data_index = Data_index + 1;
         Model_index = 1;
     else
         Model( Model_index,Data_index) = Data(i,5);
         Model_index = Model_index + 1;
     end
     
 end
 
 % trajectory calculation
TrajectoryAverage = sum(Model,1) ./ sum(Model~=0,1); %calculate average value for each profil
TrajectoryMin = Model;
TrajectoryMin(TrajectoryMin==0) = NaN;
TrajectoryMin=min(TrajectoryMin);
TrajectoryMax=max(Model);
TrajectorySTDEV=[TrajectoryAverage; TrajectoryMin];
TrajectorySTDEV=std(TrajectorySTDEV);
Trajectory = [TrajectoryMin;TrajectoryAverage;1.15*TrajectoryMin + TrajectorySTDEV];
Trajectory = mean(Trajectory);

[Y Z] = meshgrid(CoorY, Trajectory);
TrajectoryGrid = Z';

for ix=1:1:length(CoorX)
    for iy=1:1:length(CoorY)
        if (Model(iy,ix)>= (TrajectoryGrid(iy,ix)-50))&&(Model(iy,ix)<= (TrajectoryGrid(iy,ix)+50))
            Coverage(iy,ix) = 1;
        else
            Coverage(iy,ix) = 0;
        end
    end
end
f = figure(1);
surf(CoorX,CoorY,Model)
set(gcf,'color','w');
f.CurrentAxes.ZDir = 'Reverse'
shading interp
if strcmp(NameOfTheSheet,'Usta')
    zlim([375 500])
    caxis([375 430])
end 
view(3);
title(['Isometric view, Model: ', NameOfTheSheet])
xlabel('X-Axis (mm)');
ylabel('Y-Axis (mm)');
zlabel('Z-axis (Scanner Distance) (mm)');

CoveragePercentage = (sum(sum(Coverage)))/(size(Coverage,1)*size(Coverage,2))
figure(2)
subplot(1,2,1)
colormap(flipud(redgreencmap))
image(Coverage .* 255)
view(180,90);
title(['Trajectory Coverage: ',num2str(CoveragePercentage*100),'%, Model: ', NameOfTheSheet])
set(gcf,'color','w');

subplot(1,2,2)
view(2);
surf(CoorX,CoorY,Model)
colormap default
set(gcf,'color','w');
shading interp
if strcmp(NameOfTheSheet,'Usta')
    zlim([375 500])
    caxis([375 430])
end 
view(2);
title(['Top view, Model: ', NameOfTheSheet])
xlabel('X-Axis (mm)');
ylabel('Y-Axis (mm)');
zlabel('Z-axis (Scanner Distance) (mm)');

xlabel('X-Axis (mm)');
ylabel('Y-Axis (mm)');
zlabel('Z-axis (Scanner Distance) (mm)');




 
