function facpack
% The toolbox FACPACK provides software for the following problems in
% multivariate curve resolution:
% 1. baseline correction,
% 2. computation of 2- and 3-component factorizations and the area of feasible
%    solutions,
% 3. computation of 4-component factorizations and the area of feasible
%    solutions,
% 4. generalized Borgen plots for the computation of the AFS for data 
%    including slightly negative entries
% 5. combination of complementarity and AFS for 3-component systems
% 6. combination of complementarity and AFS for 4-component systems
% 7. peak group analysis for the extraction of pure component spectra
% 8. FACPACK library to view, export, add or delete pure component spectra
%    or concentration profiles
% Copyright (c) 2013 Mathias Sawall, Annekathrin Juerss, University of Rostock, Department of Mathematics.
% Copyright (c) 2023 Mathias Sawall, Annekathrin Juerss, Tomass Andersons, Martina Beese, Lukas Prestin, University of Rostock, Department of Mathematics.
% 
% The software can be used for academic, research and other similar noncommercial uses. The user
% acknowledges that this software is still in the development stage and that it is provided by the
% copyright holders and contributors "as is" and any express or implied warranties, fitness for a
% particular purpose are disclaimed. In no event shall the copyright owner or contributors be liable
% for any direct, indirect, incidental, special, exemplary, or consequential damages.
% 
% The copyright holders provide no reassurances that the source code provided does not infringe any
% patent, copyright, or any other intellectual property rights of third parties. The copyright
% holders disclaim any liability to any recipient for claims brought against recipient by any third
% party for infringement of that parties intellectual property rights.
% 
% The user has to respect the lapack license, see http://www.netlib.org/lapack/LICENSE.txt, and
% the license for the ACM routine nl2sol, see http://www.acm.org/publications/policies/softwarecrnotice,
% which are used in the associated C-code. Furthermore the user has to respect the license for the 
% file Mouse3D.m from the Mathworks File Exchange, see 
% http://www.mathworks.com/matlabcentral/fileexchange/view_license?file_info_id=18266

% Starting with this version, we use AI-assisted tools to modernize the FACPACK front end and ensure 
% its functionality with new versions of MATLAB. To maintain long-term compatibility, we are migrating 
% the interface from the legacy GUIDE/uicontrol system to the modern App Designer framework. The 
% classic structure will remain available for older releases.

version = 2.2;
fname = mfilename;
calcf = mfilename('fullpath');
calcf = calcf(1:end-length(fname));
calcfp = [calcf, 'subfcnsFP'];
path(path, calcf);
path(path, calcfp);

pause(.1);

scrsz = get(0,'ScreenSize');
figWidth = scrsz(3)/1.5;
figHeight = scrsz(4)/3;

if verLessThan('matlab', '23.2')
    fig = figure(200);
    set(fig,'Position',[scrsz(3)/4 scrsz(4)/3 scrsz(3)/1.5 scrsz(4)/3]);
    pause(.5);
    figpos = get(fig,'Position');
    set(fig,'Name',['FACPACK (version ',num2str(version, '%2.2f'),'-alpha)'],'NumberTitle','off',...
        'MenuBar','none','Toolbar','none','color','white','Visible','off');
    
    if ispc
        basicfsz = 3;
        basichi = 0.06;
    else
        basicfsz = 5;
        basichi = 0.05;
    end
else
    fig = uifigure('Name', ['FACPACK (version ', num2str(version, '%2.2f'), '-alpha)'], ...
        'Position', [scrsz(3)/4, scrsz(4)/3, figWidth, figHeight], ...
        'Color','white','Scrollable','on','Visible','off');
    basicfsz = 10; 
end

if ispc
    oldbasicfsz = 5;
else
    oldbasicfsz = 5;
end

sty = 'Helvetica';
bgc = 0.95*ones(3,1);

nb = 1;

na = 4;
ha = 0.8/na;
fa = 1.1;
hz = 0.9;
dis = 0.01;
ncol = 4;
wd0 = 0.135;
wd = (1-wd0-(ncol+2)*dis)/ncol;

nc = 0;
if verLessThan('matlab', '23.2')
    Catergory(1) = uicontrol('Style','text','String','Basic Modules',...
        'HorizontalAlignment','center','Fontsize',2.8*basicfsz,'BackgroundColor','w',...
        'Units','normalized','FontName',sty,'FontWeight','bold','Position',[(nc+1)*dis+nc*wd, hz-fa*nb*ha-0.03, wd0, ha]);

    Catergory(1) = uicontrol('Style','text','String','Expert Modules',...
        'HorizontalAlignment','center','Fontsize',2.8*basicfsz,'BackgroundColor','w',...
        'Units','normalized','FontName',sty,'FontWeight','bold','Position',[(nc+1)*dis+nc*wd, hz-fa*(nb+1)*ha-0.07, wd0, ha]);


    Catergory(2) = uicontrol('Style','text','String','Peak Group Analysis',...
        'HorizontalAlignment','center','Fontsize',2.4*basicfsz,'BackgroundColor','w',...
        'Units','normalized','FontName',sty,'FontWeight','bold','Position',[(nc+2)*dis+nc*wd+wd0, 0.91, wd, 0.06]);

    Button(nb) = uicontrol('Style','pushbutton','String',...
        '<html><center>Peak Group Analysis<br/>(for series of IR and Raman spectra)<html><center>',...
        'HorizontalAlignment','left','Fontsize',2*basicfsz,'FontWeight','bold',...
        'Units','normalized','FontName',sty,'Position',[(nc+2)*dis+nc*wd+wd0, hz-fa*nb*ha+0.02, wd, ha],...
        'Callback',{@buttonPGA, version, oldbasicfsz});
    nb = nb+1;

    Button(nb) = uicontrol('Style','pushbutton','String','Pure Component Decomposition',...
        'HorizontalAlignment','left','Fontsize',2*basicfsz,'FontWeight','bold',...
        'Units','normalized','FontName',sty,'Position',[(nc+2)*dis+nc*wd+wd0, hz-fa*nb*ha, wd, ha],'Enable','off',...
        'Callback',{@buttonPCD, version, oldbasicfsz});
    nb = nb+1;

    Button(nb) = uicontrol('Style','pushbutton','String',...
        '<html><center>Individual FACPACK library<br/>(user saved profiles)<html><center>',...
        'HorizontalAlignment','left','Fontsize',2*basicfsz,'FontWeight','bold',...
        'Units','normalized','FontName',sty,'Position',[(nc+2)*dis+nc*wd+wd0, hz-fa*nb*ha, wd, ha],...
        'Callback',{@buttonLIB, version, oldbasicfsz});
    nb = nb+1;
    nc = nc+1;


    Catergory(3) = uicontrol('Style','text','String','Area of Feasible Solutions',...
        'HorizontalAlignment','center','Fontsize',2.4*basicfsz,'BackgroundColor','w',...
        'Units','normalized','FontName',sty,'FontWeight','bold','Position',[(nc+2)*dis+nc*wd+wd0, 0.91, wd, 0.06]);
    Button(nb) = uicontrol('Style','pushbutton','String',...
        '<html><center>Polygon Inflation<br/>(2 & 3 components)</center></html>','HorizontalAlignment','left',...
        'Fontsize',2*basicfsz,'FontWeight','bold',...
        'Units','normalized','FontName',sty,'Position',[(nc+2)*dis+nc*wd+wd0, hz-fa*(nb-3)*ha+0.02, wd, ha],...
        'Callback',{@buttonAFS, version, oldbasicfsz});
    nb = nb+1;

    Button(nb) = uicontrol('Style','pushbutton','String',...
        '<html><center>Ray Casting<br/>(4 components)<html><center>','HorizontalAlignment','left',...
        'Fontsize',2*basicfsz,'FontWeight','bold',...
        'Units','normalized','FontName',sty,'Position',[(nc+2)*dis+nc*wd+wd0, hz-fa*(nb-3)*ha, wd, ha],...
        'Callback',{@buttonRAY, version, oldbasicfsz});
    nb = nb+1;

    Button(nb) = uicontrol('Style','pushbutton','String',...
        '<html><center>Generalized Borgen Plots<br/>(3 components)</center></html>','HorizontalAlignment','left',...
        'Fontsize',2*basicfsz,'FontWeight','bold',...
        'Units','normalized','FontName',sty,'Position',[(nc+2)*dis+nc*wd+wd0, hz-fa*(nb-3)*ha, wd, ha],...
        'Callback',{@buttonBKP, version, oldbasicfsz});
    nb = nb+1;

    Button(nb) = uicontrol('Style','pushbutton','String','Analytical inner boundary curve',...
        'HorizontalAlignment','left','Fontsize',2*basicfsz,'FontWeight','bold','Enable','on',...
        'Units','normalized','FontName',sty,'Position',[(nc+2)*dis+nc*wd+wd0, hz-fa*(nb-3)*ha, wd, ha],...
        'Callback',{@buttonIBC, version});
    nb = nb+1;
    nc = nc+1;


    Catergory(4) = uicontrol('Style','text','String','Duality & Area of Feasibe solutions',...
        'HorizontalAlignment','center','Fontsize',2.4*basicfsz,'BackgroundColor','w',...
        'Units','normalized','FontName',sty,'FontWeight','bold','Position',[(nc+2)*dis+nc*wd+wd0, 0.91, wd, 0.06]);
    Button(nb) = uicontrol('Style','pushbutton','String',...
        '<html><center>Duality & AFS<br/>(3 components)<html><center>','HorizontalAlignment','left',...
        'Fontsize',2*basicfsz,'FontWeight','bold',...
        'Units','normalized','FontName',sty,'Position',[(nc+2)*dis+nc*wd+wd0, hz-fa*(nb-7)*ha+0.02, wd, ha],...
        'Callback',{@buttonCAA, version, oldbasicfsz});
    nb = nb+1;

    Button(nb) = uicontrol('Style','pushbutton','String',...
        '<html><center>Duality & AFS<br/>(4 components)<html><center>','HorizontalAlignment','left',...
        'Fontsize',2*basicfsz,'FontWeight','bold',...
        'Units','normalized','FontName',sty,'Position',[(nc+2)*dis+nc*wd+wd0, hz-fa*(nb-7)*ha, wd, ha],...
        'Callback',{@buttonCA4, version, oldbasicfsz});
    nb = nb+1;
    nc = nc+1;


    Catergory(1) = uicontrol('Style','text','String','Data Preparation and Analysis',...
        'HorizontalAlignment','center','Fontsize',2.4*basicfsz,'BackgroundColor','w',...
        'Units','normalized','FontName',sty,'FontWeight','bold','Position',[(nc+2)*dis+nc*wd+wd0, 0.91, wd, 0.06]);

    Button(nb) = uicontrol('Style','pushbutton','String','Baseline Correction','HorizontalAlignment','left',...
        'Fontsize',2*basicfsz,'FontWeight','bold',...
        'Units','normalized','FontName',sty,'Position',[(nc+2)*dis+nc*wd+wd0,  hz-fa*(nb-9)*ha+0.02, wd, ha],...
        'Callback',{@buttonBLA, version, oldbasicfsz});
    nb = nb+1;

    Button(nb) = uicontrol('Style','pushbutton','String',...
        '<html><center>Essential Spectral Information<br/>(also for noisy data)</center></html>',...
        'HorizontalAlignment','left','Fontsize',2*basicfsz,'FontWeight','bold',...
        'Units','normalized','FontName',sty,'Position',[(nc+2)*dis+nc*wd+wd0, hz-fa*(nb-9)*ha, wd, ha],...
        'Callback',{@buttonEDP, version, oldbasicfsz});
    nb = nb+1;

    Button(nb) = uicontrol('Style','pushbutton','String',...
        '<html><center>Inner & Outer Polytopes<br/>(also for noisy data)</center></html>','HorizontalAlignment','left',...
        'Fontsize',2*basicfsz,'FontWeight','bold',...
        'Units','normalized','FontName',sty,'Position',[(nc+2)*dis+nc*wd+wd0, hz-fa*(nb-9)*ha, wd, ha],...
        'Callback',{@buttonIPFP, version, oldbasicfsz});
    nb = nb+1;

    Button(nb) = uicontrol('Style','pushbutton','String','Matrix from Borgen plots',...
        'HorizontalAlignment','left','Fontsize',2*basicfsz,'FontWeight','bold','Enable','on',...
        'Units','normalized','FontName',sty,'Position',[(nc+2)*dis+nc*wd+wd0, hz-fa*(nb-9)*ha, wd, ha],...
        'Callback',{@buttonIP4D, version});
    nb = nb+1;
    nc = nc+1;
else
    pos = @(nPos) [nPos(1)*figWidth, nPos(2)*figHeight, nPos(3)*figWidth, nPos(4)*figHeight];
    uilabel(fig, 'Text', 'Basic Modules', 'HorizontalAlignment', 'center', ...
        'FontSize', 1.5*basicfsz, 'FontName', sty, 'FontWeight', 'bold', ...
        'BackgroundColor', 'white', 'Position', pos([(nc+1)*dis+nc*wd, hz-fa*nb*ha-0.03, wd0, ha]));

    uilabel(fig, 'Text', 'Expert Modules', 'HorizontalAlignment', 'center', ...
        'FontSize', 1.5*basicfsz, 'FontName', sty, 'FontWeight', 'bold', ...
        'BackgroundColor', 'white', 'Position', pos([(nc+1)*dis+nc*wd, hz-fa*(nb+1)*ha-0.07, wd0, ha]));
    
    
    nc = 0;
    uilabel(fig, 'Text', 'Peak Group Analysis', 'HorizontalAlignment', 'center', ...
        'FontSize', 1.2*basicfsz, 'FontName', sty, 'FontWeight', 'bold', ...
        'BackgroundColor', 'white', 'Position', pos([(nc+2)*dis+nc*wd+wd0, 0.91, wd, 0.06]));

    uibutton(fig, 'push', 'Text', {'Peak Group Analysis', '(for series of IR and Raman spectra)'}, ...
        'FontSize', basicfsz, 'FontWeight', 'bold', 'FontName', sty, ...
        'Position', pos([(nc+2)*dis+nc*wd+wd0, hz-fa*nb*ha+0.02, wd, ha]), ...
        'ButtonPushedFcn', @(src, event) buttonPGA(src, event, version, oldbasicfsz));
    nb = nb + 1;

    uibutton(fig, 'push', 'Text', 'Pure Component Decomposition', ...
        'FontSize', basicfsz, 'FontWeight', 'bold', 'FontName', sty, ...
        'Enable', 'off', 'Position', pos([(nc+2)*dis+nc*wd+wd0, hz-fa*nb*ha, wd, ha]), ...
        'ButtonPushedFcn', @(src, event) buttonPCD(src, event, version, oldbasicfsz));
    nb = nb + 1;

    uibutton(fig, 'push', 'Text', {'Individual FACPACK library', '(user saved profiles)'}, ...
        'FontSize', basicfsz, 'FontWeight', 'bold', 'FontName', sty, ...
        'Position', pos([(nc+2)*dis+nc*wd+wd0, hz-fa*nb*ha, wd, ha]), ...
        'ButtonPushedFcn', @(src, event) buttonLIB(src, event, version, oldbasicfsz));
    nb = nb + 1;
    nc = nc + 1;

    
    uilabel(fig, 'Text', 'Area of Feasible Solutions', 'HorizontalAlignment', 'center', ...
        'FontSize', 1.2*basicfsz, 'FontName', sty, 'FontWeight', 'bold', ...
        'BackgroundColor', 'white', 'Position', pos([(nc+2)*dis+nc*wd+wd0, 0.91, wd, 0.06]));

    uibutton(fig, 'push', 'Text', {'Polygon Inflation', '(2 & 3 components)'}, ...
        'FontSize', basicfsz, 'FontWeight', 'bold', 'FontName', sty, ...
        'Position', pos([(nc+2)*dis+nc*wd+wd0, hz-fa*(nb-3)*ha+0.02, wd, ha]), ...
        'ButtonPushedFcn', @(src, event) buttonAFS(src, event, version, oldbasicfsz));
    nb = nb + 1;

    uibutton(fig, 'push', 'Text', {'Ray Casting', '(4 components)'}, ...
        'FontSize', basicfsz, 'FontWeight', 'bold', 'FontName', sty, ...
        'Position', pos([(nc+2)*dis+nc*wd+wd0, hz-fa*(nb-3)*ha, wd, ha]), ...
        'ButtonPushedFcn', @(src, event) buttonRAY(src, event, version, oldbasicfsz));
    nb = nb + 1;

    uibutton(fig, 'push', 'Text', {'Generalized Borgen Plots', '(3 components)'}, ...
        'FontSize', basicfsz, 'FontWeight', 'bold', 'FontName', sty, ...
        'Position', pos([(nc+2)*dis+nc*wd+wd0, hz-fa*(nb-3)*ha, wd, ha]), ...
        'ButtonPushedFcn', @(src, event) buttonBKP(src, event, version, oldbasicfsz));
    nb = nb + 1;

    uibutton(fig, 'push', 'Text', 'Analytical inner boundary curve', ...
        'FontSize', basicfsz, 'FontWeight', 'bold', 'FontName', sty, ...
        'Position', pos([(nc+2)*dis+nc*wd+wd0, hz-fa*(nb-3)*ha, wd, ha]), ...
        'ButtonPushedFcn', @(src, event) buttonIBC(src, event, version));
    nb = nb + 1;
    nc = nc + 1;


    uilabel(fig, 'Text', 'Duality & Area of Feasible solutions', 'HorizontalAlignment', 'center', ...
        'FontSize', 1.2*basicfsz, 'FontName', sty, 'FontWeight', 'bold', ...
        'BackgroundColor', 'white', 'Position', pos([(nc+2)*dis+nc*wd+wd0, 0.91, wd, 0.06]));

    uibutton(fig, 'push', 'Text', {'Duality & AFS', '(3 components)'}, ...
        'FontSize', basicfsz, 'FontWeight', 'bold', 'FontName', sty, ...
        'Position', pos([(nc+2)*dis+nc*wd+wd0, hz-fa*(nb-7)*ha+0.02, wd, ha]), ...
        'ButtonPushedFcn', @(src, event) buttonCAA(src, event, version, oldbasicfsz));
    nb = nb + 1;

    uibutton(fig, 'push', 'Text', {'Duality & AFS', '(4 components)'}, ...
        'FontSize', basicfsz, 'FontWeight', 'bold', 'FontName', sty, ...
        'Position', pos([(nc+2)*dis+nc*wd+wd0, hz-fa*(nb-7)*ha, wd, ha]), ...
        'ButtonPushedFcn', @(src, event) buttonCA4(src, event, version, oldbasicfsz));
    nb = nb + 1;
    nc = nc + 1;

    
    uilabel(fig, 'Text', 'Data Preparation and Analysis', 'HorizontalAlignment', 'center', ...
        'FontSize', 1.2*basicfsz, 'FontName', sty, 'FontWeight', 'bold', ...
        'BackgroundColor', 'white', 'Position', pos([(nc+2)*dis+nc*wd+wd0, 0.91, wd, 0.06]));

    uibutton(fig, 'push', 'Text', 'Baseline Correction', ...
        'FontSize', basicfsz, 'FontWeight', 'bold', 'FontName', sty, ...
        'Position', pos([(nc+2)*dis+nc*wd+wd0, hz-fa*(nb-9)*ha+0.02, wd, ha]), ...
        'ButtonPushedFcn', @(src, event) buttonBLA(src, event, version, oldbasicfsz));
    nb = nb + 1;

    uibutton(fig, 'push', 'Text', {'Essential Spectral Information', '(also for noisy data)'}, ...
        'FontSize', basicfsz, 'FontWeight', 'bold', 'FontName', sty, ...
        'Position', pos([(nc+2)*dis+nc*wd+wd0, hz-fa*(nb-9)*ha, wd, ha]), ...
        'ButtonPushedFcn', @(src, event) buttonEDP(src, event, version, oldbasicfsz));
    nb = nb + 1;

    uibutton(fig, 'push', 'Text', {'Inner & Outer Polytopes', '(also for noisy data)'}, ...
        'FontSize', basicfsz, 'FontWeight', 'bold', 'FontName', sty, ...
        'Position', pos([(nc+2)*dis+nc*wd+wd0, hz-fa*(nb-9)*ha, wd, ha]), ...
        'ButtonPushedFcn', @(src, event) buttonIPFP(src, event, version, oldbasicfsz));
    nb = nb + 1;

    uibutton(fig, 'push', 'Text', 'Matrix from Borgen plots', ...
        'FontSize', basicfsz, 'FontWeight', 'bold', 'FontName', sty, 'Enable', 'on', ...
        'Position', pos([(nc+2)*dis+nc*wd+wd0, hz-fa*(nb-9)*ha, wd, ha]), ...
        'ButtonPushedFcn', @(src, event) buttonIP4D(src, event, version));
end
set(fig,'Visible','on');


function buttonBLA(hObject, eventdata, version, basicfsz)
subGUI4BLA(version,201,basicfsz);

function buttonPGA(hObject, eventdata, version, basicfsz)
subGUI4PGA(version,202,basicfsz);

function buttonPCD(hObject, eventdata, version, basicfsz)
subGUI4PCD(version,203,basicfsz)

function buttonAFS(hObject, eventdata, version, basicfsz)
subGUI4AFS(version,204,basicfsz);

function buttonRAY(hObject, eventdata, version, basicfsz)
subGUI4RAY(version,205,basicfsz);

function buttonBKP(hObject, eventdata, version, basicfsz)
subGUI4BKP(version,206,basicfsz);

function buttonCAA(hObject, eventdata, version, basicfsz)
subGUI4CAA(version,207,basicfsz);

function buttonCA4(hObject, eventdata, version, basicfsz)
subGUI4CA4(version,208,basicfsz);

function buttonLIB(hObject, eventdata, version, basicfsz)
subGUI4LIB(version,209,basicfsz);

function buttonEDP(hObject, eventdata, version, basicfsz)
subGUI4EDP(version,210,basicfsz);

function buttonIP4D(hObject, eventdata, version)
subGUI4IP4D(version,211);

function buttonIPFP(hObject, eventdata, version, basicfsz)
subGUI4IFP(version,212,basicfsz);

function buttonIBC(hObject, eventdata, version)
figure(213);
pause(0.1)
path(path, 'subfcnsIBC');
subGUI4IBC(version,213);
