function score = build_and_eval_model(image_set, points_set, norm_type)
% BUILD_AND_EVAL_MODEL: Builds a model for the set of images and points and
%                       measures it.
%
% Code written by Katherine Smith, 2003
%
%    GENERAL
%
%      score = build_and_eval_model
%          (image_set, points_set, norm_type)
%
%    INPUT/S
%
%      -image_set:
%          The set of images.
%
%      -points_set:
%          The set of points.
%
%      -norm_type:
%          Normalisation type?
%           
%    OUTPUT/S
%
%      -score:
%           Then score of the model.
%
%    PENDING WORK
%      
%      -Find out the meaning od normalisation type. 
%
%    KNOWN BUG/S
%
%      
%
%    COMMENT/S
%
%      -This does the same thingas BUILD_MODEL but also evaluates it
%
%    RELATED FUNCTION/S
%
%      BUILD_MODEL
%
%    ABOUT
%
%      -Created:     November 23rd, 2003
%      -Last update: December 1st, 2003
%      -Revision:    0.0.2
%      -Author:      R. S. Schestowitz, University of Manchester
% ==============================================================

keep = 0.9999;
[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);

if(norm_type == 'variance')
	intensity_model.sd = std(intensity_model.params);
	shape_model.sd = std(shape_model.params);
	
	% combine params - initially normalise to variance described in model
	total_shape_variance = sum(shape_model.variances);
	total_intensity_variance = sum(intensity_model.variances);
	c_model.shape_weight = ones(1,size(shape_model.pcs,2));
	if(total_shape_variance ~= 0)
        c_model.shape_weight(:) = sqrt(total_intensity_variance/total_shape_variance);
	% w_shapeweight(:) = sqrt(ones(1,length(w_shape_variances))*total_intensity_variance/total_shape_variance);
      end
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);

% get the combined model

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 = 'Warp';

score = measure_model(c_model.variances, 50);
 % now return the evaluation of this newly constructed model