%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  CreateXMLTable.m
%
%  Copyright (c) 2012 Roberto Manduchi 
%
% This program is free software: you can redistribute it and%or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program.  If not, see <http:%%www.gnu.org%licenses/>.
%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function CreateXMLTable(ParamStructure, m, b1, b2, t_min, t_max, IX)

fid = fopen(ParamStructure.OutputXMLFileName, 'w');

fprintf(fid,'<?xml version="1.0"?>\n');

fprintf(fid, '<root>\n\n');

fprintf(fid, '<DesignParameters>\n\n');

fprintf(fid, '<BackgroundImagesFolder>\n');
fprintf(fid, '%s\n', ParamStructure.BackgroundImagesFolder);
fprintf(fid, '</BackgroundImagesFolder>\n\n');

fprintf(fid, '<TrainingDataFile>\n');
fprintf(fid, '%s\n', ParamStructure.TrainingDataFile);
fprintf(fid, '</TrainingDataFile>\n\n');

fprintf(fid, '<BackgroundImageSuffix>\n');
fprintf(fid, '%s\n', ParamStructure.BackgroundImageSuffix);
fprintf(fid, '</BackgroundImageSuffix>\n\n');

fprintf(fid, '<Intercept>\n');
fprintf(fid, '%d\n', ParamStructure.Intercept);
fprintf(fid, '</Intercept>\n\n');

fprintf(fid, '<SaturatedPointRemoval>\n');
fprintf(fid, '%d\n', ParamStructure.SaturatedPointRemoval);
fprintf(fid, '</SaturatedPointRemoval>\n\n');

fprintf(fid, '<MarginRatio>\n');
fprintf(fid, '%d\n', ParamStructure.MarginRatio);
fprintf(fid, '</MarginRatio>\n\n');

if isfield(ParamStructure,'a')
    fprintf(fid, '<GammaPar_a>\n');
    fprintf(fid, '%f %f %f\n', ParamStructure.a);
    fprintf(fid, '</GammaPar_a>\n\n');
end
if isfield(ParamStructure,'c')
    fprintf(fid, '<GammaPar_c>\n');
    fprintf(fid, '%f %f %f\n', ParamStructure.c);
    fprintf(fid, '</GammaPar_c>\n\n');
end
if isfield(ParamStructure,'g')
    fprintf(fid, '<GammaPar_g>\n');
    fprintf(fid, '%f %f %f\n', ParamStructure.g);
    fprintf(fid, '</GammaPar_g>\n\n');
end
    
fprintf(fid, '</DesignParameters>\n\n');

fprintf(fid, '<ClassifierParameters>\n\n');

for i=IX
   fprintf(fid, '<Classifier id="%d">\n', i-1); % they start from 1 in the Matlab code = moving to C convention
   fprintf(fid, '<m>');
   fprintf(fid, '%d', m(i));      
   fprintf(fid, '</m>\n');
   fprintf(fid, '<b1>');
   fprintf(fid, '%d', b1(i));      
   fprintf(fid, '</b1>\n');
   fprintf(fid, '<b2>');
   fprintf(fid, '%d', b2(i));      
   fprintf(fid, '</b2>\n');
   fprintf(fid, '<t_min_1>');
   fprintf(fid, '%d', t_min(i,1));      
   fprintf(fid, '</t_min_1>\n');
   fprintf(fid, '<t_min_2>');
   fprintf(fid, '%d', t_min(i,2));      
   fprintf(fid, '</t_min_2>\n');
   fprintf(fid, '<t_max_1>');
   fprintf(fid, '%d', t_max(i,1));      
   fprintf(fid, '</t_max_1>\n');
   fprintf(fid, '<t_max_2>');
   fprintf(fid, '%d', t_max(i,2));      
   fprintf(fid, '</t_max_2>\n');
   
   fprintf(fid, '</Classifier>\n\n', i);
end

fprintf(fid, '</ClassifierParameters>\n');
fprintf(fid, '</root>\n\n');
fclose(fid);
