function varargout = annotate(varargin)
% ANNOTATE MATLAB code for annotate.fig
%      ANNOTATE, by itself, creates a new ANNOTATE or raises the existing
%      singleton*.
%
%      H = ANNOTATE returns the handle to a new ANNOTATE or the handle to
%      the existing singleton*.
%
%      ANNOTATE('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in ANNOTATE.M with the given input arguments.
%
%      ANNOTATE('Property','Value',...) creates a new ANNOTATE or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before annotate_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to annotate_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 annotate

% Last Modified by GUIDE v2.5 30-Apr-2015 14:37:14

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @annotate_OpeningFcn, ...
                   'gui_OutputFcn',  @annotate_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


% --- Executes just before annotate is made visible.
function annotate_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 annotate (see VARARGIN)

addpath('../../../shared/Code/MatLab/Quaternions/');
addpath('../../../shared/Code/MatLab/Network/UDP_Receiver/');

pathstr = '/vol/vssp/datasets/vid+depth01/S3A_XEF_02_2015';
    % '~/Datasets/S3A_XEF_02_2015';
    % 'C:\Users\s3a\Videos\Kinect';
    % '/vol/vssp/ucdatasets/s3a/vml/2015_02_17_XEF' 
filename = '20150217_111609_00';
    % '20150217_111155_00';
    % '20150217_111609_00';
    % 20150217_112354_00';
    % 20150217_112107_00';
    % 20150217_111609_00';
    % 20150217_111155_00'
    % '../../../shared/Code/MatLab/Network/UDP_Receiver/received_data.mat';
    
mat_filename = [pathstr, filesep, filename];

% Load data file and create handles for annotation:
fprintf('Loading data from %s ... ', mat_filename);
tic;
load(mat_filename);
time = toc;
fprintf('Done in %d min %5.3f seconds.\n', floor(time/60), rem(time,60));
handles.json2data = json2data;

handles.ann_filename = [pathstr, filesep, filename, '_annotation.mat'];
if exist(handles.ann_filename, 'file') 
    % Load data file with pre-existing annotation:
    fprintf('Loading annotation %s \n', handles.ann_filename);
    ann = load(handles.ann_filename);
    handles.annotation = ann.annotation;
else
    handles.annotation.threeDIdx = 0;
    handles.annotation.RGBIdx = 0;
    handles.annotation.step_size = 10;

    % Getting RGB file names
    handles.annotation.RGBPath = [pathstr, filesep, filename, '_RGBD', filesep, 'RGB', filesep];
    imgFiles = dir([handles.annotation.RGBPath , '*.png']);
    handles.annotation.RGBfiles = cell(length(imgFiles),1);
    for f=1:length(imgFiles)
        handles.annotation.RGBfiles{f} = imgFiles(f).name;
    end

    % Loading RGB time stamps
    RGBtimestamps = load([pathstr, filesep, filename, '_RGBD/Timestamp_RGB/RGB_tick_timestamps.txt']);
    handles.annotation.RGBtimestamps = double(RGBtimestamps(:,2)); % *10; % so that it matches Kinect time stamps scale.

    handles.annotation.clap_first_3D = NaN; % Marks ThreeD time stamp of the first clap.
        % i.e., if handles.clap_first = json2data.nTime - annotation.RGBtimestamps
        % so given a handles.annotation.threeDIdx, the handles.annotation.RGBIdx is computed based on
        % the timestamp of handles.json2data{handles.annotation.threeDIdx}.nTime + handles.clap_first
    handles.annotation.clap_last_3D = NaN; % Marks ThreeD time stamp of the last clap.
    handles.annotation.clap_first_RGB= NaN; % Marks RGB time stamp of the first clap.
    handles.annotation.clap_last_RGB = NaN; % Marks RGB time stamp of the last clap.
    
   
    % Actual annotation data to be built from the GUI:
    nData = length(handles.json2data);
    person = struct('new_label', -1, 'bad_orientation', -1, 'bad_position', -1, 'not_detected', -1, 'dummy', -1);
    handles.annotation.person = repmat(person,[nData, 6]); 
    handles.annotation.threeDnTime = -ones(nData,1);
end

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

% Update handles structure
guidata(hObject, handles);


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


% --- Outputs from this function are returned to the command line.
function varargout = annotate_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 ThreeD_backward.
function ThreeD_backward_Callback(hObject, eventdata, handles)
% hObject    handle to ThreeD_backward (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
something_changed = false;
while true,
    if get(hObject,'Value')
        [handles, SC]= ThreeD_ChangeFrame(-1, handles);
        something_changed = something_changed || SC;
        if ~SC,
            set(hObject,'Value', 0);    
            break;
        end
    else
        break;
    end
end
if something_changed,
    guidata(hObject, handles);
end

% --- Executes on button press in ThreeD_play.
function ThreeD_play_Callback(hObject, eventdata, handles)
% hObject    handle to ThreeD_play (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
something_changed = false;
while true,
    if get(hObject,'Value')
        [handles, SC]= ThreeD_ChangeFrame(1, handles);
        something_changed = something_changed || SC;
        if ~SC,
            set(hObject,'Value', 0);    
            break;
        end
    else
        break;
    end
end
if something_changed,
    guidata(hObject, handles);
end

% --- Executes on button press in ThreeD_step.
function ThreeD_step_Callback(hObject, eventdata, handles)
% hObject    handle to ThreeD_step (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[handles, something_changed]= ThreeD_ChangeFrame(+1, handles);
if something_changed,
    guidata(hObject, handles);
end

% --- Executes on button press in ThreeD_step_back.
function ThreeD_step_back_Callback(hObject, eventdata, handles)
% hObject    handle to ThreeD_step_back (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[handles, something_changed]= ThreeD_ChangeFrame(-1, handles);
if something_changed,
    guidata(hObject, handles);
end


% --- Executes on button press in ThreeD_fast_forward.
function ThreeD_fast_forward_Callback(hObject, eventdata, handles)
% hObject    handle to ThreeD_fast_forward (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
something_changed = false;
while true,
    if get(hObject,'Value')
        [handles, SC]= ThreeD_ChangeFrame(handles.annotation.step_size, handles);
        something_changed = something_changed || SC;
        if ~SC,
            set(hObject,'Value', 0);    
            break;
        end
    else
        break;
    end
end
if something_changed,
    guidata(hObject, handles);
end
  


function ThreeD_frame_number_Callback(hObject, eventdata, handles)
% hObject    handle to ThreeD_frame_number (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of ThreeD_frame_number as text
%        str2double(get(hObject,'String')) returns contents of ThreeD_frame_number as a double
n = str2double(get(hObject, 'String'));
if n <= 0,
    n = 1;
elseif n > length(handles.json2data),
    n = length(handles.json2data);        
end
set(hObject, 'String', num2str(n)); % setting it just in case the number was out of range.

myStep = n - handles.annotation.threeDIdx;
[handles something_changed]= ThreeD_ChangeFrame(myStep, handles);
if something_changed, 
    guidata(hObject, handles);
end


% --- Executes during object creation, after setting all properties.
function ThreeD_frame_number_CreateFcn(hObject, eventdata, handles)
% hObject    handle to ThreeD_frame_number (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
handles.ThreeDFrameNumberHandler = hObject;
guidata(hObject, handles);

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

% Hint: get(hObject,'Value') returns toggle state of P1_bad_head_orientation


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

% Hint: get(hObject,'Value') returns toggle state of P1_not_detected


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

% Hint: get(hObject,'Value') returns toggle state of P1_bad_position


% --- Executes on button press in RGB_fast_backward.
function RGB_fast_backward_Callback(hObject, eventdata, handles)
% hObject    handle to RGB_fast_backward (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
while true,
    if get(hObject,'Value'),
        handles.annotation.RGBIdx = handles.annotation.RGBIdx-handles.annotation.step_size;
        if RGBUpdateFrameNumber(handles),
            guidata(hObject, handles);
        else
            set(hObject,'Value', 0);
            break;
        end
    else
        break
    end
end

% --- Executes on button press in RGB_backward.
function RGB_backward_Callback(hObject, eventdata, handles)
% hObject    handle to RGB_backward (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
while true,
    if get(hObject,'Value'),
        handles.annotation.RGBIdx = handles.annotation.RGBIdx-1;
        if RGBUpdateFrameNumber(handles),
            guidata(hObject, handles);
        else
            set(hObject,'Value', 0);
            break;
        end
    else
        break
    end
end


% --- Executes on button press in RGB_play.
function RGB_play_Callback(hObject, eventdata, handles)
% hObject    handle to RGB_play (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
while true,
    if get(hObject,'Value'),
        handles.annotation.RGBIdx = handles.annotation.RGBIdx+1;
        if RGBUpdateFrameNumber(handles),
            guidata(hObject, handles);
        else
            set(hObject,'Value', 0);
            break;
        end
    else
        break
    end
end


% --- Executes on button press in RGB_step.
function RGB_step_Callback(hObject, eventdata, handles)
% hObject    handle to RGB_step (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles.annotation.RGBIdx = handles.annotation.RGBIdx+1;
if RGBUpdateFrameNumber(handles),
    guidata(hObject, handles);
end % Otherwise (failure) handles is not updated.

% --- Executes on button press in RGB_step_back.
function RGB_step_back_Callback(hObject, eventdata, handles)
% hObject    handle to RGB_step_back (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles.annotation.RGBIdx = handles.annotation.RGBIdx-1;
if RGBUpdateFrameNumber(handles);
    guidata(hObject, handles);
end


% --- Executes on button press in RGB_fast_forward.
function RGB_fast_forward_Callback(hObject, eventdata, handles)
% hObject    handle to RGB_fast_forward (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
while true,
    if get(hObject,'Value')
        handles.annotation.RGBIdx = handles.annotation.RGBIdx+handles.annotation.step_size;
        if RGBUpdateFrameNumber(handles),
            guidata(hObject, handles);
        else
            set(hObject,'Value', 0);    
            break;
        end       
    else        
        break
    end
end


% --- Executes on slider movement.
function RGB_slider_Callback(hObject, eventdata, handles)
% hObject    handle to RGB_slider (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
n = ceil(get(hObject,'Value')*length(handles.annotation.RGBfiles));
if n==0,
    n=1;
end
handles.annotation.RGBIdx = n;
if RGBUpdateFrameNumber(handles),
    guidata(hObject, handles);
end



% --- Executes during object creation, after setting all properties.
function RGB_slider_CreateFcn(hObject, eventdata, handles)
% hObject    handle to RGB_slider (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end
handles.RGBSliderHandler = hObject;
guidata(hObject, handles);


function RGB_frame_number_Callback(hObject, eventdata, handles)
% hObject    handle to RGB_frame_number (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of RGB_frame_number as text
%        str2double(get(hObject,'String')) returns contents of RGB_frame_number as a double
n = str2double(get(hObject, 'String'));
if n <= 0,
    n = 1;
elseif n > length(handles.annotation.RGBfiles),
    n = length(handles.annotation.RGBfiles);
end
set(hObject, 'String', num2str(n)); % setting it just in case the number was out of range.

handles.annotation.RGBIdx = n;
if RGBUpdateFrameNumber(handles),
    guidata(hObject, handles);
end



% --- Executes during object creation, after setting all properties.
function RGB_frame_number_CreateFcn(hObject, eventdata, handles)
% hObject    handle to RGB_frame_number (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
handles.RGBFrameNumberHandler = hObject;
guidata(hObject, handles);


function P1_new_label_Callback(hObject, eventdata, handles)
% hObject    handle to P1_new_label (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of P1_new_label as text
%        str2double(get(hObject,'String')) returns contents of P1_new_label as a double


% --- Executes during object creation, after setting all properties.
function P1_new_label_CreateFcn(hObject, eventdata, handles)
% hObject    handle to P1_new_label (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


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

% Hint: get(hObject,'Value') returns toggle state of P1_move_label


% --- Executes on slider movement.
function ThreeD_slider_Callback(hObject, eventdata, handles)
% hObject    handle to ThreeD_slider (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
n = ceil(get(hObject,'Value')*length(handles.json2data));
if n==0,
    n=1;
end
myStep = n - handles.annotation.threeDIdx;
[handles something_changed]= ThreeD_ChangeFrame(myStep, handles);
if something_changed, 
    guidata(hObject, handles);
end



% --- Executes during object creation, after setting all properties.
function ThreeD_slider_CreateFcn(hObject, eventdata, handles)
% hObject    handle to ThreeD_slider (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end
handles.ThreeDSliderHandler = hObject;
guidata(hObject, handles);


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

% Hint: get(hObject,'Value') returns toggle state of P0_not_detected


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

% Hint: get(hObject,'Value') returns toggle state of P0_bad_position


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

% Hint: get(hObject,'Value') returns toggle state of P0_bad_head_orientation


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

% Hint: get(hObject,'Value') returns toggle state of P0_move_label



function P0_new_label_Callback(hObject, eventdata, handles)
% hObject    handle to P0_new_label (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of P0_new_label as text
%        str2double(get(hObject,'String')) returns contents of P0_new_label as a double


% --- Executes during object creation, after setting all properties.
function P0_new_label_CreateFcn(hObject, eventdata, handles)
% hObject    handle to P0_new_label (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


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

% Hint: get(hObject,'Value') returns toggle state of P4_not_detected


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

% Hint: get(hObject,'Value') returns toggle state of P4_bad_position


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

% Hint: get(hObject,'Value') returns toggle state of P4_bad_head_orientation


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

% Hint: get(hObject,'Value') returns toggle state of P4_move_label



function P4_new_label_Callback(hObject, eventdata, handles)
% hObject    handle to P4_new_label (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of P4_new_label as text
%        str2double(get(hObject,'String')) returns contents of P4_new_label as a double


% --- Executes during object creation, after setting all properties.
function P4_new_label_CreateFcn(hObject, eventdata, handles)
% hObject    handle to P4_new_label (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


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

% Hint: get(hObject,'Value') returns toggle state of P2_not_detected


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

% Hint: get(hObject,'Value') returns toggle state of P2_bad_position


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

% Hint: get(hObject,'Value') returns toggle state of P2_bad_head_orientation


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

% Hint: get(hObject,'Value') returns toggle state of P2_move_label



function P2_new_label_Callback(hObject, eventdata, handles)
% hObject    handle to P2_new_label (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of P2_new_label as text
%        str2double(get(hObject,'String')) returns contents of P2_new_label as a double


% --- Executes during object creation, after setting all properties.
function P2_new_label_CreateFcn(hObject, eventdata, handles)
% hObject    handle to P2_new_label (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


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

% Hint: get(hObject,'Value') returns toggle state of P5_not_detected


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

% Hint: get(hObject,'Value') returns toggle state of P5_bad_position


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

% Hint: get(hObject,'Value') returns toggle state of P5_bad_head_orientation


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

% Hint: get(hObject,'Value') returns toggle state of P5_move_label



function P5_new_label_Callback(hObject, eventdata, handles)
% hObject    handle to P5_new_label (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of P5_new_label as text
%        str2double(get(hObject,'String')) returns contents of P5_new_label as a double


% --- Executes during object creation, after setting all properties.
function P5_new_label_CreateFcn(hObject, eventdata, handles)
% hObject    handle to P5_new_label (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


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

% Hint: get(hObject,'Value') returns toggle state of P3_not_detected


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

% Hint: get(hObject,'Value') returns toggle state of P3_bad_position


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

% Hint: get(hObject,'Value') returns toggle state of P3_bad_head_orientation


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

% Hint: get(hObject,'Value') returns toggle state of P3_move_label



function P3_new_label_Callback(hObject, eventdata, handles)
% hObject    handle to P3_new_label (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of P3_new_label as text
%        str2double(get(hObject,'String')) returns contents of P3_new_label as a double


% --- Executes during object creation, after setting all properties.
function P3_new_label_CreateFcn(hObject, eventdata, handles)
% hObject    handle to P3_new_label (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- If Enable == 'on', executes on mouse press in 5 pixel border.
% --- Otherwise, executes on mouse press in 5 pixel border or over RGB_play.
function RGB_play_ButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to RGB_play (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- If Enable == 'on', executes on mouse press in 5 pixel border.
% --- Otherwise, executes on mouse press in 5 pixel border or over RGB_step.
function RGB_step_ButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to RGB_step (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on key press with focus on RGB_step and none of its controls.
function RGB_step_KeyPressFcn(hObject, eventdata, handles)
% hObject    handle to RGB_step (see GCBO)
% eventdata  structure with the following fields (see UICONTROL)
%	Key: name of the key that was pressed, in lower case
%	Character: character interpretation of the key(s) that was pressed
%	Modifier: name(s) of the modifier key(s) (i.e., control, shift) pressed
% handles    structure with handles and user data (see GUIDATA)


% --- Executes during object creation, after setting all properties.
function ThreeD_step_CreateFcn(hObject, eventdata, handles)
% hObject    handle to ThreeD_step (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called


% --- If Enable == 'on', executes on mouse press in 5 pixel border.
% --- Otherwise, executes on mouse press in 5 pixel border or over ThreeD_step.
function ThreeD_step_ButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to ThreeD_step (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- If Enable == 'on', executes on mouse press in 5 pixel border.
% --- Otherwise, executes on mouse press in 5 pixel border or over ThreeD_play.
function ThreeD_play_ButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to ThreeD_play (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on key press with focus on ThreeD_step and none of its controls.
function ThreeD_step_KeyPressFcn(hObject, eventdata, handles)
% hObject    handle to ThreeD_step (see GCBO)
% eventdata  structure with the following fields (see UICONTROL)
%	Key: name of the key that was pressed, in lower case
%	Character: character interpretation of the key(s) that was pressed
%	Modifier: name(s) of the modifier key(s) (i.e., control, shift) pressed
% handles    structure with handles and user data (see GUIDATA)



% --- Executes on button press in ThreeD_fast_backward.
function ThreeD_fast_backward_Callback(hObject, eventdata, handles)
% hObject    handle to ThreeD_fast_backward (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of ThreeD_fast_backward
something_changed = false;
while true,
    if get(hObject,'Value')
        [handles, SC]= ThreeD_ChangeFrame(-handles.annotation.step_size, handles);
        something_changed = something_changed || SC;
        if ~SC,
            set(hObject,'Value', 0);
            break;
        end
    else
        break;
    end
end
if something_changed,
    guidata(hObject, handles); % Having this here reduces the number of data transfers
end


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

% Hint: get(hObject,'Value') returns toggle state of sync


% --- Executes during object creation, after setting all properties.
function sync_CreateFcn(hObject, eventdata, handles)
% hObject    handle to sync (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
handles.SyncHandler = hObject;
guidata(hObject, handles);


% --- If Enable == 'on', executes on mouse press in 5 pixel border.
% --- Otherwise, executes on mouse press in 5 pixel border or over sync.
function sync_ButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to sync (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on key press with focus on sync and none of its controls.
function sync_KeyPressFcn(hObject, eventdata, handles)
% hObject    handle to sync (see GCBO)
% eventdata  structure with the following fields (see UICONTROL)
%	Key: name of the key that was pressed, in lower case
%	Character: character interpretation of the key(s) that was pressed
%	Modifier: name(s) of the modifier key(s) (i.e., control, shift) pressed
% handles    structure with handles and user data (see GUIDATA)

% --- Executes on button press in clap_first.
function clap_first_Callback(hObject, eventdata, handles)
% hObject    handle to clap_first (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles.annotation.clap_first_3D = double(handles.json2data{handles.annotation.threeDIdx}.nTime);
handles.annotation.clap_first_RGB = handles.annotation.RGBtimestamps(handles.annotation.RGBIdx);
guidata(hObject, handles);

% --- Executes on button press in clap_last.
function clap_last_Callback(hObject, eventdata, handles)
% hObject    handle to clap_last (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles.annotation.clap_last_3D = double(handles.json2data{handles.annotation.threeDIdx}.nTime);
handles.annotation.clap_last_RGB = handles.annotation.RGBtimestamps(handles.annotation.RGBIdx);
guidata(hObject, handles);

% --------------------------------------------------------------------
% Special save function

function save_annotation(ann_filename, handles)
annotation = handles.annotation;
fprintf('Saving data to %s\n', ann_filename);
save(ann_filename, 'annotation');

function save_ClickedCallback(hObject, eventdata, handles)
% hObject    handle to save (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% annotation.clap_first_3D = handles.annotation.clap_first_3D; % Marks ThreeD time stamp of the first clap.
% annotation.clap_last_3D  = handles.annotation.clap_last_3D; % Marks ThreeD time stamp of the last clap.
% annotation.clap_first_RGB= handles.annotation.clap_first_RGB; % Marks RGB time stamp of the first clap.
% annotation.clap_last_RGB = handles.annotation.clap_last_RGB; % Marks RGB time stamp of the last clap.

save_annotation(handles.ann_filename, handles);


% --------------------------------------------------------------------
% Custom functions
function [success, handles] = ThreeDUpdateFrameNumber(handles)
if (handles.annotation.threeDIdx <= length(handles.json2data)) && (handles.annotation.threeDIdx > 0),
    figure(2);
    json2StickManPlot(handles.json2data{handles.annotation.threeDIdx},2);
    success = true;
    set(handles.ThreeDFrameNumberHandler,'String', num2str(handles.annotation.threeDIdx));
    set(handles.ThreeDSliderHandler,'Value', (handles.annotation.threeDIdx-1)/(length(handles.json2data)-1));
    if get(handles.Keep_reading_annotation,'Value'),
        % update buttons using annotated values
        copy_annotation_to_buttons(handles);
    elseif get(handles.Keep_setting_annotation,'Value')
        % update annotation using the state of the buttons
        handles = get_annotation_from_buttons(handles);
    end
    drawnow
else
    success = false;
end

function success = RGBUpdateFrameNumber(handles)
if (handles.annotation.RGBIdx <= length(handles.annotation.RGBfiles)) && (handles.annotation.RGBIdx > 0),
    figure(3);
    img = imread([handles.annotation.RGBPath, handles.annotation.RGBfiles{handles.annotation.RGBIdx}]);
    image(img);
    title(handles.annotation.RGBfiles{handles.annotation.RGBIdx});
    axis off;    
    set(handles.RGBFrameNumberHandler,'String', num2str(handles.annotation.RGBIdx));
    set(handles.RGBSliderHandler,'Value', (handles.annotation.RGBIdx-1)/(length(handles.annotation.RGBfiles)-1));
    drawnow;
    success = true;
else
    success = false;
end

function [handles something_changed]= ThreeD_ChangeFrame(step_size, handles)
handles.annotation.threeDIdx = handles.annotation.threeDIdx+step_size;
[ThreeDUpdated, handles] = ThreeDUpdateFrameNumber(handles);
if ThreeDUpdated,
    something_changed = true;
    if get(handles.SyncHandler, 'Value'),
        % Work out the annotation.RGBIdx
        RGBIdx_backup = handles.annotation.RGBIdx;
        if isfield(handles.json2data{handles.annotation.threeDIdx},'People') && ~isnan(handles.annotation.clap_first_3D) && ~isnan(handles.annotation.clap_last_3D) && ~isnan(handles.annotation.clap_first_RGB) && ~isnan(handles.annotation.clap_last_RGB), 
            % A clap_first has been marked and people are present. 
            % Use the synchronisation information
            RGBtime3Dnorm = (double(handles.json2data{handles.annotation.threeDIdx}.nTime) - handles.annotation.clap_first_3D) / (handles.annotation.clap_last_3D - handles.annotation.clap_first_3D);
            RGBtime = RGBtime3Dnorm * (handles.annotation.clap_last_RGB - handles.annotation.clap_first_RGB) + handles.annotation.clap_first_RGB;
            [~, handles.annotation.RGBIdx] = min(abs(handles.annotation.RGBtimestamps - RGBtime));
        else % Either a clap was not annotated or there is no person in this time stamp.
            % Just move annotation.RGBIdx by the same step as 3D data.
            handles.annotation.RGBIdx = handles.annotation.RGBIdx+step_size;
        end
        if ~RGBUpdateFrameNumber(handles), 
            handles.annotation.RGBIdx = RGBIdx_backup; % undo the change
            set(handles.SyncHandler, 'Value', 0); % remove check from sync box
            % handles.annotation.sync_frames(handles.annotation.threeDIdx) = 0; % set sync to false here.
        end
    end
else
    handles.annotation.threeDIdx = handles.annotation.threeDIdx-step_size;  % undo the change
    something_changed = false;
end


% --- Executes on button press in dummyHead0.
function dummyHead0_Callback(hObject, eventdata, handles)
% hObject    handle to dummyHead0 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of dummyHead0
if get(hObject,'Value'),
    set(handles.dummyHead1_handle, 'Value', 0);
    set(handles.dummyHead2_handle, 'Value', 0);
    set(handles.dummyHead3_handle, 'Value', 0);
    set(handles.dummyHead4_handle, 'Value', 0);
    set(handles.dummyHead5_handle, 'Value', 0);
end
guidata(hObject, handles);


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

if get(hObject,'Value'), % returns toggle state of dummyHead4
    set(handles.dummyHead1_handle, 'Value', 0);
    set(handles.dummyHead2_handle, 'Value', 0);
    set(handles.dummyHead3_handle, 'Value', 0);
    set(handles.dummyHead0_handle, 'Value', 0);
    set(handles.dummyHead5_handle, 'Value', 0);
end
guidata(hObject, handles);

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

if get(hObject,'Value'), %  returns toggle state of dummyHead2
    set(handles.dummyHead1_handle, 'Value', 0);
    set(handles.dummyHead0_handle, 'Value', 0);
    set(handles.dummyHead3_handle, 'Value', 0);
    set(handles.dummyHead4_handle, 'Value', 0);
    set(handles.dummyHead5_handle, 'Value', 0);
end
guidata(hObject, handles);

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

if get(hObject,'Value'), % returns toggle state of dummyHead5
    set(handles.dummyHead1_handle, 'Value', 0);
    set(handles.dummyHead2_handle, 'Value', 0);
    set(handles.dummyHead3_handle, 'Value', 0);
    set(handles.dummyHead4_handle, 'Value', 0);
    set(handles.dummyHead0_handle, 'Value', 0);
end
guidata(hObject, handles);

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

if get(hObject,'Value'), % returns toggle state of dummyHead3
    set(handles.dummyHead1_handle, 'Value', 0);
    set(handles.dummyHead2_handle, 'Value', 0);
    set(handles.dummyHead0_handle, 'Value', 0);
    set(handles.dummyHead4_handle, 'Value', 0);
    set(handles.dummyHead5_handle, 'Value', 0);
end
guidata(hObject, handles);

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

if get(hObject,'Value'), %  returns toggle state of dummyHead1
    set(handles.dummyHead0_handle, 'Value', 0);
    set(handles.dummyHead2_handle, 'Value', 0);
    set(handles.dummyHead3_handle, 'Value', 0);
    set(handles.dummyHead4_handle, 'Value', 0);
    set(handles.dummyHead5_handle, 'Value', 0);
end
guidata(hObject, handles);

% --- Executes during object creation, after setting all properties.
function dummyHead0_CreateFcn(hObject, eventdata, handles)
% hObject    handle to dummyHead0 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
handles.dummyHead0_handle = hObject;
guidata(hObject, handles);

% --- Executes during object creation, after setting all properties.
function dummyHead1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to dummyHead1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
handles.dummyHead1_handle = hObject;
guidata(hObject, handles);


% --- Executes during object creation, after setting all properties.
function dummyHead2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to dummyHead2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
handles.dummyHead2_handle = hObject;
guidata(hObject, handles);


% --- Executes during object creation, after setting all properties.
function dummyHead3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to dummyHead3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
handles.dummyHead3_handle = hObject;
guidata(hObject, handles);


% --- Executes during object creation, after setting all properties.
function dummyHead4_CreateFcn(hObject, eventdata, handles)
% hObject    handle to dummyHead4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
handles.dummyHead4_handle = hObject;
guidata(hObject, handles);


% --- Executes during object creation, after setting all properties.
function dummyHead5_CreateFcn(hObject, eventdata, handles)
% hObject    handle to dummyHead5 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
handles.dummyHead5_handle = hObject;
guidata(hObject, handles);


% --- Executes when user attempts to close figure1.
function figure1_CloseRequestFcn(hObject, eventdata, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: delete(hObject) closes the figure
button = questdlg('Save annotation data?','Save annotation data?','Yes','No','No');
if strcmp(button,'Yes')
    save_annotation(handles.ann_filename, handles);
end
% close all
delete(hObject)
% delete(handles.output);


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

function copy_annotation_to_buttons(handles)
% Copy annotation.person to GUI 
for person=1:6,
    P = handles.annotation.person(handles.annotation.threeDIdx,person);
    if P.new_label ~= -1
        set(eval(sprintf('handles.P%d_move_label',person-1)),'Value',1);
    end
    set(eval(sprintf('handles.P%d_new_label',person-1)), 'String', num2str(P.new_label));

    set(eval(sprintf('handles.P%d_bad_head_orientation',person-1)),'Value', P.bad_orientation==1);
    set(eval(sprintf('handles.P%d_bad_position',person-1)),'Value', P.bad_position==1);
    set(eval(sprintf('handles.P%d_not_detected',person-1)),'Value', P.not_detected==1);
    set(eval(sprintf('handles.dummyHead%d',person-1)),'Value', P.dummy==1);
end


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

function handles = get_annotation_from_buttons(handles)
for person=1:6,
    P = struct('new_label', -1, 'bad_orientation', -1, 'bad_position', -1, 'not_detected', -1, 'dummy', -1);
    
    P.new_label = str2double(get(eval(sprintf('handles.P%d_new_label',person-1)), 'String'));
    P.bad_orientation = get(eval(sprintf('handles.P%d_bad_head_orientation',person-1)),'Value');
    P.bad_position = get(eval(sprintf('handles.P%d_bad_position',person-1)),'Value');
    P.not_detected = get(eval(sprintf('handles.P%d_not_detected',person-1)),'Value');
    P.dummy = get(eval(sprintf('handles.dummyHead%d',person-1)),'Value');
    
    handles.annotation.person(handles.annotation.threeDIdx,person) = P;
end



% --- Executes during object creation, after setting all properties.
function P0_move_label_CreateFcn(hObject, eventdata, handles)
% hObject    handle to P0_move_label (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called


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

% Hint: get(hObject,'Value') returns toggle state of Keep_reading_annotation


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

% Hint: get(hObject,'Value') returns toggle state of Keep_setting_annotation
