      1function [pre] = MLS_precompute(p_i,q_i,V,alpha,size_of_img,type)
      2% PRECOMPUTATION
      3
      4%% common for all  MLS tranformation methods
      5% for each eval point v all control points need to be looped
      6
      7% new variables
      8pre.w_i = zeros(size(p_i,1),size(V,1));
      9pre.sum_w_i = zeros(size(V,1),1);
     10pre.p_ast = zeros(size(V,1),2);
     11pre.Similarity = zeros(2*size(p_i,1),2*size(V,1));
     12%pre.Rigid = zeros(2*size(p_i,1),2*size(V,1));
     13
     14%pre = cell(size_of_img);
     15for eval = 1:size(V,1)
     16    v = V(eval,:);
     17
     18    if isdeployed
     19        fprintf("omg deployed")
     20    end
     21    % weights w_i = 1/length_of_vect(p_i-v)^(2alpha)
     22    aaa = repmat(v,size(p_i,1),1)
     23    aaaa = p_i-aaa
     24    w_i = ones(size(p_i,1),1) ./ (sqrt(sum(( p_i-repmat(v,size(p_i,1),1) ).^2,2)).^(2*alpha))
     25
     26    % p? and q? are weighted centroids.
     27    ss_w_i = size(w_i)
     28    ss_s_w_i = size(sum(w_i))
     29    ss_pi=size(p_i)
     30    sum_w_i = sum(w_i);
     31
     32    p_ast = sum([w_i,w_i].*p_i, 1) ./ sum_w_i;
     33    ss_p_ast = size(p_ast)
     34
     35    % p^_i for substitution
     36    p_hat_i = p_i - repmat(p_ast,size(p_i,1),1);
     37
     38
     39
     40    if strcmp(type,'Affine') || strcmp(type,'All')
     41        %pre{v(1),v(2)}.Affine = precompute_Affine(p_hat_i,p_ast,w_i,v);
     42    end
     43    if strcmp(type,'Similarity') || strcmp(type,'Rigid') || strcmp(type,'All')
     44        %[pre{v(1),v(2)}.Similarity, pre{v(1),v(2)}.Rigid] = precompute_SimilarityAndRigid(p_hat_i,p_ast,w_i,v);
     45    end
     46    % needed for q
     47    %pre{v(1),v(2)}.w_i = w_i;
     48    %pre{v(1),v(2)}.sum_w_i = sum_w_i;
     49
     50    % needed for Rigid deform
     51    %pre{v(1),v(2)}.p_ast = p_ast;
     52
     53
     54    % new pre
     55    pre.w_i(:,eval) = w_i;
     56    pre.sum_w_i(eval,1) = sum_w_i;
     57    pre.p_ast(eval,:) = p_ast;
     58
     59    [pre.Similarity(:,2*eval-1:2*eval), a] = precompute_SimilarityAndRigid(p_hat_i,p_ast,w_i,v);
     60end
     61end
     62
     63%% Aj - scalar for each control point p at given evaluation point v
     64% -> for each v vector (Aj_1;Aj_2;Aj_3;...)
     65%function Affine = precompute_Affine(p_hat_i,p_ast,w_i,v)
     66% Affine transform precompute
     67% Single scalar for each control point p -> vector
     68% ([2,2]-[1,1])*([1;1]*1*[1,1])*[1;1]
     69
     70%s = zeros(2);
     71%for i = 1:length(w_i)
     72 %   s = s + (p_hat_i(i,:))' * w_i(i) * p_hat_i(i,:);

     73% end
     74% inv_sum = 1/(s(1)*s(4)-s(3)*s(2)) * [s(4) -s(3); -s(2) s(1)];
     75% inv_sum = -inv(s); %added -
     76% Affine = zeros(length(w_i),1);
     77% for j = 1:length(w_i)
     78%     Affine(j) = (v-p_ast)*inv_sum*(p_hat_i(j,:)');
     79% end
     80% end
     81
     82%% Ai - matrix for each control point p at given evaluation point v
     83% mu_s scalar for each evaluation point v
     84function [Similarity,Rigid] = precompute_SimilarityAndRigid(p_hat_i,p_ast,w_i,v)
     85% Similarity transform precompute
     86
     87mu_s = 0;
     88for i = 1:length(w_i)
     89    mu_s = mu_s + w_i(i) * p_hat_i(i,:) * p_hat_i(i,:)';
     90end
     91
     92v_p_ast = (v-p_ast);
     93perpend = (-1)*(perpend_vec(v-p_ast));
     94Matrix_T = [(v-p_ast); (-1)*(perpend_vec(v-p_ast))]';
     95
     96%Similarity = zeros(2,2,length(w_i));
     97%Rigid = zeros(2,2,length(w_i));
     98
     99Similarity = zeros(2*length(w_i),2);
    100Rigid = zeros(2*length(w_i),2);
    101for i = 1:length(w_i)
    102    A_i_temp = w_i(i) ...
    103        * [p_hat_i(i,:) ; (-1)*perpend_vec(p_hat_i(i,:))]...
    104        * Matrix_T;
    105
    106    Similarity(2*i-1:2*i,:) = A_i_temp / mu_s;
    107    Rigid(2*i-1:2*i,:) = A_i_temp;
    108end
    109end
    110%% support function for precompute_SimilarityAndRigid
    111% (x,y) -> (-y,x)

    112function vec = perpend_vec(vec)
    113temp = vec(1);
    114vec(1) = (-1)*vec(2);
    115vec(2) = temp;
    116end
    117
    118
    119