function c_model = build_model_from_models(shape_model, intensity_model, label, norm_type, shape_weight)
% BUILD_MODEL_FROM_MODELS: Builds a combined model from shape
%                          and intensity models.
%
% Code written by Katherine Smith, 2003
%
%    GENERAL
%
%       c_model = build_model_from_models(shape_model, 
%          intensity_model, label, norm_type, shape_weight)
%
%
%    INPUT/S
%
%      -shape_model:
%           The shape model.
%
%      -intensity_model:
%           The intensity model
%
%      -label:
%           Label of the model created?
%
%      -norm_type:
%
%
%      -shape_weight:
%          The value of Ws which is the weighing factor in the model.
%
%           
%    OUTPUT/S
%
%      -c_model:
%           The model built.
%
%    PENDING WORK
%
%      
%
%    KNOWN BUG/S
%
%      
%
%    COMMENT/S
%
%        -Smiliar to BUILD_MODEL
%         It appears to build some optimised model which is combined given some model of shape and model
%         of intensity.
%
%    RELATED FUNCTION/S
%
%      BUILD_AND_EVALUATE_MODEL, BUILD_MODEL
%
%    ABOUT
%
%      -Created:     November 23rd, 2003
%      -Last update: December 1st, 2003
%      -Revision:    0.0.2
%      -Author:      R. S. Schestowitz, University of Manchester
% ==============================================================

c_model.shape_weight = ones(1,size(shape_model.pcs,2));
if(strcmp(norm_type,'variance'))
	% combine params - initially normalise to variance described 
      % in model
	total_shape_variance = sum(shape_model.variances);
	total_intensity_variance = sum(intensity_model.variances);
	if(total_shape_variance ~= 0)
        c_model.shape_weight(:) = sqrt(total_intensity_variance/total_shape_variance);
	end
elseif(strcmp(norm_type,'constant'))
      c_model.shape_weight(:) = shape_weight;
elseif(strcmp(norm_type,'edge'))
      edges = abs(diff(image_set,1,2));
      mean_edge_strength = sum(edges(:))/size(edges(:),1);
      c_model.shape_weight(:) = mean_edge_strength;
else
      error(['Unknown normalisation type: ',norm_type]);
end

[c_model.pcs, c_model.variances, c_model.params, c_model.mean, c_model.total_var, c_model.impdata] = make_combined_model(shape_model, intensity_model, c_model.shape_weight);

% make combined model with the new parematers applied.

% copy some parameters to be returned by the function

c_model.sd = sqrt(c_model.variances);
c_model.intensity_model = intensity_model;
c_model.shape_model = shape_model;
c_model.n_shape_modes = size(shape_model.sd,2);
c_model.label = label;