%% carregar point cloud
ptGridAvgDownS = % nuvem de pontos a introduzir

figcrop = figure("Name",'Crop Area Selection','NumberTitle','off');
plot(ptGridAvgDownS.Location(:,1),ptGridAvgDownS.Location(:,2),'.k')
set(gca,'ydir','reverse')
axis(gca,"equal")
xlim(gca,[min(ptGridAvgDownS.Location(:,1))-0.01 max(ptGridAvgDownS.Location(:,1))+0.01])
ylim(gca,[min(ptGridAvgDownS.Location(:,2))-0.01 max(ptGridAvgDownS.Location(:,2))+0.01])

% DESENHAR QUADRADO
h2 = drawrectangle;
wait(h2)

% RETIRAR COORDENADAS DOS PONTOS DENTRO DO QUADRADO
squareCropped_xmin = min(h2.Vertices(:,1));
squareCropped_xmax = max(h2.Vertices(:,1));
squareCropped_ymin = min(h2.Vertices(:,2));
squareCropped_ymax = max(h2.Vertices(:,2));

% PONTOS DENTRO DO QUADRADO
CroppedPoints = find(ptGridAvgDownS.Location(:,1)>squareCropped_xmin & ptGridAvgDownS.Location(:,1)<squareCropped_xmax & ...
    ptGridAvgDownS.Location(:,2)>squareCropped_ymin & ptGridAvgDownS.Location(:,2)<squareCropped_ymax );

CroppedPoints = ptGridAvgDownS.Location(CroppedPoints,:);

close(figure(figcrop))

% NUVEM RECORTADA
figure
plot3(CroppedPoints(:,1),CroppedPoints(:,3),-CroppedPoints(:,2),'k.')
title('Point Cloud Downsampled')
xlabel('X(m)')
ylabel('Y(m)')
zlabel('Z(m)')
axis("equal")
grid("on")

% FAZER PLOT DA PARTE RECORTADA NO NAVEGADOR
figure
plot3(xnuvem_down,znuvem_down,-ynuvem_down,'.k')
title('Editor Browser')
xlabel('X(m)')
ylabel('Y(m)')
zlabel('Z(m)')
axis("equal")
grid("on")
hold("on")

figure
CroppedArea = plot3(CroppedPoints(:,1),CroppedPoints(:,3),-CroppedPoints(:,2),'g.');
legend(CroppedArea,"Cropped Points",Location="southeastoutside")


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% remove points
CroppedPoints = ptGridAvgDownS.Location;

figremove = figure("Name",'Remove Points Area Selection','NumberTitle','off');
plot(CroppedPoints(:,1),CroppedPoints(:,2),'.k')
set(gca,'ydir','reverse')
axis(gca,"equal")
xlim(gca,[min(CroppedPoints(:,1))-0.01 max(CroppedPoints(:,1))+0.01])
ylim(gca,[min(CroppedPoints(:,2))-0.01 max(CroppedPoints(:,2))+0.01])

% DESENHAR QUADRADO
h3 = drawrectangle;
wait(h3)

% RETIRAR COORDENADAS DOS PONTOS DENTRO DO QUADRADO
squareRemovePoints_xmin = min(h3.Vertices(:,1));
squareRemovePoints_xmax = max(h3.Vertices(:,1));
squareRemovePoints_ymin = min(h3.Vertices(:,2));
squareRemovePoints_ymax = max(h3.Vertices(:,2));

% PONTOS DENTRO DO QUADRADO
RemovedPoints = find(CroppedPoints(:,1)>squareRemovePoints_xmin & CroppedPoints(:,1)<squareRemovePoints_xmax & ...
    CroppedPoints(:,2)>squareRemovePoints_ymin & CroppedPoints(:,2)<squareRemovePoints_ymax );

% PONTOS A RETIRAR
RemovedPoints_aRemover = CroppedPoints(RemovedPoints,:);

close(figure(figremove))

% REMOVER OS PONTOS A RETIRAR DA NUVEM
NPontosRemov = size(CroppedPoints,1);
PC_PointsRemoved = zeros(size(CroppedPoints,1),3);
for i=1:NPontosRemov
    if i ~= RemovedPoints
        PC_PointsRemoved(i,:) = CroppedPoints(i,:);
    end
end

PC_PointsRemoved(~any(PC_PointsRemoved,2),:) = [];

% FAZER PLOT DA NUVEM COM PONTOS REMOVIDOS
figure
plot3(PC_PointsRemoved(:,1),PC_PointsRemoved(:,3),-PC_PointsRemoved(:,2),'k.')
title('Point Cloud Downsampled')
xlabel('X(m)')
ylabel('Y(m)')
zlabel('Z(m)')
axis("equal")
grid("on")



% FAZER PLOT DO NAVEGADOR COM AS PARTES CROPPED E OS PONTOS REMOVIDOS
figure
plot3(xnuvem_down,znuvem_down,-ynuvem_down,'.k')
title('Editor Browser')
xlabel('X(m)')
ylabel('Y(m)')
zlabel('Z(m)')
axis("equal")
grid("on")
hold("on")

% PLOT NO NAVEGADOR
figure
if ButtonCropCloud == 1
    CroppedAreaPlot = plot3(CroppedPoints(:,1),CroppedPoints(:,3),-CroppedPoints(:,2),'g.');
    PointsRemovedPlot = plot3(RemovedPoints_aRemover(:,1),RemovedPoints_aRemover(:,3),-RemovedPoints_aRemover(:,2),'r.');
    legend([CroppedAreaPlot PointsRemovedPlot],{'Cropped Part' 'Removed Points'},Location="southeastoutside")
else
    PointsRemovedPlot = plot3(RemovedPoints_aRemover(:,1),RemovedPoints_aRemover(:,3),-RemovedPoints_aRemover(:,2),'r.');
    legend(PointsRemovedPlot,{'Removed Points'},Location="southeastoutside")
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% aumentar dens
ButtonSelectDensity = 1;

if ButtonCropCloud == 0 && ButtonRemovePoints == 0
    PC_ForDensityPoints = ptGridAvgDownS.Location;

elseif ButtonCropCloud == 1 && ButtonRemovePoints == 0
    PC_ForDensityPoints = CroppedPoints;

elseif ButtonCropCloud == 0 && ButtonRemovePoints == 1
    PC_ForDensityPoints = PC_PointsRemoved;

elseif ButtonCropCloud == 1 && ButtonRemovePoints == 1
    PC_ForDensityPoints = PC_PointsRemoved;

elseif ButtonCropCloud == 1 && ButtonRemovePoints == 1 && ButtonSelectUncompress == 1
    PC_ForDensityPoints = [PC_PointsUncom;PC_ForUncompPoints];

end

figdens = figure("Name",'Densify Area Selection','NumberTitle','off');
plot(PC_ForDensityPoints(:,1),PC_ForDensityPoints(:,2),'.k')
set(gca,'ydir','reverse')
axis(gca,"equal")
xlim(gca,[min(PC_ForDensityPoints(:,1))-0.01 max(PC_ForDensityPoints(:,1))+0.01])
ylim(gca,[min(PC_ForDensityPoints(:,2))-0.01 max(PC_ForDensityPoints(:,2))+0.01])

% DESENHAR QUADRADO
h4 = drawrectangle;
wait(h4)

% RETIRAR COORDENADAS DOS PONTOS DENTRO DO QUADRADO
squareDensityPoints_xmin = min(h4.Vertices(:,1));
squareDensityPoints_xmax = max(h4.Vertices(:,1));
squareDensityPoints_ymin = min(h4.Vertices(:,2));
squareDensityPoints_ymax = max(h4.Vertices(:,2));

% PONTOS DENTRO DO QUADRADO
DensityPoints = find(PC_ForDensityPoints(:,1)>squareDensityPoints_xmin & PC_ForDensityPoints(:,1)<squareDensityPoints_xmax & ...
    PC_ForDensityPoints(:,2)>squareDensityPoints_ymin & PC_ForDensityPoints(:,2)<squareDensityPoints_ymax );
% PONTOS A RETIRAR
DensityPoints_ARremover = PC_ForDensityPoints(DensityPoints,:);

close(figure(figdens))

% REMOVER OS PONTOS OS PONTOS DENTRO DO QUADRADO h4
NPontosDensity = size(PC_ForDensityPoints,1);
PC_PointsDensity = zeros(size(PC_ForDensityPoints,1),3);
for i=1:NPontosDensity
    if i ~= DensityPoints
        PC_PointsDensity(i,:) = PC_ForDensityPoints(i,:);
    end
end

PC_PointsDensity(~any(PC_PointsDensity,2),:) = [];

% FAZER PLOT DA NUVEM DA NUVEM SEM OS PONTOS DE DENSITY
plot3(PC_PointsDensity(:,1),PC_PointsDensity(:,3),-PC_PointsDensity(:,2),'k.')
title('Point Cloud Downsampled')

xlabel('X(m)')
ylabel('Y(m)')
zlabel('Z(m)')
axis("equal")
grid("on")

% BUSCAR OS PONTOS INICIAIS DA NUVEM ANTES DO DOWNSAMPLE PARA DAR MAIOR DENSIDADE AOS PONTOS
DensityPoints = find(ptObject.Location(:,1)>squareDensityPoints_xmin & ptObject.Location(:,1)<squareDensityPoints_xmax & ...
    ptObject.Location(:,2)>squareDensityPoints_ymin & ptObject.Location(:,2)<squareDensityPoints_ymax );

% PONTOS COM DENSIDADE MAXIMA
DensityPoints = ptObject.Location(DensityPoints,:);
hold("on")
gridStep6 = ;

% CRIAR NUVEM NOVA PARA FAZER NOVO DOWNSAMPLE
PCdens1 = pointCloud(DensityPoints);

% FAZER DOWNSAMPLE COM O VALOR INTRODUZIDO NO SLIDER
PCdenstest = pcdownsample(PCdens1,"gridAverage",gridStep6);

% FAZER PLOT DA NUVEM CRIADA ANTERIORMENTE (NUVEM COM NOVA DENSIDADE)
figure
plot3(PCdenstest.Location(:,1),PCdenstest.Location(:,3),-PCdenstest.Location(:,2),'.k')
title('Point Cloud Downsampled')
xlabel('X(m)')
ylabel('Y(m)')
zlabel('Z(m)')
grid("on")
axis("equal")


% FAZER PLOT NO NAVEGADOR COM AS LEGENDAS DE TODOS OS PONTOS ANTERIORES
figure
plot3(xnuvem_down,znuvem_down,-ynuvem_down,'.k')
title('Editor Browser')
xlabel('X(m)')
ylabel('Y(m)')
zlabel('Z(m)')
axis("equal")
grid("on")
hold("on")

% PLOT NO NAVEGADOR
figure
if ButtonCropCloud == 0 && ButtonRemovePoints == 0
    DensifyArea = plot3(DensityPoints_ARremover(:,1),DensityPoints_ARremover(:,3),-DensityPoints_ARremover(:,2),'b.');
    legend(DensifyArea,'Densified Area',Location="southeastoutside")

elseif ButtonCropCloud == 1 && ButtonRemovePoints == 0
    CroppedArea = plot3(CroppedPoints(:,1),CroppedPoints(:,3),-CroppedPoints(:,2),'g.');
    DensifyArea = plot3(DensityPoints_ARremover(:,1),DensityPoints_ARremover(:,3),-DensityPoints_ARremover(:,2),'b.');
    legend([CroppedArea DensifyArea],{'Cropped Part' 'Densified Area'},Location="southeastoutside")

elseif ButtonCropCloud == 1 && ButtonRemovePoints == 1
    CroppedArea = plot3(CroppedPoints(:,1),CroppedPoints(:,3),-CroppedPoints(:,2),'g.');
    PointsRemovedPlot = plot3(RemovedPoints_aRemover(:,1),RemovedPoints_aRemover(:,3),-RemovedPoints_aRemover(:,2),'r.');
    DensifyArea = plot3(DensityPoints_ARremover(:,1),DensityPoints_ARremover(:,3),-DensityPoints_ARremover(:,2),'b.');
    legend([CroppedArea PointsRemovedPlot DensifyArea],{'Cropped Part' 'Removed Points' 'Densified Area'},Location="southeastoutside")

elseif ButtonCropCloud == 0 && ButtonRemovePoints == 1
    PointsRemovedPlot = plot3(RemovedPoints_aRemover(:,1),RemovedPoints_aRemover(:,3),-RemovedPoints_aRemover(:,2),'r.');
    DensifyArea = plot3(DensityPoints_ARremover(:,1),DensityPoints_ARremover(:,3),-DensityPoints_ARremover(:,2),'b.');
    legend([PointsRemovedPlot DensifyArea],{'Removed Points' 'Densified Area'},Location="southeastoutside")

elseif ButtonCropCloud == 1 && ButtonRemovePoints == 1 && ButtonSelectUncompress == 1 && ButtonSelectDensity == 1
    CroppedArea = plot3(CroppedPoints(:,1),CroppedPoints(:,3),-CroppedPoints(:,2),'g.');
    PointsRemovedPlot = plot3(RemovedPoints_aRemover(:,1),RemovedPoints_aRemover(:,3),-RemovedPoints_aRemover(:,2),'r.');
    DensifyArea = plot3(DensityPoints_ARremover(:,1),DensityPoints_ARremover(:,3),-DensityPoints_ARremover(:,2),'b.');
    UncomArea = plot3(UncomPoints_aRemover(:,1),UncomPoints_aRemover(:,3),-UncomPoints_aRemover(:,2),'m.');
    legend([CroppedArea PointsRemovedPlot DensifyArea UncomArea],{'Cropped Part' 'Removed Points' 'Densified Area' 'Uncompressed Area'},Location="southeastoutside")
end


% CRIAR NUVEM NOVA PARA FAZER NOVO DOWNSAMPLE
PCdens = pointCloud(DensityPoints);

% FAZER DOWNSAMPLE COM O VALOR INTRODUZIDO NO SLIDER
PCdens_down = pcdownsample(PCdens,"gridAverage",gridStep4);

% PLOT DA NUVEM NO PLOT DA NUVEM DOWNSAMPLE
figure
plot3(PC_PointsDensity(:,1),PC_PointsDensity(:,3),-PC_PointsDensity(:,2),'k.')
title('Point Cloud Downsampled')
xlabel('X(m)')
ylabel('Y(m)')
zlabel('Z(m)')
axis("equal")
grid("on")
hold("on")

% FAZER PLOT DA NUVEM CRIADA ANTERIORMENTE (NUVEM COM NOVA DENSIDADE)
figure
plot3(PCdens_down.Location(:,1),PCdens_down.Location(:,3),-PCdens_down.Location(:,2),'.k')


figure
if ButtonCropCloud == 1 && ButtonRemovePoints == 1 && ButtonSelectUncompress == 1 && ButtonSelectDensity == 1
    CroppedArea = plot3(CroppedPoints(:,1),CroppedPoints(:,3),-CroppedPoints(:,2),'g.');
    PointsRemovedPlot = plot3(RemovedPoints_aRemover(:,1),RemovedPoints_aRemover(:,3),-RemovedPoints_aRemover(:,2),'r.');
    UncomArea = plot3(UncomPoints_aRemover(:,1),UncomPoints_aRemover(:,3),-UncomPoints_aRemover(:,2),'m.');
    DensifyArea = plot3(DensityPoints_ARremover(:,1),DensityPoints_ARremover(:,3),-DensityPoints_ARremover(:,2),'b.');
    legend([CroppedArea PointsRemovedPlot DensifyArea UncomArea],{'Cropped Part' 'Removed Points' 'Densified Area' 'Uncompressed Area'},Location="southeastoutside")
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% reduzir densidade de pontos
if ButtonCropCloud == 0 && ButtonRemovePoints == 0 && ButtonSelectDensity == 0
    PC_ForUncompPoints = ptGridAvgDownS.Location;

elseif ButtonCropCloud == 1 && ButtonRemovePoints == 0 && ButtonSelectDensity == 0
    PC_ForUncompPoints = CroppedPoints;

elseif ButtonCropCloud == 1 && ButtonRemovePoints == 1 && ButtonSelectDensity == 0
    PC_ForUncompPoints = PC_PointsRemoved;

elseif ButtonCropCloud == 1 && ButtonRemovePoints == 0 && ButtonSelectDensity == 1
    PC_ForUncompPoints = [CroppedPoints;PCdens_down.Location];

elseif ButtonCropCloud == 0 && ButtonRemovePoints == 0 && ButtonSelectDensity == 1
    PC_ForUncompPoints = [PC_PointsDensity;PCdens_down.Location];

elseif ButtonCropCloud == 0 && ButtonRemovePoints == 1 && ButtonSelectDensity == 0
    PC_ForUncompPoints = PC_PointsRemoved;

elseif ButtonCropCloud == 0 && ButtonRemovePoints == 1 && ButtonSelectDensity == 1
    PC_ForUncompPoints = [PC_ForDensityPoints;PCdens_down.Location];

elseif ButtonCropCloud == 1 && ButtonRemovePoints == 1 && ButtonSelectDensity == 1
    PC_ForUncompPoints = [PC_ForDensityPoints;PCdens_down.Location];

end

figuncomp = figure("Name",'Uncompress Area Selection','NumberTitle','off');
plot(PC_ForUncompPoints(:,1),PC_ForUncompPoints(:,2),'.k')
set(gca,'ydir','reverse')
axis(gca,"equal")
xlim(gca,[min(PC_ForUncompPoints(:,1))-0.01 max(PC_ForUncompPoints(:,1))+0.01])
ylim(gca,[min(PC_ForUncompPoints(:,2))-0.01 max(PC_ForUncompPoints(:,2))+0.01])

% DESENHAR QUADRADO
h5 = drawrectangle;
wait(h5)

% RETIRAR COORDENADAS DOS PONTOS DENTRO DO QUADRADO
squareUncomPoints_xmin = min(h5.Vertices(:,1));
squareUncomPoints_xmax = max(h5.Vertices(:,1));
squareUncomPoints_ymin = min(h5.Vertices(:,2));
squareUncomPoints_ymax = max(h5.Vertices(:,2));

% PONTOS DENTRO DO QUADRADO
UncomPoints = find(PC_ForUncompPoints(:,1)>squareUncomPoints_xmin & PC_ForUncompPoints(:,1)<squareUncomPoints_xmax & ...
    PC_ForUncompPoints(:,2)>squareUncomPoints_ymin & PC_ForUncompPoints(:,2)<squareUncomPoints_ymax );


UncomPoints_aRemover = find(xnuvem_down>squareUncomPoints_xmin & xnuvem_down<squareUncomPoints_xmax & ...
    ynuvem_down>squareUncomPoints_ymin & ynuvem_down<squareUncomPoints_ymax );

% PONTOS A RETIRAR
UncomPoints_aRemover = ptGridAvgDownS.Location(UncomPoints_aRemover,:);

close(figure(figuncomp))


% REMOVER OS PONTOS DENTRO DO QUADRADO h5
NPontosRemov = size(PC_ForUncompPoints,1);
PC_PointsUncom = zeros(size(PC_ForUncompPoints,1),3);
for i=1:NPontosRemov
    if i ~= UncomPoints
        PC_PointsUncom(i,:) = PC_ForUncompPoints(i,:);
    end
end

PC_PointsUncom(~any(PC_PointsUncom,2),:) = [];

% FAZAER PLOT DA NUVEM SEM OS PONTOS UNCOMPRESSED
figure
plot3(PC_PointsUncom(:,1),PC_PointsUncom(:,3),-PC_PointsUncom(:,2),'k.')
title('Point Cloud Downsampled')
xlabel('X(m)')
ylabel('Y(m)')
zlabel('Z(m)')
axis("equal")
grid("on")

% ENCONTRAR OS PONTOS DENTRO DO QUADRADO h5 DA NUVEM ORIGINAL DOWNSAMPLE
UncomPoints_PontosIniciais = find(ptGridAvgDownS.Location(:,1)>squareUncomPoints_xmin & ptGridAvgDownS.Location(:,1)<squareUncomPoints_xmax & ...
    ptGridAvgDownS.Location(:,2)>squareUncomPoints_ymin & ptGridAvgDownS.Location(:,2)<squareUncomPoints_ymax );

UncomPoints_PontosIniciais = ptGridAvgDownS.Location(UncomPoints_PontosIniciais,:);

% FAZER PLOT DOS PONTOS
hold("on")
plot3(UncomPoints_PontosIniciais(:,1),UncomPoints_PontosIniciais(:,3),-UncomPoints_PontosIniciais(:,2),'k.')
xlabel('X(m)')
ylabel('Y(m)')
zlabel('Z(m)')
grid("on")
axis("equal")


% FAZER PLOT DOS PONTOS UNCOMPRESS NO NAVEGADOR
figure
plot3(xnuvem_down,znuvem_down,-ynuvem_down,'.k')
title('Editor Browser')
xlabel('X(m)')
ylabel('Y(m)')
zlabel('Z(m)')
axis("equal")
grid("on")
hold("on")


figure
if ButtonCropCloud == 0 && ButtonRemovePoints == 0 && ButtonSelectDensity == 0
    UncomArea = plot3(UncomPoints_aRemover(:,1),UncomPoints_aRemover(:,3),-UncomPoints_aRemover(:,2),'m.');
    legend(UncomArea,'Uncompressed Area',Location="southeastoutside")

elseif ButtonCropCloud == 1 && ButtonRemovePoints == 0 && ButtonSelectDensity == 0
    CroppedArea = plot3(CroppedPoints(:,1),CroppedPoints(:,3),-CroppedPoints(:,2),'g.');
    UncomArea = plot3(UncomPoints_aRemover(:,1),UncomPoints_aRemover(:,3),-UncomPoints_aRemover(:,2),'m.');
    legend([CroppedArea UncomArea],{'Cropped Part' 'Uncompressed Area'},Location="southeastoutside")

elseif ButtonCropCloud == 1 && ButtonRemovePoints == 1 && ButtonSelectDensity == 0
    CroppedArea = plot3(CroppedPoints(:,1),CroppedPoints(:,3),-CroppedPoints(:,2),'g.');
    UncomArea = plot3(UncomPoints_aRemover(:,1),UncomPoints_aRemover(:,3),-UncomPoints_aRemover(:,2),'m.');
    PointsRemovedPlot = plot3(RemovedPoints_aRemover(:,1),RemovedPoints_aRemover(:,3),-RemovedPoints_aRemover(:,2),'r.');
    legend([CroppedArea PointsRemovedPlot UncomArea],{'Cropped Part' 'Removed Points' 'Uncompressed Area'},Location="southeastoutside")

elseif ButtonCropCloud == 1 && ButtonRemovePoints == 0 && ButtonSelectDensity == 1
    CroppedArea = plot3(CroppedPoints(:,1),CroppedPoints(:,3),-CroppedPoints(:,2),'g.');
    UncomArea = plot3(UncomPoints_aRemover(:,1),UncomPoints_aRemover(:,3),-UncomPoints_aRemover(:,2),'m.');
    DensifyArea = plot3(DensityPoints_ARremover(:,1),DensityPoints_ARremover(:,3),-DensityPoints_ARremover(:,2),'b.');
    legend([CroppedArea DensifyArea UncomArea],{'Cropped Part' 'Densified Area' 'Uncompressed Area'},Location="southeastoutside")

elseif ButtonCropCloud == 1 && ButtonRemovePoints == 1 && ButtonSelectDensity == 1
    CroppedArea = plot3(CroppedPoints(:,1),CroppedPoints(:,3),-CroppedPoints(:,2),'g.');
    PointsRemovedPlot = plot3(RemovedPoints_aRemover(:,1),RemovedPoints_aRemover(:,3),-RemovedPoints_aRemover(:,2),'r.');
    DensifyArea = plot3(DensityPoints_ARremover(:,1),DensityPoints_ARremover(:,3),-DensityPoints_ARremover(:,2),'b.');
    UncomArea = plot3(UncomPoints_aRemover(:,1),UncomPoints_aRemover(:,3),-UncomPoints_aRemover(:,2),'m.');
    legend([CroppedArea PointsRemovedPlot DensifyArea UncomArea],{'Cropped Part' 'Removed Points' 'Densified Area' 'Uncompressed Area'},Location="southeastoutside")

elseif ButtonCropCloud == 0 && ButtonRemovePoints == 0 && ButtonSelectDensity == 1
    DensifyArea = plot3(DensityPoints_ARremover(:,1),DensityPoints_ARremover(:,3),-DensityPoints_ARremover(:,2),'b.');
    UncomArea = plot3(UncomPoints_aRemover(:,1),UncomPoints_aRemover(:,3),-UncomPoints_aRemover(:,2),'m.');
    legend([DensifyArea UncomArea],{'Densified Area' 'Uncompressed Area'},Location="southeastoutside")

elseif ButtonCropCloud == 0 && ButtonRemovePoints == 1 && ButtonSelectDensity == 0
    UncomArea = plot3(UncomPoints_aRemover(:,1),UncomPoints_aRemover(:,3),-UncomPoints_aRemover(:,2),'m.');
    PointsRemovedPlot = plot3(RemovedPoints_aRemover(:,1),RemovedPoints_aRemover(:,3),-RemovedPoints_aRemover(:,2),'r.');
    legend([PointsRemovedPlot UncomArea],{'Removed Points' 'Uncompressed Area'},Location="southeastoutside")

elseif ButtonCropCloud == 0 && ButtonRemovePoints == 1 && ButtonSelectDensity == 1
    PointsRemovedPlot = plot3(RemovedPoints_aRemover(:,1),RemovedPoints_aRemover(:,3),-RemovedPoints_aRemover(:,2),'r.');
    DensifyArea = plot3(DensityPoints_ARremover(:,1),DensityPoints_ARremover(:,3),-DensityPoints_ARremover(:,2),'b.');
    UncomArea = plot3(UncomPoints_aRemover(:,1),UncomPoints_aRemover(:,3),-UncomPoints_aRemover(:,2),'m.');
    legend([PointsRemovedPlot DensifyArea UncomArea],{'Removed Points' 'Densified Area' 'Uncompressed Area'},Location="southeastoutside")
end


% CRIAR NOVA NUVEM PARA FAZER NOVO DOWNSAMPLE
PCuncomp = pointCloud(UncomPoints_aRemover);

% FAZER DOWNSAMPLE COM O VALOR INTRODUZIDO NO SLIDER
PCuncomp_down = pcdownsample(PCuncomp,"gridAverage",gridStep8);

% PLOT DA NUVEM NO PLOT DA NUVEM DOWNSAMPLE
figure
plot3(PC_PointsUncom(:,1),PC_PointsUncom(:,3),-PC_PointsUncom(:,2),'k.')
title('Point Cloud Downsampled')
xlabel('X(m)')
ylabel('Y(m)')
zlabel('Z(m)')
axis("equal")
grid("on")
hold("on")

% FAZER PLOT DA NUVEM CRIADA ANTERIORMENTE (NUVEM COM NOVA DENSIDADE)
figure
plot3(PCuncomp_down.Location(:,1),PCuncomp_down.Location(:,3),-PCuncomp_down.Location(:,2),'.k')

if ButtonSelectDensity == 1 || ButtonSelectUncompress == 1
    if ButtonCropCloud == 0 && ButtonRemovePoints == 0
        ResetPoints = ptGridAvgDownS.Location;

    elseif ButtonCropCloud == 1 && ButtonRemovePoints == 0
        ResetPoints = CroppedPoints;

    elseif ButtonCropCloud == 0 && ButtonRemovePoints == 1
        ResetPoints = PC_PointsRemoved;

    elseif ButtonCropCloud == 1 && ButtonRemovePoints == 1
        ResetPoints = PC_PointsRemoved;
    end
end

% FAZER PLOT DA NUVEM ORIGINAL DOWNSAMPLED
figure
plot3(ResetPoints(:,1),ResetPoints(:,3),-ResetPoints(:,2),'.k')
title('Point Cloud Downsampled')

xlabel('X(m)')
ylabel('Y(m)')
zlabel('Z(m)')

axis("equal")
grid("on")

% FAZER PLOT DA NUVEM ORIGINAL NO NAVEGADOR
figure
plot3(xnuvem_down,znuvem_down,-ynuvem_down,'.k')
xlabel('X(m)')
ylabel('Y(m)')
zlabel('Z(m)')
axis("equal")
grid("on")
hold("on")

figure
if ButtonCropCloud == 1 && ButtonRemovePoints == 1
    CroppedArea = plot3(CroppedPoints(:,1),CroppedPoints(:,3),-CroppedPoints(:,2),'g.');
    PointsRemovedPlot = plot3(RemovedPoints_aRemover(:,1),RemovedPoints_aRemover(:,3),-RemovedPoints_aRemover(:,2),'r.');
    legend([CroppedArea PointsRemovedPlot],{'Cropped Part' 'Removed Points'},Location="southeastoutside")

elseif ButtonCropCloud == 1 && ButtonRemovePoints == 0
    CroppedArea = plot3(CroppedPoints(:,1),CroppedPoints(:,3),-CroppedPoints(:,2),'g.');
    legend(CroppedArea,{'Cropped Part'},Location="southeastoutside")

elseif ButtonCropCloud == 0 && ButtonRemovePoints == 1
    PointsRemovedPlot = plot3(RemovedPoints_aRemover(:,1),RemovedPoints_aRemover(:,3),-RemovedPoints_aRemover(:,2),'r.');
    legend(PointsRemovedPlot,{'Removed Points'},Location="southeastoutside")
end



if ButtonCropCloud == 0 && ButtonRemovePoints == 0
    TotalNumberofPointsEditField.Value = ptGridAvgDownS.Count;

elseif ButtonCropCloud == 1 && ButtonRemovePoints == 0
    TotalNumberofPointsEditField.Value = size(CroppedPoints,1);

elseif ButtonCropCloud == 0 && ButtonRemovePoints == 1
    TotalNumberofPointsEditField.Value = size(PC_PointsRemoved,1);

elseif ButtonCropCloud == 1 && ButtonRemovePoints == 1
    TotalNumberofPointsEditField.Value = size(PC_PointsRemoved,1);
end

