%% Hand 3D point relabeling

clear all; close all; clc

global data_file tags t connections
data_file = 'data/data_in_labeling.mat';
load(data_file)

close all

background_color = [0.3 0.3 0.3];

figure('color',background_color,'position',[80 20 900 900])
set(gca,'Color',background_color);

hold on

cx = 0;
cy = 2;
cz = 1;

cube_width = 10.0;

axis([cx-cube_width/2 cx+cube_width/2 cy-cube_width/2 cy+cube_width/2 cz-cube_width/2 cz+cube_width/2])
% axis equal
axis vis3d
grid on
view(3)

cube_width = 9.8;

ground_plane(1,:) = [cx-cube_width/2 cx+cube_width/2 cx+cube_width/2 cx-cube_width/2];
ground_plane(2,:) = [cy-cube_width/2 cy-cube_width/2 cy+cube_width/2 cy+cube_width/2];
ground_plane(3,:) = [0 0 0 0];

ground_plane_color = [0.1 0.1 0.1];

hp = fill3(ground_plane(1,:),ground_plane(2,:),ground_plane(3,:),'b');
set(hp,'facecolor',ground_plane_color)

hp=plot3(0,0,0);
set(hp,'markersize',20,'marker','+','markeredgecolor',[0 0 1],'markerfacecolor',[0 0 1],'linewidth',2)


set(gcf,'renderer','opengl')

xlabel('x (mm)')
ylabel('y (mm)')
zlabel('z (mm)')


%Add the figure rotator tool to the figure
addpath figure_rotator
f = FigureRotator(gca, 'CameraTarget',    [0 0 0], ...
                       'CameraPosition',  [12 12 12], ...
                       'CameraViewAngle', 60);

f.AttachCallback('WindowButtonDownFcn', @callbackPointSelection);
set(gcf, 'menubar', 'none');

global k h colors

colors = [21, 72, 144
          21, 72, 144
          255, 0, 0
          255, 0, 0
          255, 102, 0
          255, 255, 255];
      
colors = colors/255;
      
k=1;
h=[];

person=zeros(3,length(tags));

for i=1:length(tags)
    eval(sprintf('person(1,%d) = %s(1,%d);',i,char(tags{i}),k));
    eval(sprintf('person(2,%d) = %s(2,%d);',i,char(tags{i}),k));
    eval(sprintf('person(3,%d) = %s(3,%d);',i,char(tags{i}),k));
end
    
h=plotPerson(gca,person,connections,tags,colors);
    
% set(gcf,'WindowButtonDownFcn',{@callbackPointSelection});
set(gcf,'WindowKeyPressFcn',{@callbackKeyPressed});

disp('Use the figure to change the points labels.')
disp('Chose a point by clicking on it and press ''c'', this will prompt you with a new label for that point.')
disp('List of keyboard-functions:')
disp('    c, change selected point label')
disp('    q, create a new connections linking two labels')
disp('    right-arrow, move to next frame')
disp('    left-arrow, move to previous frame')
disp('    +, move 50 frames into the future')
disp('    -, move 50 frames to the past')
disp('    k, select the frame number')
disp('    l, load the global data_file, this will overwrite any unsaved changes')
disp('Attentions, these functions only work when the figure is selected and no figure tool is active (zoom, rotate, etc.)')


