function disC = obtainDistributionContinuous(name, varargin)
% obtain the density function for a distribution
%
% Syntax: disC = obtainDistributionContinuous(name, par)
%   OR    disC = obtainDistributionContinuous(name, 'option', value, ..)
%
% The input "name" is one out of the following list. The input "par"
%   is a structure containing the parameters of the distribution. 
%   Different parameters are required, dependent ond the distribution.
%   A common parameter is 'totalValue'that is used to scale the density 
%   function to (e.g.) a total  particle number (default: 1). The dimension
%   of the distribution is determined by the given parameters.
%
% The output disC is a structure with the fields:
%     name - the name of the distribution
%     fhandle - a function handle with the density function
%     nDim - the dimension of the distribution
%     parContinuous - the parameters of the distribution (altering these 
%       parameters after creation does not alter the distribution)
%
% Name: normal, normal distribution (Gaussian normal distribution)
% Parameters: mean (vector with mean values); std (vector with standard
%   deviations)
%
% Name: ray, uniform ray, ray uniform (Uniform distribution in a cone
%   around a given ray)
% Parameters: ray (the center ray); radius (radius of the base at the ent
%   of the ray)
%
% See also: obtainDistributionDiscrete

% 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
par = mapOptionStruct(varargin, 'mean', 0, 'cov', 1, ...
    'totalValue', 1);

if strcmpi(name, 'normal') || strcmpi(name, 'normal distribution')
    
    %% normal distribution
    disC.name = 'normal distribution';
    [mean, cov] = ensureMeanAndCov([], par.mean, par.cov);
    disC.nDim = numel(mean);
    disC.parContinuous = struct('mean', mean, ...
        'cov', cov, 'totalValue', par.totalValue);
    
    disC.fhandle = @(h)(mvnpdf(h', disC.parContinuous.mean', ...
        disC.parContinuous.cov)' * par.totalValue);

elseif strcmpi(name, 'ray') || strcmpi(name, 'ray uniform') || ...
        strcmpi(name, 'uniform ray')
    
    %% uniform in cone around ray
    disC.name = 'uniform ray';
    disC.nDim = length(ray);
    disC.parContinuous.ray = par.ray(:);
    disC.parContinuous.radius = par.radius;
    
    volume = pi*disC.radius^2*norm(disC.ray);
    
    disC.fhandle = @(h)(disC.parContinuous.totalValue/volume * ...
        (h'*disC.parContinuous.ray >= 0) * ...
        (h'*disC.parContinuous.ray/norm(disC.ray) <= norm(disC.parContinuous.ray)) *...
        ((h'*h) - (h'*disC.parContinuous.ray/norm(disC.ray))^2 <= ...
         (disC.parContinuous.radius*h'*disC.parContinuous.ray/norm(disC.parContinuous.ray)^2)^2) ...
        );
    
else
    error('elk:distribution:wrongInput', ...
        ['The given distribution name (' name ') is not supported.']);
end