-
Notifications
You must be signed in to change notification settings - Fork 73
/
faceneighbors.m
97 lines (84 loc) · 2.75 KB
/
faceneighbors.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
function facenb = faceneighbors(t, opt)
%
% facenb=faceneighbors(t,opt)
%
% to find 4 face-neighboring elements of a tetrahedron
%
% author: Qianqian Fang, <q.fang at neu.edu>
%
% input:
% t: tetrahedron element list, 4 columns of integers
% opt: if opt='surface', return boundary triangle list
% (should be the same as the face output from v2m)
% if opt='rowmajor', same as 'surface', except the
% order of the triangles are in the row-major order
%%
% otherwise, return the element list for each element:
% each row contains 4 numbers, representing the element
% indices sharing triangular faces [1 2 3],[1 2 4],[1 3 4]
% and [2 3 4] in order, where 1~4 is the node local index.
% if the index is 0, indicating the face has no neighbor
% (i.e. a boundary face)
%
% output:
% facenb: see opt
%
% -- this function is part of iso2mesh toolbox (http://iso2mesh.sf.net)
%
faces = [t(:, [1, 2, 3])
t(:, [1, 2, 4])
t(:, [1, 3, 4])
t(:, [2, 3, 4])];
faces = sort(faces, 2);
[foo, ix, jx] = unique(faces, 'rows');
if (isoctavemesh)
u = unique(jx);
qx = u(hist(jx, u) == 2);
else
vec = histc(jx, 1:max(jx));
qx = find(vec == 2);
end
nn = max(t(:));
ne = size(t, 1);
facenb = zeros(size(t));
% now I need to find all repeatitive elements
% that share a face, to do this, unique('first')
% will give me the 1st element, and 'last' will
% give me the second. There will be no more than 2
% doing this is 60 times faster than doing find(jx==qx(i))
% inside a loop
if (isoctavemesh || datenum(version('-date')) > datenum('January 27 2006')) % compare to matlab 7.2
[ujx, ii] = unique(jx, 'first');
[ujx, ii2] = unique(jx, 'last');
else
ujx = unique(jx);
[t1, ii2] = ismember(ujx, jx);
[t1, ii] = ismember(ujx, flipwd(jx(:)));
ii = length(jx) - ii + 1;
end
% iddup is the list of all pairs that share a common face
iddup = [ii(qx) ii2(qx)];
faceid = ceil(iddup / ne);
eid = mod(iddup, ne);
eid(eid == 0) = ne;
% now rearrange this list into an element format
for i = 1:length(qx)
facenb(eid(i, 1), faceid(i, 1)) = eid(i, 2);
facenb(eid(i, 2), faceid(i, 2)) = eid(i, 1);
end
% facenb may contain 0s, that just means the corresponding
% face is a boundary face and has no neighbor.
% if the second option is 'surface', I am going to find
% and return surface patches only
if (nargin == 2)
if (strcmp(opt, 'surface'))
facenb = faces(find(facenb == 0), :);
elseif (strcmp(opt, 'rowmajor'))
index = [1:length(faces)];
index = (reshape(index, [], 4))';
faces = faces(index(:), :);
facenb = faces(find(facenb' == 0), :);
else
error(['supplied option "' opt '" is not supported.']);
end
end