-
Notifications
You must be signed in to change notification settings - Fork 20
mesh_decimation #734
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Zhouyuan-Chen
wants to merge
14
commits into
deprecated
Choose a base branch
from
zhouyuan/mesh_reducing
base: deprecated
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
mesh_decimation #734
Changes from 3 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
bcdb7a8
mesh_decimation first version
Zhouyuan-Chen 67fe701
add a safe average length function
Zhouyuan-Chen 1042a16
finished mesh_decimation component
Zhouyuan-Chen 5eca7c3
cleanup the code and added 2d test
Zhouyuan-Chen cbeebb4
re-arrange the order of the invariants
Zhouyuan-Chen 23ca6bb
keep incident tet
Zhouyuan-Chen 38b2574
change the collapse operation name
Zhouyuan-Chen b50ccb5
refine the compnent
Zhouyuan-Chen 9122c67
Merge branch 'main' into zhouyuan/mesh_reducing
Zhouyuan-Chen b0fd42b
Merge branch 'main' into zhouyuan/mesh_reducing
Zhouyuan-Chen d681877
consolidate before checking convergence
Zhouyuan-Chen fb6a53a
rename the test
Zhouyuan-Chen fa9963a
Merge branch 'main' into zhouyuan/mesh_reducing
Zhouyuan-Chen e9f1fdc
Merge branch 'main' into zhouyuan/mesh_reducing
Zhouyuan-Chen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| #include <catch2/catch_test_macros.hpp> | ||
| #include <nlohmann/json.hpp> | ||
| #include <tools/DEBUG_TetMesh.hpp> | ||
| #include <tools/DEBUG_TriMesh.hpp> | ||
| #include <tools/TetMesh_examples.hpp> | ||
| #include <tools/TriMesh_examples.hpp> | ||
| #include <wmtk/Mesh.hpp> | ||
| #include <wmtk/TriMesh.hpp> | ||
| #include <wmtk/components/mesh_decimation/internal/MeshDecimation.hpp> | ||
| #include <wmtk/components/mesh_decimation/internal/MeshDecimationOptions.hpp> | ||
| #include <wmtk/components/mesh_decimation/mesh_decimation.hpp> | ||
| #include <wmtk/function/simplex/AMIPS.hpp> | ||
| #include <wmtk/invariants/TodoInvariant.hpp> | ||
| #include <wmtk/io/MeshReader.hpp> | ||
| #include <wmtk/io/ParaviewWriter.hpp> | ||
| #include <wmtk/operations/EdgeCollapse.hpp> | ||
| #include <wmtk/operations/attribute_new/CollapseNewAttributeStrategy.hpp> | ||
| #include <wmtk/simplex/link.hpp> | ||
| #include <wmtk/utils/mesh_utils.hpp> | ||
|
|
||
| using json = nlohmann::json; | ||
| using namespace wmtk; | ||
|
|
||
| const std::filesystem::path data_dir = WMTK_DATA_DIR; | ||
|
|
||
| TEST_CASE("component_mesh_decimation_options", "[components][mesh_decimation]") | ||
| { | ||
| using namespace components::internal; | ||
|
|
||
| json o = { | ||
| {"input", "input_mesh"}, | ||
| {"output", "output_mesh"}, | ||
| {"constrait_value", 1}, | ||
| {"target_len", 1.0}, | ||
| {"cell_constrait_tag_name", "tag"}, | ||
| {"pass_through", {"vertices"}}}; | ||
|
|
||
| CHECK_NOTHROW(o.get<MeshDecimationOptions>()); | ||
| } | ||
|
|
||
| TEST_CASE("preprocess", "[.]") | ||
| { | ||
| using namespace wmtk::components; | ||
|
|
||
| auto mesh_in = wmtk::read_mesh(data_dir / "3d_images/sphere_regularized.hdf5"); | ||
| Mesh& mesh = static_cast<Mesh&>(*mesh_in); | ||
Zhouyuan-Chen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| std::vector<wmtk::attribute::MeshAttributeHandle> pass_though; | ||
| wmtk::attribute::MeshAttributeHandle vertex = | ||
Zhouyuan-Chen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| mesh.get_attribute_handle<int64_t>("vertex_tag", PrimitiveType::Vertex); | ||
| wmtk::attribute::MeshAttributeHandle edge = | ||
| mesh.get_attribute_handle<int64_t>("edge_tag", PrimitiveType::Edge); | ||
| wmtk::attribute::MeshAttributeHandle face = | ||
| mesh.get_attribute_handle<int64_t>("face_tag", PrimitiveType::Triangle); | ||
| pass_though.push_back(vertex); | ||
| pass_though.push_back(edge); | ||
| pass_though.push_back(face); | ||
| internal::MeshDecimation MD(mesh, "tag", 1, 5, pass_though); | ||
Zhouyuan-Chen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| MD.process(); | ||
Zhouyuan-Chen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| wmtk::io::ParaviewWriter | ||
| writer(data_dir / "3d_images/out.hdf", "vertices", mesh, false, true, true, false); | ||
Zhouyuan-Chen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| mesh.serialize(writer); | ||
| } | ||
13 changes: 13 additions & 0 deletions
13
components/wmtk_components/mesh_decimation/wmtk/components/mesh_decimation/CMakeLists.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
|
|
||
| set(SRC_FILES | ||
| internal/MeshDecimationOptions.hpp | ||
| internal/MeshDecimationOptions.cpp | ||
| internal/MeshDecimation.hpp | ||
| internal/MeshDecimation.cpp | ||
| mesh_decimation.hpp | ||
| mesh_decimation.cpp | ||
| ) | ||
|
|
||
|
|
||
| #CURRENT_COMPONENT_LIB_NAME is set from the main cmake | ||
| target_sources(${CURRENT_COMPONENT_LIB_NAME} PRIVATE ${SRC_FILES}) |
172 changes: 172 additions & 0 deletions
172
...tk_components/mesh_decimation/wmtk/components/mesh_decimation/internal/MeshDecimation.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,172 @@ | ||
| #include "MeshDecimation.hpp" | ||
| #include "wmtk/Scheduler.hpp" | ||
| #include "wmtk/function/simplex/AMIPS.hpp" | ||
| #include "wmtk/invariants/MultiMeshLinkConditionInvariant.hpp" | ||
| #include "wmtk/invariants/SimplexInversionInvariant.hpp" | ||
| #include "wmtk/invariants/SmallerFunctionInvariant.hpp" | ||
| #include "wmtk/invariants/TodoInvariant.hpp" | ||
| #include "wmtk/operations/EdgeCollapse.hpp" | ||
| #include "wmtk/operations/attribute_new/CollapseNewAttributeStrategy.hpp" | ||
| #include "wmtk/operations/attribute_update/AttributeTransferStrategy.hpp" | ||
|
|
||
| namespace wmtk::components::internal { | ||
|
|
||
| MeshDecimation::MeshDecimation( | ||
| Mesh& mesh, | ||
| std::string constriant_name, | ||
Zhouyuan-Chen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| int64_t constrait_value, | ||
Zhouyuan-Chen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| double target_len, | ||
| const std::vector<attribute::MeshAttributeHandle>& pass_through_attributes) | ||
| : m_mesh(mesh) | ||
| , m_constrait_value(constrait_value) | ||
| , m_constriant_name(constriant_name) | ||
| , m_target_len(target_len) | ||
| , m_pass_through_attributes(pass_through_attributes) | ||
| {} | ||
|
|
||
| void MeshDecimation::process() | ||
| { | ||
| using namespace wmtk::attribute; | ||
| using namespace wmtk::invariants; | ||
| PrimitiveType PV = PrimitiveType::Vertex; | ||
Zhouyuan-Chen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| PrimitiveType PE = PrimitiveType::Edge; | ||
| PrimitiveType PF = PrimitiveType::Triangle; | ||
| PrimitiveType PT = PrimitiveType::Tetrahedron; | ||
|
|
||
| volatile PrimitiveType x = m_mesh.top_simplex_type(); | ||
Zhouyuan-Chen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| MeshAttributeHandle cell_tag_handle = | ||
| m_mesh.get_attribute_handle<int64_t>(m_constriant_name, m_mesh.top_simplex_type()); | ||
| MeshAttributeHandle position = m_mesh.get_attribute_handle<double>("vertices", PV); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Position should be either in the
Zhouyuan-Chen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| MeshAttributeHandle edge_handle = m_mesh.register_attribute<int64_t>("todo_edge_", PE, 1); | ||
Zhouyuan-Chen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| MeshAttributeHandle vertex_handle = m_mesh.register_attribute<int64_t>("todo_vertex_", PV, 1); | ||
| MeshAttributeHandle edge_len_handle = m_mesh.register_attribute<double>("len_edge_", PE, 1); | ||
|
|
||
| Accessor<int64_t> acc_cell = m_mesh.create_accessor<int64_t>(cell_tag_handle); | ||
| Accessor<double> acc_pos = m_mesh.create_accessor<double>(position); | ||
| Accessor<int64_t> acc_edge = m_mesh.create_accessor<int64_t>(edge_handle); | ||
| Accessor<int64_t> acc_vertex = m_mesh.create_accessor<int64_t>(vertex_handle); | ||
| Accessor<double> acc_len = m_mesh.create_accessor<double>(edge_len_handle); | ||
|
|
||
| switch (m_mesh.top_cell_dimension()) { | ||
Zhouyuan-Chen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| case 2: | ||
| for (const Tuple& edge : m_mesh.get_all(PE)) { | ||
| if (m_mesh.is_boundary(PE, edge)) { | ||
| acc_vertex.scalar_attribute(edge) = 1; | ||
| acc_vertex.scalar_attribute(m_mesh.switch_tuple(edge, PV)) = 1; | ||
|
|
||
| acc_edge.scalar_attribute(edge) = 1; | ||
| } else if ( | ||
| acc_cell.scalar_attribute(edge) != | ||
| acc_cell.scalar_attribute(m_mesh.switch_tuple(edge, PF))) { | ||
| acc_vertex.scalar_attribute(edge) = 1; | ||
| acc_vertex.scalar_attribute(m_mesh.switch_tuple(edge, PV)) = 1; | ||
|
|
||
| acc_edge.scalar_attribute(edge) = 1; | ||
| } | ||
| } | ||
| break; | ||
| case 3: { | ||
| for (const Tuple& face : m_mesh.get_all(PF)) { | ||
| if (m_mesh.is_boundary(PF, face)) { | ||
| acc_vertex.scalar_attribute(face) = 1; | ||
| acc_vertex.scalar_attribute(m_mesh.switch_tuple(face, PV)) = 1; | ||
| acc_vertex.scalar_attribute(m_mesh.switch_tuples(face, {PE, PV})) = 1; | ||
|
|
||
| acc_edge.scalar_attribute(face) = 1; | ||
| acc_edge.scalar_attribute(m_mesh.switch_tuple(face, PE)) = 1; | ||
| acc_edge.scalar_attribute(m_mesh.switch_tuples(face, {PV, PE})) = 1; | ||
| } else if ( | ||
| acc_cell.scalar_attribute(face) != | ||
| acc_cell.scalar_attribute(m_mesh.switch_tuple(face, PT))) { | ||
| acc_vertex.scalar_attribute(face) = 1; | ||
| acc_vertex.scalar_attribute(m_mesh.switch_tuple(face, PV)) = 1; | ||
| acc_vertex.scalar_attribute(m_mesh.switch_tuples(face, {PE, PV})) = 1; | ||
|
|
||
| acc_edge.scalar_attribute(face) = 1; | ||
| acc_edge.scalar_attribute(m_mesh.switch_tuple(face, PE)) = 1; | ||
| acc_edge.scalar_attribute(m_mesh.switch_tuples(face, {PV, PE})) = 1; | ||
| } | ||
| } | ||
| break; | ||
| } | ||
| default: | ||
| std::runtime_error("MeshDecimation.cpp: mesh_decimation component only supports tetmesh " | ||
Zhouyuan-Chen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| "and trimesh for now!"); | ||
| } | ||
|
|
||
| for (const Tuple& edge : m_mesh.get_all(PE)) { | ||
| if (acc_vertex.scalar_attribute(edge) == 1 || | ||
| acc_vertex.scalar_attribute(m_mesh.switch_tuple(edge, PV)) == 1) { | ||
| acc_edge.scalar_attribute(edge) = 1; | ||
| } | ||
| acc_len.scalar_attribute(edge) = (acc_pos.vector_attribute(edge) - | ||
Zhouyuan-Chen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| acc_pos.vector_attribute(m_mesh.switch_tuple(edge, PV))) | ||
| .norm(); | ||
| } | ||
|
|
||
| auto op_scaffold = std::make_shared<operations::EdgeCollapse>(m_mesh); | ||
Zhouyuan-Chen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| op_scaffold->add_invariant( | ||
| std::make_shared<TodoInvariant>(m_mesh, edge_handle.as<int64_t>(), 0)); | ||
| op_scaffold->add_invariant(std::make_shared<TodoSmallerInvariant>( | ||
| m_mesh, | ||
| edge_len_handle.as<double>(), | ||
| 4.0 / 5.0 * m_target_len)); | ||
Zhouyuan-Chen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| auto m_amips = std::make_shared<function::AMIPS>(m_mesh, position); | ||
| auto m_link_conditions = std::make_shared<InvariantCollection>(m_mesh); | ||
| m_link_conditions->add(std::make_shared<MultiMeshLinkConditionInvariant>(m_mesh)); | ||
| auto m_function_invariant = | ||
| std::make_shared<SmallerFunctionInvariant>(m_mesh.top_simplex_type(), m_amips, 30); | ||
| auto m_inversion_invariant = | ||
| std::make_shared<SimplexInversionInvariant>(m_mesh, position.as<double>()); | ||
|
|
||
| // Edge length update | ||
| auto compute_edge_length = [](const Eigen::MatrixXd& P) -> Eigen::VectorXd { | ||
| assert(P.cols() == 2); | ||
| assert(P.rows() == 2 || P.rows() == 3); | ||
| return Eigen::VectorXd::Constant(1, (P.col(0) - P.col(1)).norm()); | ||
| }; | ||
| auto m_edge_length_update = | ||
Zhouyuan-Chen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| std::make_shared<wmtk::operations::SingleAttributeTransferStrategy<double, double>>( | ||
| edge_len_handle, | ||
| position, | ||
| compute_edge_length); | ||
| auto m_prio_short_edges_first = [&](const simplex::Simplex& s) { | ||
| assert(s.primitive_type() == PrimitiveType::Edge); | ||
| auto acc = m_mesh.create_accessor<double>(edge_len_handle); | ||
| return std::vector<double>({acc.scalar_attribute(s.tuple())}); | ||
| }; | ||
|
|
||
|
|
||
| op_scaffold->add_invariant(m_link_conditions); | ||
| op_scaffold->add_invariant(m_function_invariant); | ||
| op_scaffold->add_invariant(m_inversion_invariant); | ||
|
|
||
| op_scaffold->add_transfer_strategy(m_edge_length_update); | ||
|
|
||
| op_scaffold->set_priority(m_prio_short_edges_first); | ||
|
|
||
| op_scaffold->set_new_attribute_strategy( | ||
| position, | ||
| wmtk::operations::CollapseBasicStrategy::Mean); | ||
| op_scaffold->set_new_attribute_strategy(vertex_handle); | ||
|
|
||
| op_scaffold->set_new_attribute_strategy(edge_handle); | ||
Zhouyuan-Chen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| op_scaffold->set_new_attribute_strategy(edge_len_handle); | ||
| op_scaffold->set_new_attribute_strategy(cell_tag_handle); | ||
|
|
||
| // pass_through | ||
| for (const auto& attr : m_pass_through_attributes) { | ||
| op_scaffold->set_new_attribute_strategy(attr); | ||
| } | ||
|
|
||
| while (true) { | ||
| Scheduler scheduler; | ||
| SchedulerStats pass_stats = scheduler.run_operation_on_all(*op_scaffold); | ||
| if (pass_stats.number_of_successful_operations() == 0) break; | ||
Zhouyuan-Chen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| } // namespace wmtk::components::internal | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.