function [score, warped_points] = eval_model_cps_warp(params, warp_centre, r, unwarped_image_vec, unwarped_points_vec, image_set, points_set, n_modes, start_points_vec, model_evaluation_method, weighting_normalisation_method, shape_weight, variation_kept, show_warps_while_optimising)
% EVAL_MODEL_CPS_WARP: apply a simple warp and evaluate model
%
% Code written by Katherine Smith, 2003
%
%    GENERAL
%
%      
%
%    INPUT/S
%
%      -
%           
%    OUTPUT/S
%
%      -
%           
%
%    PENDING WORK
%
%      -
%
%    KNOWN BUG/S
%
%      -None.
%
%    COMMENT/S
%
%      -
%
%    RELATED FUNCTION/S
%
%      P
%
%    ABOUT
%
%      -Created:     November 2003
%      -Last update: March 26th, 2004
%      -Revision:    0.0.2
%      -Author:      R. S. Schestowitz, University of Manchester
% ==============================================================

%d = params(1);
start_points = unwarped_points_vec;
max_d = 0.67;

if (any((abs(params) > max_d)) | any(warp_centre - r < -1) | any(warp_centre + r > 1))
  score = realmax;
  warped_points = unwarped_points_vec;
else
    
      % return maximum value if warp is invalid

      
  for i=1:size(params,2),
%       index=i
%       cent=warp_centre(i)
%       rad=r(i)
%       param=params(i)
%       rand_num = (rand - 0.5)/ 10;
      
%      figure(50);
%      hold on;
%      plot(start_points);
%      plot(start_points_vec);
%      axis([0 size(unwarped_image_vec,1) -1 1]) 
%      hold off;


%      params(i)
%      start_points_vec
%      warp_centre(i)
%      r(i)

%     params(i) = params(i) + 0.5;



    warped_points = cps_warp_1d(start_points_vec, warp_centre(i), r(i), params(i));
    
    
	if (show_warps_while_optimising == 1),    
         figure(40);
         plot(warped_points);
         axis([0 size(unwarped_image_vec,1) -1 1])
	end

    start_points = warped_points;
  end
  
  warped_image = interp1(unwarped_points_vec, unwarped_image_vec, warped_points, 'linear', 0);
  
  % figure(102);
  % plot(warped_image)
  
  image_set(:,size(image_set,2) + 1) = warped_image;
  points_set(:,size(points_set,2) + 1) = warped_points;
  
  
  
  % figure(30);
  % plot(warped_image);
 

  c_model = build_model(image_set, points_set, variation_kept, '', weighting_normalisation_method, shape_weight);
  score = measure_model(c_model.variances, n_modes, model_evaluation_method, c_model);
end
