function stringList = convertMillerToString(millerMatrix, brackets, useComma)
% convertMiller2str converts a miller vector (mil) to a string with
% appropriate brackets {'(', '{', '['}, if provided.
%
% THIS IS NO USER FUNCTION

% The elk-library: convex geometry applied to crystallization modeling.
%   Copyright (C) 2013 Alexander Reinhold
%
% 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/>.

%% input handling
if ~exist('brackets', 'var')
    brackets = '{';
elseif ~isstr(brackets) || length(brackets) ~= 1
    error('elk:crystal:wrongInput', ['The provided brackets parameter must ' ...
        'be a string of length 1']);
elseif  ~strcmp(brackets, '(') && ~strcmp(brackets, '{') && ~strcmp(brackets, '[')
    if strcmp(brackets, ')')
        brackets = '(';
    elseif strcmp(brackets, '}')
        brackets = '{';
    elseif strcmp(brackets, ']')
        brackets = '[';
    else
        error('elk:crystal:wrongInput', ['The provided brackets parameter ' ...
            'must be ''('', ''{'' or ''[''']);
    end
end
if ~exist('useComma', 'var') || isempty(useComma)
    useComma = 1;
end
if useComma
    commaString = ', ';
else
    commaString = '';
end

% correct size of input matrix
if size(millerMatrix, 1) == 3
    millerMatrix = millerMatrix';
end


%% prepare opening/closing bracket
switch brackets
    case '('
        b_open = '(';
        b_close = ')';
    case '{'
        b_open = '{';
        b_close = '}';
    case '['
        b_open = '[';
        b_close = ']';
end

%% conversion
for i = 1:size(millerMatrix, 1)
    stringList{i} = [b_open num2str(millerMatrix(i,1)) commaString ...
                     num2str(millerMatrix(i,2)) commaString ...
                     num2str(millerMatrix(i,3)) b_close];
end

% return a simple string
if length(stringList) == 1
    stringList = stringList{1};
end