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

% Last Modified by GUIDE v2.5 02-Mar-2014 22:04:36

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

% Choose default command line output for conformal_reconstruction_gui
handles.output = hObject;
% display \lambda in the text label
% set(handles.cf_text,'FontName','Symbol');
% set(handles.cf_text,'String','l');
try
    mesh = load('face.mat');
    handles.mesh = mesh;
    % show mesh in original mesh axes
    axes(handles.origin_mesh);
    trimesh(mesh.face,mesh.vertex(:,1),mesh.vertex(:,2),mesh.vertex(:,3),mesh.vertex(:,3));
    eval(mesh.style.mesh);    
    freezeColors
    axis equal
    axis off
    set(handles.log,'String','load face mesh succussfully');
catch
    disp('data are not properly stored. Need face, vertex and corner');
end
% Update handles structure
guidata(hObject, handles);


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


% --- Outputs from this function are returned to the command line.
function varargout = conformal_reconstruction_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 load_mesh.
function load_mesh_Callback(hObject, eventdata, handles)
% hObject    handle to load_mesh (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[matfile,matpath] = uigetfile('*.mat','Select mesh data file');
mesh = load([matpath matfile]);
handles.mesh = mesh;
guidata(hObject,handles);
cla(handles.origin_mesh,'reset')
cla(handles.conformal_factor,'reset')
cla(handles.mean_curvature,'reset')
cla(handles.reconstructed_mesh,'reset')
axes(handles.origin_mesh);
trimesh(mesh.face,mesh.vertex(:,1),mesh.vertex(:,2),mesh.vertex(:,3),mesh.vertex(:,3));
eval(mesh.style.mesh);
axis equal
axis off
freezeColors
set(handles.log,'String',['load ' matfile(1:end-4) ' mesh succussfully']);

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

% Hint: place code in OpeningFcn to populate origin_mesh


% --- Executes on button press in compute_lh.
function compute_lh_Callback(hObject, eventdata, handles)
% hObject    handle to compute_lh (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
mesh = handles.mesh;
set(handles.log,'String','computing conformal factor and mean curvature...');
pause(0.1)
% compute conformal factor and mean curvature
[r,ru,rv,normal,cf,h,mu,uh,vh] = compute_lh(mesh.face,mesh.vertex,mesh.corner);
mesh.r = r;
mesh.ru = ru;
mesh.rv = rv;
mesh.normal = normal;
mesh.cf = cf;
mesh.h  = h;
mesh.mu = mu;
mesh.uh = uh;
mesh.vh = vh;
handles.mesh = mesh;
guidata(hObject,handles);
% update origin_mesh with interpolated regular mesh
axes(handles.origin_mesh);
surf(r(1:2:end,1:2:end,1),r(1:2:end,1:2:end,2),r(1:2:end,1:2:end,3))
eval(mesh.style.mesh);
axis equal
axis off
freezeColors
% show conformal factor and mean curvature
% option.handle = handles.conformal_factor;
set(handles.log,'String','drawing conformal factor and mean curvature');
axes(handles.conformal_factor);
surf(cf)
eval(mesh.style.cf);
% axis equal
axis off
freezeColors
axes(handles.mean_curvature);
surf(h)
eval(mesh.style.h);
axis equal
axis off
freezeColors
set(handles.log,'String','compute conformal factor and mean curvature successfully');

% --- Executes on button press in reconstruct.
function reconstruct_Callback(hObject, eventdata, handles)
% hObject    handle to reconstruct (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
mesh = handles.mesh;
set(handles.log,'String','reconstructing surface from conformal factor and mean curvature...');
pause(0.1);
% reconstruct mesh from conformal factor and mean curvature, need boundary
% condition of original mesh
mask2 = false(size(mesh.cf));
mask2([1:10,end-9:end],:) = true;
mask2(:,[1:10,end-9:end]) = true;
rzb = (mesh.ru - mesh.rv)/2;
rzb([~mask2,~mask2,~mask2]) = 0;
rz2b = (mesh.ru + mesh.rv)/2;
rz2b([~mask2,~mask2,~mask2]) = 0;
normalb = mesh.normal;
normalb([~mask2,~mask2,~mask2]) = 0;
[rz1,rz21,normal1] = solve_natural_frame_from_lh(rzb,rz2b,normalb,mask2,mesh.cf,mesh.h,mesh.mu,mesh.uh,mesh.vh);
% solve surface from Ru,Rv
mask1 = false(size(mesh.cf));
mask1([1:9,end-8:end],:) = true;
mask1(:,[1:9,end-8:end]) = true;
ru1 = real((rz1 + rz21));
rv1 = real((rz1 - rz21)*1i);
rb = mesh.r;
rb(~[mask1,mask1,mask1]) = 0;
r2 = solve_surface_from_natural_frame(ru1,rv1,rb,mask1,mesh.uh,mesh.vh);
mesh.r2 = r2;
handles.mesh = mesh;
guidata(hObject,handles);
% show reconstructed mesh in reconstructed_mesh axes
% show_mesh(face,vertex);
axes(handles.reconstructed_mesh);
surf(r2(1:2:end,1:2:end,1),r2(1:2:end,1:2:end,2),r2(1:2:end,1:2:end,3))
eval(mesh.style.mesh);
% axis([0.5 1.6 0.3 1.2 0.9 1.4])
% view(-92,-88)
% lighting phong
% light('Position',[0,0,-5],'Style','infinite');
% colormap(gca,'pink')

axis equal
axis off
freezeColors
assignin('base','mesh',mesh);
set(handles.log,'String','reconstruct surface from conformal factor and mean curvature successfully');
