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

% Last Modified by GUIDE v2.5 25-Jan-2010 00:08:45

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

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

view(3);
view(75,20);
grid on;
rotate3d on;
set(hObject,'toolbar','figure');

handles.cams   = getappdata(0,'cams');
handles.voxels = getappdata(0,'voxels');
handles.space  = getappdata(0,'space');
handles.latovoxel = getappdata(0,'latovoxel');
handles.dist   = getappdata(0,'dist');
handles.fov_angle = getappdata(0,'fov_angle');
handles.pixels = getappdata(0,'pixels');
worksp = evalin('base','who');

set(handles.listbox_workspace,'String',worksp);
set(handles.check_show_cam1,'Value',1);


% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = Discretizza_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 push_next.
function push_next_Callback(hObject, eventdata, handles)
% hObject    handle to push_next (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if ~isfield(handles.cams,'voxels')
   errordlg('Non esiste il set di voxels visiti da una telecamera',...
           'Incorrect Selection','modal')
else
  setappdata(0,'cams',handles.cams);
  setappdata(0,'voxels',handles.voxels);
  setappdata(0,'space',handles.space);
  setappdata(0,'latovoxel',handles.latovoxel);
  setappdata(0,'dist',handles.dist);
  setappdata(0,'fov_angle',handles.fov_angle);
  setappdata(0,'pixels',handles.pixels);
  
  Chose
  close(Discretizza)
end

% --- Executes on button press in push_dynimic.
function push_dynimic_Callback(hObject, eventdata, handles)
% hObject    handle to push_dynimic (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if ~isfield(handles.cams,'voxels')
   errordlg('Non esiste il set di voxels visiti da una telecamera',...
                'Incorrect Selection','modal');
else
    
end
% --- Executes on button press in push_discretizza.
function push_discretizza_Callback(hObject, eventdata, handles)
% hObject    handle to push_discretizza (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% tot=size(handles.cams,2)*size(handles.voxels,2);
% for x=1:tot
%  
% stopBar= progressbar(x/tot);
%  
% if (stopBar) break; end
%  
% end
[handles.cams handles.voxels] = space_partition2(handles.cams,handles.space,...
                                handles.fov_angle,handles.latovoxel,...
                                handles.dist(1),handles.dist(2),...
                                handles.pixels);

%[handles.cams handles.voxels]=match_camvox(handles.cams,handles.space,handles.voxels,handles.latovoxel);

% disp('cams(1).voxels')
% handles.cams(1).voxels
% disp('cams(2).voxels')
% handles.cams(1).voxels

guidata(hObject,handles);

% --- Executes on selection change in listbox_workspace.
function listbox_workspace_Callback(hObject, eventdata, handles)
% hObject    handle to listbox_workspace (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns listbox_workspace contents as cell array
%        contents{get(hObject,'Value')} returns selected item from listbox_workspace


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

% Hint: listbox 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



function edit_cams_Callback(hObject, eventdata, handles)
% hObject    handle to edit_cams (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 edit_cams as text
%        str2double(get(hObject,'String')) returns contents of edit_cams as a double


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

cla(handles.plot_cams);
guidata(hObject, handles);

% --- Executes on button press in push_plot.
function push_plot_Callback(hObject, eventdata, handles)
% hObject    handle to push_plot (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
set(handles.plot_cams,'XLim',[-2 handles.space(1)+2],...
                      'YLim',[-2 handles.space(2)+2],...
                      'ZLim',[-2 handles.space(3)+2]);
              
if ~isfield(handles.cams,'voxels')
    errordlg('Not exist Field Voxels in cams. Try to push Discretizza!',...
                     'Incorrect Selection','modal'); 
else
    label   = str2num(get(handles.edit_cams,'String')); 
    for i=1:size(label,2)
        if label(i)>size(handles.cams,2)
            errordlg(['La telecamera ', num2str(label(i)) ,' non esiste'],...
                     'Incorrect Selection','modal'); 
        end
    end
    % get(handles.check_show_cam1,'Value')
    % get(handles.check_show_versori1,'Value')
    % get(handles.check_show_tronco1,'Value')
    % get(handles.check_show_voxinside,'Value')
    handles.show=[get(handles.check_show_cam1,'Value'),...
                  get(handles.check_show_versori1,'Value'),...
                  get(handles.check_show_tronco1,'Value'),...
                  get(handles.check_show_campo,'Value'),...
                  get(handles.check_show_voxinside,'Value'),...
                  0,0];
    plotcam3(handles.cams(label),handles.space,handles.voxels,...
             handles.latovoxel,handles.show);
    if get(handles.check_show_campo,'Value')==1
         voxel(handles.space/2,handles.space,'c',0.02);
    end
    
    for i=1:size(label,2)
        text(handles.cams(label(i)).position(1),...
             handles.cams(label(i)).position(2),...
             handles.cams(label(i)).position(3)+0.45,...
             num2str(label(i)),'fontsize',11,'color','m');
        
    end

end
guidata(hObject,handles)

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

if isfield(handles.cams,'voxels')
    prompt = {'Enter a name for the struct varaible that save cameras'' info:'};
    title = 'Image display - assignin varaible';
    lines = 1;
    def = {'cams'};
    answer = inputdlg(prompt,title,lines,def);
    assignin('base', answer{1} , handles.cams);
    assignin('base', 'voxels' , handles.voxels);
    worksp = evalin('base','who');
    set(handles.listbox_workspace,'String',worksp);
else
    errordlg('Prima devi discretizzare','Incorrect Selection','modal')
end

guidata(hObject,handles)

% --- Executes on button press in check_show_cam1.
function check_show_cam1_Callback(hObject, eventdata, handles)
% hObject    handle to check_show_cam1 (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 check_show_cam1


% --- Executes on button press in check_show_versori1.
function check_show_versori1_Callback(hObject, eventdata, handles)
% hObject    handle to check_show_versori1 (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 check_show_versori1


% --- Executes on button press in check_show_tronco1.
function check_show_tronco1_Callback(hObject, eventdata, handles)
% hObject    handle to check_show_tronco1 (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 check_show_tronco1


% --- Executes on button press in check_show_voxinside.
function check_show_voxinside_Callback(hObject, eventdata, handles)
% hObject    handle to check_show_voxinside (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 check_show_voxinside


% --- Executes when selected object is changed in uipanel4.
function uipanel4_SelectionChangeFcn(hObject, eventdata, handles)
% hObject    handle to the selected object in uipanel4 
% eventdata  structure with the following fields (see UIBUTTONGROUP)
%	EventName: string 'SelectionChanged' (read only)
%	OldValue: handle of the previously selected object or empty if none was selected
%	NewValue: handle of the currently selected object
% handles    structure with handles and user data (see GUIDATA)

if hObject == handles.radio_current_param
    set(handles.listbox_workspace,'visible','off');
    set(handles.push_import_cams,'visible','off');
    set(handles.push_import_space,'visible','off');
else
    set(handles.listbox_workspace,'visible','on');
    set(handles.push_import_cams,'visible','on');
end
guidata(hObject,handles);


% --- Executes on button press in check_show_campo.
function check_show_campo_Callback(hObject, eventdata, handles)
% hObject    handle to check_show_campo (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 check_show_campo


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


% --- Executes on button press in push_import_space.
function push_import_space_Callback(hObject, eventdata, handles)
% hObject    handle to push_import_space (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
    list_entries = get(handles.listbox_workspace,'String');
    index_selected = get(handles.listbox_workspace,'Value');
    if length(index_selected) ~= 1
        errordlg('You must select 1 variable','Incorrect Selection','modal');
    else 
        var1 = list_entries{index_selected(1)};
        %var1.position
        handles.cams=evalin('base',var1);
        if ~isfield(handles.cams,'position') | ...
           ~isfield(handles.cams,'setcameras')
            errordlg('Not a correct struct variable',...
                     'Incorrect Selection','modal')
        else
            set(handles.push_import_cams,'visible','off');
            set(handles.push_import_space,'visible','off');
        end
        
    end 

% --- Executes on button press in push_import_cams.
function push_import_cams_Callback(hObject, eventdata, handles)
% hObject    handle to push_import_cams (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
    list_entries = get(handles.listbox_workspace,'String');
    index_selected = get(handles.listbox_workspace,'Value');
    if length(index_selected) ~= 1
        errordlg('You must select 1 variable','Incorrect Selection','modal')
    else 
        var1 = list_entries{index_selected(1)};
        %var1.position
        handles.cams=evalin('base',var1);
        if ~isfield(handles.cams,'position') | ...
           ~isfield(handles.cams,'angles')   | ...
           ~isfield(handles.cams,'voxels')
            errordlg('Not a correct struct variable',...
                     'Incorrect Selection','modal');
        else
            set(handles.push_import_cams,'visible','off');
            set(handles.push_import_space,'visible','on');
            
        end
        
    end 
    
    
    
