From 3c6843b269454dbf03d2e2e752b3522adf050efa Mon Sep 17 00:00:00 2001 From: Zhicheng Wang <1627343141@qq.com> Date: Sat, 18 Apr 2026 00:42:22 +0800 Subject: [PATCH] fix tet build error --- network_process/interface/fwd_types.hpp | 92 +++++++++---------- .../connect_by_topo/topology_ray_shooting.cpp | 9 +- network_process/src/network_process.cpp | 3 +- .../build_tetrahedron_and_adjacency.cpp | 45 +++++---- network_process/src/prim_gen/extract_mesh.cpp | 20 ++-- .../src/prim_gen/extract_vertex_infos.cpp | 8 +- 6 files changed, 81 insertions(+), 96 deletions(-) diff --git a/network_process/interface/fwd_types.hpp b/network_process/interface/fwd_types.hpp index 0905050..2e3bf60 100644 --- a/network_process/interface/fwd_types.hpp +++ b/network_process/interface/fwd_types.hpp @@ -16,61 +16,55 @@ using pod_key_t = std::array; // ============================================================================== -struct scene_bg_mesh_info_t { - Eigen::Vector3d grid_offset{}; - Eigen::Vector3d grid_size{}; - Eigen::Vector3d grid_size_inv{}; - Eigen::Vector vert_resolution{}; -}; - // x as the most significant bit, y as the second most significant bit, z as the least significant bit -static inline uint32_t hash_vert_pos(size_t x, size_t y, size_t z, const scene_bg_mesh_info_t& info) -{ - return static_cast(z + info.vert_resolution.z() * (y + info.vert_resolution.y() * x)); -} - -static inline uint32_t pos_hash_max(const scene_bg_mesh_info_t& info) -{ - return hash_vert_pos(info.vert_resolution.x() - 1, info.vert_resolution.y() - 1, info.vert_resolution.z() - 1, info); -} - -static inline uint32_t hash_increment_x(uint32_t hashed_value, const scene_bg_mesh_info_t& info) -{ - return hashed_value + static_cast(info.vert_resolution.y() * info.vert_resolution.z()); -} - -static inline uint32_t hash_increment_y(uint32_t hashed_value, const scene_bg_mesh_info_t& info) -{ - return hashed_value + static_cast(info.vert_resolution.z()); -} +struct scene_bg_mesh_info_t { + void init(const aabb_t& aabb, uint32_t resolution) + { + this->grid_offset = aabb.min(); + this->grid_size = aabb.sizes() / resolution; + this->vert_resolution = resolution + 1; + this->grid_resolution = resolution; + + if (this->vert_resolution % 2 == 1) { + this->invert_hash_grid = [this](uint32_t hashed_value) { + auto y = (hashed_value / this->grid_resolution) % this->grid_resolution; + auto x = hashed_value / (this->grid_resolution * this->grid_resolution); + auto s = (x + y) & 1; + auto z = (hashed_value % this->grid_resolution) ^ s; + return std::array{x, y, z}; + }; + } else { + this->invert_hash_grid = [this](uint32_t hashed_value) { + auto z = hashed_value % this->grid_resolution; + auto y = (hashed_value / this->grid_resolution) % this->grid_resolution; + auto x = hashed_value / (this->grid_resolution * this->grid_resolution); + return std::array{x, y, z}; + }; + } + } -static inline uint32_t hash_increment_z(uint32_t hashed_value, const scene_bg_mesh_info_t& info) { return hashed_value + 1; } + inline uint32_t hash_grid_pos(uint32_t x, uint32_t y, uint32_t z) const noexcept + { + return z + this->vert_resolution * (y + this->vert_resolution * x); + } -static inline Eigen::Vector3d get_grid_pos(size_t x, size_t y, size_t z, const scene_bg_mesh_info_t& info) -{ - return info.grid_offset + Eigen::Vector3d(x, y, z).cwiseProduct(info.grid_size); -} + inline Eigen::Vector3d get_vert_pos(uint32_t hashed_value) const + { + auto z = hashed_value % this->vert_resolution; + auto y = (hashed_value / this->vert_resolution) % this->vert_resolution; + auto x = hashed_value / (this->vert_resolution * this->vert_resolution); + return this->grid_offset + Eigen::Vector3d(x, y, z).cwiseProduct(this->grid_size); + } -static inline Eigen::Vector3d get_grid_pos(const Eigen::Vector3d& pos, const scene_bg_mesh_info_t& info) -{ - return (pos - info.grid_offset).cwiseProduct(info.grid_size_inv); -} + uint32_t grid_count() const noexcept { return grid_resolution * grid_resolution * grid_resolution; } -static inline Eigen::Vector3d get_vert_pos(uint32_t hashed_value, const scene_bg_mesh_info_t& info) -{ - size_t z = static_cast(hashed_value % (info.vert_resolution.z())); - size_t y = static_cast((hashed_value / info.vert_resolution.z()) % info.vert_resolution.y()); - size_t x = static_cast(hashed_value / (info.vert_resolution.z() * info.vert_resolution.y())); - return get_grid_pos(x, y, z, info); -} + std::function(uint32_t)> invert_hash_grid{}; -static inline std::array get_grid_pos(uint32_t hashed_value, const scene_bg_mesh_info_t& info) -{ - size_t z = static_cast(hashed_value % (info.vert_resolution.z())); - size_t y = static_cast((hashed_value / info.vert_resolution.z()) % info.vert_resolution.y()); - size_t x = static_cast(hashed_value / (info.vert_resolution.z() * info.vert_resolution.y())); - return {x, y, z}; -} + Eigen::Vector3d grid_offset{}; + Eigen::Vector3d grid_size{}; + uint32_t vert_resolution{}; + uint32_t grid_resolution{}; +}; // ============================================================================== diff --git a/network_process/src/connect_by_topo/topology_ray_shooting.cpp b/network_process/src/connect_by_topo/topology_ray_shooting.cpp index 7be9c86..f55fa92 100644 --- a/network_process/src/connect_by_topo/topology_ray_shooting.cpp +++ b/network_process/src/connect_by_topo/topology_ray_shooting.cpp @@ -72,8 +72,7 @@ struct equal_to { // ============================================================================================ -void topo_ray_shooting(const scene_bg_mesh_info_t &scene_bg_mesh_info, - const std::vector> &tetrahedrons, +void topo_ray_shooting(const std::vector> &tetrahedrons, const flat_hash_map &vertex_lexigraphical_adjacency, const std::vector &tetrahedron_arrangements, const std::vector &iso_verts, @@ -95,8 +94,7 @@ void topo_ray_shooting(const scene_bg_mesh_info_t &scene_bg_mesh flat_hash_map iso_face_id_of_tet_face{}; // map: (tet_id, tet_vert_id) --> (iso_vert_id, component_id) flat_hash_map iso_vId_compId_of_tet_vert{}; - find_extremal_edges(scene_bg_mesh_info, - iso_verts, + find_extremal_edges(iso_verts, iso_faces, patches, components, @@ -215,8 +213,7 @@ void topo_ray_shooting(const scene_bg_mesh_info_t &scene_bg_mesh } } -void find_extremal_edges(const scene_bg_mesh_info_t &scene_bg_mesh_info, - const std::vector &iso_verts, +void find_extremal_edges(const std::vector &iso_verts, const std::vector &iso_faces, const std::vector> &patches, const std::vector> &components, diff --git a/network_process/src/network_process.cpp b/network_process/src/network_process.cpp index f99791e..3228702 100644 --- a/network_process/src/network_process.cpp +++ b/network_process/src/network_process.cpp @@ -253,8 +253,7 @@ ISNP_API void build_implicit_network_by_blobtree(const s_settings& s } else { { std::vector> shell_links{}; - topo_ray_shooting(scene_bg_mesh_info, - tetrahedrons, + topo_ray_shooting(tetrahedrons, vertex_lexigraphical_adjacency, tetrahedron_arrangements, iso_verts, diff --git a/network_process/src/prim_gen/build_tetrahedron_and_adjacency.cpp b/network_process/src/prim_gen/build_tetrahedron_and_adjacency.cpp index 57e5fd8..67d6bd4 100644 --- a/network_process/src/prim_gen/build_tetrahedron_and_adjacency.cpp +++ b/network_process/src/prim_gen/build_tetrahedron_and_adjacency.cpp @@ -21,9 +21,8 @@ void build_tetrahedron_and_adjacency(const scene_bg_mesh_info_t& flat_hash_map>& reverse_vertex_adjacency, btree_map>& vertex_to_tet_mapping) { - scene_bg_mesh_info_t culled_scene_bg_mesh_info = scene_bg_mesh_info; - culled_scene_bg_mesh_info.vert_resolution -= Eigen::Vector::Ones(); - tetrahedrons.reserve((culled_scene_bg_mesh_info.vert_resolution.array() - 1).prod() * 5); + auto res = scene_bg_mesh_info.grid_resolution - 1; + tetrahedrons.reserve(res * res * res * 5); auto insert_or_compare_vertex_adjacency = [&](uint32_t src, uint32_t dst) { reverse_vertex_adjacency[dst].reserve(reverse_vertex_adjacency[dst].size() + 3); @@ -32,16 +31,16 @@ void build_tetrahedron_and_adjacency(const scene_bg_mesh_info_t& reverse_vertex_adjacency[dst].emplace_back(src); }; - for (size_t i = 0; i < pos_hash_max(culled_scene_bg_mesh_info); i += 2) { - auto [x, y, z] = get_grid_pos(i, culled_scene_bg_mesh_info); - auto v0 = hash_vert_pos(x, y, z, scene_bg_mesh_info); - auto v1 = hash_increment_x(v0, scene_bg_mesh_info); // +yz - auto v2 = hash_increment_y(v1, scene_bg_mesh_info); // +yz+z - auto v3 = hash_increment_y(v0, scene_bg_mesh_info); // +z - auto v4 = hash_increment_z(v0, scene_bg_mesh_info); // +1 - auto v5 = hash_increment_x(v4, scene_bg_mesh_info); // +yz+1 - auto v6 = hash_increment_y(v5, scene_bg_mesh_info); // +yz+z+1 - auto v7 = hash_increment_y(v4, scene_bg_mesh_info); // +z+1 + for (size_t i = 0; i < scene_bg_mesh_info.grid_count(); i += 2) { + auto [x, y, z] = scene_bg_mesh_info.invert_hash_grid(i); + auto v0 = scene_bg_mesh_info.hash_grid_pos(x, y, z); + auto v1 = scene_bg_mesh_info.hash_grid_pos(x + 1, y, z); + auto v2 = scene_bg_mesh_info.hash_grid_pos(x + 1, y + 1, z); + auto v3 = scene_bg_mesh_info.hash_grid_pos(x, y + 1, z); + auto v4 = scene_bg_mesh_info.hash_grid_pos(x, y, z + 1); + auto v5 = scene_bg_mesh_info.hash_grid_pos(x + 1, y, z + 1); + auto v6 = scene_bg_mesh_info.hash_grid_pos(x + 1, y + 1, z + 1); + auto v7 = scene_bg_mesh_info.hash_grid_pos(x, y + 1, z + 1); // vertex lexigraphical ordering: v0, v4, v3, v1, v7, v5, v2, v6 tetrahedrons.emplace_back(std::array{v4, v3, v1, v6}); @@ -59,16 +58,16 @@ void build_tetrahedron_and_adjacency(const scene_bg_mesh_info_t& insert_or_compare_vertex_adjacency(v7, v4); } - for (size_t i = 1; i < pos_hash_max(culled_scene_bg_mesh_info); i += 2) { - auto [x, y, z] = get_grid_pos(i, culled_scene_bg_mesh_info); - auto v0 = hash_vert_pos(x, y, z, scene_bg_mesh_info); - auto v1 = hash_increment_x(v0, scene_bg_mesh_info); - auto v2 = hash_increment_y(v1, scene_bg_mesh_info); - auto v3 = hash_increment_y(v0, scene_bg_mesh_info); - auto v4 = hash_increment_z(v0, scene_bg_mesh_info); - auto v5 = hash_increment_x(v4, scene_bg_mesh_info); - auto v6 = hash_increment_y(v5, scene_bg_mesh_info); - auto v7 = hash_increment_y(v4, scene_bg_mesh_info); + for (size_t i = 1; i < scene_bg_mesh_info.grid_count(); i += 2) { + auto [x, y, z] = scene_bg_mesh_info.invert_hash_grid(i); + auto v0 = scene_bg_mesh_info.hash_grid_pos(x, y, z); + auto v1 = scene_bg_mesh_info.hash_grid_pos(x + 1, y, z); + auto v2 = scene_bg_mesh_info.hash_grid_pos(x + 1, y + 1, z); + auto v3 = scene_bg_mesh_info.hash_grid_pos(x, y + 1, z); + auto v4 = scene_bg_mesh_info.hash_grid_pos(x, y, z + 1); + auto v5 = scene_bg_mesh_info.hash_grid_pos(x + 1, y, z + 1); + auto v6 = scene_bg_mesh_info.hash_grid_pos(x + 1, y + 1, z + 1); + auto v7 = scene_bg_mesh_info.hash_grid_pos(x, y + 1, z + 1); // vertex lexigraphical ordering: v0, v4, v3, v1, v7, v5, v2, v6 tetrahedrons.emplace_back(std::array{v0, v7, v5, v2}); diff --git a/network_process/src/prim_gen/extract_mesh.cpp b/network_process/src/prim_gen/extract_mesh.cpp index 9d9c59d..e510d26 100644 --- a/network_process/src/prim_gen/extract_mesh.cpp +++ b/network_process/src/prim_gen/extract_mesh.cpp @@ -174,10 +174,10 @@ void extract_iso_mesh(const std::array& tet_active_sub impl2[mapped_not_boundary_vIds[2]], impl2[mapped_not_boundary_vIds[3]]}; const auto coord = compute_barycentric_coords(f1, f2, f3); - iso_pts.emplace_back(coord[0] * get_vert_pos(not_boundary_vIds[0], scene_bg_mesh_info) - + coord[1] * get_vert_pos(not_boundary_vIds[1], scene_bg_mesh_info) - + coord[2] * get_vert_pos(not_boundary_vIds[2], scene_bg_mesh_info) - + coord[3] * get_vert_pos(not_boundary_vIds[3], scene_bg_mesh_info)); + iso_pts.emplace_back(coord[0] * scene_bg_mesh_info.get_vert_pos(not_boundary_vIds[0]) + + coord[1] * scene_bg_mesh_info.get_vert_pos(not_boundary_vIds[1]) + + coord[2] * scene_bg_mesh_info.get_vert_pos(not_boundary_vIds[2]) + + coord[3] * scene_bg_mesh_info.get_vert_pos(not_boundary_vIds[3])); } else if (num_boundary_planes == 1) { // on tet face generate_new_iso_vert(vert_on_tetFace, [&] { const auto & impl0 = vertex_infos.col(implicit_pIds[0]), &impl1 = vertex_infos.col(implicit_pIds[1]); @@ -188,21 +188,21 @@ void extract_iso_mesh(const std::array& tet_active_sub impl1[mapped_not_boundary_vIds[1]], impl1[mapped_not_boundary_vIds[2]]}; const auto coord = compute_barycentric_coords(f1, f2); - iso_pts.emplace_back(coord[0] * get_vert_pos(not_boundary_vIds[0], scene_bg_mesh_info) - + coord[1] * get_vert_pos(not_boundary_vIds[1], scene_bg_mesh_info) - + coord[2] * get_vert_pos(not_boundary_vIds[2], scene_bg_mesh_info)); + iso_pts.emplace_back(coord[0] * scene_bg_mesh_info.get_vert_pos(not_boundary_vIds[0]) + + coord[1] * scene_bg_mesh_info.get_vert_pos(not_boundary_vIds[1]) + + coord[2] * scene_bg_mesh_info.get_vert_pos(not_boundary_vIds[2])); }); } else if (num_boundary_planes == 2) { // on tet edge generate_new_iso_vert(vert_on_tetEdge, [&] { const auto f1 = vertex_infos(vertex_indices_mapping.at(not_boundary_vIds[0]), implicit_pIds[0]); const auto f2 = vertex_infos(vertex_indices_mapping.at(not_boundary_vIds[1]), implicit_pIds[0]); const auto coord = compute_barycentric_coords(f1, f2); - iso_pts.emplace_back(coord[0] * get_vert_pos(not_boundary_vIds[0], scene_bg_mesh_info) - + coord[1] * get_vert_pos(not_boundary_vIds[1], scene_bg_mesh_info)); + iso_pts.emplace_back(coord[0] * scene_bg_mesh_info.get_vert_pos(not_boundary_vIds[0]) + + coord[1] * scene_bg_mesh_info.get_vert_pos(not_boundary_vIds[1])); }); } else { // on tet vertex generate_new_iso_vert(vert_on_tetVert, - [&] { iso_pts.emplace_back(get_vert_pos(not_boundary_vIds[0], scene_bg_mesh_info)); }); + [&] { iso_pts.emplace_back(scene_bg_mesh_info.get_vert_pos(not_boundary_vIds[0])); }); } } // create iso-faces diff --git a/network_process/src/prim_gen/extract_vertex_infos.cpp b/network_process/src/prim_gen/extract_vertex_infos.cpp index 7fa8fe5..bbd45b3 100644 --- a/network_process/src/prim_gen/extract_vertex_infos.cpp +++ b/network_process/src/prim_gen/extract_vertex_infos.cpp @@ -26,11 +26,7 @@ void extract_vertex_infos(const s_settings& settings, subface_active_vertices.resize(tree.subfaces.size()); auto scene_aabb = mark_primitive_boundings(settings.scene_aabb_margin, tree, primitive_boundings); - - scene_bg_mesh_info.grid_offset = scene_aabb.min(); - scene_bg_mesh_info.grid_size = scene_aabb.sizes() / settings.resolution; - scene_bg_mesh_info.grid_size_inv = scene_bg_mesh_info.grid_size.cwiseInverse(); - scene_bg_mesh_info.vert_resolution = Eigen::Vector::Constant(settings.resolution + 1); + scene_bg_mesh_info.init(scene_aabb, settings.resolution); // compute the SDF values for each vertex for (uint32_t i = 0; i < tree.subfaces.size(); ++i) { @@ -39,7 +35,7 @@ void extract_vertex_infos(const s_settings& settings, const auto subface_type = tree.subface_types[i]; auto sdf_evaluator = std::bind(internal::get_eval_sdf_ptr(subface_type), subface_ptr, std::placeholders::_1); for (uint32_t j = 0; j < subface_vert_info.size(); ++j) { - auto pos = get_vert_pos(j, scene_bg_mesh_info); + auto pos = scene_bg_mesh_info.get_vert_pos(j); subface_vert_info[j] = sdf_evaluator(pos); } }