function varargout = Show_Tree(varargin)

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

% Last Modified by GUIDE v2.5 10-Feb-2010 23:40:10

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @Show_Tree_OpeningFcn, ...
                   'gui_OutputFcn',  @Show_Tree_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 Show_Tree is made visible.
function Show_Tree_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 Show_Tree (see VARARGIN)

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

handles.cams   = getappdata(0,'cams');
handles.voxels = getappdata(0,'voxels');
handles.space  = getappdata(0,'space');
handles.latovoxel = getappdata(0,'latovoxel');
handles.markers = getappdata(0,'markers');
handles.mode = getappdata(0,'mode');
handles.color = jet(size(handles.cams,2));

set(handles.plot_cam,'view',[75,20]);
grid(handles.plot_cam,'on');
set(handles.plot_cam,'XLim',[-2 handles.space(1)+2],...
                      'YLim',[-2 handles.space(2)+2],...
                      'ZLim',[-2 handles.space(3)+2]); 

set(hObject,'toolbar','figure');

if handles.mode==1
    C=zeros(size(handles.cams,2),size(handles.cams,2));
    for i=1:size(handles.cams,2)
        for j=(i+1):size(handles.cams,2)
            i
            j
            [c mark_comuni mark_rimast]=costo2(handles.cams,i,j)
            C(i,j)=c;
            C(j,i)=c;
        end
    end
    %
    handles.Tree=association_tree_mod(C);
    for i=1:size(handles.cams,2)
        for j=(i+1):size(handles.cams,2)
            i
            j
            [c mark_comuni mark_rimast]=costo2(handles.cams,i,j)
            C(i,j)=c;
            C(j,i)=c;
        end
    end
else
    handles.Tree = static_association(handles.cams);
end
    
handles.livelli=size(handles.Tree,1)+1;
[rate rimasti associati]= rate4level3(handles.cams,handles.Tree);
handles.rate=rate;
handles.rimasti=rimasti;
handles.associati=associati;
columnname =   {'rate','associati','rimasti'};
for i=1:handles.livelli-1
     handles.rowname{i} = ['livello' num2str(i)];
     handles.data(i,:)={['   [  ' num2str(handles.rate(i)) ' ]'],...
                        ['   [  ' num2str(handles.associati{i}) ' ]'],...
                        ['   [  ' num2str(handles.rimasti{i}) ' ]']};
end
set(handles.uitable_levels,'ColumnName', columnname,...
                      'Rowname', handles.rowname,...
                      'Units','char',...
                      'columnwidth',{'auto','auto','auto'},...
                      'Data',handles.data);      

set(handles.ax_tree,'XLim',[0 handles.livelli],...
                      'YLim',[0 size(handles.cams,2)/2+1],...
                      'ZLim',[0 10]);
%axis normal               
%axis equal;
handles.on=0;
guidata(hObject, handles);
lato=0.25;
cla(handles.ax_tree);
for j=1:handles.livelli-1
    if j==1
        start=1;
    else
        start=start-1+round(size(handles.Tree{j-1},2)/4);
    end
    for i=1:size(handles.Tree{j},2)
        handles.quas1 = [j-lato j+lato j+lato j-lato; ....
                  i-1+start-lato i-1+start-lato,i-1+start+lato i-1+start+lato]';
        handles.p1 = struct('vertices',handles.quas1,'faces',[1 2 3 4]);
        guidata(hObject, handles)
        axes(handles.ax_tree)
        handles.h1 = patch(handles.p1,'facecolor','w','EdgeColor', 'r','ButtonDownFcn',{@fillFcn,hObject,handles.p1,handles});
        guidata(hObject, handles);
        %set(handles.h1,'facecolor','w','EdgeColor', 'r');
    end
end

% handles.quas1=[1 2 2 1; 1 1,2 2]';
% handles.p1 = struct('vertices',handles.quas1,'faces',[1 2 3 4]);
% guidata(hObject, handles)
% handles.h1 = patch(handles.p1,'ButtonDownFcn',{@fillFcn,handles.p1,handles.on});
% set(handles.h1,'facecolor','w','EdgeColor', 'r');

%set(handles.ax_tree,'hittest','off');

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = Show_Tree_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;

%function fillFcn(varagin)
function fillFcn(hObject1,hObject2,hObject,struct,handles)

stato=handles.on;
vert=struct.vertices(1,:);
pos_level=ceil(vert(1));
pos_start= 1;

for i=2:pos_level
    pos_start= pos_start+round(size(handles.Tree{1},2)/(2^(i)))-1
end

pos_coppia=-pos_start+ceil(vert(2))+1;
if stato==0
    handles.on=1;
    axes(handles.ax_tree)
    %patch(struct,'facecolor','r');
    patch(struct,'facecolor','w','EdgeColor', 'g',...
          'ButtonDownFcn',{@fillFcn,hObject,struct,handles});
    axes(handles.plot_cam)
    cameras=cell2mat(handles.Tree{pos_level}(1,pos_coppia));
    plotcam3(handles.cams(cameras),handles.space,handles.voxels,handles.latovoxel,[1 1 1 0 0 0 0]);

    guidata(hObject,handles);
    set(handles.text_level,'string',pos_level);
    set(handles.text_couple,'string',pos_coppia);
    set(handles.text_set,'string',num2str(cameras));
else
    handles.on=0;
    axes(handles.ax_tree)
    patch(struct,'facecolor','w','EdgeColor', 'r',...
          'ButtonDownFcn',{@fillFcn,hObject,struct,handles});
    guidata(hObject,handles);
    axes(handles.plot_cam);
    cla;
end
 guidata(hObject,handles)

% %--- Executes on mouse press over axes background.
% function ax_tree_ButtonDownFcn(hObject, eventdata, handles)
% % hObject    handle to ax_tree (see GCBO)
% % eventdata  reserved - to be defined in a future version of MATLAB
% % handles    structure with handles and user data (see GUIDATA)
% fillFcn(hObject,eventdata,handles)
% guidata(hObject, handles);
% % %set(handles.ax_tree,'hittest','off');
% % % set(handles.axes2,'hittest','off');
% % guidata(hObject, handles);


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


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


% --- Executes during object creation, after setting all properties.
function text_set_CreateFcn(hObject, eventdata, handles)
% hObject    handle to text_set (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 push_prova.
function push_prova_Callback(hObject, eventdata, handles)
% hObject    handle to push_prova (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles
