From 81edca713d99a1532e975f53677eb594f4a8cd47 Mon Sep 17 00:00:00 2001 From: Zhicheng Wang <1627343141@qq.com> Date: Fri, 25 Jul 2025 01:20:07 +0800 Subject: [PATCH] revert to dense sample for primitive generation --- network_process/include/helper.hpp | 8 -- network_process/include/prim_gen.hpp | 39 +++---- .../src/prim_gen/comp_imp_arrange.cpp | 19 ++-- network_process/src/prim_gen/extract_mesh.cpp | 77 ++++++------- .../src/prim_gen/extract_vertex_infos.cpp | 8 +- .../src/prim_gen/filter_tet_by_subface.cpp | 105 +++++++++--------- network_process/src/process.cpp | 10 +- 7 files changed, 133 insertions(+), 133 deletions(-) diff --git a/network_process/include/helper.hpp b/network_process/include/helper.hpp index c9b2e35..61b017d 100644 --- a/network_process/include/helper.hpp +++ b/network_process/include/helper.hpp @@ -4,14 +4,6 @@ #include -template -static inline auto find_subface_by_index(const stl_vector_mp& faces, Index subface_index) -{ - return std::find_if(faces.begin(), faces.end(), [&](const vert_face_info_t& x) { - return x.subface_index == subface_index; - }); -} - // compute barycentric coordinate of Point (intersection of three planes) // Point in tet cell template diff --git a/network_process/include/prim_gen.hpp b/network_process/include/prim_gen.hpp index 14d4923..7606879 100644 --- a/network_process/include/prim_gen.hpp +++ b/network_process/include/prim_gen.hpp @@ -9,35 +9,36 @@ using grid_region = std::pair, Eigen::Vector>; -void extract_vertex_infos(const s_settings& settings, - const baked_blobtree_t& tree, - scene_bg_mesh_info_t& scene_bg_mesh_info, - flat_hash_map_mp& vertex_infos, - stl_vector_mp& vertex_exist_regions); +void extract_vertex_infos(const s_settings& settings, + const baked_blobtree_t& tree, + scene_bg_mesh_info_t& scene_bg_mesh_info, + Eigen::Ref vertex_infos); void pair_tetrahedron(const scene_bg_mesh_info_t& scene_bg_mesh_info, - const stl_vector_mp& vertex_exist_regions, stl_vector_mp>& tetrahedrons, flat_hash_map_mp>& vertex_to_tet_mapping); void filter_tet_by_subface(const flat_hash_map_mp>& vertex_to_tet_mapping, - flat_hash_map_mp& vertex_infos, + Eigen::Ref vertex_infos, + flat_hash_map_mp& vertex_indices_mapping, stl_vector_mp>& tetrahedrons, stl_vector_mp& tetrahedron_active_subface_start_index, stl_vector_mp& active_subface_indices, flat_hash_map_mp>& zero_vertex_to_incident_tet_mapping); // in turn: num_1_func, num_2_func, num_more_func -std::array compute_implicit_arrangements(const flat_hash_map_mp& vertex_infos, - const stl_vector_mp>& tetrahedrons, +std::array compute_implicit_arrangements(const Eigen::Ref vertex_infos, + const flat_hash_map_mp& vertex_indices_mapping, + const stl_vector_mp>& tetrahedrons, const stl_vector_mp& tetrahedron_active_subface_start_index, const stl_vector_mp& active_subface_indices, stl_vector_mp& tetrahedron_arrangements); // extract iso-mesh (topology only) -void extract_iso_mesh(const std::array& tet_active_subface_counts, - const stl_vector_mp& tetrahedron_arrangements, - const scene_bg_mesh_info_t& scene_bg_mesh_info, - const stl_vector_mp>& tetrahedrons, - const stl_vector_mp& tetrahedron_active_subface_start_index, - const stl_vector_mp& active_subface_indices, - const flat_hash_map_mp& vertex_infos, - stl_vector_mp& iso_pts, - stl_vector_mp& iso_verts, - stl_vector_mp& iso_faces); \ No newline at end of file +void extract_iso_mesh(const std::array& tet_active_subface_counts, + const stl_vector_mp& tetrahedron_arrangements, + const scene_bg_mesh_info_t& scene_bg_mesh_info, + const stl_vector_mp>& tetrahedrons, + const stl_vector_mp& tetrahedron_active_subface_start_index, + const stl_vector_mp& active_subface_indices, + const Eigen::Ref vertex_infos, + const flat_hash_map_mp& vertex_indices_mapping, + stl_vector_mp& iso_pts, + stl_vector_mp& iso_verts, + stl_vector_mp& iso_faces); \ No newline at end of file diff --git a/network_process/src/prim_gen/comp_imp_arrange.cpp b/network_process/src/prim_gen/comp_imp_arrange.cpp index f6cf2c7..bd04c49 100644 --- a/network_process/src/prim_gen/comp_imp_arrange.cpp +++ b/network_process/src/prim_gen/comp_imp_arrange.cpp @@ -1,8 +1,8 @@ #include -#include -std::array compute_implicit_arrangements(const flat_hash_map_mp& vertex_infos, - const stl_vector_mp>& tetrahedrons, +std::array compute_implicit_arrangements(const Eigen::Ref vertex_infos, + const flat_hash_map_mp& vertex_indices_mapping, + const stl_vector_mp>& tetrahedrons, const stl_vector_mp& tetrahedron_active_subface_start_index, const stl_vector_mp& active_subface_indices, stl_vector_mp& tetrahedron_arrangements) @@ -19,19 +19,16 @@ std::array compute_implicit_arrangements(const flat_hash_map_mpsdf_value, - -find_subface_by_index(vert_faces1, *iter)->sdf_value, - -find_subface_by_index(vert_faces2, *iter)->sdf_value, - -find_subface_by_index(vert_faces3, *iter)->sdf_value}); + planes.emplace_back(plane_t{-vert_faces0[*iter], -vert_faces1[*iter], -vert_faces2[*iter], -vert_faces3[*iter]}); } tetrahedron_arrangements.emplace_back(std::move(compute_arrangement(planes))); diff --git a/network_process/src/prim_gen/extract_mesh.cpp b/network_process/src/prim_gen/extract_mesh.cpp index 0eb5493..004a523 100644 --- a/network_process/src/prim_gen/extract_mesh.cpp +++ b/network_process/src/prim_gen/extract_mesh.cpp @@ -1,17 +1,17 @@ #include #include -#include "fwd_types.hpp" -void extract_iso_mesh(const std::array& tet_active_subface_counts, - const stl_vector_mp& tetrahedron_arrangements, - const scene_bg_mesh_info_t& scene_bg_mesh_info, - const stl_vector_mp>& tetrahedrons, - const stl_vector_mp& tetrahedron_active_subface_start_index, - const stl_vector_mp& active_subface_indices, - const flat_hash_map_mp& vertex_infos, - stl_vector_mp& iso_pts, - stl_vector_mp& iso_verts, - stl_vector_mp& iso_faces) +void extract_iso_mesh(const std::array& tet_active_subface_counts, + const stl_vector_mp& tetrahedron_arrangements, + const scene_bg_mesh_info_t& scene_bg_mesh_info, + const stl_vector_mp>& tetrahedrons, + const stl_vector_mp& tetrahedron_active_subface_start_index, + const stl_vector_mp& active_subface_indices, + const Eigen::MatrixXd& vertex_infos, + const flat_hash_map_mp& vertex_indices_mapping, + stl_vector_mp& iso_pts, + stl_vector_mp& iso_verts, + stl_vector_mp& iso_faces) { // estimate number of iso-verts and iso-faces const auto& [num_1_func, num_2_func, num_more_func] = tet_active_subface_counts; @@ -120,8 +120,8 @@ void extract_iso_mesh(const std::array& tet iso_vert.simplex_vertex_indices = {vId1, vId2}; iso_vert.subface_indices = {implicit_pIds[0]}; - const auto f1 = find_subface_by_index(vertex_infos.at(vId1), implicit_pIds[0])->sdf_value; - const auto f2 = find_subface_by_index(vertex_infos.at(vId2), implicit_pIds[0])->sdf_value; + const auto f1 = vertex_infos(vertex_indices_mapping.at(vId1), implicit_pIds[0]); + const auto f2 = vertex_infos(vertex_indices_mapping.at(vId2), implicit_pIds[0]); const auto coord = compute_barycentric_coords(f1, f2); iso_pts.emplace_back(coord[0] * get_vert_pos(vId1, scene_bg_mesh_info) + coord[1] * get_vert_pos(vId2, scene_bg_mesh_info)); @@ -146,15 +146,16 @@ void extract_iso_mesh(const std::array& tet iso_vert.subface_indices = {implicit_pIds[0], implicit_pIds[1]}; // - const auto &vert_face0 = vertex_infos.at(vIds3[0]), vert_face1 = vertex_infos.at(vIds3[1]), - vert_face2 = vertex_infos.at(vIds3[2]); - const auto f1 = std::array{find_subface_by_index(vert_face0, implicit_pIds[0])->sdf_value, - find_subface_by_index(vert_face1, implicit_pIds[0])->sdf_value, - find_subface_by_index(vert_face2, implicit_pIds[0])->sdf_value}; - const auto f2 = std::array{find_subface_by_index(vert_face0, implicit_pIds[1])->sdf_value, - find_subface_by_index(vert_face1, implicit_pIds[1])->sdf_value, - find_subface_by_index(vert_face2, implicit_pIds[1])->sdf_value}; - const auto coord = compute_barycentric_coords(f1, f2); + const auto &vert_face0 = vertex_infos.row(vertex_indices_mapping.at(vIds3[0])), + vert_face1 = vertex_infos.row(vertex_indices_mapping.at(vIds3[1])), + vert_face2 = vertex_infos.row(vertex_indices_mapping.at(vIds3[2])); + const auto f1 = std::array{vert_face0(implicit_pIds[0]), + vert_face1(implicit_pIds[0]), + vert_face2(implicit_pIds[0])}; + const auto f2 = std::array{vert_face0(implicit_pIds[1]), + vert_face1(implicit_pIds[1]), + vert_face2(implicit_pIds[1])}; + const auto coord = compute_barycentric_coords(f1, f2); iso_pts.emplace_back(coord[0] * get_vert_pos(vIds3[0], scene_bg_mesh_info) + coord[1] * get_vert_pos(vIds3[1], scene_bg_mesh_info) + coord[2] * get_vert_pos(vIds3[2], scene_bg_mesh_info)); @@ -170,22 +171,22 @@ void extract_iso_mesh(const std::array& tet iso_vert.simplex_vertex_indices = tetrahedrons[i]; iso_vert.subface_indices = implicit_pIds; - const auto &vert_face0 = vertex_infos.at(tetrahedrons[i][0]), - vert_face1 = vertex_infos.at(tetrahedrons[i][1]), - vert_face2 = vertex_infos.at(tetrahedrons[i][2]), - vert_face3 = vertex_infos.at(tetrahedrons[i][3]); - const auto f1 = std::array{find_subface_by_index(vert_face0, implicit_pIds[0])->sdf_value, - find_subface_by_index(vert_face1, implicit_pIds[0])->sdf_value, - find_subface_by_index(vert_face2, implicit_pIds[0])->sdf_value, - find_subface_by_index(vert_face3, implicit_pIds[0])->sdf_value}; - const auto f2 = std::array{find_subface_by_index(vert_face0, implicit_pIds[1])->sdf_value, - find_subface_by_index(vert_face1, implicit_pIds[1])->sdf_value, - find_subface_by_index(vert_face2, implicit_pIds[1])->sdf_value, - find_subface_by_index(vert_face3, implicit_pIds[1])->sdf_value}; - const auto f3 = std::array{find_subface_by_index(vert_face0, implicit_pIds[2])->sdf_value, - find_subface_by_index(vert_face1, implicit_pIds[2])->sdf_value, - find_subface_by_index(vert_face2, implicit_pIds[2])->sdf_value, - find_subface_by_index(vert_face3, implicit_pIds[2])->sdf_value}; + const auto &vert_face0 = vertex_infos.row(vertex_indices_mapping.at(tetrahedrons[i][0])), + vert_face1 = vertex_infos.row(vertex_indices_mapping.at(tetrahedrons[i][1])), + vert_face2 = vertex_infos.row(vertex_indices_mapping.at(tetrahedrons[i][2])), + vert_face3 = vertex_infos.row(vertex_indices_mapping.at(tetrahedrons[i][3])); + const auto f1 = std::array{vert_face0(implicit_pIds[0]), + vert_face1(implicit_pIds[0]), + vert_face2(implicit_pIds[0]), + vert_face3(implicit_pIds[0])}; + const auto f2 = std::array{vert_face0(implicit_pIds[1]), + vert_face1(implicit_pIds[1]), + vert_face2(implicit_pIds[1]), + vert_face3(implicit_pIds[1])}; + const auto f3 = std::array{vert_face0(implicit_pIds[2]), + vert_face1(implicit_pIds[2]), + vert_face2(implicit_pIds[2]), + vert_face3(implicit_pIds[2])}; const auto coord = compute_barycentric_coords(f1, f2, f3); iso_pts.emplace_back(coord[0] * get_vert_pos(tetrahedrons[i][0], scene_bg_mesh_info) + coord[1] * get_vert_pos(tetrahedrons[i][1], scene_bg_mesh_info) diff --git a/network_process/src/prim_gen/extract_vertex_infos.cpp b/network_process/src/prim_gen/extract_vertex_infos.cpp index c754e93..bb8859c 100644 --- a/network_process/src/prim_gen/extract_vertex_infos.cpp +++ b/network_process/src/prim_gen/extract_vertex_infos.cpp @@ -21,10 +21,10 @@ aabb_t mark_primitive_boundings(double aabb_margin, const baked_blobtree_t& tree return scene_aabb; } -void extract_vertex_infos(const s_settings& settings, - const baked_blobtree_t& tree, - scene_bg_mesh_info_t& scene_bg_mesh_info, - Eigen::MatrixXd& vertex_infos) +void extract_vertex_infos(const s_settings& settings, + const baked_blobtree_t& tree, + scene_bg_mesh_info_t& scene_bg_mesh_info, + Eigen::Ref vertex_infos) { stl_vector_mp primitive_boundings{}; stl_vector_mp> subface_active_vertices{}; diff --git a/network_process/src/prim_gen/filter_tet_by_subface.cpp b/network_process/src/prim_gen/filter_tet_by_subface.cpp index 7d916ed..2a83adf 100644 --- a/network_process/src/prim_gen/filter_tet_by_subface.cpp +++ b/network_process/src/prim_gen/filter_tet_by_subface.cpp @@ -1,36 +1,43 @@ #include #include +#include "base/primitive.hpp" -struct packed_sign_counter { - uint8_t positive : 4; - uint8_t negative : 4; +// struct packed_sign_counter { +// uint8_t positive : 4; +// uint8_t negative : 4; - inline packed_sign_counter& operator+=(const packed_sign_counter& rhs) - { - positive += rhs.positive; - negative += rhs.negative; - return *this; - } -}; +// inline packed_sign_counter& operator+=(const packed_sign_counter& rhs) +// { +// positive += rhs.positive; +// negative += rhs.negative; +// return *this; +// } +// }; void filter_tet_by_subface(const flat_hash_map_mp>& vertex_to_tet_mapping, - flat_hash_map_mp& vertex_infos, + Eigen::Ref vertex_infos, + flat_hash_map_mp& vertex_indices_mapping, stl_vector_mp>& tetrahedrons, stl_vector_mp& tetrahedron_active_subface_start_index, stl_vector_mp& active_subface_indices, flat_hash_map_mp>& zero_vertex_to_incident_tet_mapping) { - stl_vector_mp> sign_counts{}; + using sign_count_buffer_t = Eigen::Vector; + stl_vector_mp sign_counts{}; + dynamic_bitset_mp<> sign_count_initialized{}; sign_counts.resize(tetrahedrons.size()); + sign_count_initialized.resize(tetrahedrons.size()); - btree_map_mp temp_sign_counts{}; + // the highest 4 bits as positive sign count, the lowest 4 bits as negative sign count + sign_count_buffer_t temp_sign_counts(vertex_infos.rows()); + temp_sign_counts += temp_sign_counts; for (const auto& [vertex_index, tet_indices] : vertex_to_tet_mapping) { - const auto& vertex_info = vertex_infos.at(vertex_index); + auto vertex_info = vertex_infos.row(vertex_index); - temp_sign_counts.clear(); - for (const auto& subface_info : vertex_info) { - switch (subface_info.sdf_sign) { + temp_sign_counts.setZero(); + for (auto i = 0; i < vertex_info.size(); ++i) { + switch (sign_by_sdf(vertex_info[i])) { case sign_t::zero: { const auto& vertex_index_ = vertex_index; const auto& tet_indices_ = tet_indices; @@ -39,60 +46,58 @@ void filter_tet_by_subface(const flat_hash_map_mpsecond += counts; + for (const auto& tet_index : tet_indices) { + if (!sign_count_initialized[tet_index]) { + sign_counts[tet_index] = temp_sign_counts; + sign_count_initialized.set(tet_index); + } else { + sign_counts[tet_index] += temp_sign_counts; } } } // filter tetrahedrons' subfaces which have same but not 0 signs + // and filter active terahedrons by the way + stl_vector_mp> filtered_tets{}; + stl_vector_mp filtered_active_vertices{}; + filtered_tets.reserve(tetrahedrons.size()); + filtered_active_vertices.reserve(tetrahedrons.size() * 4); tetrahedron_active_subface_start_index.reserve(tetrahedrons.size() + 1); active_subface_indices.reserve(tetrahedrons.size()); tetrahedron_active_subface_start_index.emplace_back(0); - for (const auto& tet_info : sign_counts) { - for (const auto& [subface_index, counts] : tet_info) { - if (counts.positive < 4 && counts.negative < 4) active_subface_indices.emplace_back(subface_index); + for (auto i = 0; i < tetrahedrons.size(); ++i) { + const auto& tet_info = sign_counts[i]; + for (auto j = 0; j < tet_info.size(); ++j) { + auto sign = tet_info[j]; + if ((sign & 0x0F) < 4 && (sign >> 4) < 4) { active_subface_indices.emplace_back(j); } } - tetrahedron_active_subface_start_index.emplace_back(active_subface_indices.size()); - } - active_subface_indices.shrink_to_fit(); - - stl_vector_mp> filtered_tets{}; - stl_vector_mp filtered_tetrahedron_active_subface_start_index{}; - stl_vector_mp filtered_active_vertices{}; - filtered_tets.reserve(tetrahedrons.size()); - filtered_tetrahedron_active_subface_start_index.reserve(tetrahedrons.size()); - filtered_active_vertices.reserve(tetrahedrons.size()); - for (size_t i = 0; i < tetrahedrons.size(); ++i) { - if (tetrahedron_active_subface_start_index[i] < tetrahedron_active_subface_start_index[i + 1]) { - filtered_tetrahedron_active_subface_start_index.emplace_back(tetrahedron_active_subface_start_index[i]); + if (active_subface_indices.size() > tetrahedron_active_subface_start_index.back()) { + tetrahedron_active_subface_start_index.emplace_back(active_subface_indices.size()); const auto& vertices = filtered_tets.emplace_back(std::move(tetrahedrons[i])); filtered_active_vertices.insert(filtered_active_vertices.end(), vertices.begin(), vertices.end()); } } - // always keep the last element of tetrahedron_active_subface_start_index as an ender - filtered_tetrahedron_active_subface_start_index.emplace_back(tetrahedron_active_subface_start_index.back()); - filtered_tetrahedron_active_subface_start_index.shrink_to_fit(); + active_subface_indices.shrink_to_fit(); filtered_tets.shrink_to_fit(); - std::swap(filtered_tetrahedron_active_subface_start_index, tetrahedron_active_subface_start_index); + filtered_active_vertices.shrink_to_fit(); std::swap(filtered_tets, tetrahedrons); - flat_hash_map_mp filtered_vert_infos{}; std::sort(filtered_active_vertices.begin(), filtered_active_vertices.end()); - auto end_iter = std::unique(filtered_active_vertices.begin(), filtered_active_vertices.end()); + auto end_iter = std::unique(filtered_active_vertices.begin(), filtered_active_vertices.end()); + auto vertex_count = std::distance(filtered_active_vertices.begin(), end_iter); + Eigen::MatrixXd filtered_vert_infos(vertex_count, vertex_infos.cols()); + auto filtered_vert_infos_iter = filtered_vert_infos.rowwise().begin(); for (auto iter = filtered_active_vertices.begin(); iter != end_iter; ++iter) { - const auto vertex_index = *iter; - filtered_vert_infos.emplace(vertex_index, vertex_infos.at(vertex_index)); + const auto vertex_index = *iter; + *filtered_vert_infos_iter = vertex_infos.row(vertex_index); + vertex_indices_mapping[vertex_index] = std::distance(filtered_active_vertices.begin(), iter); + filtered_vert_infos_iter++; } - std::swap(filtered_vert_infos, vertex_infos); + vertex_infos = filtered_vert_infos; } \ No newline at end of file diff --git a/network_process/src/process.cpp b/network_process/src/process.cpp index 5cd1405..e6a8330 100644 --- a/network_process/src/process.cpp +++ b/network_process/src/process.cpp @@ -24,16 +24,18 @@ ISNP_API void build_implicit_network_by_blobtree(const s_settings& stl_vector_mp iso_faces{}; // primitive generation { - flat_hash_map_mp vertex_infos{}; + Eigen::MatrixXd vertex_infos{}; + flat_hash_map_mp vertex_indices_mapping{}; { flat_hash_map_mp> vertex_to_tet_mapping{}; { stl_vector_mp vertex_exist_regions{}; - extract_vertex_infos(settings, tree, scene_bg_mesh_info, vertex_infos, vertex_exist_regions); - pair_tetrahedron(scene_bg_mesh_info, vertex_exist_regions, tetrahedrons, vertex_to_tet_mapping); + extract_vertex_infos(settings, tree, scene_bg_mesh_info, vertex_infos); + pair_tetrahedron(scene_bg_mesh_info, tetrahedrons, vertex_to_tet_mapping); } filter_tet_by_subface(vertex_to_tet_mapping, vertex_infos, + vertex_indices_mapping, tetrahedrons, tetrahedron_active_subface_start_index, active_subface_indices, @@ -41,6 +43,7 @@ ISNP_API void build_implicit_network_by_blobtree(const s_settings& } auto tet_active_subface_counts = compute_implicit_arrangements(vertex_infos, + vertex_indices_mapping, tetrahedrons, tetrahedron_active_subface_start_index, active_subface_indices, @@ -52,6 +55,7 @@ ISNP_API void build_implicit_network_by_blobtree(const s_settings& tetrahedron_active_subface_start_index, active_subface_indices, vertex_infos, + vertex_indices_mapping, iso_pts, iso_verts, iso_faces);