 function constraintM = delta(cens2,cens1,pPath,r)
% delta
%
% funkce pro vypocet matice pro zrychleni vypoctu DTW
%
% Stepan Miklanek, 2018

constraintM(1:length(cens2),1:length(cens1)) = 0;

limit1 = length(cens1)-r;
limit2 = length(cens2)-r;

limit = min(limit1,limit2);

for n = 1:length(pPath)
    
    constraintM((pPath(n,1)),pPath(n,2)) = 5;
   
    if n <= r
    
    col = pPath(n,2) - 1;
    row = pPath(n,1) - 1;
    
    constraintM(pPath(n,1),pPath(n,2)-col:pPath(n,2)) = 5;   
    constraintM(pPath(n,1),pPath(n,2):pPath(n,2)+r) = 5;     
   
    constraintM(pPath(n,1)-row:pPath(n,1),pPath(n,2)) = 5;
    constraintM(pPath(n,1):pPath(n,1)+r,pPath(n,2)) = 5;
    
    elseif n <= limit
        ncheck = pPath(n,2)-r;
        ncheck2 = pPath(n,1)-r;
        
        if ncheck < 1
            newrad = r + ncheck - 1;
            constraintM(pPath(n,1),pPath(n,2)-newrad:pPath(n,2)) = 5;   
        else
            constraintM(pPath(n,1),pPath(n,2)-r:pPath(n,2)) = 5;
        end
        
        if ncheck2 < 1
            newrad2 = r + ncheck2 - 1;
            constraintM(pPath(n,1)-newrad2:pPath(n,1),pPath(n,2)) = 5; 
        else 
            constraintM(pPath(n,1)-r:pPath(n,1),pPath(n,2)) = 5;
        end
        
    constraintM(pPath(n,1),pPath(n,2):pPath(n,2)+r) = 5;
    constraintM(pPath(n,1):pPath(n,1)+r,pPath(n,2)) = 5;    
 
    elseif n > limit
        ncheck = abs(pPath(n,2)-length(cens1)); 
        if ncheck > r
            constraintM(pPath(n,1),pPath(n,2):pPath(n,2)+r) = 5;
        else
            constraintM(pPath(n,1),pPath(n,2):pPath(n,2)+ncheck) = 5; 
        end  
    
        ncheck2 = abs(pPath(n,2)-length(cens1));
        if ncheck2 > r
            constraintM(pPath(n,1):pPath(n,1)+r,pPath(n,2)) = 5; 
        else
            constraintM(pPath(n,1):pPath(n,1)+ncheck2,pPath(n,2)) = 5;
        end
        
        constraintM(pPath(n,1),pPath(n,2)-r:pPath(n,2)) = 5;
        constraintM(pPath(n,1)-r:pPath(n,1),pPath(n,2)) = 5;
    end
    
end

constraintM = constraintM(1:length(cens2),1:length(cens1));

end

