Wildmeshing Toolkit
TupleTag.cpp
Go to the documentation of this file.
1 #include "TupleTag.hpp"
3 #include <wmtk/Primitive.hpp>
5 TupleTag::TupleTag(Mesh& mesh, const std::set<int64_t>& critical_points)
6  : m_mesh(mesh)
7  , m_critical_points(critical_points)
8  , m_vertex_tag_acc(mesh.create_accessor(
9  mesh.register_attribute<int64_t>("vertex_tag", PrimitiveType::Vertex, 1).as<int64_t>()))
10  , m_edge_tag_acc(mesh.create_accessor(
11  mesh.register_attribute<int64_t>("edge_tag", PrimitiveType::Edge, 1).as<int64_t>()))
12 {
13  initialize();
14 }
15 
17 {
18  std::vector<Tuple> v_tuples = mesh().get_all(PrimitiveType::Vertex);
19  std::vector<Tuple> e_tuples = mesh().get_all(PrimitiveType::Edge);
20  // initializing all the edge tags to be -1
21  for (const Tuple& e : e_tuples) {
22  if (mesh().is_boundary(PrimitiveType::Edge, e)) {
23  set_edge_tag(e, -1);
24  }
25  }
26  // initializing all the vertex tags to be the vertex id
27  for (const Tuple& v : v_tuples) {
28  if (mesh().is_boundary(PrimitiveType::Vertex, v)) {
29  set_vertex_tag(v, vid(v));
30  }
31  }
32 }
33 
34 std::set<int64_t> TupleTag::run()
35 {
36  std::set<int64_t> tags;
37  std::vector<Tuple> v_tuples = mesh().get_all(PrimitiveType::Vertex);
38  std::vector<Tuple> e_tuples = mesh().get_all(PrimitiveType::Edge);
39  int64_t vid_max = mesh().capacity(PrimitiveType::Vertex);
40  // the pass to tag all vertices
41  for (const Tuple& e : e_tuples) {
42  if (mesh().is_boundary(PrimitiveType::Edge, e)) {
43  run(e);
44  }
45  }
46  // the pass to tag all edges
47  int64_t edge_tag = 0;
48  for (const Tuple& e : e_tuples) {
49  if (mesh().is_boundary(PrimitiveType::Edge, e)) {
50  Tuple v1 = e;
52  // both vertices are critical points
53  if (is_critical_vertex(v1) && is_critical_vertex(v2)) {
54  set_edge_tag(e, edge_tag + vid_max);
55  tags.insert(edge_tag + vid_max);
56  edge_tag++;
57  } else if (is_critical_vertex(v1)) {
58  int64_t v2_root = vertex_get_root(v2);
59  set_edge_tag(e, v2_root);
60  tags.insert(v2_root);
61  } else if (is_critical_vertex(v2)) {
62  int64_t v1_root = vertex_get_root(v1);
63  set_edge_tag(e, v1_root);
64  tags.insert(v1_root);
65  } else {
66  int64_t v1_root = vertex_get_root(v1);
67  int64_t v2_root = vertex_get_root(v2);
68  assert(v1_root == v2_root);
69  set_edge_tag(e, v1_root);
70  tags.insert(v1_root);
71  }
72  }
73  }
74  return tags;
75 }
76 
77 
78 bool TupleTag::is_critical_vertex(const Tuple& v) const
79 {
80  return m_critical_points.find(vid(v)) != m_critical_points.end();
81 }
82 
83 int64_t TupleTag::get_vertex_tag(const Tuple& tuple) const
84 {
86 }
87 int64_t TupleTag::get_edge_tag(const Tuple& tuple) const
88 {
90 }
91 
92 void TupleTag::set_vertex_tag(const Tuple& tuple, int64_t tag)
93 {
95 }
96 
97 void TupleTag::set_edge_tag(const Tuple& tuple, int64_t tag)
98 {
99  m_edge_tag_acc.scalar_attribute(tuple) = tag;
100 }
101 
102 int64_t TupleTag::vid(const Tuple& tuple) const
103 {
104  return mesh().id(tuple, PrimitiveType::Vertex);
105 }
106 
107 Tuple TupleTag::v_tuple(int64_t vid) const
108 {
110  return v_tuple;
111 }
112 
113 bool TupleTag::vertex_is_root(const Tuple& v) const
114 {
115  return get_vertex_tag(v) == vid(v);
116 }
117 
118 
119 int64_t TupleTag::vertex_get_root(const Tuple& v) const
120 {
121  Tuple mutable_v = v;
122  while (!vertex_is_root(mutable_v)) {
123  int64_t parent_vid = get_vertex_tag(mutable_v);
124  mutable_v = v_tuple(parent_vid);
125  }
126  return get_vertex_tag(mutable_v);
127 }
128 
129 void TupleTag::vertex_set_root(const Tuple& v, int64_t root)
130 {
131  Tuple mutable_v = v;
132  while (!vertex_is_root(mutable_v)) {
133  int tmp = get_vertex_tag(mutable_v);
134  set_vertex_tag(mutable_v, root);
135  mutable_v = v_tuple(tmp);
136  }
137  set_vertex_tag(mutable_v, root);
138 }
139 
140 void TupleTag::vertex_sets_unify(const Tuple& v1, const Tuple& v2)
141 {
142  int64_t v1_root = vertex_get_root(v1);
143  int64_t v2_root = vertex_get_root(v2);
144  if (v1_root == v2_root) {
145  return;
146  }
147  if (is_critical_vertex(v1) || is_critical_vertex(v2)) {
148  return;
149  } else {
150  int64_t root = std::min(v1_root, v2_root);
151  vertex_set_root(v1, root);
152  vertex_set_root(v2, root);
153  }
154 }
155 
156 void TupleTag::run(const Tuple& e)
157 {
158  Tuple v1 = e;
160  int64_t vid1 = vid(v1);
161  int64_t vid2 = vid(v2);
162  // both vertices are critical points
163  if (is_critical_vertex(v1) && is_critical_vertex(v2)) {
164  return;
165  } else {
166  vertex_sets_unify(v1, v2);
167  }
168 }
169 
170 } // namespace wmtk::multimesh::utils::internal
int64_t capacity(PrimitiveType type) const
read in the m_capacities return the upper bound for the number of entities of the given dimension
int64_t id(const Tuple &tuple, PrimitiveType type) const
return the global id of the Tuple of the given dimension
Definition: Mesh.hpp:1020
virtual Tuple tuple_from_id(const PrimitiveType type, const int64_t gid) const =0
internal function that returns the tuple of requested type, and has the global index cid
std::vector< Tuple > get_all(PrimitiveType type) const
Generate a vector of Tuples from global vertex/edge/triangle/tetrahedron index.
Definition: Mesh.cpp:18
virtual Tuple switch_tuple(const Tuple &tuple, PrimitiveType type) const =0
switch the orientation of the Tuple of the given dimension
T & scalar_attribute(const ArgType &t)
T const_scalar_attribute(const ArgType &t) const
Definition: Accessor.hxx:112
wmtk::attribute::Accessor< int64_t > m_vertex_tag_acc
Definition: TupleTag.hpp:67
int64_t get_edge_tag(const Tuple &tuple) const
Definition: TupleTag.cpp:87
bool vertex_is_root(const Tuple &v) const
Definition: TupleTag.cpp:113
void set_edge_tag(const Tuple &tuple, int64_t tag)
Definition: TupleTag.cpp:97
bool is_critical_vertex(const Tuple &v) const
Check if a vertex is a critical point.
Definition: TupleTag.cpp:78
void vertex_set_root(const Tuple &v, int64_t root)
Definition: TupleTag.cpp:129
wmtk::attribute::Accessor< int64_t > m_edge_tag_acc
Definition: TupleTag.hpp:68
void set_vertex_tag(const Tuple &tuple, int64_t tag)
Definition: TupleTag.cpp:92
void vertex_sets_unify(const Tuple &v1, const Tuple &v2)
Given two vertex tuple, join the sets that contain them.
Definition: TupleTag.cpp:140
TupleTag(Mesh &mesh, const std::set< int64_t > &critical_points)
Definition: TupleTag.cpp:5
int64_t vertex_get_root(const Tuple &v) const
Definition: TupleTag.cpp:119
void initialize()
Go through edges of the parent mesh (triangle mesh) and initialize all the vertex tags to be -1.
Definition: TupleTag.cpp:16
int64_t vid(const Tuple &tuple) const
Definition: TupleTag.cpp:102
int64_t get_vertex_tag(const Tuple &tuple) const
Definition: TupleTag.cpp:83