ItsNeighborIndex.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. #include <iostream>
  2. #include <vector>
  3. #include <unordered_map>
  4. #include <map>
  5. #include "ItsNeighborIndex.hpp"
  6. #include "libslic3r/Execution/ExecutionTBB.hpp"
  7. #include "libslic3r/Execution/ExecutionSeq.hpp"
  8. #include "tbb/parallel_sort.h"
  9. namespace Slic3r {
  10. FaceNeighborIndex its_create_neighbors_index_1(const indexed_triangle_set &its)
  11. {
  12. // Just to be clear what type of object are we referencing
  13. using FaceID = size_t;
  14. using VertexID = uint64_t;
  15. using EdgeID = uint64_t;
  16. constexpr auto UNASSIGNED = std::numeric_limits<FaceID>::max();
  17. struct Edge // Will contain IDs of the two facets touching this edge
  18. {
  19. FaceID first, second;
  20. Edge() : first{UNASSIGNED}, second{UNASSIGNED} {}
  21. void assign(FaceID fid)
  22. {
  23. first == UNASSIGNED ? first = fid : second = fid;
  24. }
  25. };
  26. // All vertex IDs will fit into this number of bits. (Used for hashing)
  27. const int max_vertex_id_bits = std::ceil(std::log2(its.vertices.size()));
  28. assert(max_vertex_id_bits <= 32);
  29. std::unordered_map< EdgeID, Edge> edge_index;
  30. // Edge id is constructed by concatenating two vertex ids, starting with
  31. // the lowest in MSB
  32. auto hash = [max_vertex_id_bits] (VertexID a, VertexID b) {
  33. if (a > b) std::swap(a, b);
  34. return (a << max_vertex_id_bits) + b;
  35. };
  36. // Go through all edges of all facets and mark the facets touching each edge
  37. for (size_t face_id = 0; face_id < its.indices.size(); ++face_id) {
  38. const Vec3i &face = its.indices[face_id];
  39. EdgeID e1 = hash(face(0), face(1)), e2 = hash(face(1), face(2)),
  40. e3 = hash(face(2), face(0));
  41. edge_index[e1].assign(face_id);
  42. edge_index[e2].assign(face_id);
  43. edge_index[e3].assign(face_id);
  44. }
  45. FaceNeighborIndex index(its.indices.size());
  46. // Now collect the neighbors for each facet into the final index
  47. for (size_t face_id = 0; face_id < its.indices.size(); ++face_id) {
  48. const Vec3i &face = its.indices[face_id];
  49. EdgeID e1 = hash(face(0), face(1)), e2 = hash(face(1), face(2)),
  50. e3 = hash(face(2), face(0));
  51. const Edge &neighs1 = edge_index[e1];
  52. const Edge &neighs2 = edge_index[e2];
  53. const Edge &neighs3 = edge_index[e3];
  54. std::array<size_t, 3> &neighs = index[face_id];
  55. neighs[0] = neighs1.first == face_id ? neighs1.second : neighs1.first;
  56. neighs[1] = neighs2.first == face_id ? neighs2.second : neighs2.first;
  57. neighs[2] = neighs3.first == face_id ? neighs3.second : neighs3.first;
  58. }
  59. return index;
  60. }
  61. std::vector<Vec3i> its_create_neighbors_index_2(const indexed_triangle_set &its)
  62. {
  63. std::vector<Vec3i> out(its.indices.size(), Vec3i(-1, -1, -1));
  64. // Create a mapping from triangle edge into face.
  65. struct EdgeToFace {
  66. // Index of the 1st vertex of the triangle edge. vertex_low <= vertex_high.
  67. int vertex_low;
  68. // Index of the 2nd vertex of the triangle edge.
  69. int vertex_high;
  70. // Index of a triangular face.
  71. int face;
  72. // Index of edge in the face, starting with 1. Negative indices if the edge was stored reverse in (vertex_low, vertex_high).
  73. int face_edge;
  74. bool operator==(const EdgeToFace &other) const { return vertex_low == other.vertex_low && vertex_high == other.vertex_high; }
  75. bool operator<(const EdgeToFace &other) const { return vertex_low < other.vertex_low || (vertex_low == other.vertex_low && vertex_high < other.vertex_high); }
  76. };
  77. std::vector<EdgeToFace> edges_map;
  78. edges_map.assign(its.indices.size() * 3, EdgeToFace());
  79. for (uint32_t facet_idx = 0; facet_idx < its.indices.size(); ++ facet_idx)
  80. for (int i = 0; i < 3; ++ i) {
  81. EdgeToFace &e2f = edges_map[facet_idx * 3 + i];
  82. e2f.vertex_low = its.indices[facet_idx][i];
  83. e2f.vertex_high = its.indices[facet_idx][(i + 1) % 3];
  84. e2f.face = facet_idx;
  85. // 1 based indexing, to be always strictly positive.
  86. e2f.face_edge = i + 1;
  87. if (e2f.vertex_low > e2f.vertex_high) {
  88. // Sort the vertices
  89. std::swap(e2f.vertex_low, e2f.vertex_high);
  90. // and make the face_edge negative to indicate a flipped edge.
  91. e2f.face_edge = - e2f.face_edge;
  92. }
  93. }
  94. std::sort(edges_map.begin(), edges_map.end());
  95. // Assign a unique common edge id to touching triangle edges.
  96. int num_edges = 0;
  97. for (size_t i = 0; i < edges_map.size(); ++ i) {
  98. EdgeToFace &edge_i = edges_map[i];
  99. if (edge_i.face == -1)
  100. // This edge has been connected to some neighbor already.
  101. continue;
  102. // Unconnected edge. Find its neighbor with the correct orientation.
  103. size_t j;
  104. bool found = false;
  105. for (j = i + 1; j < edges_map.size() && edge_i == edges_map[j]; ++ j)
  106. if (edge_i.face_edge * edges_map[j].face_edge < 0 && edges_map[j].face != -1) {
  107. // Faces touching with opposite oriented edges and none of the edges is connected yet.
  108. found = true;
  109. break;
  110. }
  111. if (! found) {
  112. //FIXME Vojtech: Trying to find an edge with equal orientation. This smells.
  113. // admesh can assign the same edge ID to more than two facets (which is
  114. // still topologically correct), so we have to search for a duplicate of
  115. // this edge too in case it was already seen in this orientation
  116. for (j = i + 1; j < edges_map.size() && edge_i == edges_map[j]; ++ j)
  117. if (edges_map[j].face != -1) {
  118. // Faces touching with equally oriented edges and none of the edges is connected yet.
  119. found = true;
  120. break;
  121. }
  122. }
  123. // Assign an edge index to the 1st face.
  124. // out[edge_i.face](std::abs(edge_i.face_edge) - 1) = num_edges;
  125. if (found) {
  126. EdgeToFace &edge_j = edges_map[j];
  127. out[edge_i.face](std::abs(edge_i.face_edge) - 1) = edge_j.face;
  128. out[edge_j.face](std::abs(edge_j.face_edge) - 1) = edge_i.face;
  129. // Mark the edge as connected.
  130. edge_j.face = -1;
  131. }
  132. ++ num_edges;
  133. }
  134. return out;
  135. }
  136. std::vector<Vec3i> its_create_neighbors_index_3(const indexed_triangle_set &its)
  137. {
  138. std::vector<Vec3i> out(its.indices.size(), Vec3i(-1, -1, -1));
  139. // Create a mapping from triangle edge into face.
  140. struct EdgeToFace {
  141. // Index of the 1st vertex of the triangle edge. vertex_low <= vertex_high.
  142. int vertex_low;
  143. // Index of the 2nd vertex of the triangle edge.
  144. int vertex_high;
  145. // Index of a triangular face.
  146. int face;
  147. // Index of edge in the face, starting with 1. Negative indices if the edge was stored reverse in (vertex_low, vertex_high).
  148. int face_edge;
  149. bool operator==(const EdgeToFace &other) const { return vertex_low == other.vertex_low && vertex_high == other.vertex_high; }
  150. bool operator<(const EdgeToFace &other) const { return vertex_low < other.vertex_low || (vertex_low == other.vertex_low && vertex_high < other.vertex_high); }
  151. };
  152. std::vector<EdgeToFace> edges_map;
  153. edges_map.assign(its.indices.size() * 3, EdgeToFace());
  154. for (uint32_t facet_idx = 0; facet_idx < its.indices.size(); ++ facet_idx)
  155. for (int i = 0; i < 3; ++ i) {
  156. EdgeToFace &e2f = edges_map[facet_idx * 3 + i];
  157. e2f.vertex_low = its.indices[facet_idx][i];
  158. e2f.vertex_high = its.indices[facet_idx][(i + 1) % 3];
  159. e2f.face = facet_idx;
  160. // 1 based indexing, to be always strictly positive.
  161. e2f.face_edge = i + 1;
  162. if (e2f.vertex_low > e2f.vertex_high) {
  163. // Sort the vertices
  164. std::swap(e2f.vertex_low, e2f.vertex_high);
  165. // and make the face_edge negative to indicate a flipped edge.
  166. e2f.face_edge = - e2f.face_edge;
  167. }
  168. }
  169. tbb::parallel_sort(edges_map.begin(), edges_map.end());
  170. // Assign a unique common edge id to touching triangle edges.
  171. int num_edges = 0;
  172. for (size_t i = 0; i < edges_map.size(); ++ i) {
  173. EdgeToFace &edge_i = edges_map[i];
  174. if (edge_i.face == -1)
  175. // This edge has been connected to some neighbor already.
  176. continue;
  177. // Unconnected edge. Find its neighbor with the correct orientation.
  178. size_t j;
  179. bool found = false;
  180. for (j = i + 1; j < edges_map.size() && edge_i == edges_map[j]; ++ j)
  181. if (edge_i.face_edge * edges_map[j].face_edge < 0 && edges_map[j].face != -1) {
  182. // Faces touching with opposite oriented edges and none of the edges is connected yet.
  183. found = true;
  184. break;
  185. }
  186. if (! found) {
  187. //FIXME Vojtech: Trying to find an edge with equal orientation. This smells.
  188. // admesh can assign the same edge ID to more than two facets (which is
  189. // still topologically correct), so we have to search for a duplicate of
  190. // this edge too in case it was already seen in this orientation
  191. for (j = i + 1; j < edges_map.size() && edge_i == edges_map[j]; ++ j)
  192. if (edges_map[j].face != -1) {
  193. // Faces touching with equally oriented edges and none of the edges is connected yet.
  194. found = true;
  195. break;
  196. }
  197. }
  198. // Assign an edge index to the 1st face.
  199. // out[edge_i.face](std::abs(edge_i.face_edge) - 1) = num_edges;
  200. if (found) {
  201. EdgeToFace &edge_j = edges_map[j];
  202. out[edge_i.face](std::abs(edge_i.face_edge) - 1) = edge_j.face;
  203. out[edge_j.face](std::abs(edge_j.face_edge) - 1) = edge_i.face;
  204. // Mark the edge as connected.
  205. edge_j.face = -1;
  206. }
  207. ++ num_edges;
  208. }
  209. return out;
  210. }
  211. FaceNeighborIndex its_create_neighbors_index_4(const indexed_triangle_set &its)
  212. {
  213. // Just to be clear what type of object are we referencing
  214. using FaceID = size_t;
  215. using VertexID = uint64_t;
  216. using EdgeID = uint64_t;
  217. constexpr auto UNASSIGNED = std::numeric_limits<FaceID>::max();
  218. struct Edge // Will contain IDs of the two facets touching this edge
  219. {
  220. FaceID first, second;
  221. Edge() : first{UNASSIGNED}, second{UNASSIGNED} {}
  222. void assign(FaceID fid)
  223. {
  224. first == UNASSIGNED ? first = fid : second = fid;
  225. }
  226. };
  227. Benchmark bm;
  228. bm.start();
  229. // All vertex IDs will fit into this number of bits. (Used for hashing)
  230. // const int max_vertex_id_bits = std::ceil(std::log2(its.vertices.size()));
  231. // assert(max_vertex_id_bits <= 32);
  232. const uint64_t Vn = its.vertices.size();
  233. // const uint64_t Fn = 3 * its.indices.size();
  234. // double MaxQ = double(Vn) * (Vn + 1) / Fn;
  235. // const uint64_t Nq = MaxQ < 0 ? 0 : std::ceil(std::log2(MaxQ));
  236. // const uint64_t Nr = std::ceil(std::log2(std::min(Vn * (Vn + 1), Fn)));
  237. // const uint64_t Nfn = std::ceil(std::log2(Fn));
  238. //// const uint64_t max_edge_ids = (uint64_t(1) << (Nq + Nr));
  239. // const uint64_t max_edge_ids = MaxQ * Fn + (std::min(Vn * (Vn + 1), Fn)); //(uint64_t(1) << Nfn);
  240. const uint64_t Fn = 3 * its.indices.size();
  241. std::vector< Edge > edge_index(3 * Fn);
  242. // Edge id is constructed by concatenating two vertex ids, starting with
  243. // the lowest in MSB
  244. auto hash = [Vn, Fn /*, Nr*/] (VertexID a, VertexID b) {
  245. if (a > b) std::swap(a, b);
  246. uint64_t C = Vn * a + b;
  247. uint64_t Q = C / Fn, R = C % Fn;
  248. return Q * Fn + R;
  249. };
  250. // Go through all edges of all facets and mark the facets touching each edge
  251. for (size_t face_id = 0; face_id < its.indices.size(); ++face_id) {
  252. const Vec3i &face = its.indices[face_id];
  253. EdgeID e1 = hash(face(0), face(1)), e2 = hash(face(1), face(2)),
  254. e3 = hash(face(2), face(0));
  255. edge_index[e1].assign(face_id);
  256. edge_index[e2].assign(face_id);
  257. edge_index[e3].assign(face_id);
  258. }
  259. FaceNeighborIndex index(its.indices.size());
  260. // Now collect the neighbors for each facet into the final index
  261. for (size_t face_id = 0; face_id < its.indices.size(); ++face_id) {
  262. const Vec3i &face = its.indices[face_id];
  263. EdgeID e1 = hash(face(0), face(1)), e2 = hash(face(1), face(2)),
  264. e3 = hash(face(2), face(0));
  265. const Edge &neighs1 = edge_index[e1];
  266. const Edge &neighs2 = edge_index[e2];
  267. const Edge &neighs3 = edge_index[e3];
  268. std::array<size_t, 3> &neighs = index[face_id];
  269. neighs[0] = neighs1.first == face_id ? neighs1.second : neighs1.first;
  270. neighs[1] = neighs2.first == face_id ? neighs2.second : neighs2.first;
  271. neighs[2] = neighs3.first == face_id ? neighs3.second : neighs3.first;
  272. }
  273. bm.stop();
  274. std::cout << "Creating neighbor index took: " << bm.getElapsedSec() << " seconds." << std::endl;
  275. return index;
  276. }
  277. // Create an index of faces belonging to each vertex. The returned vector can
  278. // be indexed with vertex indices and contains a list of face indices for each
  279. // vertex.
  280. std::vector<std::vector<size_t>> create_vertex_faces_index(const indexed_triangle_set &its)
  281. {
  282. std::vector<std::vector<size_t>> index;
  283. if (! its.vertices.empty()) {
  284. size_t res = its.indices.size() / its.vertices.size();
  285. index.assign(its.vertices.size(), reserve_vector<size_t>(res));
  286. for (size_t fi = 0; fi < its.indices.size(); ++fi) {
  287. auto &face = its.indices[fi];
  288. index[face(0)].emplace_back(fi);
  289. index[face(1)].emplace_back(fi);
  290. index[face(2)].emplace_back(fi);
  291. }
  292. }
  293. return index;
  294. }
  295. //static int get_vertex_index(size_t vertex_index, const stl_triangle_vertex_indices &triangle_indices) {
  296. // if (vertex_index == triangle_indices[0]) return 0;
  297. // if (vertex_index == triangle_indices[1]) return 1;
  298. // if (vertex_index == triangle_indices[2]) return 2;
  299. // return -1;
  300. //}
  301. //static Vec2crd get_edge_indices(int edge_index, const stl_triangle_vertex_indices &triangle_indices)
  302. //{
  303. // int next_edge_index = (edge_index == 2) ? 0 : edge_index + 1;
  304. // coord_t vi0 = triangle_indices[edge_index];
  305. // coord_t vi1 = triangle_indices[next_edge_index];
  306. // return Vec2crd(vi0, vi1);
  307. //}
  308. static std::vector<std::vector<size_t>> create_vertex_faces_index(
  309. const std::vector<stl_triangle_vertex_indices>& indices, size_t count_vertices)
  310. {
  311. if (count_vertices == 0) return {};
  312. std::vector<std::vector<size_t>> index;
  313. size_t res = indices.size() / count_vertices;
  314. index.assign(count_vertices, reserve_vector<size_t>(res));
  315. for (size_t fi = 0; fi < indices.size(); ++fi) {
  316. auto &face = indices[fi];
  317. index[face(0)].emplace_back(fi);
  318. index[face(1)].emplace_back(fi);
  319. index[face(2)].emplace_back(fi);
  320. }
  321. return index;
  322. }
  323. std::vector<Vec3crd> its_create_neighbors_index_5(const indexed_triangle_set &its)
  324. {
  325. const std::vector<stl_triangle_vertex_indices> &indices = its.indices;
  326. size_t vertices_size = its.vertices.size();
  327. if (indices.empty() || vertices_size == 0) return {};
  328. std::vector<std::vector<size_t>> vertex_triangles = create_vertex_faces_index(indices, vertices_size);
  329. coord_t no_value = -1;
  330. std::vector<Vec3crd> neighbors(indices.size(), Vec3crd(no_value, no_value, no_value));
  331. for (const stl_triangle_vertex_indices& triangle_indices : indices) {
  332. coord_t index = &triangle_indices - &indices.front();
  333. Vec3crd& neighbor = neighbors[index];
  334. for (int edge_index = 0; edge_index < 3; ++edge_index) {
  335. // check if done
  336. coord_t& neighbor_edge = neighbor[edge_index];
  337. if (neighbor_edge != no_value) continue;
  338. Vec2crd edge_indices = get_edge_indices(edge_index, triangle_indices);
  339. // IMPROVE: use same vector for 2 sides of triangle
  340. const std::vector<size_t> &faces = vertex_triangles[edge_indices[0]];
  341. for (const size_t &face : faces) {
  342. if (face <= index) continue;
  343. const stl_triangle_vertex_indices &face_indices = indices[face];
  344. int vertex_index = get_vertex_index(edge_indices[1], face_indices);
  345. // NOT Contain second vertex?
  346. if (vertex_index < 0) continue;
  347. // Has NOT oposit direction?
  348. if (edge_indices[0] != face_indices[(vertex_index + 1) % 3]) continue;
  349. neighbor_edge = face;
  350. neighbors[face][vertex_index] = index;
  351. break;
  352. }
  353. // must be paired
  354. assert(neighbor_edge != no_value);
  355. }
  356. }
  357. return neighbors;
  358. }
  359. std::vector<std::array<size_t, 3>> its_create_neighbors_index_6(const indexed_triangle_set &its)
  360. {
  361. constexpr auto UNASSIGNED_EDGE = std::numeric_limits<uint64_t>::max();
  362. constexpr auto UNASSIGNED_FACE = std::numeric_limits<size_t>::max();
  363. struct Edge
  364. {
  365. uint64_t id = UNASSIGNED_EDGE;
  366. size_t face_id = UNASSIGNED_FACE;
  367. bool operator < (const Edge &e) const { return id < e.id; }
  368. };
  369. const size_t facenum = its.indices.size();
  370. // All vertex IDs will fit into this number of bits. (Used for hashing)
  371. const int max_vertex_id_bits = std::ceil(std::log2(its.vertices.size()));
  372. assert(max_vertex_id_bits <= 32);
  373. // Edge id is constructed by concatenating two vertex ids, starting with
  374. // the lowest in MSB
  375. auto hash = [max_vertex_id_bits] (uint64_t a, uint64_t b) {
  376. if (a > b) std::swap(a, b);
  377. return (a << max_vertex_id_bits) + b;
  378. };
  379. std::vector<Edge> edge_map(3 * facenum);
  380. // Go through all edges of all facets and mark the facets touching each edge
  381. for (size_t face_id = 0; face_id < facenum; ++face_id) {
  382. const Vec3i &face = its.indices[face_id];
  383. edge_map[face_id * 3] = {hash(face(0), face(1)), face_id};
  384. edge_map[face_id * 3 + 1] = {hash(face(1), face(2)), face_id};
  385. edge_map[face_id * 3 + 2] = {hash(face(2), face(0)), face_id};
  386. }
  387. std::sort(edge_map.begin(), edge_map.end());
  388. std::vector<std::array<size_t, 3>> out(facenum, {UNASSIGNED_FACE, UNASSIGNED_FACE, UNASSIGNED_FACE});
  389. auto add_neighbor = [](std::array<size_t, 3> &slot, size_t face_id) {
  390. if (slot[0] == UNASSIGNED_FACE) { slot[0] = face_id; return; }
  391. if (slot[1] == UNASSIGNED_FACE) { slot[1] = face_id; return; }
  392. if (slot[2] == UNASSIGNED_FACE) { slot[2] = face_id; return; }
  393. };
  394. for (auto it = edge_map.begin(); it != edge_map.end();) {
  395. size_t face_id = it->face_id;
  396. uint64_t edge_id = it->id;
  397. while (++it != edge_map.end() && (it->id == edge_id)) {
  398. size_t other_face_id = it->face_id;
  399. add_neighbor(out[other_face_id], face_id);
  400. add_neighbor(out[face_id], other_face_id);
  401. }
  402. }
  403. return out;
  404. }
  405. std::vector<std::array<size_t, 3>> its_create_neighbors_index_7(const indexed_triangle_set &its)
  406. {
  407. constexpr auto UNASSIGNED_EDGE = std::numeric_limits<uint64_t>::max();
  408. constexpr auto UNASSIGNED_FACE = std::numeric_limits<size_t>::max();
  409. struct Edge
  410. {
  411. uint64_t id = UNASSIGNED_EDGE;
  412. size_t face_id = UNASSIGNED_FACE;
  413. bool operator < (const Edge &e) const { return id < e.id; }
  414. };
  415. const size_t facenum = its.indices.size();
  416. // All vertex IDs will fit into this number of bits. (Used for hashing)
  417. const int max_vertex_id_bits = std::ceil(std::log2(its.vertices.size()));
  418. assert(max_vertex_id_bits <= 32);
  419. // Edge id is constructed by concatenating two vertex ids, starting with
  420. // the lowest in MSB
  421. auto hash = [max_vertex_id_bits] (uint64_t a, uint64_t b) {
  422. if (a > b) std::swap(a, b);
  423. return (a << max_vertex_id_bits) + b;
  424. };
  425. std::vector<Edge> edge_map(3 * facenum);
  426. // Go through all edges of all facets and mark the facets touching each edge
  427. for (size_t face_id = 0; face_id < facenum; ++face_id) {
  428. const Vec3i &face = its.indices[face_id];
  429. edge_map[face_id * 3] = {hash(face(0), face(1)), face_id};
  430. edge_map[face_id * 3 + 1] = {hash(face(1), face(2)), face_id};
  431. edge_map[face_id * 3 + 2] = {hash(face(2), face(0)), face_id};
  432. }
  433. tbb::parallel_sort(edge_map.begin(), edge_map.end());
  434. std::vector<std::array<size_t, 3>> out(facenum, {UNASSIGNED_FACE, UNASSIGNED_FACE, UNASSIGNED_FACE});
  435. auto add_neighbor = [](std::array<size_t, 3> &slot, size_t face_id) {
  436. if (slot[0] == UNASSIGNED_FACE) { slot[0] = face_id; return; }
  437. if (slot[1] == UNASSIGNED_FACE) { slot[1] = face_id; return; }
  438. if (slot[2] == UNASSIGNED_FACE) { slot[2] = face_id; return; }
  439. };
  440. for (auto it = edge_map.begin(); it != edge_map.end();) {
  441. size_t face_id = it->face_id;
  442. uint64_t edge_id = it->id;
  443. while (++it != edge_map.end() && (it->id == edge_id)) {
  444. size_t other_face_id = it->face_id;
  445. add_neighbor(out[other_face_id], face_id);
  446. add_neighbor(out[face_id], other_face_id);
  447. }
  448. }
  449. return out;
  450. }
  451. FaceNeighborIndex its_create_neighbors_index_8(const indexed_triangle_set &its)
  452. {
  453. // Just to be clear what type of object are we referencing
  454. using FaceID = size_t;
  455. using VertexID = uint64_t;
  456. using EdgeID = uint64_t;
  457. constexpr auto UNASSIGNED = std::numeric_limits<FaceID>::max();
  458. struct Edge // Will contain IDs of the two facets touching this edge
  459. {
  460. FaceID first, second;
  461. Edge() : first{UNASSIGNED}, second{UNASSIGNED} {}
  462. void assign(FaceID fid)
  463. {
  464. first == UNASSIGNED ? first = fid : second = fid;
  465. }
  466. };
  467. // All vertex IDs will fit into this number of bits. (Used for hashing)
  468. const int max_vertex_id_bits = std::ceil(std::log2(its.vertices.size()));
  469. assert(max_vertex_id_bits <= 32);
  470. std::map< EdgeID, Edge > edge_index;
  471. // Edge id is constructed by concatenating two vertex ids, starting with
  472. // the lowest in MSB
  473. auto hash = [max_vertex_id_bits] (VertexID a, VertexID b) {
  474. if (a > b) std::swap(a, b);
  475. return (a << max_vertex_id_bits) + b;
  476. };
  477. // Go through all edges of all facets and mark the facets touching each edge
  478. for (size_t face_id = 0; face_id < its.indices.size(); ++face_id) {
  479. const Vec3i &face = its.indices[face_id];
  480. EdgeID e1 = hash(face(0), face(1)), e2 = hash(face(1), face(2)),
  481. e3 = hash(face(2), face(0));
  482. edge_index[e1].assign(face_id);
  483. edge_index[e2].assign(face_id);
  484. edge_index[e3].assign(face_id);
  485. }
  486. FaceNeighborIndex index(its.indices.size());
  487. // Now collect the neighbors for each facet into the final index
  488. for (size_t face_id = 0; face_id < its.indices.size(); ++face_id) {
  489. const Vec3i &face = its.indices[face_id];
  490. EdgeID e1 = hash(face(0), face(1)), e2 = hash(face(1), face(2)),
  491. e3 = hash(face(2), face(0));
  492. const Edge &neighs1 = edge_index[e1];
  493. const Edge &neighs2 = edge_index[e2];
  494. const Edge &neighs3 = edge_index[e3];
  495. std::array<size_t, 3> &neighs = index[face_id];
  496. neighs[0] = neighs1.first == face_id ? neighs1.second : neighs1.first;
  497. neighs[1] = neighs2.first == face_id ? neighs2.second : neighs2.first;
  498. neighs[2] = neighs3.first == face_id ? neighs3.second : neighs3.first;
  499. }
  500. return index;
  501. }
  502. std::vector<Vec3crd> its_create_neighbors_index_9(const indexed_triangle_set &its)
  503. {
  504. return create_face_neighbors_index(ex_seq, its);
  505. }
  506. std::vector<Vec3i> its_create_neighbors_index_10(const indexed_triangle_set &its)
  507. {
  508. return create_face_neighbors_index(ex_tbb, its);
  509. }
  510. } // namespace Slic3r