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

% Last Modified by GUIDE v2.5 13-Feb-2010 17:38:09

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

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

% Update handles structure
%view(3);
set(handles.plot_cams,'view',[75,20]);
grid on;
rotate3d on;
set(hObject,'toolbar','figure');
cla(handles.plot_cams);
set(handles.check_show_cam,'Value',1);
%handles.show=[1 1 1 0 0 0 0];

space     = [str2num(get(handles.edit_space,'String'))];
set(handles.plot_cams,'XLim',[-2 space(1)+2],...
                      'YLim',[-2 space(2)+2],...
                      'ZLim',[-2 space(3)+2]);



worksp = evalin('base','who');
set(handles.listbox_workspace,'String',worksp);

guidata(hObject,handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = position_interface_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 edit_space_Callback(hObject, eventdata, handles)
% hObject    handle to edit_space (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_space as text
%        str2double(get(hObject,'String')) returns contents of edit_space as a double
edit_space=str2double(get(hObject,'String'));
guidata(hObject,handles)

% --- Executes during object creation, after setting all properties.
function edit_space_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_space (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



function edit_latovoxel_Callback(hObject, eventdata, handles)
% hObject    handle to edit_latovoxel (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_latovoxel as text
%        str2double(get(hObject,'String')) returns contents of edit_latovoxel as a double
edit_latovoxel=str2double(get(hObject,'String'));
guidata(hObject,handles)

% --- Executes during object creation, after setting all properties.
function edit_latovoxel_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_latovoxel (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 radio_manuale.
function radio_manuale_Callback(hObject, eventdata, handles)
% hObject    handle to radio_manuale (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 radio_manuale


% --- Executes on button press in radio_da_file.
function radio_da_file_Callback(hObject, eventdata, handles)
% hObject    handle to radio_da_file (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
guidata(hObject,handles);
% Hint: get(hObject,'Value') returns toggle state of radio_da_file



function edit_num_cam_Callback(hObject, eventdata, handles)
% hObject    handle to edit_num_cam (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_num_cam as text
%        str2double(get(hObject,'String')) returns contents of edit_num_cam as a double
edit_num_cam=str2double(get(hObject,'String'));
guidata(hObject,handles)

% --- Executes during object creation, after setting all properties.
function edit_num_cam_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_num_cam (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



function edit_fov_angle_Callback(hObject, eventdata, handles)
% hObject    handle to edit_fov_angle (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_fov_angle as text
%        str2double(get(hObject,'String')) returns contents of edit_fov_angle as a double
edit_fov_angle=str2double(get(hObject,'String'));
guidata(hObject,handles)

% --- Executes during object creation, after setting all properties.
function edit_fov_angle_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_fov_angle (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



function edit_fov_depth_Callback(hObject, eventdata, handles)
% hObject    handle to edit_fov_depth (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_fov_depth as text
%        str2double(get(hObject,'String')) returns contents of edit_fov_depth as a double
edit_fov_depth=str2double(get(hObject,'String'));
guidata(hObject,handles)

% --- Executes during object creation, after setting all properties.
function edit_fov_depth_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_fov_depth (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



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

edit_dist_x=str2double(get(hObject,'String'));
guidata(hObject,handles)

% --- Executes during object creation, after setting all properties.
function edit_dist_x_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_dist_x (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



function edit_num_row_Callback(hObject, eventdata, handles)
% hObject    handle to edit_num_row (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_num_row as text
%        str2double(get(hObject,'String')) returns contents of edit_num_row as a double
edit_num_row=str2double(get(hObject,'String'));
guidata(hObject,handles)

% --- Executes during object creation, after setting all properties.
function edit_num_row_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_num_row (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



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

edit_dist_row=str2num(get(hObject,'String'));
guidata(hObject,handles)

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


% --- Executes during object creation, after setting all properties.
function edit_dist_row_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_dist_row (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 check_show_cam.
function check_show_cam_Callback(hObject, eventdata, handles)
% hObject    handle to check_show_cam (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_cam


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


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


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


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

set(handles.text_danger2,'Visible','off');
space     = [str2num(get(handles.edit_space,'String'))];
latovoxel = str2double(get(handles.edit_latovoxel,'String'));
if  isfield(handles,'voxels')
    rmfield(handles,'voxels');
end
handles.voxels=voxeling(space(1),space(2),space(3),latovoxel);
% set(handles.text_danger2,'Visible','off');
% if size(handles.voxels,2)>=500
%     set(handles.danger,'Visible','on');
% else
%     set(handles.danger,'Visible','off');
% end
%profile viewer
  
guidata(hObject,handles)

% --- Executes on button press in push_clear1.
function push_clear1_Callback(hObject, eventdata, handles)
% hObject    handle to push_clear1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
set(handles.check_show_voxels,'Value',0);
% set(handles.danger,'Visible','off');
cla(handles.plot_cams);
guidata(hObject, handles);

% --- Executes on button press in push_plot1.
function push_plot1_Callback(hObject, eventdata, handles)
% hObject    handle to push_plot1 (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);
%set(handles.check_show_voxels,'Value',1);
space     = [str2num(get(handles.edit_space,'String'))];
latovoxel = str2double(get(handles.edit_latovoxel,'String'));
set(handles.plot_cams,'XLim',[-2 space(1)+2],...
                      'YLim',[-2 space(2)+2],...
                      'ZLim',[-2 space(3)+2]);
% for i=1:size(handles.voxels,2)
%     %voxel(handles.voxels(i).position,[latovoxel latovoxel latovoxel],'g',0.08)
%     DrawCuboid( [latovoxel latovoxel latovoxel]',...
%                  handles.voxels(i).position,...
%                  [0,0,0], 'c', 0.5);
% end
%oppure
    temp=0;
    while temp<=space(1)
        h=patch([temp temp temp temp],[0 0 space(2) space(2)],[0 space(3) space(3) 0],'b');
        set(h,'facecolor','c','FaceAlpha',0.05,'EdgeColor', 'b');
        temp=temp+latovoxel;
    end

    temp=0;
    while temp<=space(3)
        h=patch([0 space(1) space(1) 0],[0 0 space(2) space(2)],[temp temp temp temp],'b');
        set(h,'facecolor','c','FaceAlpha',0.05,'EdgeColor', 'b');
        temp=temp+latovoxel;
    end

     temp=0;
    while temp<=space(2)
        h=patch([0 space(1) space(1) 0],[temp temp temp temp],[0 0 space(3) space(3)],'b');
        set(h,'facecolor','c','FaceAlpha',0.05,'EdgeColor', 'b');
        temp=temp+latovoxel;
    end
guidata(hObject,handles)

% --- Executes on button press in push_clear2.
function push_clear2_Callback(hObject, eventdata, handles)
% hObject    handle to push_clear2 (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_plot2.
function push_plot2_Callback(hObject, eventdata, handles)
% hObject    handle to push_plot2 (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);

space     = [str2num(get(handles.edit_space,'String'))];
latovoxel = str2double(get(handles.edit_latovoxel,'String'));
fov_angle = str2num(get(handles.edit_fov_angle,'String')) ;
dist      = [str2num(get(handles.edit_fov_depth,'String'))];

set(handles.plot_cams,'XLim',[-2 space(1)+2],...
                  'YLim',[-2 space(2)+2],...
                  'ZLim',[-2 space(3)+2]);
              
              
if get(handles.radio_da_file,'value')==1
    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')
            errordlg('Not a correct struct variable','Incorrect Selection','modal')
        end
    end 
else  
    % if handles.show==[0 0 0 0 0 0 0];
    %     set(handles.text_danger4,'Visible','on');
    % end
    num_cam   = str2double(get(handles.edit_num_cam,'String'));
   	row   = str2double(get(handles.edit_num_row,'String'));
    
    handles.cams = auto_position1(num_cam,space,1,row);
    %handles.cams = auto_position1(num_cam,space);
    for i=1:size(handles.cams,2)
        handles.cams(i).fov_angle=fov_angle;
        handles.cams(i).dist=dist;
        
        text(handles.cams(i).position(1),...
             handles.cams(i).position(2),...
             handles.cams(i).position(3)+0.45,...
             num2str(i),'fontsize',11,'color','m');
    end
            
    guidata(hObject,handles);
end

if isfield(handles,'voxels')
        handles.show=[get(handles.check_show_cam,'Value'),...
                      get(handles.check_show_versori,'Value'),...
                      get(handles.check_show_tronco,'Value'),...
                      get(handles.check_show_voxels,'Value'),...
                      0,0,0];
        plotcam3(handles.cams,space,handles.voxels,latovoxel,handles.show);
        set(handles.text_danger3,'Visible','off');
else
        set(handles.text_danger2,'Visible','on');
end

guidata(hObject,handles);


% --- 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,{'voxels','cams'})
  space     = [str2num(get(handles.edit_space,'String'))];
  latovoxel = str2double(get(handles.edit_latovoxel,'String'));
  fov_angle = str2num(get(handles.edit_fov_angle,'String'));
  dist      = str2num(get(handles.edit_fov_depth,'String'));
  pixels    = str2num(get(handles.edit_pixels,'String'));
  
  setappdata(0,'cams',handles.cams);
  setappdata(0,'voxels',handles.voxels);
  setappdata(0,'space',space);
  setappdata(0,'latovoxel',latovoxel);
  setappdata(0,'fov_angle',fov_angle);
  setappdata(0,'dist',dist);
  setappdata(0,'pixels',pixels);
  
  Discretizza
  close(position_interface)
  %prova3
  %plotcam()
  
    %richiamo la prossima finestra
else
    set(handles.text_danger3,'Visible','on');
    guidata(hObject,handles);
end
    

% --- If Enable == 'on', executes on mouse press in 5 pixel border.
% --- Otherwise, executes on mouse press in 5 pixel border or over text_danger1.
function text_danger1_ButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to text_danger1 (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_export_data.
function push_export_data_Callback(hObject, eventdata, handles)
% hObject    handle to push_export_data (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
space     = [str2num(get(handles.edit_space,'String'))];
latovoxel = str2double(get(handles.edit_latovoxel,'String'));
    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','space', space);
assignin('base','voxels',handles.voxels);
assignin('base','latovoxel',latovoxel);
worksp = evalin('base','who');
set(handles.listbox_workspace,'String',worksp);
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


% --- Executes when selected object is changed in uipanel_checkinsert.
function uipanel_insert_SelectionChangeFcn(hObject, eventdata, handles)
% hObject    handle to the selected object in uipanel_checkinsert 
% 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 get(handles.radio_da_file,'Value')==1
%     set(handles.listbox_workspace,'visible','on');
% else
%     set(handles.listbox_workspace,'visible','off');
% end

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



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


% --- Executes during object creation, after setting all properties.
function edit_pixels_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_pixels (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
