% Archana Gupta
% Project
% function to recalculate new vertices during smoothing of simplified mesh

function newv = newvert(v,f)

% call to diffnorm function to calulate triangle area, diffused normals and
% triangle centroids
[areatri, vold, ndiffnorm, centroid]=diffnorm(v,f);

frow=size(f,1);

vrow = size(vold,1);
newv=[];

%vertex update
for j = 1:vrow
    
    %find for each vertex the position of triangles adjacent to it
    pos=find(f==j);
    %call to function uniqueneigh to find unique index positions of the
    pos=uniqueneigh(pos,frow);
    
    possize=size(pos,1);
    
    areatot=0;
    summation=[0 0 0];
    
    for i=1:possize
        areatot = areatot + areatri(pos(i));
    end
              
    for i=1:possize
        center=centroid(pos(i),:);
        proj = [(dot((center-vold(j,:)),(ndiffnorm(pos(i),:))))/(dot( ndiffnorm(pos(i),:)  , ndiffnorm(pos(i),:)))] * (ndiffnorm(pos(i),:));
        summation = summation + (areatri(pos(i)) * proj);
    end
    
    newv(j,1:3) = vold(j,:) + (summation / areatot);
end
   
   
   
    