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

% Last Modified by GUIDE v2.5 20-May-2018 23:15:21

% Begin initialization code - DO NOT EDIT
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)
% 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 gui (see VARARGIN)

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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = gui_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 Nacitanie.
function Nacitanie_Callback(hObject, eventdata, handles)
% hObject    handle to Nacitanie (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% NAČÍTANIE DÁT

[dicom_data] = uigetfile('*.*', 'Nacitaj DICOM subor');

hwaitbar = waitbar(0,'Načítanie DICOM súboru');

nacitanie = dicomread(dicom_data);
global nacitanie

delete(hwaitbar);

info = dicominfo(dicom_data);
global info

zobraz_prvy = image(nacitanie(:,:,:,1));
colormap(gray(256));
drawnow;

min_value = set(handles.Slider,'Min',1);
max_value = set(handles.Slider,'Max',info.NumberOfFrames);
set(handles.Slider,'Value',1);

guidata(hObject, handles);



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

% Hint: slider controls usually have a light gray background.

if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end

guidata(hObject, handles);


% --- Executes on slider movement.
function Slider_Callback(hObject, eventdata, handles)
% hObject    handle to Slider (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,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider

% POSÚVAČ

global nacitanie
global info


slider_location = round(get(handles.Slider,'Value'));
slider_value = set(handles.Slider,'Value', slider_location);

axes(handles.axes1);
image(nacitanie(:,:,slider_location));
drawnow;


guidata(hObject, handles);



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


guidata(hObject, handles);


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

% PREHRATIE/POZASTAVENIE SMYČKY

global nacitanie
global info
global ih

nFrames = info.NumberOfFrames;
frame_time = info.FrameTime;


start_f=get(handles.Slider,'Value');

while start_f <= nFrames
    
    if get(hObject,'Value')
        ih = image(nacitanie(:,:,:,start_f));
        colormap(gray(256));
        set(handles.Slider,'Value',start_f);
        start_f = start_f + 1;
        set(handles.Prehrat, 'String', 'Pozastaviť');
    else
         set(handles.Prehrat, 'String', 'Prehrať');
        break
    end
    
    drawnow;
    pause(0.01);
end

guidata(hObject, handles);


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

global nacitanie
global info
global ih

% Vykreslenie oblasti komoryy
area_line = imfreehand(gca);
pos = getPosition(area_line);

% Vykreslenie dlzky L
ciara = imline(gca);
pos_line = getPosition(ciara);

p1 = pos_line(1,:);
p2 = pos_line(2,:);

dist_line = sqrt((p1(1) - p2(1))^2 + (p1(2) - p2(2))^2);

start_line = pos_line(1,:);
end_line = pos_line(2,:);

n = 15;
t = linspace(0,1,n);  
    
v = end_line - start_line;
x = start_line(1) + t*v(1);
y = start_line(2) + t*v(2);


sinx = pos(:,1);
siny = 280-pos(:,2);

dlzkaKolmic = zeros(n , 1);
KROK = 0.5;
for i = 1:n;
    xTemp = [x(i)+v(2), x(i)-v(2)];
    yTemp = 280-[y(i)-v(1), y(i)+v(1)];

    [coefPriamka] = lstSq(xTemp, yTemp);
    xPriamka = min(sinx)-5  : KROK : max(sinx)+5;
    
    yPriamka = coefPriamka(1) + coefPriamka(2).*xPriamka; 
    euklVzdial = zeros(size(xPriamka));
    euklVzdialPos =  zeros(size(xPriamka));
    for k = 1 : length(xPriamka);
        [euklVzdial(k), euklVzdialPos(k)] = min(abs(sqrt((sinx-xPriamka(k)).^2 + (siny-yPriamka(k)).^2)));
    end
    euklVzdial(euklVzdial > 3) = 10;
    [~, xBod] = findPeaksBetter(-euklVzdial);
    
    yBod = yPriamka(xBod);
    xBod = xPriamka(xBod);
    try
        dlzkaKolmic(i) = sqrt(((xBod(end)-xBod(1))^2) + ((yBod(end)-yBod(1))^2));
    end

end
axis equal

assignin('base', 'ED_Simps', dlzkaKolmic);

for i = 1 : length(dlzkaKolmic);
    
    EDV(i) = dlzkaKolmic(i)*dlzkaKolmic(i)*(dist_line/15);
    
end

EDV_Simps = sum(EDV)*(pi/4);
assignin('base', 'EDV_Simps', EDV_Simps);

global EDV_Simps

guidata(hObject, handles);



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

% VYKRESLENIE End-diastoly

global nacitanie
global info
global ih


ciara = imline(gca);
ED_line = getPosition(ciara);
p1 = ED_line(1,:);
p2 = ED_line(2,:);

global EDD
EDD = sqrt((p1(1) - p2(1))^2 + (p1(2) - p2(2))^2);

assignin('base', 'EDD', EDD);

guidata(hObject, handles);


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

% VÝPOČET EF TEICHNOLTZ

global nacitanie
global info
global ih

global EDD
global ESD

EDD = evalin('base', 'EDD');
ESD = evalin('base', 'ESD');

LVEDV = (7*EDD^3)/(2.4+EDD);
LVESV = (7*ESD^3)/(2.4+ESD);

EF_Teich = ((LVEDV-LVESV)/LVEDV)*100;

EF_Teich = round(EF_Teich,0);
text = [num2str(EF_Teich) ' %'];
set(handles.text5, 'String', text);

guidata(hObject, handles);

% --- Executes during object creation, after setting all properties.
function Teichholz_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Teichholz (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 Vymazat.
function Vymazat_Callback(hObject, eventdata, handles)
% hObject    handle to Vymazat (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% evalin( 'base', 'clear variables' )

% VYMAZANIE PREMENNÝCH

clearvars -global
evalin('base', 'clear variables');
cla reset;
clc;
text1 = 'Ejekčná frakcia';
text2 = 'Frakčné skrát.';
set(handles.text4, 'String', text1);
set(handles.text5, 'String', text1);
set(handles.text6, 'String', text1);
set(handles.text7, 'String', text2);
set(handles.text8, 'String','Hmotnosť ĽK');
set(handles.text9, 'String','Rel. hrúbka steny');
slider_value = set(handles.Slider,'Value', 1);

guidata(hObject, handles);


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

% LV MASS VYPOCET (end-diastola)

global nacitanie
global info
global ih

global IVS
global LVID
global PW

IVS = evalin('base', 'IVS');
LVID = evalin('base', 'LVID');
PW = evalin('base', 'PW');

IVS = (IVS*10)/27 % prepočet na mm
LVID = (LVID*10)/27 % prepočet na mm
PW = (PW*10)/27 % prepočet na mm

LV_vol = (IVS+LVID+PW)^3; % LV volume (objem ľavej komory)
VBP = (LVID)^3; % Volume of blood pool (objem krvi)
M_vol = LV_vol-VBP; % Myocardial volume (objem myokardu)

LV_mass = (0.8*(1.04*M_vol)+0.6)/1000; % LV Mass (hmotnosť ľavej komory)

LV_mass = round(LV_mass,0);
text = [num2str(LV_mass) ' g'];
set(handles.text8,'String',text);

RWT = (2*PW)/LVID; % Relative wall thickness - relatívna hrúbka steny
RWT = round(RWT,2);
set(handles.text9, 'String', RWT);

guidata(hObject, handles);


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

% VYKRESLENIE VZDIALENOSTI END-SYSTOLY

ciara = imline(gca);
ES_line = getPosition(ciara);
p1 = ES_line(1,:);
p2 = ES_line(2,:);

global ESD
ESD = sqrt((p1(1) - p2(1))^2 + (p1(2) - p2(2))^2);

assignin('base', 'ESD', ESD);

guidata(hObject, handles);


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

global nacitanie
global info
global ih

% Vykreslenie oblasti komory
area_line = imfreehand(gca);
pos = getPosition(area_line);

% Vykreslenie dĺžky L
ciara = imline(gca);
pos_line = getPosition(ciara);
p1 = pos_line(1,:);
p2 = pos_line(2,:);

dist_line = sqrt((p1(1) - p2(1))^2 + (p1(2) - p2(2))^2);


start_line = pos_line(1,:);
end_line = pos_line(2,:);


n = 15;
t = linspace(0,1,n);  
    
v = end_line - start_line;
x = start_line(1) + t*v(1);
y = start_line(2) + t*v(2);

sinx = pos(:,1);
siny = 280-pos(:,2);

dlzkaKolmic = zeros(n , 1);
KROK = 0.5;
for i = 1:n;
    xTemp = [x(i)+v(2), x(i)-v(2)];
    yTemp = 280-[y(i)-v(1), y(i)+v(1)];

    [coefPriamka] = lstSq(xTemp, yTemp);
    xPriamka = min(sinx)-5  : KROK : max(sinx)+5;
    
    yPriamka = coefPriamka(1) + coefPriamka(2).*xPriamka;
    euklVzdial = zeros(size(xPriamka));
    euklVzdialPos =  zeros(size(xPriamka));
    for k = 1 : length(xPriamka);
        [euklVzdial(k), euklVzdialPos(k)] = min(abs(sqrt((sinx-xPriamka(k)).^2 + (siny-yPriamka(k)).^2)));
    end
    euklVzdial(euklVzdial > 3) = 10;
    [~, xBod] = findPeaksBetter(-euklVzdial);
    
    yBod = yPriamka(xBod);
    xBod = xPriamka(xBod);
    try
        dlzkaKolmic(i) = sqrt(((xBod(end)-xBod(1))^2) + ((yBod(end)-yBod(1))^2));
    end

end
axis equal

assignin('base', 'ES_Simps', dlzkaKolmic);

for i = 1 : length(dlzkaKolmic);
    
    ESV(i) = dlzkaKolmic(i)*dlzkaKolmic(i)*(dist_line/15);
    
end

global ESV_Simps
ESV_Simps = sum(ESV)*(pi/4);
assignin('base', 'ESV_Simps', ESV_Simps);


guidata(hObject, handles);

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

% EJEKČNÁ FRAKCIA SIMPSONOVA METÓDA

global nacitanie
global info
global ih

global EDV_Simps
global ESV_Simps
global EF_Simps

if EDV_Simps > ESV_Simps
   EF_Simpson = ((EDV_Simps - ESV_Simps)/EDV_Simps)*100;

else
    EF_Simpson = 0;
end

EF_Simpson = round(EF_Simpson,0);
text = [num2str(EF_Simpson) ' %'];
set(handles.text4, 'String', text);


guidata(hObject, handles);


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

% FRAKČNÉ SKRÁTENIE

global nacitanie
global info
global ih

global EDD
global ESD

FS = ((EDD-ESD)/EDD)*100;
FS = round(FS,0);
text = [num2str(FS) ' %'];
set(handles.text7, 'String', text);

guidata(hObject, handles);


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

% EKEJKČNÁ FRAKCIA CUBE METÓDA

global nacitanie
global info
global ih

global EDD
global ESD

LVEDV = EDD^3;
LVESV = ESD^3;

EF_Cube = ((LVEDV-LVESV)/LVEDV)*100;
EF_Cube = round(EF_Cube,0);
text = [num2str(EF_Cube) ' %'];
set(handles.text6, 'String',text);

guidata(hObject, handles);


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

% Vykreslenie vzdialeností LV Mass

ciara = imline(gca);
IVS_line = getPosition(ciara);
p1 = IVS_line(1,:);
p2 = IVS_line(2,:);
global IVS
IVS = sqrt((p1(1) - p2(1))^2 + (p1(2) - p2(2))^2);
assignin('base', 'IVS', IVS);

ciara = imline(gca);
LVID_line = getPosition(ciara);
p1 = LVID_line(1,:);
p2 = LVID_line(2,:);
global LVID
LVID = sqrt((p1(1) - p2(1))^2 + (p1(2) - p2(2))^2);
assignin('base', 'LVID', LVID);

ciara = imline(gca);
PW_line = getPosition(ciara);
p1 = PW_line(1,:);
p2 = PW_line(2,:);
global PW
PW = sqrt((p1(1) - p2(1))^2 + (p1(2) - p2(2))^2);
assignin('base', 'PW', PW);

guidata(hObject, handles);
