function h = CS_KB2hpv(KB,offset,side)
% CS_clause2hpv - converts a KB to a hyperplane normal form 
%    set of coefficients
% On input:
%     KB(struct KB): KB info (n atoms)
%       KB(k).clasues (1xk vector): disjunction as integers
%       KB(k).prob (float): clause probability
%     offset (float): amount to back (n-1)-D hyperplane from H_k
%     side (float): amount to back off H_n faces from 0 and 1
% On output:
%     h (m+2n by (n+1) vector): hyperplane coefficients for each clause
% Call:
%     h = CS_KB2hpv(KB,0.99,1);
% Author:
%     T. Henderson
%     UU
%     Summer 2022
% 

cl = length(KB);
n = max(CS_vars(KB, []));
%tch n=2;
h = zeros(cl+2*n, n+1);
for c = 1:cl
    hp = CS_clause2hpv(KB(c).clauses,n,offset);
    h(c,:) = hp;
end

% constrain to inside hypercube
for k = 1:n
    index = cl + (k-1)*2 + 1;
    h(index,k) = 1;
    h(index,end) = side;
    index = index + 1;
    h(index,k) = -1;
    h(index,end) = 1 + side;
end
