function varargout = gui(varargin)

gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @gui_OpeningFcn, ...
                   'gui_OutputFcn',  @gui_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 gui is made visible.

function gui_OpeningFcn(hObject, eventdata, handles, varargin)

global ideal
global barva
global faz
global delka_vstupu
global fast
global slow
global addr
global everything
global rot1 rot2 rot3
global pol
global pocet_sekci
global ver hor rot
global pom
% Choose default command line output for gui
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

DGD=sqrt(str2double(get(handles.edit_delka_trasy,'String')))*str2double(get(handles.edit_PMDq,'String'));
set(handles.textDGD,'String', DGD);
set(handles.fast_radio,'Value',1);
set(handles.slow_radio,'Value',1);
set(handles.add_radio,'Value',1);

pocet_sekci=1;
set(handles.edit_sekci,'String',pocet_sekci);
axes(handles.axes1);
cla;

set(handles.edit_PMDq,'String', 0);
set(handles.textDGD,'String', 0);
barva='g';
faz=zeros(1,pocet_sekci);
delka_vstupu =str2double(get(handles.edit_delka_trasy,'String'))*1000/pocet_sekci;

ideal=1;
fast=0;
slow=0;
addr=0;
everything=0;

if (get(handles.fast_radio,'Value') == get(handles.fast_radio,'Max')) 
    fast=1;
end
if (get(handles.slow_radio,'Value') == get(handles.slow_radio,'Max')) 
        slow=1;
end
if (get(handles.add_radio,'Value') == get(handles.add_radio,'Max')) 
            addr=1;
end  
pom=1;

% --- Outputs from this function are returned to the command line.
function checkbox1_Callback(hObject, eventdata, handles)
global v
global pom
if (get(hObject,'Value') == get(hObject,'Max'))
    % Checkbox is checked-take approriate action
    
if (pom==1)
    v=get(handles.axes1,'CameraPosition');
    pom=0;
end
    rotate3d on
else
    % Checkbox is not checked-take approriate action

set(handles.axes1,'CameraPosition',v);
rotate3d off

end

function varargout = gui_OutputFcn(hObject, eventdata, handles)

varargout{1} = handles.output;

% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)

global ideal
global barva
global faz
global delka_vstupu
global fast
global slow
global addr
global everything
global rot1 rot2 rot3
global pol
global pocet_sekci
global ver hor rot

user_entry = str2double(get(handles.edit_sekci,'String'));
pocet_sekci=user_entry;
axes(handles.axes1);
cla;
barva='r';
%faz = rand(1,pocet_sekci).*(2*pi);
faz=zeros(1,pocet_sekci);
delka_vstupu =str2double(get(handles.edit_delka_trasy,'String'))*1000/pocet_sekci;
delka_vstupu=round(delka_vstupu);
ideal=0;
fast=0;
slow=0;
addr=0;
everything=0;

if (get(handles.fast_radio,'Value') == get(handles.fast_radio,'Max')) 
    fast=1;
end
if (get(handles.slow_radio,'Value') == get(handles.slow_radio,'Max')) 
        slow=1;
end
if (get(handles.add_radio,'Value') == get(handles.add_radio,'Max')) 
            addr=1;
end     
       
   
    
mod3d

function pushbutton5_Callback(hObject, eventdata, handles)
global ideal
global barva
global faz
global delka_vstupu
global fast
global slow
global addr
global everything
global rot1 rot2 rot3
global pol
global pocet_sekci
global ver hor rot
pocet_sekci=1;
set(handles.edit_sekci,'String',pocet_sekci);
axes(handles.axes1);
cla;

set(handles.edit_PMDq,'String', 0);
set(handles.textDGD,'String', 0);
barva='g';
faz=zeros(1,pocet_sekci);
delka_vstupu =str2double(get(handles.edit_delka_trasy,'String'))*1000/pocet_sekci;
delka_vstupu=round(delka_vstupu);
ideal=1;
fast=0;
slow=0;
addr=0;
everything=0;

if (get(handles.fast_radio,'Value') == get(handles.fast_radio,'Max')) 
    fast=1;
end
if (get(handles.slow_radio,'Value') == get(handles.slow_radio,'Max')) 
        slow=1;
end
if (get(handles.add_radio,'Value') == get(handles.add_radio,'Max')) 
            addr=1;
end  
  mod3d

% --------------------------------------------------------------------
function FileMenu_Callback(hObject, eventdata, handles)
% --------------------------------------------------------------------
function OpenMenuItem_Callback(hObject, eventdata, handles)

file = uigetfile('*.fig');
if ~isequal(file, 0)
    open(file);
end

% --------------------------------------------------------------------
function PrintMenuItem_Callback(hObject, eventdata, handles)

printdlg(handles.figure1)

% --------------------------------------------------------------------
function CloseMenuItem_Callback(hObject, eventdata, handles)

selection = questdlg(['Close ' get(handles.figure1,'Name') '?'],...
                     ['Close ' get(handles.figure1,'Name') '...'],...
                     'Yes','No','Yes');
if strcmp(selection,'No')
    return;
end

delete(handles.figure1)


% --- Executes on selection change in popupmenu1.
function popupmenu1_Callback(hObject, eventdata, handles)

% --- Executes during object creation, after setting all properties.
function popupmenu1_CreateFcn(hObject, eventdata, handles)

if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end

set(hObject, 'String', {'plot(rand(5))', 'plot(sin(1:0.01:25))', 'bar(1:.5:10)', 'plot(membrane)', 'surf(peaks)'});

function edit2_Callback(hObject, eventdata, handles)

function edit_PMDq_Callback(hObject, eventdata, handles)

DGD=sqrt(str2double(get(handles.edit_delka_trasy,'String')))*str2double(get(handles.edit_PMDq,'String'));
set(handles.textDGD,'String', DGD);

% --- Executes during object creation, after setting all properties.
function edit_PMDq_CreateFcn(hObject, eventdata, handles)

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

% --- Executes during object creation, after setting all properties.
function edit2_CreateFcn(hObject, eventdata, handles)

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

% --- Executes on selection change in popupmenu2.
function popupmenu2_Callback(hObject, eventdata, handles)

% --- Executes during object creation, after setting all properties.
function popupmenu2_CreateFcn(hObject, eventdata, handles)

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

function edit_sekci_Callback(hObject, eventdata, handles)

% --- Executes during object creation, after setting all properties.
function edit_sekci_CreateFcn(hObject, eventdata, handles)

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

% --- Executes on selection change in popupmenu4.
function popupmenu4_Callback(hObject, eventdata, handles)

% --- Executes during object creation, after setting all properties.
function popupmenu4_CreateFcn(hObject, eventdata, handles)

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

% --- Executes on selection change in popupmenu5.
function popupmenu5_Callback(hObject, eventdata, handles)

% --- Executes during object creation, after setting all properties.
function popupmenu5_CreateFcn(hObject, eventdata, handles)

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

function edit_delka_trasy_Callback(hObject, eventdata, handles)

DGD=sqrt(str2double(get(handles.edit_delka_trasy,'String')))*str2double(get(handles.edit_PMDq,'String'));
set(handles.textDGD,'String', DGD);

% --- Executes during object creation, after setting all properties.
function edit_delka_trasy_CreateFcn(hObject, eventdata, handles)

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

% --- Executes on button press in fast_radio.
function fast_radio_Callback(hObject, eventdata, handles)

global ideal
global barva
global faz
global delka_vstupu
global fast
global slow
global addr
global everything
global rot1 rot2 rot3
global pocet_sekci
global pol
global ver hor rot
% Hint: get(hObject,'Value') returns toggle state of fast_radio
fast=0;
slow=0;
addr=0;

if (get(handles.fast_radio,'Value') == get(handles.fast_radio,'Max')) 
    fast=1;
end
if (get(handles.slow_radio,'Value') == get(handles.slow_radio,'Max')) 
        slow=1;
end
if (get(handles.add_radio,'Value') == get(handles.add_radio,'Max')) 
            addr=1;
end 
axes(handles.axes1);
cla;

if(ideal==0)
for m=1:pocet_sekci
% vertikalni

if(fast==1)

graf=plot3((m-1)*delka_vstupu+1:1:(m-1)*delka_vstupu+delka_vstupu, zeros(1,delka_vstupu), ver((m-1)*delka_vstupu+1:(m-1)*delka_vstupu+delka_vstupu),'b:'); zlabel('vertikalni');

rotate(graf,[1 0 0],rot(m:m));hold on;
end
%
% horizontalni
if(slow==1)
graf1=plot3((m-1)*delka_vstupu+1:1:(m-1)*delka_vstupu+delka_vstupu, hor((m-1)*delka_vstupu+1:(m-1)*delka_vstupu+delka_vstupu), zeros(1,delka_vstupu),'r:'); ylabel('horizontalni');hold on;
rotate(graf1,[1 0 0],rot(m:m));
end
% soucet
if(addr==1)

pom_pol = ver((m-1)*delka_vstupu+1:(m-1)*delka_vstupu+delka_vstupu) + hor((m-1)*delka_vstupu+1:(m-1)*delka_vstupu+delka_vstupu)*i;
pol=cat(1,pol,pom_pol(:));


% polarizace

graf2=plot3((m-1)*delka_vstupu+1:1:(m-1)*delka_vstupu+delka_vstupu,real(pol((m-1)*delka_vstupu+1:(m-1)*delka_vstupu+delka_vstupu)),imag(pol((m-1)*delka_vstupu+1:(m-1)*delka_vstupu+delka_vstupu)),'g');hold on;
rotate(graf2,[1 0 0],rot(m:m));

end
end

else

  
% vertikalni
if(fast==1)
plot3(1:1:delka_vstupu*pocet_sekci, zeros(1,delka_vstupu*pocet_sekci), ver(1:delka_vstupu*pocet_sekci),':'); zlabel('vertikalni'); hold on; 
hold on;

end
% horizontalni
if(slow==1)
plot3(1:1:delka_vstupu*pocet_sekci, hor(1:delka_vstupu*pocet_sekci), zeros(1,delka_vstupu*pocet_sekci),'r:'); ylabel('horizontalni');hold on;
hold on;
end

if(addr==1)
% soucet
% polarizace
plot3(1:1:delka_vstupu*pocet_sekci,real(pol(1:delka_vstupu*pocet_sekci)),imag(pol(1:delka_vstupu*pocet_sekci)),barva);hold on;
hold on;
end
end
% --- Executes on button press in slow_radio.
function slow_radio_Callback(hObject, eventdata, handles)

global ideal
global barva
global faz
global delka_vstupu
global fast
global slow
global addr
global everything
global rot1 rot2 rot3
global pocet_sekci
global pol
global ver hor rot
% Hint: get(hObject,'Value') returns toggle state of fast_radio
fast=0;
slow=0;
addr=0;

if (get(handles.fast_radio,'Value') == get(handles.fast_radio,'Max')) 
    fast=1;
end
if (get(handles.slow_radio,'Value') == get(handles.slow_radio,'Max')) 
        slow=1;
end
if (get(handles.add_radio,'Value') == get(handles.add_radio,'Max')) 
            addr=1;
end 
axes(handles.axes1);
cla;

if(ideal==0)
for m=1:pocet_sekci
% vertikalni

if(fast==1)

graf=plot3((m-1)*delka_vstupu+1:1:(m-1)*delka_vstupu+delka_vstupu, zeros(1,delka_vstupu), ver((m-1)*delka_vstupu+1:(m-1)*delka_vstupu+delka_vstupu),'b:'); zlabel('vertikalni');

rotate(graf,[1 0 0],rot(m:m));hold on;
end
%
% horizontalni
if(slow==1)
graf1=plot3((m-1)*delka_vstupu+1:1:(m-1)*delka_vstupu+delka_vstupu, hor((m-1)*delka_vstupu+1:(m-1)*delka_vstupu+delka_vstupu), zeros(1,delka_vstupu),'r:'); ylabel('horizontalni');hold on;
rotate(graf1,[1 0 0],rot(m:m));
end
% soucet
if(addr==1)

pom_pol = ver((m-1)*delka_vstupu+1:(m-1)*delka_vstupu+delka_vstupu) + hor((m-1)*delka_vstupu+1:(m-1)*delka_vstupu+delka_vstupu)*i;
pol=cat(1,pol,pom_pol(:));

% polarizace

graf2=plot3((m-1)*delka_vstupu+1:1:(m-1)*delka_vstupu+delka_vstupu,real(pol((m-1)*delka_vstupu+1:(m-1)*delka_vstupu+delka_vstupu)),imag(pol((m-1)*delka_vstupu+1:(m-1)*delka_vstupu+delka_vstupu)),'g');hold on;
rotate(graf2,[1 0 0],rot(m:m));

end
end

else

   
% vertikalni
if(fast==1)
plot3(1:1:delka_vstupu*pocet_sekci, zeros(1,delka_vstupu*pocet_sekci), ver(1:delka_vstupu*pocet_sekci),':'); zlabel('vertikalni'); hold on; 
hold on;

end
% horizontalni
if(slow==1)
plot3(1:1:delka_vstupu*pocet_sekci, hor(1:delka_vstupu*pocet_sekci), zeros(1,delka_vstupu*pocet_sekci),'r:'); ylabel('horizontalni');hold on;
hold on;
end

if(addr==1)
% soucet


% polarizace

plot3(1:1:delka_vstupu*pocet_sekci,real(pol(1:delka_vstupu*pocet_sekci)),imag(pol(1:delka_vstupu*pocet_sekci)),barva);hold on;
hold on;
end
end

% --- Executes on button press in add_radio.
function add_radio_Callback(hObject, eventdata, handles)

global ideal
global barva
global faz
global delka_vstupu
global fast
global slow
global addr
global everything
global rot1 rot2 rot3
global pocet_sekci
global pol
global ver hor rot
% Hint: get(hObject,'Value') returns toggle state of fast_radio
fast=0;
slow=0;
addr=0;


if (get(handles.fast_radio,'Value') == get(handles.fast_radio,'Max')) 
    fast=1;
end
if (get(handles.slow_radio,'Value') == get(handles.slow_radio,'Max')) 
        slow=1;
end
if (get(handles.add_radio,'Value') == get(handles.add_radio,'Max')) 
            addr=1;
end 
axes(handles.axes1);
cla;

if(ideal==0)
for m=1:pocet_sekci
% vertikalni

if(fast==1)

graf=plot3((m-1)*delka_vstupu+1:1:(m-1)*delka_vstupu+delka_vstupu, zeros(1,delka_vstupu), ver((m-1)*delka_vstupu+1:(m-1)*delka_vstupu+delka_vstupu),'b:'); zlabel('vertikalni');

rotate(graf,[1 0 0],rot(m:m));hold on;
end
%
% horizontalni
if(slow==1)
graf1=plot3((m-1)*delka_vstupu+1:1:(m-1)*delka_vstupu+delka_vstupu, hor((m-1)*delka_vstupu+1:(m-1)*delka_vstupu+delka_vstupu), zeros(1,delka_vstupu),'r:'); ylabel('horizontalni');hold on;
rotate(graf1,[1 0 0],rot(m:m));
end
% soucet
if(addr==1)

pom_pol = ver((m-1)*delka_vstupu+1:(m-1)*delka_vstupu+delka_vstupu) + hor((m-1)*delka_vstupu+1:(m-1)*delka_vstupu+delka_vstupu)*i;
pol=cat(1,pol,pom_pol(:));

% polarizace

graf2=plot3((m-1)*delka_vstupu+1:1:(m-1)*delka_vstupu+delka_vstupu,real(pol((m-1)*delka_vstupu+1:(m-1)*delka_vstupu+delka_vstupu)),imag(pol((m-1)*delka_vstupu+1:(m-1)*delka_vstupu+delka_vstupu)),'g');hold on;
rotate(graf2,[1 0 0],rot(m:m));

end
end

else
   
% vertikalni
if(fast==1)
plot3(1:1:delka_vstupu*pocet_sekci, zeros(1,delka_vstupu*pocet_sekci), ver(1:delka_vstupu*pocet_sekci),':'); zlabel('vertikalni'); hold on; 
hold on;

end
% horizontalni
if(slow==1)
plot3(1:1:delka_vstupu*pocet_sekci, hor(1:delka_vstupu*pocet_sekci), zeros(1,delka_vstupu*pocet_sekci),'r:'); ylabel('horizontalni');hold on;
hold on;
end

if(addr==1)
% soucet


% polarizace

plot3(1:1:delka_vstupu*pocet_sekci,real(pol(1:delka_vstupu*pocet_sekci)),imag(pol(1:delka_vstupu*pocet_sekci)),barva);hold on;
hold on;
end
end

