      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{v(1),v(2)}.w_i;
     40    sum_w_i = pre{v(1),v(2)}.sum_w_i;
     41    Ai= pre{v(1),v(2)}.Rigid;
     42    p_ast = pre{v(1),v(2)}.p_ast;
     43
     44    [q_ast,q_hat_i] = MLS_q(w_i,sum_w_i,q_i);
     45
     46    % loop through control points
     47    f_vec=zeros(1,2);
     48    for j = 1:size(q_i,1)
     49
     50        f_vec = f_vec + q_hat_i(j,:)*Ai(:,:,j);
     51    end
     52    len_f_vec = sqrt(sum(f_vec.^2));
     53
     54    f_r = sqrt(sum((v-p_ast).^2)) * f_vec / len_f_vec + q_ast;
     55
     56    % save deformation for given control point
     57    f(v(1),v(2),[1,2]) = f_r;
     58end
     59end
     60%% similatrity defromation
     61function f = MLS_Similarity(q_i,V,pre,size_of_img)
     62f = zeros([size_of_img,2]);
     63
     64% loop through evaluation points v
     65for i = 1:size(V,1)
     66    v = V(i,:);
     67    % get param for given evaluation point v
     68    w_i = pre.w_i(:,i);
     69    sum_w_i = pre.sum_w_i(i);
     70    Ai_mu= pre.Similarity(:,2*i-1:2*i);
     71
     72    [q_ast,q_hat_i] = MLS_q(w_i,sum_w_i,q_i);
     73
     74    % loop through control points
     75    sum_f=[0,0];
     76    for j = 1:size(q_i,1)
     77        sum_f = sum_f + q_hat_i(j,:)*Ai_mu(2*j-1:2*j,:);
     78    end
     79    ss = size(sum_f)
     80    sum_f = sum_f + q_ast;
     81    size(sum_f)
     82    % save deformation for given control point
     83    f(v(1),v(2),[1,2]) = sum_f;
     84end
     85end

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