21 if (hmi.
type == MeshType::TRI || hmi.
type == MeshType::QUA || hmi.
type == MeshType::H_SUR)
23 std::vector<std::tuple<uint32_t, uint32_t, uint32_t, uint32_t>> temp;
24 temp.reserve(hmi.
faces.size() * 3);
25 for (uint32_t i = 0; i < hmi.
faces.size(); ++i)
27 int vn = hmi.
faces[i].vs.size();
28 for (uint32_t j = 0; j < vn; ++j)
30 uint32_t v0 = hmi.
faces[i].vs[j], v1 = hmi.
faces[i].vs[(j + 1) % vn];
33 temp.push_back(std::make_tuple(v0, v1, i, j));
35 hmi.
faces[i].es.resize(vn);
37 std::sort(temp.begin(), temp.end());
38 hmi.
edges.reserve(temp.size() / 2);
43 for (uint32_t i = 0; i < temp.size(); ++i)
45 if (i == 0 || (i != 0 && (std::get<0>(temp[i]) != std::get<0>(temp[i - 1]) || std::get<1>(temp[i]) != std::get<1>(temp[i - 1]))))
49 e.vs[0] = std::get<0>(temp[i]);
50 e.vs[1] = std::get<1>(temp[i]);
51 hmi.
edges.push_back(e);
53 else if (i != 0 && (std::get<0>(temp[i]) == std::get<0>(temp[i - 1]) && std::get<1>(temp[i]) == std::get<1>(temp[i - 1])))
54 hmi.
edges[E_num - 1].boundary =
false;
56 hmi.
faces[std::get<2>(temp[i])].es[std::get<3>(temp[i])] = E_num - 1;
61 for (uint32_t i = 0; i < hmi.
edges.size(); ++i)
62 if (hmi.
edges[i].boundary)
67 else if (hmi.
type == MeshType::HEX)
70 std::vector<std::vector<uint32_t>> total_fs(hmi.
elements.size() * 6);
71 std::vector<std::tuple<uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t>> tempF(hmi.
elements.size() * 6);
72 std::vector<uint32_t> vs(4);
73 for (uint32_t i = 0; i < hmi.
elements.size(); ++i)
75 for (
short j = 0; j < 6; j++)
77 for (
short k = 0; k < 4; k++)
79 uint32_t
id = 6 * i + j;
81 std::sort(vs.begin(), vs.end());
82 tempF[id] = std::make_tuple(vs[0], vs[1], vs[2], vs[3],
id, i, j);
86 std::sort(tempF.begin(), tempF.end());
87 hmi.
faces.reserve(tempF.size() / 3);
91 for (uint32_t i = 0; i < tempF.size(); ++i)
93 if (i == 0 || (i != 0 && (std::get<0>(tempF[i]) != std::get<0>(tempF[i - 1]) || std::get<1>(tempF[i]) != std::get<1>(tempF[i - 1]) || std::get<2>(tempF[i]) != std::get<2>(tempF[i - 1]) || std::get<3>(tempF[i]) != std::get<3>(tempF[i - 1]))))
97 f.vs = total_fs[std::get<4>(tempF[i])];
98 hmi.
faces.push_back(f);
100 else if (i != 0 && (std::get<0>(tempF[i]) == std::get<0>(tempF[i - 1]) && std::get<1>(tempF[i]) == std::get<1>(tempF[i - 1]) && std::get<2>(tempF[i]) == std::get<2>(tempF[i - 1]) && std::get<3>(tempF[i]) == std::get<3>(tempF[i - 1])))
101 hmi.
faces[F_num - 1].boundary =
false;
103 hmi.
elements[std::get<5>(tempF[i])].fs[std::get<6>(tempF[i])] = F_num - 1;
106 std::vector<std::tuple<uint32_t, uint32_t, uint32_t, uint32_t>> temp(hmi.
faces.size() * 4);
107 for (uint32_t i = 0; i < hmi.
faces.size(); ++i)
109 for (uint32_t j = 0; j < 4; ++j)
111 uint32_t v0 = hmi.
faces[i].vs[j], v1 = hmi.
faces[i].vs[(j + 1) % 4];
114 temp[4 * i + j] = std::make_tuple(v0, v1, i, j);
116 hmi.
faces[i].es.resize(4);
118 std::sort(temp.begin(), temp.end());
119 hmi.
edges.reserve(temp.size() / 2);
124 for (uint32_t i = 0; i < temp.size(); ++i)
126 if (i == 0 || (i != 0 && (std::get<0>(temp[i]) != std::get<0>(temp[i - 1]) || std::get<1>(temp[i]) != std::get<1>(temp[i - 1]))))
130 e.vs[0] = std::get<0>(temp[i]);
131 e.vs[1] = std::get<1>(temp[i]);
132 hmi.
edges.push_back(e);
134 hmi.
faces[std::get<2>(temp[i])].es[std::get<3>(temp[i])] = E_num - 1;
139 for (uint32_t i = 0; i < hmi.
faces.size(); ++i)
140 if (hmi.
faces[i].boundary)
141 for (uint32_t j = 0; j < 4; ++j)
143 uint32_t eid = hmi.
faces[i].es[j];
144 hmi.
edges[eid].boundary =
true;
148 else if (hmi.
type == MeshType::HYB || hmi.
type == MeshType::TET)
150 vector<bool> bf_flag(hmi.
faces.size(),
false);
153 bf_flag[f] = !bf_flag[f];
154 for (
auto &f : hmi.
faces)
155 f.boundary = bf_flag[f.id];
157 std::vector<std::tuple<uint32_t, uint32_t, uint32_t, uint32_t>> temp;
158 for (uint32_t i = 0; i < hmi.
faces.size(); ++i)
160 int fl = hmi.
faces[i].vs.size();
161 for (uint32_t j = 0; j < hmi.
faces[i].vs.size(); ++j)
163 uint32_t v0 = hmi.
faces[i].vs[j], v1 = hmi.
faces[i].vs[(j + 1) % fl];
166 temp.push_back(std::make_tuple(v0, v1, i, j));
168 hmi.
faces[i].es.resize(fl);
170 std::sort(temp.begin(), temp.end());
171 hmi.
edges.reserve(temp.size() / 2);
176 for (uint32_t i = 0; i < temp.size(); ++i)
178 if (i == 0 || (i != 0 && (std::get<0>(temp[i]) != std::get<0>(temp[i - 1]) || std::get<1>(temp[i]) != std::get<1>(temp[i - 1]))))
182 e.vs[0] = std::get<0>(temp[i]);
183 e.vs[1] = std::get<1>(temp[i]);
184 hmi.
edges.push_back(e);
186 hmi.
faces[std::get<2>(temp[i])].es[std::get<3>(temp[i])] = E_num - 1;
191 for (uint32_t i = 0; i < hmi.
faces.size(); ++i)
192 if (hmi.
faces[i].boundary)
193 for (uint32_t j = 0; j < hmi.
faces[i].vs.size(); ++j)
195 uint32_t eid = hmi.
faces[i].es[j];
196 hmi.
edges[eid].boundary =
true;
201 for (
auto &f : hmi.
faces)
202 f.neighbor_hs.clear();
203 for (uint32_t i = 0; i < hmi.
elements.size(); i++)
205 for (uint32_t j = 0; j < hmi.
elements[i].fs.size(); j++)
211 for (
auto &e : hmi.
edges)
212 e.neighbor_fs.clear();
214 v.neighbor_fs.clear();
215 for (uint32_t i = 0; i < hmi.
faces.size(); i++)
217 for (uint32_t j = 0; j < hmi.
faces[i].es.size(); j++)
218 hmi.
edges[hmi.
faces[i].es[j]].neighbor_fs.push_back(i);
219 for (uint32_t j = 0; j < hmi.
faces[i].vs.size(); j++)
225 v.neighbor_es.clear();
226 v.neighbor_vs.clear();
228 for (uint32_t i = 0; i < hmi.
edges.size(); i++)
230 uint32_t v0 = hmi.
edges[i].vs[0], v1 = hmi.
edges[i].vs[1];
231 hmi.
vertices[v0].neighbor_es.push_back(i);
232 hmi.
vertices[v1].neighbor_es.push_back(i);
233 hmi.
vertices[v0].neighbor_vs.push_back(v1);
234 hmi.
vertices[v1].neighbor_vs.push_back(v0);
237 for (
auto &e : hmi.
edges)
238 e.neighbor_hs.clear();
241 for (uint32_t i = 0; i < hmi.
edges.size(); i++)
243 std::vector<uint32_t> nhs;
244 for (uint32_t j = 0; j < hmi.
edges[i].neighbor_fs.size(); j++)
246 uint32_t nfid = hmi.
edges[i].neighbor_fs[j];
247 nhs.insert(nhs.end(), hmi.
faces[nfid].neighbor_hs.begin(), hmi.
faces[nfid].neighbor_hs.end());
249 std::sort(nhs.begin(), nhs.end());
250 nhs.erase(std::unique(nhs.begin(), nhs.end()), nhs.end());
251 hmi.
edges[i].neighbor_hs = nhs;
252 for (
auto nhid : nhs)
256 if (hmi.
type != MeshType::HYB && hmi.
type != MeshType::TET)
260 v.neighbor_hs.clear();
261 for (uint32_t i = 0; i < hmi.
elements.size(); i++)
265 vs.insert(vs.end(), hmi.
faces[fid].vs.begin(), hmi.
faces[fid].vs.end());
266 sort(vs.begin(), vs.end());
267 vs.erase(unique(vs.begin(), vs.end()), vs.end());
273 for (
auto nvid : hmi.
vertices[vid].neighbor_vs)
274 if (
find(vs.begin(), vs.end(), nvid) != vs.end())
283 if (hmi.
elements[i].hex && (vs.size() != 8 || !degree3))
290 int top_fid = hmi.
elements[i].fs[0];
293 std::set<uint32_t> s_model(vs.begin(), vs.end());
294 std::set<uint32_t> s_pattern(hmi.
faces[top_fid].vs.begin(), hmi.
faces[top_fid].vs.end());
295 vector<uint32_t> vs_left;
296 std::set_difference(s_model.begin(), s_model.end(), s_pattern.begin(), s_pattern.end(), std::back_inserter(vs_left));
298 for (
auto vid : hmi.
faces[top_fid].vs)
299 for (
auto nvid : hmi.
vertices[vid].neighbor_vs)
300 if (
find(vs_left.begin(), vs_left.end(), nvid) != vs_left.end())
306 function<int(vector<uint32_t> &,
int &)> WHICH_F = [&](vector<uint32_t> &vs0,
int &f_flag) ->
int {
308 sort(vs0.begin(), vs0.end());
309 bool found_f =
false;
310 for (uint32_t j = 0; j < hmi.
elements[i].fs.size(); j++)
313 vector<uint32_t> vs1 = hmi.
faces[fid].vs;
314 sort(vs1.begin(), vs1.end());
315 if (vs0.size() == vs1.size() && std::equal(vs0.begin(), vs0.end(), vs1.begin()))
317 f_flag = hmi.
elements[i].fs_flag[j];
326 vector<bool> fs_flag;
327 fs_flag.push_back(hmi.
elements[i].fs_flag[0]);
328 fs.push_back(top_fid);
329 vector<uint32_t> vs_temp;
331 vs_temp.insert(vs_temp.end(), hmi.
elements[i].vs.begin() + 4, hmi.
elements[i].vs.end());
333 int bottom_fid = WHICH_F(vs_temp, f_flag);
334 fs_flag.push_back(f_flag);
335 fs.push_back(bottom_fid);
338 vs_temp.push_back(hmi.
elements[i].vs[0]);
339 vs_temp.push_back(hmi.
elements[i].vs[1]);
340 vs_temp.push_back(hmi.
elements[i].vs[4]);
341 vs_temp.push_back(hmi.
elements[i].vs[5]);
343 int front_fid = WHICH_F(vs_temp, f_flag);
344 fs_flag.push_back(f_flag);
345 fs.push_back(front_fid);
348 vs_temp.push_back(hmi.
elements[i].vs[2]);
349 vs_temp.push_back(hmi.
elements[i].vs[3]);
350 vs_temp.push_back(hmi.
elements[i].vs[6]);
351 vs_temp.push_back(hmi.
elements[i].vs[7]);
353 int back_fid = WHICH_F(vs_temp, f_flag);
354 fs_flag.push_back(f_flag);
355 fs.push_back(back_fid);
358 vs_temp.push_back(hmi.
elements[i].vs[1]);
359 vs_temp.push_back(hmi.
elements[i].vs[2]);
360 vs_temp.push_back(hmi.
elements[i].vs[5]);
361 vs_temp.push_back(hmi.
elements[i].vs[6]);
363 int left_fid = WHICH_F(vs_temp, f_flag);
364 fs_flag.push_back(f_flag);
365 fs.push_back(left_fid);
368 vs_temp.push_back(hmi.
elements[i].vs[3]);
369 vs_temp.push_back(hmi.
elements[i].vs[0]);
370 vs_temp.push_back(hmi.
elements[i].vs[7]);
371 vs_temp.push_back(hmi.
elements[i].vs[4]);
373 int right_fid = WHICH_F(vs_temp, f_flag);
374 fs_flag.push_back(f_flag);
375 fs.push_back(right_fid);
382 for (uint32_t j = 0; j < hmi.
elements[i].vs.size(); j++)
386 if (hmi.
type == MeshType::TET)
388 hmi.
EV.resize(2, hmi.
edges.size());
389 for (
const auto &e : hmi.
edges)
391 hmi.
EV(0, e.id) = e.vs[0];
392 hmi.
EV(1, e.id) = e.vs[1];
394 hmi.
FV.resize(3, hmi.
faces.size());
395 hmi.
FE.resize(3, hmi.
faces.size());
396 hmi.
FH.resize(2, hmi.
faces.size());
397 hmi.
FHi.resize(2, hmi.
faces.size());
398 for (
const auto &f : hmi.
faces)
400 hmi.
FV(0, f.id) = f.vs[0];
401 hmi.
FV(1, f.id) = f.vs[1];
402 hmi.
FV(2, f.id) = f.vs[2];
404 hmi.
FE(0, f.id) = f.es[0];
405 hmi.
FE(1, f.id) = f.es[1];
406 hmi.
FE(2, f.id) = f.es[2];
408 hmi.
FH(0, f.id) = f.neighbor_hs[0];
409 for (
int i = 0; i < hmi.
elements[f.neighbor_hs[0]].fs.size(); i++)
410 if (f.id == hmi.
elements[f.neighbor_hs[0]].fs[i])
411 hmi.
FHi(0, f.id) = i;
413 hmi.
FH(1, f.id) = -1;
414 hmi.
FHi(1, f.id) = -1;
415 if (f.neighbor_hs.size() == 2)
417 hmi.
FH(1, f.id) = f.neighbor_hs[1];
418 for (
int i = 0; i < hmi.
elements[f.neighbor_hs[1]].fs.size(); i++)
419 if (f.id == hmi.
elements[f.neighbor_hs[1]].fs[i])
420 hmi.
FHi(1, f.id) = i;
427 hmi.
HV(0, h.id) = h.vs[0];
428 hmi.
HV(1, h.id) = h.vs[1];
429 hmi.
HV(2, h.id) = h.vs[2];
430 hmi.
HV(3, h.id) = h.vs[3];
432 hmi.
HF(0, h.id) = h.fs[0];
433 hmi.
HF(1, h.id) = h.fs[1];
434 hmi.
HF(2, h.id) = h.fs[2];
435 hmi.
HF(3, h.id) = h.fs[3];
440 std::vector<bool> bv_flag(hmi.
vertices.size(),
false), be_flag(hmi.
edges.size(),
false), bf_flag(hmi.
faces.size(),
false);
441 for (
auto f : hmi.
faces)
442 if (f.boundary && hmi.
elements[f.neighbor_hs[0]].hex)
443 bf_flag[f.id] =
true;
444 else if (!f.boundary)
446 int ele0 = f.neighbor_hs[0], ele1 = f.neighbor_hs[1];
448 bf_flag[f.id] =
true;
450 for (uint32_t i = 0; i < hmi.
faces.size(); ++i)
452 for (uint32_t j = 0; j < hmi.
faces[i].vs.size(); ++j)
454 uint32_t eid = hmi.
faces[i].es[j];
456 bv_flag[hmi.
faces[i].vs[j]] =
true;
460 v.boundary_hex =
false;
461 for (
auto &e : hmi.
edges)
462 e.boundary_hex =
false;
463 for (
auto &f : hmi.
faces)
464 f.boundary_hex = bf_flag[f.id];
468 for (
auto vid : ele.vs)
473 for (
auto nfid : hmi.
vertices[vid].neighbor_fs)
474 if (bf_flag[nfid] && std::find(ele.fs.begin(), ele.fs.end(), nfid) != ele.fs.end())
477 hmi.
vertices[vid].boundary_hex =
true;
479 for (
auto eid : ele.es)
484 for (
auto nfid : hmi.
edges[eid].neighbor_fs)
485 if (bf_flag[nfid] && std::find(ele.fs.begin(), ele.fs.end(), nfid) != ele.fs.end())
488 hmi.
edges[eid].boundary_hex =
true;
717 for (
int i = 0; i < iter; i++)
720 std::vector<int> Refinement_Levels;
724 M_.
type = MeshType::HYB;
726 vector<int> E2V(M.edges.size()), F2V(M.faces.size()), Ele2V(M.elements.size());
729 for (
auto v : M.vertices)
734 for (
int j = 0; j < 3; j++)
735 v_.
v[j] = M.points(j, v.id);
739 for (
auto e : M.edges)
747 for (
auto vid : e.vs)
748 center += M.points.col(vid);
749 center /= e.vs.size();
750 for (
int j = 0; j < 3; j++)
756 for (
auto f : M.faces)
764 for (
auto vid : f.vs)
765 center += M.points.col(vid);
766 center /= f.vs.size();
767 for (
int j = 0; j < 3; j++)
773 for (
auto ele : M.elements)
779 v.
v = ele.v_in_Kernel;
782 Ele2V[ele.id] = v.
id;
785 std::vector<std::vector<uint32_t>> total_fs;
786 total_fs.reserve(M.elements.size() * 8 * 6);
787 std::vector<std::tuple<uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t>> tempF;
788 tempF.reserve(M.elements.size() * 8 * 6);
789 std::vector<uint32_t> vs(4);
791 int elen = 0, fn = 0;
792 for (
auto ele : M.elements)
796 for (
auto vid : ele.vs)
799 vector<int> top_vs(4);
802 for (
auto nfid : M.vertices[vid].neighbor_fs)
803 if (
find(ele.fs.begin(), ele.fs.end(), nfid) != ele.fs.end())
809 top_vs[2] = F2V[fid];
811 int v_ind =
find(M.faces[fid].vs.begin(), M.faces[fid].vs.end(), vid) - M.faces[fid].vs.begin();
812 int e_pre = M.faces[fid].es[(v_ind - 1 + 4) % 4];
813 int e_aft = M.faces[fid].es[v_ind];
814 top_vs[1] = E2V[e_pre];
815 top_vs[3] = E2V[e_aft];
817 vector<int> bottom_vs(4);
819 auto nvs = M.vertices[vid].neighbor_vs;
821 for (
auto nvid : nvs)
822 if (
find(ele.vs.begin(), ele.vs.end(), nvid) != ele.vs.end())
824 if (nvid != M.edges[e_pre].vs[0] && nvid != M.edges[e_pre].vs[1] && nvid != M.edges[e_aft].vs[0] && nvid != M.edges[e_aft].vs[1])
826 vector<uint32_t> sharedes, es0 = M.vertices[vid].neighbor_es, es1 = M.vertices[nvid].neighbor_es;
827 sort(es0.begin(), es0.end());
828 sort(es1.begin(), es1.end());
829 set_intersection(es0.begin(), es0.end(), es1.begin(), es1.end(), back_inserter(sharedes));
830 assert(sharedes.size());
837 bottom_vs[0] = E2V[e_per];
838 bottom_vs[2] = Ele2V[ele.id];
841 vector<uint32_t> sharedfs, fs0 = M.edges[e_pre].neighbor_fs, fs1 = M.edges[e_per].neighbor_fs;
842 sort(fs0.begin(), fs0.end());
843 sort(fs1.begin(), fs1.end());
844 set_intersection(fs0.begin(), fs0.end(), fs1.begin(), fs1.end(), back_inserter(sharedfs));
845 for (
auto sfid : sharedfs)
846 if (
find(ele.fs.begin(), ele.fs.end(), sfid) != ele.fs.end())
855 fs0 = M.edges[e_aft].neighbor_fs;
856 fs1 = M.edges[e_per].neighbor_fs;
857 sort(fs0.begin(), fs0.end());
858 sort(fs1.begin(), fs1.end());
859 set_intersection(fs0.begin(), fs0.end(), fs1.begin(), fs1.end(), back_inserter(sharedfs));
860 for (
auto sfid : sharedfs)
861 if (
find(ele.fs.begin(), ele.fs.end(), sfid) != ele.fs.end())
868 bottom_vs[1] = F2V[f_pre];
869 bottom_vs[3] = F2V[f_aft];
871 vector<int> ele_vs = top_vs;
872 ele_vs.insert(ele_vs.end(), bottom_vs.begin(), bottom_vs.end());
874 for (
short j = 0; j < 6; j++)
876 for (
short k = 0; k < 4; k++)
878 total_fs.push_back(vs);
879 std::sort(vs.begin(), vs.end());
880 tempF.push_back(std::make_tuple(vs[0], vs[1], vs[2], vs[3], fn++, elen, j));
885 ele_.
fs.resize(6, -1);
893 for (
auto evid : ele_vs)
894 for (
int j = 0; j < 3; j++)
895 center[j] += M_.
vertices[evid].v[j];
896 center /= ele_vs.size();
897 for (
int j = 0; j < 3; j++)
901 Parents.push_back(ele.id);
906 int level = Refinement_Levels[ele.id];
910 std::vector<std::vector<int>> local_V2Vs;
911 std::map<int, int> local_vi_map;
912 for (
auto vid : ele.vs)
914 std::vector<int> v2v;
916 for (
int r = 0; r < level; r++)
922 for (
int j = 0; j < 3; j++)
923 v_.
v[j] = M_.
vertices[vid].v[j] + (ele.v_in_Kernel[j] - M_.
vertices[vid].v[j]) * (r + 1.0) / (
double)(level + 1);
927 for (
int j = 0; j < 3; j++)
928 v_.
v[j] = M_.
vertices[vid].v[j] + (M_.
vertices[vid].v[j] - ele.v_in_Kernel[j]) * (r + 1.0) / (
double)(level + 1);
932 v2v.push_back(v_.
id);
934 local_vi_map[vid] = local_V2Vs.size();
935 local_V2Vs.push_back(v2v);
939 for (
auto fid : ele.fs)
940 es.insert(es.end(), M.faces[fid].es.begin(), M.faces[fid].es.end());
941 sort(es.begin(), es.end());
942 es.erase(unique(es.begin(), es.end()), es.end());
944 std::vector<std::vector<int>> local_E2Vs;
945 std::map<int, int> local_ei_map;
948 std::vector<int> e2v;
949 e2v.push_back(E2V[eid]);
950 for (
int r = 0; r < level; r++)
958 for (
auto vid : M.edges[eid].vs)
959 for (
int j = 0; j < 3; j++)
960 center[j] += M_.
vertices[local_V2Vs[local_vi_map[vid]][r + 1]].v[j];
961 center /= M.edges[eid].vs.size();
962 for (
int j = 0; j < 3; j++)
966 e2v.push_back(v_.
id);
968 local_ei_map[eid] = local_E2Vs.size();
969 local_E2Vs.push_back(e2v);
972 std::vector<std::vector<int>> local_F2Vs;
973 std::map<int, int> local_fi_map;
974 for (
auto fid : ele.fs)
976 std::vector<int> f2v;
977 f2v.push_back(F2V[fid]);
978 for (
int r = 0; r < level; r++)
986 for (
auto vid : M.faces[fid].vs)
987 for (
int j = 0; j < 3; j++)
988 center[j] += M_.
vertices[local_V2Vs[local_vi_map[vid]][r + 1]].v[j];
989 center /= M.faces[fid].vs.size();
990 for (
int j = 0; j < 3; j++)
994 f2v.push_back(v_.
id);
996 local_fi_map[fid] = local_F2Vs.size();
997 local_F2Vs.push_back(f2v);
1001 for (
auto fid : ele.fs)
1003 auto &fvs = M.faces[fid].vs;
1004 auto &fes = M.faces[fid].es;
1005 int fvn = M.faces[fid].vs.size();
1006 for (uint32_t j = 0; j < fvn; j++)
1008 vs[0] = local_E2Vs[local_ei_map[fes[(j - 1 + fvn) % fvn]]][level];
1009 vs[1] = local_V2Vs[local_vi_map[fvs[j]]][level];
1010 vs[2] = local_E2Vs[local_ei_map[fes[j]]][level];
1011 vs[3] = local_F2Vs[local_fi_map[fid]][level];
1015 vs[0] = local_E2Vs[local_ei_map[fes[(j - 1 + fvn) % fvn]]][0];
1016 vs[1] = local_V2Vs[local_vi_map[fvs[j]]][0];
1017 vs[2] = local_E2Vs[local_ei_map[fes[j]]][0];
1018 vs[3] = local_F2Vs[local_fi_map[fid]][0];
1021 total_fs.push_back(vs);
1022 std::sort(vs.begin(), vs.end());
1023 tempF.push_back(std::make_tuple(vs[0], vs[1], vs[2], vs[3], fn++, elen, local_fn++));
1029 ele_.
fs.resize(local_fn, -1);
1030 ele_.
fs_flag.resize(local_fn, 1);
1035 Parents.push_back(ele.id);
1037 for (
int r = 0; r < level; r++)
1039 for (
auto fid : ele.fs)
1041 auto &fvs = M.faces[fid].vs;
1042 auto &fes = M.faces[fid].es;
1043 int fvn = M.faces[fid].vs.size();
1044 for (uint32_t j = 0; j < fvn; j++)
1046 vector<int> ele_vs(8);
1047 ele_vs[0] = local_E2Vs[local_ei_map[fes[(j - 1 + fvn) % fvn]]][r + 1];
1048 ele_vs[1] = local_V2Vs[local_vi_map[fvs[j]]][r + 1];
1049 ele_vs[2] = local_E2Vs[local_ei_map[fes[j]]][r + 1];
1050 ele_vs[3] = local_F2Vs[local_fi_map[fid]][r + 1];
1052 ele_vs[4] = local_E2Vs[local_ei_map[fes[(j - 1 + fvn) % fvn]]][r];
1053 ele_vs[5] = local_V2Vs[local_vi_map[fvs[j]]][r];
1054 ele_vs[6] = local_E2Vs[local_ei_map[fes[j]]][r];
1055 ele_vs[7] = local_F2Vs[local_fi_map[fid]][r];
1058 for (
short j = 0; j < 6; j++)
1060 for (
short k = 0; k < 4; k++)
1062 total_fs.push_back(vs);
1063 std::sort(vs.begin(), vs.end());
1064 tempF.push_back(std::make_tuple(vs[0], vs[1], vs[2], vs[3], fn++, elen, j));
1069 ele_.
fs.resize(6, -1);
1076 for (
auto vid : ele_vs)
1077 for (
int j = 0; j < 3; j++)
1078 center[j] += M_.
vertices[vid].v[j];
1079 center /= ele_vs.size();
1080 for (
int j = 0; j < 3; j++)
1083 Parents.push_back(ele.id);
1090 std::sort(tempF.begin(), tempF.end());
1091 M_.
faces.reserve(tempF.size() / 3);
1095 for (uint32_t i = 0; i < tempF.size(); ++i)
1097 if (i == 0 || (i != 0 && (std::get<0>(tempF[i]) != std::get<0>(tempF[i - 1]) || std::get<1>(tempF[i]) != std::get<1>(tempF[i - 1]) || std::get<2>(tempF[i]) != std::get<2>(tempF[i - 1]) || std::get<3>(tempF[i]) != std::get<3>(tempF[i - 1]))))
1101 f.vs = total_fs[std::get<4>(tempF[i])];
1102 M_.
faces.push_back(f);
1104 else if (i != 0 && (std::get<0>(tempF[i]) == std::get<0>(tempF[i - 1]) && std::get<1>(tempF[i]) == std::get<1>(tempF[i - 1]) && std::get<2>(tempF[i]) == std::get<2>(tempF[i - 1]) && std::get<3>(tempF[i]) == std::get<3>(tempF[i - 1])))
1105 M_.
faces[F_num - 1].boundary =
false;
1107 M_.
elements[std::get<5>(tempF[i])].fs[std::get<6>(tempF[i])] = F_num - 1;
1112 for (
int j = 0; j < 3; j++)
1113 M_.
points(j, v.id) = v.v[j];
1136 for (
int i = 0; i < iter; i++)
1140 M_.
type = MeshType::TET;
1142 vector<int> E2V(M.edges.size());
1145 for (
const auto &v : M.vertices)
1150 for (
int j = 0; j < 3; j++)
1151 v_.
v[j] = M.points(j, v.id);
1155 for (
const auto &e : M.edges)
1163 for (
auto vid : e.vs)
1164 center += M.points.col(vid);
1165 center /= e.vs.size();
1166 for (
int j = 0; j < 3; j++)
1191 auto shared_edge = [&](
int v0,
int v1,
int &e) ->
bool {
1192 auto &es0 = M.vertices[v0].neighbor_es, &es1 = M.vertices[v1].neighbor_es;
1193 std::sort(es0.begin(), es0.end());
1194 std::sort(es1.begin(), es1.end());
1195 std::vector<uint32_t> es;
1196 std::set_intersection(es0.begin(), es0.end(), es1.begin(), es1.end(), back_inserter(es));
1205 std::vector<bool> e_flag(M.edges.size(),
false);
1207 for (
const auto &ele : M.elements)
1210 for (
short i = 0; i < 4; i++)
1214 ele_.
fs.resize(4, -1);
1220 ele_.
vs.push_back(ele.vs[i]);
1221 for (
short j = 0; j < 4; j++)
1225 int v0 = ele.vs[i], v1 = ele.vs[j];
1227 if (shared_edge(v0, v1, e))
1228 ele_.
vs.push_back(E2V[e]);
1232 for (
const auto &evid : ele_.
vs)
1233 for (
int j = 0; j < 3; j++)
1234 center[j] += M_.
vertices[evid].v[j];
1235 center /= ele_.
vs.size();
1236 for (
int j = 0; j < 3; j++)
1243 std::vector<int> edges(6);
1244 for (
int i = 0; i < 6; i++)
1248 if (shared_edge(v0, v1, e))
1252 int lv0 = E2V[edges[0]], lv1 = E2V[edges[5]];
1253 e_flag[edges[0]] =
true;
1254 e_flag[edges[5]] =
true;
1255 for (
short i = 0; i < 4; i++)
1259 ele_.
fs.resize(4, -1);
1265 ele_.
vs.push_back(lv0);
1266 ele_.
vs.push_back(lv1);
1267 for (
short j = 0; j < 3; j++)
1269 int c_e = M.faces[ele.fs[i]].es[j];
1272 ele_.
vs.push_back(E2V[c_e]);
1276 for (
const auto &evid : ele_.
vs)
1277 for (
int j = 0; j < 3; j++)
1278 center[j] += M_.
vertices[evid].v[j];
1279 center /= ele_.
vs.size();
1280 for (
int j = 0; j < 3; j++)
1285 e_flag[edges[0]] = e_flag[edges[5]] =
false;
1290 for (
int j = 0; j < 3; j++)
1291 M_.
points(j, v.id) = v.v[j];
1294 const auto &t = M.elements[0].vs;
1295 Vector3d c0 = M.points.col(t[0]);
1296 Vector3d c1 = M.points.col(t[1]);
1297 Vector3d c2 = M.points.col(t[2]);
1298 Vector3d c3 = M.points.col(t[3]);
1303 c0 = M_.
points.col(ele.vs[0]);
1304 c1 = M_.
points.col(ele.vs[1]);
1305 c2 = M_.
points.col(ele.vs[2]);
1306 c3 = M_.
points.col(ele.vs[3]);
1307 bool sign =
a_jacobian(c0, c1, c2, c3) > 0 ? true :
false;
1309 std::swap(ele.vs[1], ele.vs[3]);
1313 std::vector<std::vector<uint32_t>> total_fs;
1314 total_fs.reserve(M.elements.size() * 4);
1315 std::vector<std::tuple<uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t>> tempF;
1316 tempF.reserve(M.elements.size() * 4);
1317 std::vector<uint32_t> vs(3);
1318 for (
const auto &ele : M_.
elements)
1320 for (
int i = 0; i < 4; i++)
1325 total_fs.push_back(vs);
1326 std::sort(vs.begin(), vs.end());
1327 tempF.push_back(std::make_tuple(vs[0], vs[1], vs[2], ele.id * 4 + i, ele.id, i));
1330 std::sort(tempF.begin(), tempF.end());
1331 M_.
faces.reserve(tempF.size() / 3);
1335 for (uint32_t i = 0; i < tempF.size(); ++i)
1337 if (i == 0 || (i != 0 && (std::get<0>(tempF[i]) != std::get<0>(tempF[i - 1]) || std::get<1>(tempF[i]) != std::get<1>(tempF[i - 1]) || std::get<2>(tempF[i]) != std::get<2>(tempF[i - 1]))))
1341 f.vs = total_fs[std::get<3>(tempF[i])];
1342 M_.
faces.push_back(f);
1344 else if (i != 0 && (std::get<0>(tempF[i]) == std::get<0>(tempF[i - 1]) && std::get<1>(tempF[i]) == std::get<1>(tempF[i - 1]) && std::get<2>(tempF[i]) == std::get<2>(tempF[i - 1])))
1345 M_.
faces[F_num - 1].boundary =
false;
1347 M_.
elements[std::get<4>(tempF[i])].fs[std::get<5>(tempF[i])] = F_num - 1;
1356 double hmin = 10000, hmax = 0, havg = 0;
1357 for (
const auto &e : M.edges)
1359 Eigen::Vector3d v0 = M.points.col(e.vs[0]), v1 = M.points.col(e.vs[1]);
1360 double len = (v0 - v1).norm();
1367 havg /= M.edges.size();
1557 M_sur.
type = MeshType::H_SUR;
1562 M_sur.
points.resize(3, bvn);
1564 vector<int> V_map(hmi.
vertices.size(), -1), V_map_reverse;
1573 V_map[v.id] = v_.
id;
1574 V_map_reverse.push_back(v.id);
1576 for (
auto f : hmi.
faces)
1579 for (
int j = 0; j < f.vs.size(); j++)
1581 f.id = M_sur.
faces.size();
1583 f.neighbor_hs.clear();
1584 f.vs[j] = V_map[f.vs[j]];
1585 M_sur.
vertices[f.vs[j]].neighbor_fs.push_back(f.id);
1587 M_sur.
faces.push_back(f);
1593 for (
auto &f : hmi.
faces)
1596 for (
int j = 0; j < f.vs.size(); j++)
1597 f.vs[j] = V_map_reverse[M_sur.
faces[fn_].vs[j]];
1601 vector<bool> F_tag(hmi.
faces.size(),
true);
1602 std::vector<short> F_visit(hmi.
faces.size(), 0);
1603 for (uint32_t j = 0; j < hmi.
faces.size(); j++)
1604 if (hmi.
faces[j].boundary)
1608 std::vector<bool> F_state(hmi.
faces.size(),
false);
1609 std::vector<bool> P_visit(hmi.
elements.size(),
false);
1612 std::vector<uint32_t> candidates;
1613 for (uint32_t j = 0; j < F_visit.size(); j++)
1614 if (F_visit[j] == 1)
1615 candidates.push_back(j);
1616 if (!candidates.size())
1618 for (
auto ca : candidates)
1620 if (F_visit[ca] == 2)
1622 uint32_t pid = hmi.
faces[ca].neighbor_hs[0];
1624 if (hmi.
faces[ca].neighbor_hs.size() == 2)
1625 pid = hmi.
faces[ca].neighbor_hs[1];
1634 uint32_t start_f = ca;
1635 F_tag[start_f] =
true;
1638 F_state[ca] =
false;
1642 std::queue<uint32_t> pf_temp;
1643 pf_temp.push(start_f);
1644 while (!pf_temp.empty())
1646 uint32_t fid = pf_temp.front();
1648 for (
auto eid : hmi.
faces[fid].es)
1649 for (
auto nfid : hmi.
edges[eid].neighbor_fs)
1654 uint32_t v0 = hmi.
edges[eid].vs[0], v1 = hmi.
edges[eid].vs[1];
1655 int32_t v0_pos = std::find(hmi.
faces[fid].vs.begin(), hmi.
faces[fid].vs.end(), v0) - hmi.
faces[fid].vs.begin();
1656 int32_t v1_pos = std::find(hmi.
faces[fid].vs.begin(), hmi.
faces[fid].vs.end(), v1) - hmi.
faces[fid].vs.begin();
1658 if ((v0_pos + 1) % hmi.
faces[fid].vs.size() != v1_pos)
1661 int32_t v0_pos_ = std::find(hmi.
faces[nfid].vs.begin(), hmi.
faces[nfid].vs.end(), v0) - hmi.
faces[nfid].vs.begin();
1662 int32_t v1_pos_ = std::find(hmi.
faces[nfid].vs.begin(), hmi.
faces[nfid].vs.end(), v1) - hmi.
faces[nfid].vs.begin();
1666 if ((v0_pos_ + 1) % hmi.
faces[nfid].vs.size() == v1_pos_)
1667 F_state[nfid] =
false;
1669 F_state[nfid] =
true;
1671 else if (!F_state[fid])
1673 if ((v0_pos_ + 1) % hmi.
faces[nfid].vs.size() == v1_pos_)
1674 F_state[nfid] =
true;
1676 F_state[nfid] =
false;
1685 P_visit[pid] =
true;
1686 for (uint32_t j = 0; j < fs.size(); j++)
1687 hmi.
elements[pid].fs_flag[j] = F_state[fs[j]];