Skip to content

Commit

Permalink
Merge branch '468-optimize-the-find_local_switch_sequence-function' of
Browse files Browse the repository at this point in the history
…https://github.com/wildmeshing/wildmeshing-toolkit into 434-edgemesh-to-trimesh-multimesh-register
  • Loading branch information
zlyfunction committed Oct 19, 2023
2 parents 3d9b93f + e99021d commit 95bc719
Showing 1 changed file with 135 additions and 30 deletions.
165 changes: 135 additions & 30 deletions src/wmtk/multimesh/utils/find_local_switch_sequence.cpp
Original file line number Diff line number Diff line change
@@ -1,48 +1,132 @@
#include "find_local_switch_sequence.hpp"
#include <cassert>
#include "local_switch_tuple.hpp"
namespace wmtk::multimesh::utils {


namespace {

std::vector<PrimitiveType> find_local_switch_sequence_trimesh(
std::pair<bool, std::vector<PrimitiveType>> find_local_switch_sequence_on_edge(
const Tuple& source,
const Tuple& target)
const Tuple& target,
const PrimitiveType mesh_pt)
{
// TODO: assert that base_source and base_target use the same global CID
assert(mesh_pt >= PrimitiveType::Edge);
if (source == target) {
return std::vector<PrimitiveType>{};
return {true, std::vector<PrimitiveType>{}};
} else if (local_switch_tuple(mesh_pt, source, PrimitiveType::Vertex) == target) {
return {true, std::vector<PrimitiveType>{PrimitiveType::Vertex}};
}
return {false, std::vector<PrimitiveType>{}};
}

std::pair<bool, std::vector<PrimitiveType>> find_local_switch_sequence_on_triangle(
const Tuple& source,
const Tuple& target,
const PrimitiveType mesh_pt)
{
assert(mesh_pt >= PrimitiveType::Face);

// circulate
Tuple cur_tuple = source;
std::vector<PrimitiveType> switches;
auto try_and_record = [&](PrimitiveType pt) -> bool {
cur_tuple = local_switch_tuple(PrimitiveType::Face, cur_tuple, pt);
switches.emplace_back(pt);
return cur_tuple == target;
};
for (long j = 0; j < 3; ++j) {
for (PrimitiveType pt : {PrimitiveType::Vertex, PrimitiveType::Edge}) {
if (try_and_record(pt)) {
return switches;
}
}
}
throw "switch sequence was unable to find a sequence of switches to match tuples";
return switches;
{
auto [success, edge_local_operations] =
find_local_switch_sequence_on_edge(cur_tuple, target, mesh_pt);
if (success) {
return {true, edge_local_operations};
}
}
switches.emplace_back(PrimitiveType::Edge);
cur_tuple = local_switch_tuples(mesh_pt, source, switches);
{
auto [success, edge_local_operations] =
find_local_switch_sequence_on_edge(cur_tuple, target, mesh_pt);
if (success) {
switches.insert(
switches.end(),

Check warning on line 45 in src/wmtk/multimesh/utils/find_local_switch_sequence.cpp

View check run for this annotation

Codecov / codecov/patch

src/wmtk/multimesh/utils/find_local_switch_sequence.cpp#L44-L45

Added lines #L44 - L45 were not covered by tests
edge_local_operations.begin(),
edge_local_operations.end());
return {true, switches};

Check warning on line 48 in src/wmtk/multimesh/utils/find_local_switch_sequence.cpp

View check run for this annotation

Codecov / codecov/patch

src/wmtk/multimesh/utils/find_local_switch_sequence.cpp#L48

Added line #L48 was not covered by tests
}
}

Check warning on line 50 in src/wmtk/multimesh/utils/find_local_switch_sequence.cpp

View check run for this annotation

Codecov / codecov/patch

src/wmtk/multimesh/utils/find_local_switch_sequence.cpp#L50

Added line #L50 was not covered by tests

switches.insert(switches.begin(), PrimitiveType::Vertex);
cur_tuple = local_switch_tuples(mesh_pt, source, switches);
{
auto [success, edge_local_operations] =
find_local_switch_sequence_on_edge(cur_tuple, target, mesh_pt);
if (success) {
switches.insert(
switches.end(),
edge_local_operations.begin(),
edge_local_operations.end());
return {true, switches};
}
}
return {false, std::vector<PrimitiveType>{}};

Check warning on line 65 in src/wmtk/multimesh/utils/find_local_switch_sequence.cpp

View check run for this annotation

Codecov / codecov/patch

src/wmtk/multimesh/utils/find_local_switch_sequence.cpp#L65

Added line #L65 was not covered by tests
}
std::vector<PrimitiveType> find_local_switch_sequence_edgemesh(

std::pair<bool, std::vector<PrimitiveType>> find_local_switch_sequence_on_tet(

Check warning on line 68 in src/wmtk/multimesh/utils/find_local_switch_sequence.cpp

View check run for this annotation

Codecov / codecov/patch

src/wmtk/multimesh/utils/find_local_switch_sequence.cpp#L68

Added line #L68 was not covered by tests
const Tuple& source,
const Tuple& target)
const Tuple& target,
const PrimitiveType mesh_pt)
{
if (source != target) {
return std::vector<PrimitiveType>{PrimitiveType::Vertex};
} else {
return std::vector<PrimitiveType>{};
// TODO: test it in the future
throw "not tested yet";

Check warning on line 74 in src/wmtk/multimesh/utils/find_local_switch_sequence.cpp

View check run for this annotation

Codecov / codecov/patch

src/wmtk/multimesh/utils/find_local_switch_sequence.cpp#L74

Added line #L74 was not covered by tests
assert(mesh_pt == PrimitiveType::Tetrahedron);
Tuple cur_tuple = source;
std::vector<PrimitiveType> switches;

{
auto [success, triangle_local_operations] =
find_local_switch_sequence_on_triangle(cur_tuple, target, mesh_pt);
if (success) {
return {true, triangle_local_operations};
}
}
switches.emplace_back(PrimitiveType::Face);
cur_tuple = local_switch_tuples(mesh_pt, source, switches);
{
auto [success, triangle_local_operations] =
find_local_switch_sequence_on_triangle(cur_tuple, target, mesh_pt);
if (success) {
switches.insert(
switches.end(),
triangle_local_operations.begin(),
triangle_local_operations.end());
return {true, switches};
}
}

switches.insert(switches.begin(), PrimitiveType::Edge);
cur_tuple = local_switch_tuples(mesh_pt, source, switches);
{
auto [success, triangle_local_operations] =
find_local_switch_sequence_on_triangle(cur_tuple, target, mesh_pt);
if (success) {
switches.insert(
switches.end(),
triangle_local_operations.begin(),
triangle_local_operations.end());
return {true, switches};
}
}

switches.insert(switches.begin(), PrimitiveType::Vertex);
cur_tuple = local_switch_tuples(mesh_pt, source, switches);
{
auto [success, triangle_local_operations] =
find_local_switch_sequence_on_triangle(cur_tuple, target, mesh_pt);
if (success) {
switches.insert(
switches.end(),
triangle_local_operations.begin(),
triangle_local_operations.end());
return {true, switches};
}
}
throw "switch sequence was unable to find a sequence of switches to match tuples";
return {false, std::vector<PrimitiveType>{}};
}

} // namespace

// Maps the tuple source according to the operation sequence
Expand All @@ -52,10 +136,31 @@ std::vector<PrimitiveType>
find_local_switch_sequence(const Tuple& source, const Tuple& target, PrimitiveType primitive_type)
{
switch (primitive_type) {
case PrimitiveType::Face: return find_local_switch_sequence_trimesh(source, target);
case PrimitiveType::Edge: return find_local_switch_sequence_edgemesh(source, target);
case PrimitiveType::Edge: {
auto [success, operations] =
find_local_switch_sequence_on_edge(source, target, PrimitiveType::Edge);
if (!success) {
throw "switch sequence was unable to find a sequence of switches to match tuples";

Check warning on line 143 in src/wmtk/multimesh/utils/find_local_switch_sequence.cpp

View check run for this annotation

Codecov / codecov/patch

src/wmtk/multimesh/utils/find_local_switch_sequence.cpp#L139-L143

Added lines #L139 - L143 were not covered by tests
}
return operations;
}

Check warning on line 146 in src/wmtk/multimesh/utils/find_local_switch_sequence.cpp

View check run for this annotation

Codecov / codecov/patch

src/wmtk/multimesh/utils/find_local_switch_sequence.cpp#L145-L146

Added lines #L145 - L146 were not covered by tests
case PrimitiveType::Face: {
auto [success, operations] =
find_local_switch_sequence_on_triangle(source, target, PrimitiveType::Face);
if (!success) {
throw "switch sequence was unable to find a sequence of switches to match tuples";

Check warning on line 151 in src/wmtk/multimesh/utils/find_local_switch_sequence.cpp

View check run for this annotation

Codecov / codecov/patch

src/wmtk/multimesh/utils/find_local_switch_sequence.cpp#L151

Added line #L151 was not covered by tests
}
return operations;
}
case PrimitiveType::Tetrahedron: {
auto [success, operations] =
find_local_switch_sequence_on_tet(source, target, PrimitiveType::Tetrahedron);

Check warning on line 157 in src/wmtk/multimesh/utils/find_local_switch_sequence.cpp

View check run for this annotation

Codecov / codecov/patch

src/wmtk/multimesh/utils/find_local_switch_sequence.cpp#L155-L157

Added lines #L155 - L157 were not covered by tests
if (!success) {
throw "switch sequence was unable to find a sequence of switches to match tuples";
}
return operations;
}
case PrimitiveType::Vertex: return {};
case PrimitiveType::Tetrahedron:
default: return {};
}
}
Expand Down

0 comments on commit 95bc719

Please sign in to comment.