%% 获取边界信息 Get boundary information
% bedgesID.{T，B，E，W，N，S}：边界棱边索引 Boundary edge index
% T：min(z); B:max(z); E: max(y); W: min(y) ;  N: max(x); S: min(x);
% edgeCen：边界棱边中心坐标 Coordinates of the boundary edge center
% edgeUnit :边界棱边单位矢量 Unit vector of the boundary edge
% elemID ：边界四面体编号 Boundary tetrahedron index
% elemCen：边界四面体中心坐标 Coordinates of the center of tetrahedron on boundary
%
%      Schematic diagram of polarization modes and model boundaries
%

function [bnd] = get_boundary(node2coord,edgeCen,elemCen,edgeUnit,elem2edge)

edgeID.T = find( abs( edgeCen(:,3)-min(node2coord(:,3)) )<0.1 );% The surface is oriented upwards as negative (air), top boundary edge index
edgeID.B = find( abs( edgeCen(:,3)-max(node2coord(:,3)) )<0.1 );% The surface is oriented downward as positive (layer), bottom boundary edge index
edgeID.E = find( abs( edgeCen(:,2)-max(node2coord(:,2)) )<0.1 );% East boundary edge index
edgeID.W = find( abs( edgeCen(:,2)-min(node2coord(:,2)) )<0.1 );% West boundary edge index
edgeID.N = find( abs( edgeCen(:,1)-max(node2coord(:,1)) )<0.1 );% North boundary edge index
edgeID.S = find( abs( edgeCen(:,1)-min(node2coord(:,1)) )<0.1 );% South boundary edge index
edgeID.all = find( abs( edgeCen(:,1)-min(node2coord(:,1)) )<0.1 |  abs( edgeCen(:,1)-max(node2coord(:,1)) )<0.1 ...
    | abs( edgeCen(:,2)-min(node2coord(:,2)) )<0.1 |  abs(edgeCen(:,2)-max(node2coord(:,2)))<0.1 ...
    | abs( edgeCen(:,3)-min(node2coord(:,3)) )<0.1 |  abs(edgeCen(:,3)-max(node2coord(:,3)))<0.1 );% No duplicate boundary edges
bnd.edgeID =edgeID;

bnd.edgeCen.T = edgeCen(edgeID.T,:);% Coordinates of the east boundary edge center
bnd.edgeCen.B = edgeCen(edgeID.B,:);
bnd.edgeCen.E = edgeCen(edgeID.E,:);
bnd.edgeCen.W = edgeCen(edgeID.W,:);
bnd.edgeCen.N = edgeCen(edgeID.N,:);
bnd.edgeCen.S = edgeCen(edgeID.S,:);
bnd.edgeCen.all = edgeCen(edgeID.all,:);

% scatter3(bnd.edgeCen.T(:,1),bnd.edgeCen.T(:,2),bnd.edgeCen.T(:,3))
% hold on;scatter3(bnd.edgeCen.B(:,1),bnd.edgeCen.B(:,2),bnd.edgeCen.B(:,3))
% hold on;scatter3(bnd.edgeCen.E(:,1),bnd.edgeCen.E(:,2),bnd.edgeCen.E(:,3))
% hold on;scatter3(bnd.edgeCen.W(:,1),bnd.edgeCen.W(:,2),bnd.edgeCen.W(:,3))
% hold on;scatter3(bnd.edgeCen.N(:,1),bnd.edgeCen.N(:,2),bnd.edgeCen.N(:,3))
% hold on;scatter3(bnd.edgeCen.S(:,1),bnd.edgeCen.S(:,2),bnd.edgeCen.S(:,3))

unit.T = edgeUnit(edgeID.T,:);% Unit vector of the east boundary edge
unit.B = edgeUnit(edgeID.B,:);
unit.E = edgeUnit(edgeID.E,:);
unit.W = edgeUnit(edgeID.W,:);
unit.N = edgeUnit(edgeID.N,:);
unit.S = edgeUnit(edgeID.S,:);
unit.all = edgeUnit(edgeID.all,:);
bnd.unit=unit;
% 
% LenEW = max(node2coord(:,1)) -min(node2coord(:,1)) ;
% LenNS = max(node2coord(:,2)) -min(node2coord(:,2)) ;
% a = 0.1;
% elemID.E = find( abs( elemCen(:,1)-max(node2coord(:,1)) )<a*LenEW ); % 东边界四面体编号
% elemID.W = find( abs( elemCen(:,1)-min(node2coord(:,1)) )<a*LenEW ); % 西边界四面体编号
% elemID.N = find( abs( elemCen(:,2)-max(node2coord(:,2)) )<a*LenNS ); % 东边界四面体编号
% elemID.S = find( abs( elemCen(:,2)-min(node2coord(:,2)) )<a*LenNS ); % 西边界四面体编号

[row, ~] = find(ismember(elem2edge,edgeID.T));
elemID.T = unique(row); % 上边界四面体编号 Tetrahedron index of the top boundary
[row, ~] = find(ismember(elem2edge,edgeID.B));
elemID.B = unique(row); % 底边界四面体编号 Tetrahedron index of the bottom boundary
[row, ~] = find(ismember(elem2edge,edgeID.E));
elemID.E = unique(row); % 东边界四面体编号 Tetrahedron index of the east boundary
[row, ~] = find(ismember(elem2edge,edgeID.W));
elemID.W = unique(row); % 西边界四面体编号 Tetrahedron index of the west boundary
[row, ~] = find(ismember(elem2edge,edgeID.N));
elemID.N = unique(row); % 北边界四面体编号 Tetrahedron index of the north boundary
[row, ~] = find(ismember(elem2edge,edgeID.S));
elemID.S = unique(row); % 南边界四面体编号 Tetrahedron index of the south boundary

bnd.elemID=elemID;

bnd.elemCen.E = elemCen(elemID.E,:); % Coordinates of the center of tetrahedron on the east boundary
bnd.elemCen.W = elemCen(elemID.W,:);
bnd.elemCen.N = elemCen(elemID.N,:);
bnd.elemCen.S = elemCen(elemID.S,:);

end


%%