      1function [f,pre] = MLS_getDeform(control_points,deformed_position_of_control_points,points_of_evaluation,alpha,type,precomputed,size_of_img)
      2%UNTITLED2 Summary of this function goes here
      3%   type:
      4%        Affine = translation, rotation, nonuniform scaling and shear
      5%        Similarity = translation, rotation and uniform scaling
      6%        Rigid = translation, rotation
      7%   alpha: for weigth function (precompute)
      8%   p_i,q_i,v_i are row vectros eg. [x,y] so p,q,v = [x1,y1;x2,y2;x3,y3;...]
      9%   size_of_img = [height,width]
     10p = control_points;
     11q = deformed_position_of_control_points;
     12v = points_of_evaluation;
     13
     14pre = precomputed;
     15f = [];
     16% switch the MLS deformation type
     17if strcmp(type,'Affine')
     18    f = MLS_Affine(q,v,pre,size_of_img);
     19elseif strcmp(type,'Similarity')
     20    f = MLS_Similarity(q,v,pre,size_of_img);
     21elseif strcmp(type,'Rigid')
     22     f = MLS_Rigid(q,v,pre,size_of_img);
     23else
     24    %ME = MException('type:PassedWrongVariable', ...
     25    %    'Variable %s should be one of following: Affine, Similarity, Rigid',type);
     26    %throw(ME)
     27    fprintf('PassedWrongVariable')
     28end
     29end
     30%MLS deformation
     31%% rigid deformation
     32function f = MLS_Rigid(q_i,V,pre,size_of_img)
     33f = zeros([size_of_img,2]);
     34
     35% loop through evaluation points v
     36for i = 1:size(V,1)
     37    v = V(i,:);
     38    % get param for given evaluation point v
     39    w_i = pre.w_i(:,i);
     40    sum_w_i = pre.sum_w_i(i);
     41    Ai= pre.Rigid(:,2*i-1:2*i);
     42    p_ast = pre.p_ast(i,:);
     43    [q_ast,q_hat_i] = MLS_q(w_i,sum_w_i,q_i);
     44
     45    % loop through control points
     46    f_vec=zeros(1,2);
     47    for j = 1:size(q_i,1)
     48
     49        f_vec = f_vec + q_hat_i(j,:)*Ai(2*j-1:2*j,:);
     50    end
     51    len_f_vec = sqrt(sum(f_vec.^2));
     52
     53    f_r = sqrt(sum((v-p_ast).^2)) * f_vec / len_f_vec + q_ast;
     54
     55    % save deformation for given control point
     56    f(v(1),v(2),[1,2]) = f_r;
     57end
     58end

     59%% similatrity defromation
     60function f = MLS_Similarity(q_i,V,pre,size_of_img)
     61f = zeros([size_of_img,2]);
     62
     63% loop through evaluation points v
     64for i = 1:size(V,1)
     65    v = V(i,:);
     66    % get param for given evaluation point v
     67    w_i = pre.w_i(:,i);
     68    sum_w_i = pre.sum_w_i(i);
     69    Ai_mu= pre.Similarity(:,2*i-1:2*i);
     70
     71    [q_ast,q_hat_i] = MLS_q(w_i,sum_w_i,q_i);
     72
     73    % loop through control points
     74    sum_f=[0,0];
     75    for j = 1:size(q_i,1)
     76        sum_f = sum_f + q_hat_i(j,:)*Ai_mu(2*j-1:2*j,:);
     77    end
     78
     79    sum_f = sum_f + q_ast;
     80
     81    % save deformation for given control point
     82    f(v(1),v(2),[1,2]) = sum_f;
     83end
     84end
     85
     86% %% affine deformation

     87function f = MLS_Affine(q_i,V,pre,size_of_img)
     88f = zeros([size_of_img,2]);
     89% loop through evaluation points v
     90for i = 1:size(V,1)
     91    v = V(i,:);
     92    % get param for given evaluation point v
     93    w_i = pre.w_i(:,i);
     94    sum_w_i = pre.sum_w_i(i);
     95    Aj = pre.Affine(:,i);
     96
     97    [q_ast,q_hat_i] = MLS_q(w_i,sum_w_i,q_i);
     98
     99    % loop through control points
    100    sum_f=[0,0];
    101    for j = 1:size(q_i,1)
    102        sum_f = sum_f + Aj(j)*q_hat_i(j,:);
    103    end
    104    sum_f = sum_f + q_ast;
    105    % save deformation for given control point
    106    %if isequal(v,p)
    107    f(v(1),v(2),[1,2]) = sum_f;
    108end
    109end
    110
    111%% parameters for deformation function, q = deformed positions of the control points p
    112function [q_ast,q_hat_i] = MLS_q(w_i,sum_w_i,q_i)
    113% [w_i,w_i] to make matlab coder happy
    114q_ast = sum([w_i,w_i].*q_i, 1) ./ sum_w_i;
    115q_hat_i = q_i - repmat(q_ast,size(q_i,1),1);
    116end
    117
    118
    119
    120