classdef App_Limpo < matlab.apps.AppBase

    % Properties that correspond to app components
    properties (Access = public)
        UIFigure             matlab.ui.Figure
        PointCloudPanel      matlab.ui.container.Panel
        GetPointCloudButton  matlab.ui.control.Button
        UIAxes3              matlab.ui.control.UIAxes
        VideoStreamsPanel    matlab.ui.container.Panel
        StopStreamsButton    matlab.ui.control.Button
        StartStreamsButton   matlab.ui.control.Button
        UIAxes2              matlab.ui.control.UIAxes
        UIAxes               matlab.ui.control.UIAxes
    end

    
    methods (Access = private)      

        
       
    end


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

        % Button pushed function: StartStreamsButton
        function StartStreamsButtonPushed(app, event)
            %% CAMERA DEVICES
            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.UIAxes,zeros(size(frameRGB),'uint8'));

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

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

            %% DEPTH Video Stream
            global frameDEPTH
            global depthImage

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

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

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

        end

        % Callback function
        function GetPointCloudButtonPushed(app, event)
            
            
        end

        % Callback function
        function GetRGBImageButtonPushed(app, event)

        end

        % Button pushed function: GetPointCloudButton
        function GetPointCloudButtonPushed2(app, event)
            global DEPTHdevice
            global RGBdevice
            
            colorDevice = RGBdevice;
            depthDevice = DEPTHdevice;

            %% INICIAR AS CAMARAS
            colorDevice();
            depthDevice();

            %% CAPTURAR UM FRAMDE DOS DISPOSITIVOS
            colorImg = getsnapshot(colorDevice);
            depthImg = getsnapshot(depthDevice);

            %% EXTRAIR A NUVEM DE PONTOS
            ptCloud = pcfromkinect(depthDevice, depthImg, colorImg);

            xlabel(app.UIAxes3, 'X(m)');
            ylabel(app.UIAxes3, 'Y(m)');
            zlabel(app.UIAxes3, 'Z(m)');            

            % POINT CLOUD
            pcshow(ptCloud,'Parent',app.UIAxes3,'VerticalAxis','Y','VerticalAxisDir','Down');
            
        end

        % Button pushed function: StopStreamsButton
        function StopStreamsButtonPushed(app, event)
            
        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.Position = [100 100 1681 710];
            app.UIFigure.Name = 'MATLAB App';

            % Create VideoStreamsPanel
            app.VideoStreamsPanel = uipanel(app.UIFigure);
            app.VideoStreamsPanel.TitlePosition = 'centertop';
            app.VideoStreamsPanel.Title = 'Video Streams';
            app.VideoStreamsPanel.Position = [1 253 320 458];

            % Create UIAxes
            app.UIAxes = uiaxes(app.VideoStreamsPanel);
            title(app.UIAxes, 'RGB Video Stream')
            app.UIAxes.Position = [10 247 300 185];

            % Create UIAxes2
            app.UIAxes2 = uiaxes(app.VideoStreamsPanel);
            title(app.UIAxes2, 'Depth Video Stream')
            app.UIAxes2.Position = [10 51 300 185];

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

            % Create StopStreamsButton
            app.StopStreamsButton = uibutton(app.VideoStreamsPanel, 'push');
            app.StopStreamsButton.ButtonPushedFcn = createCallbackFcn(app, @StopStreamsButtonPushed, true);
            app.StopStreamsButton.Position = [184 18 100 22];
            app.StopStreamsButton.Text = 'Stop Streams';

            % Create PointCloudPanel
            app.PointCloudPanel = uipanel(app.UIFigure);
            app.PointCloudPanel.TitlePosition = 'centertop';
            app.PointCloudPanel.Title = 'Point Cloud';
            app.PointCloudPanel.Position = [321 253 575 458];

            % Create UIAxes3
            app.UIAxes3 = uiaxes(app.PointCloudPanel);
            title(app.UIAxes3, 'Point Cloud')
            app.UIAxes3.XTick = [0 0.2 0.4 0.6 0.8 1];
            app.UIAxes3.ZTick = [0 0.2 0.4 0.6 0.8 1];
            app.UIAxes3.ZTickLabel = {'0'; '0.2'; '0.4'; '0.6'; '0.8'; '1'};
            app.UIAxes3.Position = [32 50 512 382];

            % Create GetPointCloudButton
            app.GetPointCloudButton = uibutton(app.PointCloudPanel, 'push');
            app.GetPointCloudButton.ButtonPushedFcn = createCallbackFcn(app, @GetPointCloudButtonPushed2, true);
            app.GetPointCloudButton.Position = [238 18 100 22];
            app.GetPointCloudButton.Text = 'Get Point Cloud';

            % 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 = App_Limpo

            % 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