srcdir = 'e:\research\codes\sbsrclean';
svgfile = 'e:\codes\sbsrpart\localdata\userinput\chair\user0_21_special_19.svg';
% srcdir = 'e:\codes\sbsrpart';
% svgfile = 'e:\codes\sbsrpart\localdata\userinput\chair\user0_21_special_19.svg';
% loaddatabase chair;
curdir = pwd;

%%
done = false;
while ~done
	try
% 		x=SBSRSKETCH.init_with_vecimg(fulldata.m_sketchdata.m_vecimg);
% 		x.show_2dsegs;
		vecimg = SBSRVECIMAGE.init_with_svg('sketch.svg');
%         fulldata = fulldata.clear_non_essential();
% 		sketchdata = srec_load_sketch_from_svg(svgfile,database);     
%         sketchdata = srec_label_sketch(sketchdata,database);
%  		sketchdata = srec_set_view(sketchdata,database);
%  
% % 		% set database view
%  		database = database.set_view(sketchdata.m_view,'fastupdate',true);
%  		database.show_thumbnails;
% 
% 		% match
%  		fulldata = srec_local_interpret(sketchdata,database,'idxdbobj',1);
% 		samplegraph = fulldata.m_samplegraph;
%  		idxcompts_retain = srec_refine_local_interpretation(...
% 				fulldata.m_samplegraph,fulldata.m_database,...
% 				'sparsity',false,'usehdist',true,'removebadmatch',0.5);
% %%
% 		mipopt = SBSRMIPOPT.init_with_nan;
% 		mipopt.m_subgraph_idxcompts = idxcompts_retain;
% 		mipopt.m_mincover = 0.3;
% 		mipopt.m_useoverlap = false;
%  		[gmodel,linmodel] = srec_global_interpret_formulate(sketchdata,samplegraph,'mipopt',mipopt);
% %%
% 		params.TimeLimit = 10*60;
% 		rs = gurobi(gmodel,params);

		%%
% 		sol = srec_global_interpret_explain(rs.x,linmodel,...
% 			sketchdata,samplegraph,database);
		
		%%
% 		close all;
% 		util_show_solution(sol,samplegraph,database,'full',true);
		
		%% compute accuracy
% 		gteval = SBSRGTEVAL.init_with_lbmap(sketchdata,sol.m_lbmap);
% 		fprintf(1,'accuracy by pixel = %f\n',gteval.acc_by_pixel);
% 		fprintf(1,'accuracy by component = %f\n',gteval.acc_by_comp);
		
  		done = true;
	catch err
		disp(err.message);
		if strcmpi(err.identifier,'MATLAB:UndefinedFunction')
			msg = err.message;
			ptn = 'Undefined function (or variable )?''(?<funcname>\w+)''';
			x = regexp(msg,ptn,'names');
			funcname = x.funcname;
			
			% can we find this?
			srcfilename = sprintf('%s/%s.m',srcdir,funcname);
			if exist(srcfilename,'file') == 0
				done = true;
			else
				destfilename = [funcname,'.m'];
				fprintf(1,'copying %s\n',srcfilename);
				copyfile(srcfilename,destfilename,'f');
			end
		else
 			done = true;
		end
	end
end