You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

146 lines
7.7 KiB

#include <stack>
#include <queue>
#include <post_topo.hpp>
1 week ago
stl_vector_mp<dynamic_bitset_mp<>> propagate_subface_labels(size_t subface_count,
const stl_vector_mp<polygon_face_t>& faces,
const stl_vector_mp<stl_vector_mp<uint32_t>>& patches,
const stl_vector_mp<stl_vector_mp<uint32_t>>& arrangement_cells,
const stl_vector_mp<uint32_t>& shell_of_half_patch,
const stl_vector_mp<stl_vector_mp<uint32_t>>& shells,
const stl_vector_mp<uint32_t>& shell_to_cell)
{
1 week ago
// in turn: [subface_index][cell_index] = sign
stl_vector_mp<dynamic_bitset_mp<>> cell_subface_signs(subface_count, dynamic_bitset_mp<>(arrangement_cells.size()));
stl_vector_mp<bool> visited_cells(arrangement_cells.size(), false);
1 week ago
stl_vector_mp<bool> visited_subfaces(subface_count, false);
stl_vector_mp<stl_vector_mp<uint32_t>> cell_indices_of_inactive_subfaces(subface_count);
std::queue<uint32_t> Q{};
flat_hash_map_mp<uint32_t, std::pair<uint32_t, bool>> cell_neighbors_map{};
Q.emplace(0);
while (!Q.empty()) {
const auto cell_index = Q.front();
const auto cell = arrangement_cells[cell_index];
Q.pop();
if (!visited_cells[cell_index]) {
visited_cells[cell_index] = true;
cell_neighbors_map.clear();
for (auto shell : cell) {
for (auto half_patch : shells[shell]) {
auto subface_label = faces[patches[half_patch / 2][0]].subface_index;
// CAUTION: we assume that the sign is 1 when the surface is inside the sdf before
// but in blobtree we assume that the sign is 0 when the surface is inside the sdf
// so we need to flip the sign here
1 week ago
// EDIT: to keep sign operation nice and easy, we'll just keep the sign being 1 when the surface is inside
// the sdf
bool sign = (half_patch % 2 == 0) ? 1 : 0;
auto oppose_cell = shell_to_cell[shell_of_half_patch[sign ? (half_patch + 1) : (half_patch - 1)]];
// bool sign = (half_patch % 2 == 0) ? 0 : 1;
// auto oppose_cell = shell_to_cell[shell_of_half_patch[!sign ? (half_patch + 1) : (half_patch - 1)]];
cell_neighbors_map[oppose_cell] = std::make_pair(subface_label, sign);
#ifndef RELEASE_BRANCH
if (visited_subfaces[subface_label] != false && cell_subface_signs[subface_label][cell_index] != sign) {
throw std::runtime_error("ERROR: Inconsistent Cell Function Labels.");
}
#endif
cell_subface_signs[subface_label][cell_index] = sign;
visited_subfaces[subface_label] = true;
// propagate the function signs to all previously inactive cells
if (cell_indices_of_inactive_subfaces[subface_label].size() > 0) {
for (const auto cell_index : cell_indices_of_inactive_subfaces[subface_label]) {
cell_subface_signs[subface_label][cell_index] = sign;
}
cell_indices_of_inactive_subfaces[subface_label].clear();
}
}
}
// fetch inactive subface index
1 week ago
for (uint32_t subface_index = 0; subface_index < subface_count; subface_index++) {
if (visited_subfaces[subface_index] == false) {
cell_indices_of_inactive_subfaces[subface_index].emplace_back(cell_index);
for (const auto& [other_cell_index, _] : cell_neighbors_map) {
cell_indices_of_inactive_subfaces[subface_index].emplace_back(other_cell_index);
}
}
}
// propagate to neighboring cells
for (const auto& [other_cell_index, other_cell_func_label] : cell_neighbors_map) {
if (!visited_cells[other_cell_index]) Q.emplace(other_cell_index);
const auto& [func_index, sign] = other_cell_func_label;
for (uint32_t subface_index = 0; subface_index < func_index; ++subface_index)
cell_subface_signs[subface_index][other_cell_index] = cell_subface_signs[subface_index][cell_index];
// opposite cell has opposite sign on same patch
cell_subface_signs[func_index][other_cell_index] = !sign;
1 week ago
for (uint32_t subface_index = func_index + 1; subface_index < subface_count; ++subface_index)
cell_subface_signs[subface_index][other_cell_index] = cell_subface_signs[subface_index][cell_index];
}
}
}
#ifndef RELEASE_BRANCH
1 week ago
for (size_t i = 0; i < subface_count; ++i) {
if (visited_subfaces[i] == false) { throw std::logic_error("ERROR: Still have sign-unknown subfaces."); }
}
#endif
1 week ago
return cell_subface_signs;
}
dynamic_bitset_mp<> filter_cells_by_boolean(const baked_blobtree_t& tree,
stl_vector_mp<dynamic_bitset_mp<>>& cell_primitive_signs)
{
struct compact_node_info {
dynamic_bitset_mp<> cell_signs{};
uint32_t parent_index{};
};
std::stack<compact_node_info> stacked_nodes{};
auto iter = tree.nodes.begin();
1 week ago
stacked_nodes.emplace(compact_node_info{std::move(cell_primitive_signs[iter->primitive_index]), iter->parent_index});
iter++;
while (iter != tree.nodes.end()) {
// each out iteration must start with leaf node, only 1 leaf node is absorbed in one iteration
assert(iter->is_primitive_node());
compact_node_info temp_info{std::move(cell_primitive_signs[iter->primitive_index]), iter->parent_index};
iter++; // to parent or neighboring node
while (!stacked_nodes.empty() && temp_info.parent_index == stacked_nodes.top().parent_index) {
// do bool operation, util meet next primitive node.
assert(iter->is_operation_node());
5 days ago
// HINT: other_cell_signs here should always be left nodes
const auto& other_cell_sign = stacked_nodes.top().cell_signs;
switch (iter->get_operation()) {
1 week ago
case internal::eNodeOperation::unionOp: temp_info.cell_signs |= other_cell_sign; break;
case internal::eNodeOperation::intersectionOp: temp_info.cell_signs &= other_cell_sign; break;
case internal::eNodeOperation::differenceOp:
// stacked nodes are always left childs
5 days ago
temp_info.cell_signs = other_cell_sign & ~temp_info.cell_signs;
break;
default: throw std::runtime_error("ERROR: baked blobtree with unknown type operation node"); break;
}
temp_info.parent_index = iter->parent_index;
stacked_nodes.pop();
iter++; // to parent or neighboring node
}
stacked_nodes.emplace(std::move(temp_info));
}
assert(stacked_nodes.size() == 1);
assert(stacked_nodes.top().parent_index == 0xFFFFFFFF);
// sign 0 in blobtree means inside, so we need to flip the sign
1 week ago
// EDIT: no need to flip signs here
return stacked_nodes.top().cell_signs;
}