function mesh=resortIndex(mesh,theta,err)

%查找对应索引
dstIndex=find((abs(mesh.Vertex(:,2))<err)&(mesh.Vertex(:,1)>err));
tempTheta=atan(mesh.Vertex(:,2)./mesh.Vertex(:,1));
srcIndex=find(abs(tempTheta-theta)<pi/100);
origIndex=find(abs(sqrt(mesh.Vertex(:,2).*mesh.Vertex(:,2)+mesh.Vertex(:,1).*mesh.Vertex(:,1)))<err);

%变换矩阵
traMat=[cos(theta) -sin(theta) 0;
    sin(theta) cos(theta) 0;
    0 0 1];

%src和dst对应关系
nbrIndex=length(srcIndex);
PBCIndex=zeros(nbrIndex,2);
for i=1:nbrIndex
    for j=1:nbrIndex
        vertex1=mesh.Vertex(srcIndex(i),:)';
        vertex2=mesh.Vertex(dstIndex(j),:)';
        vertex2=traMat*vertex2;
        
        ll=abs(norm(vertex1-vertex2));
        if (ll<err)
            PBCIndex(i,1)=srcIndex(i);
            PBCIndex(i,2)=dstIndex(j);
            break;
        else
            continue;
        end
    end
end

%orig src dst 新索引
nbrOrig=length(origIndex);
nbrSrc=length(srcIndex);
newSrcDstIndex=zeros(mesh.NbrVertex,2);
nn=0;
for i=1:nbrOrig
    nn=nn+1;
    newSrcDstIndex(nn,1)=nn;
    newSrcDstIndex(nn,2)=origIndex(i);
end
for i=1:nbrSrc
    nn=nn+1;
    newSrcDstIndex(nn,1)=nn;
    newSrcDstIndex(nn,2)=PBCIndex(i,1);%src
end
for i=1:nbrSrc
    nn=nn+1;
    newSrcDstIndex(nn,1)=nn;
    newSrcDstIndex(nn,2)=PBCIndex(i,2);%dst
end

%其余顶点重排序
otherIndex=(1:mesh.NbrVertex)';
otherIndex(newSrcDstIndex(1:nn,2))=[];
nbrOther=length(otherIndex);
for i=1:nbrOther
    nn=nn+1;
    newSrcDstIndex(nn,1)=nn;
    newSrcDstIndex(nn,2)=otherIndex(i);
end

%顶点和tet重编码
mesh.Vertex=mesh.Vertex(newSrcDstIndex(:,2),:);
[~, sorted_idx] = sort(newSrcDstIndex(:,2));
tempTet=zeros(mesh.NbrTet,4);
tempTet(:,1)=sorted_idx(mesh.Tet(:,1));
tempTet(:,2)=sorted_idx(mesh.Tet(:,2));
tempTet(:,3)=sorted_idx(mesh.Tet(:,3));
tempTet(:,4)=sorted_idx(mesh.Tet(:,4));
mesh.Tet=sort(tempTet,2);

tempTri=zeros(mesh.NbrTri,3);
tempTri(:,1)=sorted_idx(mesh.Tri(:,1));
tempTri(:,2)=sorted_idx(mesh.Tri(:,2));
tempTri(:,3)=sorted_idx(mesh.Tri(:,3));
mesh.Tri=sort(tempTri,2);