function c_model = build_model(image_set, points_set, keep, label, norm_type, shape_weight)
% BUILD_MODEL: Builds a model for the set of images and points.
%
% Code written by Katherine Smith, 2003
%
%    GENERAL
%
%      c_model = build_model(image_set, points_set, 
%                 keep, label, norm_type, shape_weight)
%
%
%    INPUT/S
%
%      -image_set:
%           The input images set.
%
%      -points_set:
%           The input points set.
%
%      -keep:
%           The precision required from PCA??
%
%      -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
%
%      Review input/output documentation.
%
%    KNOWN BUG/S
%
%      
%
%    COMMENT/S
%
%      -This also resembles some parts of eval_app_model_obj_fn.
%      -same as BUILD_AND_EVAL but no evaluation at bottom.
%
%    RELATED FUNCTION/S
%
%      BUILD_AND_EVALUATE_MODEL
%
%    ABOUT
%
%      -Created:     November 23rd, 2003
%      -Last update: December 1st, 2003
%      -Revision:    0.0.2
%      -Author:      R. S. Schestowitz, University of Manchester
% ==============================================================

[intensity_model.pcs,intensity_model.variances,intensity_model.params, intensity_model.mean, intensity_model.var_r, intensity_model.total_var, intensity_model.impdata] = st_pca(image_set', keep);
[shape_model.pcs,shape_model.variances,shape_model.params, shape_model.mean, shape_model.var_r, shape_model.total_var, shape_model.impdata] = st_pca(points_set', keep);
   % get shape and intensity model
c_model.shape_weight = ones(1,size(shape_model.pcs,2));
   % set weight to be a column vector of 1's? Would that mean equal weighing for shape and intensity?
intensity_model.sd = std(intensity_model.params);
  % get and record standard deviation
shape_model.sd = std(shape_model.params);
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
%     disp(['Shapeweight: ',num2str(c_model.shape_weight(1))]);
elseif (strcmp(norm_type,'constant')),
      c_model.shape_weight(:) = shape_weight;
         % simply assign the predefined value
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;
%     disp(['edge weight: ',num2str(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);
c_model.sd = sqrt(c_model.variances);
c_model.intensity_model = intensity_model;

% intensity_model
% shape_model
% figure;
% plot(intensity_model);
        % attempt to look at the models built from the data (RSS)

c_model.shape_model = shape_model;
c_model.n_shape_modes = size(shape_model.sd,2);
c_model.label = label;
   % save some of the values to be returned by the function
