function dsp = gtCrystDSpacing(hkl, Bmat)

% Calculates d-spacing for given hkl-s and lattice parameters.
% 
% INPUT  hkl  - set of Miller indices (3,n) or (4,n)
%        Bmat - 'B matrix' - (hkl) to Cartesian matrix (3,3)
%
% OUTPUT dsp - row vector of d-spacings <single>


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Lattice vectors in real space, in a Cartesian basis
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% If hexagonal material, transform the four indices into three
if size(hkl,1) == 4
    hkl = hkl([1 2 4],:);
end

% Reciprocal lattice point in the Cartesian basis (a vector normal to hkl plane)
ghkl = Bmat*hkl;

% D-spacings
dsp = 1./sqrt(sum(ghkl.*ghkl,1));

dsp = single(dsp);

end % of function


% % To be checked against:
% % Lattice base vectors in rows:
% av = [a1; a2; a3]
% % Reciprocal space vectors in coloumns:
% bv = inv(av)
% ghkl = bv*hkl
% dsp = 1/norm(ghkl)