classdef AppStreamPC < matlab.apps.AppBase

    % Properties that correspond to app components
    properties (Access = public)
        UIFigure                      matlab.ui.Figure
        ObjectIdentificationPanel     matlab.ui.container.Panel
        TabGroup                      matlab.ui.container.TabGroup
        ProximityTab                  matlab.ui.container.Tab
        UIAxes_3                      matlab.ui.control.UIAxes
        ContrastTab                   matlab.ui.container.Tab
        Slider_3                      matlab.ui.control.Slider
        Slider_3Label                 matlab.ui.control.Label
        Slider_2                      matlab.ui.control.Slider
        Slider_2Label                 matlab.ui.control.Label
        Slider                        matlab.ui.control.Slider
        SliderLabel                   matlab.ui.control.Label
        UIAxes_2                      matlab.ui.control.UIAxes
        PointCloudVisualizationPanel  matlab.ui.container.Panel
        GetPointCloudButton           matlab.ui.control.Button
        UIAxesPointCloud              matlab.ui.control.UIAxes
        VideoStreamsPanel             matlab.ui.container.Panel
        StartStreamsButton            matlab.ui.control.Button
        UIAxesDEPTHStream             matlab.ui.control.UIAxes
        UIAxesRGBStream               matlab.ui.control.UIAxes
    end

    
    methods (Access = public)      

        
    end
    

    % Callbacks that handle component events
    methods (Access = private)

        % Button pushed function: StartStreamsButton
        function StartStreamsButtonPushed(app, event)
            global DEPTHdevice
            global RGBdevice

            RGBdevice = videoinput('kinect',1,'RGB_640x480');
            DEPTHdevice = videoinput('kinect',2,'Depth_640x480');

            %% RGB VIDEO STREAM
            global frameRGB
            global imRGB

            frameRGB = RGBdevice();
            imRGB = image(app.UIAxesRGBStream,zeros(size(frameRGB),'uint8'));

            axis(app.UIAxesRGBStream,'image');
            preview(RGBdevice,imRGB);

            % RGB VIDEO STREAM ASPECT RATIO
            app.UIAxesRGBStream.XLim = [0,640];
            app.UIAxesRGBStream.YLim = [0,480];
            app.UIAxesRGBStream.XTick = [];
            app.UIAxesRGBStream.YTick = [];
            pbaspect(app.UIAxesRGBStream,[640,480,1]);

            %% DEPTH Video Stream
            global frameDEPTH
            global depthImage

            % DEPTH VIDEO STREAM ASPECT RATIO
            app.UIAxesDEPTHStream.XLim = [0,640];
            app.UIAxesDEPTHStream.YLim = [0,480];
            app.UIAxesDEPTHStream.XTick = [];
            app.UIAxesDEPTHStream.YTick = [];
            pbaspect(app.UIAxesDEPTHStream,[640,480,2]);

            frameDEPTH = getsnapshot(DEPTHdevice);
            depthImage = image(app.UIAxesDEPTHStream,zeros(size(frameDEPTH),'uint16'));

            axis(app.UIAxesDEPTHStream,'image');
            preview(DEPTHdevice,depthImage);
            colormap(app.UIAxesDEPTHStream,'hsv')
        end

        % Button pushed function: GetPointCloudButton
        function GetPointCloudButtonPushed(app, event)
            global DEPTHdevice
            global RGBdevice
            global frameDEPTH

            RGBFrame = getsnapshot(RGBdevice);
           
            %% NUVEM DE PONTOS COMPLETA
            % EXTRAIR A NUVEM DE PONTOS
            ptCloudOriginal = pcfromkinect(DEPTHdevice, frameDEPTH, RGBFrame);       % CRIAR NUVEM DE PONTOS

            figure
            pcshow(ptCloudOriginal,"VerticalAxis","Y",VerticalAxisDir="Down")
            title('Original Point Cloud')
            xlabel('X(m)')
            ylabel('Y(m)')
            zlabel('Z(m)')
        end
    end

    % Component initialization
    methods (Access = private)

        % Create UIFigure and components
        function createComponents(app)

            % Create UIFigure and hide until all components are created
            app.UIFigure = uifigure('Visible', 'off');
            app.UIFigure.AutoResizeChildren = 'off';
            app.UIFigure.Position = [100 100 1699 876];
            app.UIFigure.Name = 'MATLAB App';
            app.UIFigure.Resize = 'off';
            app.UIFigure.HandleVisibility = 'on';

            % Create VideoStreamsPanel
            app.VideoStreamsPanel = uipanel(app.UIFigure);
            app.VideoStreamsPanel.AutoResizeChildren = 'off';
            app.VideoStreamsPanel.Title = 'Video Streams';
            app.VideoStreamsPanel.Position = [30 345 374 509];

            % Create UIAxesRGBStream
            app.UIAxesRGBStream = uiaxes(app.VideoStreamsPanel);
            title(app.UIAxesRGBStream, 'RGB Stream')
            app.UIAxesRGBStream.Position = [59 286 268 185];

            % Create UIAxesDEPTHStream
            app.UIAxesDEPTHStream = uiaxes(app.VideoStreamsPanel);
            title(app.UIAxesDEPTHStream, 'Depth Stream')
            app.UIAxesDEPTHStream.Position = [60 74 268 185];

            % Create StartStreamsButton
            app.StartStreamsButton = uibutton(app.VideoStreamsPanel, 'push');
            app.StartStreamsButton.ButtonPushedFcn = createCallbackFcn(app, @StartStreamsButtonPushed, true);
            app.StartStreamsButton.Position = [145 23 100 22];
            app.StartStreamsButton.Text = 'Start Streams';

            % Create PointCloudVisualizationPanel
            app.PointCloudVisualizationPanel = uipanel(app.UIFigure);
            app.PointCloudVisualizationPanel.AutoResizeChildren = 'off';
            app.PointCloudVisualizationPanel.Title = 'Point Cloud Visualization';
            app.PointCloudVisualizationPanel.Position = [434 419 596 434];

            % Create UIAxesPointCloud
            app.UIAxesPointCloud = uiaxes(app.PointCloudVisualizationPanel);
            title(app.UIAxesPointCloud, 'Point Cloud')
            app.UIAxesPointCloud.Position = [29 53 540 344];

            % Create GetPointCloudButton
            app.GetPointCloudButton = uibutton(app.PointCloudVisualizationPanel, 'push');
            app.GetPointCloudButton.ButtonPushedFcn = createCallbackFcn(app, @GetPointCloudButtonPushed, true);
            app.GetPointCloudButton.Position = [250 13 100 22];
            app.GetPointCloudButton.Text = 'Get Point Cloud';

            % Create ObjectIdentificationPanel
            app.ObjectIdentificationPanel = uipanel(app.UIFigure);
            app.ObjectIdentificationPanel.AutoResizeChildren = 'off';
            app.ObjectIdentificationPanel.Title = 'Object Identification';
            app.ObjectIdentificationPanel.Position = [1055 419 596 434];

            % Create TabGroup
            app.TabGroup = uitabgroup(app.ObjectIdentificationPanel);
            app.TabGroup.AutoResizeChildren = 'off';
            app.TabGroup.Position = [1 0 597 416];

            % Create ProximityTab
            app.ProximityTab = uitab(app.TabGroup);
            app.ProximityTab.AutoResizeChildren = 'off';
            app.ProximityTab.Title = 'Proximity';

            % Create UIAxes_3
            app.UIAxes_3 = uiaxes(app.ProximityTab);
            title(app.UIAxes_3, 'Title')
            app.UIAxes_3.Position = [0 71 597 321];

            % Create ContrastTab
            app.ContrastTab = uitab(app.TabGroup);
            app.ContrastTab.AutoResizeChildren = 'off';
            app.ContrastTab.Title = 'Contrast';

            % Create UIAxes_2
            app.UIAxes_2 = uiaxes(app.ContrastTab);
            title(app.UIAxes_2, 'Title')
            app.UIAxes_2.Position = [0 71 597 321];

            % Create SliderLabel
            app.SliderLabel = uilabel(app.ContrastTab);
            app.SliderLabel.HorizontalAlignment = 'right';
            app.SliderLabel.Position = [55 49 36 22];
            app.SliderLabel.Text = 'Slider';

            % Create Slider
            app.Slider = uislider(app.ContrastTab);
            app.Slider.Position = [112 58 87 3];

            % Create Slider_2Label
            app.Slider_2Label = uilabel(app.ContrastTab);
            app.Slider_2Label.HorizontalAlignment = 'right';
            app.Slider_2Label.Position = [230 49 36 22];
            app.Slider_2Label.Text = 'Slider';

            % Create Slider_2
            app.Slider_2 = uislider(app.ContrastTab);
            app.Slider_2.Position = [287 58 87 3];

            % Create Slider_3Label
            app.Slider_3Label = uilabel(app.ContrastTab);
            app.Slider_3Label.HorizontalAlignment = 'right';
            app.Slider_3Label.Position = [405 49 36 22];
            app.Slider_3Label.Text = 'Slider';

            % Create Slider_3
            app.Slider_3 = uislider(app.ContrastTab);
            app.Slider_3.Position = [462 58 87 3];

            % Show the figure after all components are created
            app.UIFigure.Visible = 'on';
        end
    end

    % App creation and deletion
    methods (Access = public)

        % Construct app
        function app = AppStreamPC

            % Create UIFigure and components
            createComponents(app)

            % Register the app with App Designer
            registerApp(app, app.UIFigure)

            if nargout == 0
                clear app
            end
        end

        % Code that executes before app deletion
        function delete(app)

            % Delete UIFigure when app is deleted
            delete(app.UIFigure)
        end
    end
end