function hrep = liftHrepDimension(hrep, rotOrTargetDim, x0, flatness)
% lift hrep into higher dimension
%
% Syntax: hrep = liftHrepDimension(hrep, targetDim)
%   OR    hrep = liftHrepDimesnion(hrep, rotMatrix)
%
% The given H-representation of dimension nIn is lifted to targetDim
%   dimensions while it remains embedded in the first nIn coordinate axes.
%   Therefore, constraints of the type:
%     [0 .. 0  1;
%      0 .. 0 -1]  * x <= 0
%   are added. When a rotation matrix (rotMatrix) is provided, the polytope
%   is rotated afterwards while targetDim = size(rotMatrix, 1) holds.
%
% See also: reduceHrepDimension, obtainHrep, viewPolytope

% 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/>.

if ~exist('flatness', 'var')
    flatness = 0;
end

if numel(rotOrTargetDim) == 1
    targetDim = 1;
    rotMatrix = [];
else
    if size(rotOrTargetDim, 1) ~= size(rotOrTargetDim, 2)
    end
    targetDim = size(rotOrTargetDim, 1);
    rotMatrix = rotOrTargetDim;
end
   

% figure,viewPolytope(hrep)
while size(hrep.A, 2) < targetDim
    nFacet = size(hrep.A, 1);
    nDim = size(hrep.A, 2);
    hrep.A = [hrep.A zeros(nFacet, 1);
                     zeros(1, nDim) 1;...
                     zeros(1, nDim) -1];
    hrep.h = [hrep.h; flatness/2; flatness/2];
end
if ~isempty(rotMatrix)
    hrep.A = hrep.A*rotMatrix';
end
if exist('x0', 'var') && ~isempty(x0)
    hrep = moveHrep(hrep, x0);
end