Skip to content

Commit

Permalink
Merge pull request #845 from mtao/mtao/flag_accessor
Browse files Browse the repository at this point in the history
Flag Accessor
  • Loading branch information
mtao authored Dec 11, 2024
2 parents 22b78e8 + 42d5ab0 commit bd2e000
Show file tree
Hide file tree
Showing 26 changed files with 306 additions and 124 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ endif()
project(WildMeshingToolkit DESCRIPTION "A mesh optimization toolkit" LANGUAGES C CXX)

# ###############################################################################
option(WMTK_ENABLE_APPLICATIONS "Enable applications (required for any applications to build)" ${WILDMESHING_TOOLKIT_TOPLEVEL_PROJECT})
option(WMTK_BUILD_DOCS "Build doxygen" OFF)
option (BUILD_SHARED_LIBS "Build Shared Libraries" OFF) # we globally want to disable this option due to use of TBB

Expand Down Expand Up @@ -207,5 +208,7 @@ endif()
# Compile extras only if this is a top-level project
if(WILDMESHING_TOOLKIT_TOPLEVEL_PROJECT)
add_subdirectory(tests)
endif()
if(WMTK_ENABLE_APPLICATIONS)
add_subdirectory(applications)
endif()
31 changes: 19 additions & 12 deletions src/wmtk/EdgeMesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <numeric>
#include <wmtk/utils/Logger.hpp>
namespace wmtk {
EdgeMesh::~EdgeMesh() = default;
EdgeMesh::~EdgeMesh() = default;
EdgeMesh::EdgeMesh()
: MeshCRTP<EdgeMesh>(1)
, m_ve_handle(register_attribute_typed<int64_t>("m_ve", PrimitiveType::Vertex, 1, false, -1))
Expand Down Expand Up @@ -111,21 +111,21 @@ void EdgeMesh::initialize(
attribute::Accessor<int64_t> ee_accessor = create_accessor<int64_t>(m_ee_handle);
attribute::Accessor<int64_t> ve_accessor = create_accessor<int64_t>(m_ve_handle);

attribute::Accessor<char> v_flag_accessor = get_flag_accessor(PrimitiveType::Vertex);
attribute::Accessor<char> e_flag_accessor = get_flag_accessor(PrimitiveType::Edge);
attribute::FlagAccessor<EdgeMesh> v_flag_accessor = get_flag_accessor(PrimitiveType::Vertex);
attribute::FlagAccessor<EdgeMesh> e_flag_accessor = get_flag_accessor(PrimitiveType::Edge);

// iterate over the matrices and fill attributes

for (int64_t i = 0; i < capacity(PrimitiveType::Edge); ++i) {
ev_accessor.index_access().vector_attribute<2>(i) = EV.row(i).transpose();
ee_accessor.index_access().vector_attribute<2>(i) = EE.row(i).transpose();

e_flag_accessor.index_access().scalar_attribute(i) |= 0x1;
e_flag_accessor.index_access().activate(i);
}
// m_ve
for (int64_t i = 0; i < capacity(PrimitiveType::Vertex); ++i) {
ve_accessor.index_access().scalar_attribute(i) = VE(i);
v_flag_accessor.index_access().scalar_attribute(i) |= 0x1;
v_flag_accessor.index_access().activate(i);
}
}

Expand Down Expand Up @@ -239,31 +239,38 @@ bool EdgeMesh::is_connectivity_valid() const
const attribute::Accessor<int64_t> ev_accessor = create_const_accessor<int64_t>(m_ev_handle);
const attribute::Accessor<int64_t> ee_accessor = create_const_accessor<int64_t>(m_ee_handle);
const attribute::Accessor<int64_t> ve_accessor = create_const_accessor<int64_t>(m_ve_handle);
const attribute::Accessor<char> v_flag_accessor = get_flag_accessor(PrimitiveType::Vertex);
const attribute::Accessor<char> e_flag_accessor = get_flag_accessor(PrimitiveType::Edge);
const attribute::FlagAccessor<EdgeMesh> v_flag_accessor =
get_flag_accessor(PrimitiveType::Vertex);
const attribute::FlagAccessor<EdgeMesh> e_flag_accessor =
get_flag_accessor(PrimitiveType::Edge);

// VE and EV
for (int64_t i = 0; i < capacity(PrimitiveType::Vertex); ++i) {
if (v_flag_accessor.index_access().const_scalar_attribute(i) == 0) {
wmtk::logger().debug("Vertex {} is deleted", i);
if (!v_flag_accessor.index_access().is_active(i)) {
continue;
}
int cnt = 0;
for (int64_t j = 0; j < 2; ++j) {
if (ev_accessor.index_access().const_vector_attribute<2>(
ve_accessor.index_access().const_scalar_attribute(i))[j] == i) {
ve_accessor.index_access().const_scalar_attribute(i))(j) == i) {
cnt++;
}
}
if (cnt == 0) {
int64_t idx = ve_accessor.index_access().const_scalar_attribute(i);
wmtk::logger().error(
"EV[VE[{}]={},:]] ({}) = doesn't contain {}",
i,
idx,
fmt::join(ev_accessor.index_access().const_vector_attribute<2>(idx), ","),
i);
return false;
}
}

// EV and EE
for (int64_t i = 0; i < capacity(PrimitiveType::Edge); ++i) {
if (e_flag_accessor.index_access().const_scalar_attribute(i) == 0) {
wmtk::logger().debug("Edge {} is deleted", i);
if (!e_flag_accessor.index_access().is_active(i)) {
continue;
}
// TODO: need to handle cornor case (self-loop)
Expand Down
2 changes: 1 addition & 1 deletion src/wmtk/EdgeMeshOperationExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void EdgeMesh::EdgeMeshOperationExecutor::delete_simplices()
{
for (size_t d = 0; d < simplex_ids_to_delete.size(); ++d) {
for (const int64_t id : simplex_ids_to_delete[d]) {
flag_accessors[d].index_access().scalar_attribute(id) = 0;
flag_accessors[d].index_access().deactivate(id) ;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/wmtk/EdgeMeshOperationExecutor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class EdgeMesh::EdgeMeshOperationExecutor : public operations::edge_mesh::EdgeOp
void delete_simplices();
void update_cell_hash();

std::array<attribute::Accessor<char>, 2> flag_accessors;
std::array<attribute::FlagAccessor<EdgeMesh>, 2> flag_accessors;
attribute::Accessor<int64_t, EdgeMesh> ee_accessor;
attribute::Accessor<int64_t, EdgeMesh> ev_accessor;
attribute::Accessor<int64_t, EdgeMesh> ve_accessor;
Expand Down
29 changes: 16 additions & 13 deletions src/wmtk/Mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ std::vector<simplex::IdSimplex> Mesh::get_all_id_simplex(

const int64_t cap = capacity(type);

const attribute::Accessor<char> flag_accessor = get_flag_accessor(type);
const attribute::CachingAccessor<char>& flag_accessor_indices = flag_accessor.index_access();
const attribute::FlagAccessor<> flag_accessor = get_flag_accessor(type);
const attribute::IndexFlagAccessor<>& flag_accessor_indices = flag_accessor.index_access();
ret.reserve(cap);
for (size_t index = 0; index < cap; ++index) {
if (flag_accessor_indices.const_scalar_attribute(index) & 1)
if (flag_accessor_indices.is_active(index))
ret.emplace_back(simplex::IdSimplex(type, index));
else if (include_deleted)
ret.emplace_back();
Expand All @@ -77,14 +77,15 @@ std::vector<Tuple> Mesh::get_all(PrimitiveType type, const bool include_deleted)

const int64_t cap = capacity(type);

const attribute::Accessor<char> flag_accessor = get_flag_accessor(type);
const attribute::CachingAccessor<char>& flag_accessor_indices = flag_accessor.index_access();
const attribute::FlagAccessor<> flag_accessor = get_flag_accessor(type);
const attribute::IndexFlagAccessor<>& flag_accessor_indices = flag_accessor.index_access();
ret.reserve(cap);
for (size_t index = 0; index < cap; ++index) {
if (flag_accessor_indices.const_scalar_attribute(index) & 1)
if (flag_accessor_indices.is_active(index)) {
ret.emplace_back(tuple_from_id(type, index));
else if (include_deleted)
} else if (include_deleted) {
ret.emplace_back();
}
}
return ret;
}
Expand Down Expand Up @@ -137,7 +138,7 @@ bool Mesh::is_removed(int64_t index) const
bool Mesh::is_removed(int64_t index, PrimitiveType pt) const
{
const auto& flag_accessor = get_const_flag_accessor(pt);
return !(flag_accessor.index_access().const_scalar_attribute(index) & 0x1);
return !flag_accessor.index_access().is_active(index);
}

bool Mesh::is_valid(const simplex::Simplex& s) const
Expand All @@ -155,17 +156,19 @@ bool Mesh::is_valid(const simplex::Simplex& s) const
}


const attribute::Accessor<char> Mesh::get_flag_accessor(PrimitiveType type) const
const attribute::FlagAccessor<Mesh> Mesh::get_flag_accessor(PrimitiveType type) const
{
return get_const_flag_accessor(type);
}
const attribute::Accessor<char> Mesh::get_const_flag_accessor(PrimitiveType type) const
const attribute::FlagAccessor<Mesh> Mesh::get_const_flag_accessor(PrimitiveType type) const
{
return create_const_accessor(m_flag_handles.at(get_primitive_type_id(type)));
return attribute::FlagAccessor<Mesh>(
create_const_accessor(m_flag_handles.at(get_primitive_type_id(type))));
}
attribute::Accessor<char> Mesh::get_flag_accessor(PrimitiveType type)
attribute::FlagAccessor<Mesh> Mesh::get_flag_accessor(PrimitiveType type)
{
return create_accessor(m_flag_handles.at(get_primitive_type_id(type)));
return attribute::FlagAccessor<Mesh>(
create_accessor(m_flag_handles.at(get_primitive_type_id(type))));
}


Expand Down
7 changes: 4 additions & 3 deletions src/wmtk/Mesh.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "attribute/AttributeScopeHandle.hpp"
#include "attribute/MeshAttributeHandle.hpp"
#include "attribute/MeshAttributes.hpp"
#include "attribute/FlagAccessor.hpp"
#include "multimesh/attribute/AttributeScopeHandle.hpp"

#include "multimesh/attribute/UseParentScopeRAII.hpp"
Expand Down Expand Up @@ -295,8 +296,8 @@ class Mesh : public std::enable_shared_from_this<Mesh>, public wmtk::utils::Merk
decltype(auto) parent_scope(Functor&& f, Args&&... args) const;


const attribute::Accessor<char> get_flag_accessor(PrimitiveType type) const;
const attribute::Accessor<char> get_const_flag_accessor(PrimitiveType type) const;
const attribute::FlagAccessor<Mesh> get_flag_accessor(PrimitiveType type) const;
const attribute::FlagAccessor<Mesh> get_const_flag_accessor(PrimitiveType type) const;


bool operator==(const Mesh& other) const;
Expand All @@ -307,7 +308,7 @@ class Mesh : public std::enable_shared_from_this<Mesh>, public wmtk::utils::Merk
virtual std::vector<Tuple> orient_vertices(const Tuple& t) const = 0;

protected: // member functions
attribute::Accessor<char> get_flag_accessor(PrimitiveType type);
attribute::FlagAccessor<> get_flag_accessor(PrimitiveType type);


protected:
Expand Down
12 changes: 6 additions & 6 deletions src/wmtk/Mesh_attributes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ std::vector<int64_t> Mesh::request_simplex_indices(PrimitiveType type, int64_t c
int64_t current_capacity = capacity(type);

// enable newly requested simplices
attribute::Accessor<char> flag_accessor = get_flag_accessor(type);
int64_t max_size = flag_accessor.reserved_size();
attribute::FlagAccessor<Mesh> flag_accessor = get_flag_accessor(type);
int64_t max_size = flag_accessor.base_accessor().reserved_size();

if (current_capacity + count > max_size) {
logger().warn(
Expand All @@ -71,11 +71,11 @@ std::vector<int64_t> Mesh::request_simplex_indices(PrimitiveType type, int64_t c

m_attribute_manager.m_capacities[primitive_id] = new_capacity;

attribute::CachingAccessor<char>& flag_accessor_indices = flag_accessor.index_access();
attribute::IndexFlagAccessor<Mesh>& flag_accessor_indices = flag_accessor.index_access();

for (const int64_t simplex_index : ret) {
// wmtk::logger().trace("Activating {}-simplex {}", primitive_id, simplex_index);
flag_accessor_indices.scalar_attribute(simplex_index) |= 0x1;
flag_accessor_indices.activate(simplex_index);
}

return ret;
Expand Down Expand Up @@ -218,10 +218,10 @@ std::tuple<std::vector<std::vector<int64_t>>, std::vector<std::vector<int64_t>>>

// Initialize both maps
for (int64_t d = 0; d < tcp; d++) {
attribute::Accessor<char> flag_accessor =
attribute::FlagAccessor<Mesh> flag_accessor =
get_flag_accessor(wmtk::get_primitive_type_from_id(d));
for (int64_t i = 0; i < capacity(wmtk::get_primitive_type_from_id(d)); ++i) {
if (flag_accessor.index_access().scalar_attribute(i) & 1) {
if (flag_accessor.index_access().is_active(i)) {
old2new[d].push_back(new2old[d].size());
new2old[d].push_back(old2new[d].size() - 1); // -1 since we just pushed into it
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/wmtk/PointMesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ void PointMesh::initialize(int64_t count)
{
set_capacities({count});
reserve_attributes_to_fit();
attribute::Accessor<char> v_flag_accessor = get_flag_accessor(PrimitiveType::Vertex);
attribute::FlagAccessor<PointMesh> v_flag_accessor = get_flag_accessor(PrimitiveType::Vertex);
for (int64_t i = 0; i < capacity(PrimitiveType::Vertex); ++i) {
v_flag_accessor.index_access().scalar_attribute(i) |= 0x1;
v_flag_accessor.index_access().activate(i);
}
}

Expand Down
Loading

0 comments on commit bd2e000

Please sign in to comment.