function varargout = GUI_test(varargin)
% GUI_TEST MATLAB code for GUI_test.fig
%      GUI_TEST, by itself, creates a new GUI_TEST or raises the existing
%      singleton*.
%
%      H = GUI_TEST returns the handle to a new GUI_TEST or the handle to
%      the existing singleton*.
%
%      GUI_TEST('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in GUI_TEST.M with the given input arguments.
%
%      GUI_TEST('Property','Value',...) creates a new GUI_TEST or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before GUI_test_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to GUI_test_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help GUI_test

% Last Modified by GUIDE v2.5 21-Mar-2022 14:48:59

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @GUI_test_OpeningFcn, ...
                   'gui_OutputFcn',  @GUI_test_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT

%%%%%%%%%%%%%%%%%%%% FUNÇÕES INICIAIS %%%%%%%%%%%%%%%%%%%%
% --- Executes just before GUI_test is made visible.
function GUI_test_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to GUI_test (see VARARGIN)

% Fazer reset à Kinect
% imaqreset;

% Limpar as variáveis globais
clear global;

% Choose default command line output for GUI_test
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes GUI_test wait for user response (see UIRESUME)
% uiwait(handles.figure1);

% VARIAVEIS GLOBAIS
global colorDevice;
global depthDevice;

% --- Outputs from this function are returned to the command line.
function varargout = GUI_test_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in GetStreams_pushbutton.
function GetStreams_pushbutton_Callback(hObject, eventdata, handles)
% hObject    handle to GetStreams_pushbutton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global colorDevice;
global depthDevice;

% Camara RGB
axes(handles.axes1);
colorDevice = videoinput('kinect',1,'RGB_640x480');
hImage = image(zeros(640,480,3),'Parent',handles.axes1);
preview(colorDevice,hImage);

% Camara Depth
axes(handles.axes2);
depthDevice = videoinput('kinect',2,'Depth_640x480');
hImage1 = image(zeros(640,480,3),'Parent',handles.axes2);
preview(depthDevice,hImage1);
colormap('hsv');


% --- Executes on button press in Get_PtCloudpushbutton2.
function Get_PtCloudpushbutton2_Callback(hObject, eventdata, handles)
% hObject    handle to Get_PtCloudpushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

handles.output = hObject; % faz output da handle
global depthDevice;
global colorDevice;

colorI=getsnapshot(colorDevice);
depthI=getsnapshot(depthDevice);

ptCloud = pcfromkinect(depthDevice,depthI,colorI);

color=ptCloud.Color; % Cor da nuvem de pontos
r=color(:,:,1);
g=color(:,:,2);
b=color(:,:,3);

r=im2double(reshape(r,[],1));
g=im2double(reshape(g,[],1));
b=im2double(reshape(b,[],1));

cor=[r g b];

points=ptCloud.Location; % Localização dos pontos

x=points(:,:,1); % Distancias Z dos pontos
x=reshape(x,[],1);

y=points(:,:,2); % Distancias Z dos pontos
y=reshape(y,[],1);

z=points(:,:,3); % Distancias Z dos pontos
z=reshape(z,[],1);

axes(handles.axes3);

scatter3(x,y,z-0.8,[],cor,'.','Parent',handles.axes3);
view([ -0,-77.4400]);

pause(0.2);



