Skip to content

Commit 83e27ca

Browse files
committed
Clean up VTU writing.
Next, I'll add a base class to meshes to avoid having to template the writer.
1 parent 8f5c36b commit 83e27ca

File tree

7 files changed

+122
-140
lines changed

7 files changed

+122
-140
lines changed

components/simplicial_embedding/wmtk/components/simplicial_embedding/SimplicialEmbeddingTriMesh.cpp

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -51,35 +51,6 @@ void SimplicialEmbeddingTriMesh::set_num_threads(const int64_t num_threads)
5151
NUM_THREADS = num_threads;
5252
}
5353

54-
Eigen::MatrixXi SimplicialEmbeddingTriMesh::get_F() const
55-
{
56-
const std::vector<Tuple> face_tuples = get_faces();
57-
Eigen::MatrixXi F(face_tuples.size(), 3);
58-
59-
for (int i = 0; i < face_tuples.size(); ++i) {
60-
const auto vs = oriented_tri_vids(face_tuples[i]);
61-
for (int j = 0; j < 3; j++) {
62-
F(i, j) = vs[j];
63-
}
64-
}
65-
66-
return F;
67-
}
68-
69-
Eigen::MatrixXd SimplicialEmbeddingTriMesh::get_V() const
70-
{
71-
const std::vector<Tuple> vertex_tuples = get_vertices();
72-
Eigen::MatrixXd V;
73-
V.resize(vertex_tuples.size(), 2);
74-
75-
for (const Tuple& v : vertex_tuples) {
76-
const auto vid = v.vid(*this);
77-
V.row(vid) = vertex_attrs[vid].pos;
78-
}
79-
80-
return V;
81-
}
82-
8354
void SimplicialEmbeddingTriMesh::cache_edge_positions(const Tuple& t)
8455
{
8556
position_cache.local().v1p = vertex_attrs[t.vid(*this)].pos;
@@ -235,15 +206,5 @@ bool SimplicialEmbeddingTriMesh::uniform_remeshing(double L, int iterations)
235206
wmtk::logger().info("+++++++++finished+++++++++");
236207
return true;
237208
}
238-
bool SimplicialEmbeddingTriMesh::write_mesh(const std::filesystem::path& filename)
239-
{
240-
const auto V = get_V();
241-
const auto F = get_F();
242-
243-
paraviewo::VTUWriter writer;
244-
// writer.add_field("")
245-
bool r = writer.write_mesh(filename.string(), V, F);
246-
return r;
247-
}
248209

249210
} // namespace wmtk::components::simplicial_embedding

components/simplicial_embedding/wmtk/components/simplicial_embedding/SimplicialEmbeddingTriMesh.hpp

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,6 @@ class SimplicialEmbeddingTriMesh : public wmtk::TriMesh
2626
void set_positions(const Eigen::MatrixXd& V);
2727
void set_num_threads(const int64_t num_threads);
2828

29-
Eigen::MatrixXi get_F() const;
30-
Eigen::MatrixXd get_V() const;
31-
32-
VectorXd position(size_t vid) const { return vertex_attrs[vid].pos; }
33-
std::vector<VectorXd> serialize_vertex_attributes(size_t vid) const
34-
{
35-
const auto& attrs = vertex_attrs[vid];
36-
return {attrs.pos};
37-
}
38-
std::vector<std::string> serialize_vertex_attributes_names() const { return {"position"}; }
39-
4029
struct PositionInfoCache
4130
{
4231
Eigen::Vector2d v1p;
@@ -63,8 +52,6 @@ class SimplicialEmbeddingTriMesh : public wmtk::TriMesh
6352
double compute_edge_cost_split(const TriMesh::Tuple& t, double L) const;
6453
bool split_remeshing(double L);
6554
bool uniform_remeshing(double L, int interations);
66-
67-
bool write_mesh(const std::filesystem::path& filename);
6855
};
6956

7057
} // namespace wmtk::components::simplicial_embedding

components/simplicial_embedding/wmtk/components/simplicial_embedding/tests/test_simplicial_embedding.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,10 @@ TEST_CASE("write", "")
1717
m.create_mesh(VF.F);
1818
m.set_positions(VF.V);
1919

20-
// m.write_mesh("simplicial_embedding_out.vtu");
21-
2220
io::VTUWriter writer(m);
23-
writer.add_vertex_attribute("position2", [&m](int i) -> VectorXd {
24-
return m.vertex_attrs[i].pos;
25-
});
21+
writer.add_vertex_positions([&m](int i) { return m.vertex_attrs[i].pos; });
22+
writer.add_vertex_attribute("position", [&m](int i) { return m.vertex_attrs[i].pos; });
23+
writer.add_vertex_attribute("vid", [&m](int i) { return VectorXd::Constant(1, i); });
24+
writer.add_triangle_attribute("fid", [&m](int i) { return VectorXd::Constant(1, i); });
2625
writer.write_triangles("simplicial_embedding_out.vtu");
2726
}

src/wmtk/TriMesh.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -763,8 +763,13 @@ std::array<wmtk::TriMesh::Tuple, 3> TriMesh::oriented_tri_vertices(
763763

764764
std::array<size_t, 3> TriMesh::oriented_tri_vids(const Tuple& t) const
765765
{
766-
std::array<size_t, 3> incident_verts;
767766
size_t fid = t.fid(*this);
767+
return oriented_tri_vids(fid);
768+
}
769+
770+
std::array<size_t, 3> TriMesh::oriented_tri_vids(const int fid) const
771+
{
772+
std::array<size_t, 3> incident_verts;
768773
auto indices = m_tri_connectivity[fid].m_indices;
769774

770775
incident_verts[0] = indices[0];

src/wmtk/TriMesh.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,7 @@ class TriMesh
539539
* @return global vids of incident vertices
540540
*/
541541
std::array<size_t, 3> oriented_tri_vids(const Tuple& t) const;
542+
std::array<size_t, 3> oriented_tri_vids(const int i) const;
542543

543544
/**
544545
* Generate a face Tuple using global fid

src/wmtk/Types.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,8 @@ using VectorXd = Vector<double, Eigen::Dynamic>;
2424
using Vector2r = Vector<Rational, 2>;
2525
using Vector3r = Vector<Rational, 3>;
2626

27+
using Vector2i = Vector<int, 2>;
28+
using Vector3i = Vector<int, 3>;
29+
using VectorXi = Vector<int, Eigen::Dynamic>;
30+
2731
} // namespace wmtk

src/wmtk/io/VTUWriter.hpp

Lines changed: 107 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -16,102 +16,127 @@ class VTUWriter
1616
public:
1717
VTUWriter(MeshT& mesh);
1818

19-
bool write_triangles(const std::filesystem::path& filename)
20-
{
21-
Eigen::MatrixXi F;
22-
{
23-
const std::vector<Tuple> face_tuples = m_mesh.get_faces();
24-
25-
if (face_tuples.empty()) {
26-
return false; // cannot print mesh without faces
27-
}
28-
29-
F.resize(face_tuples.size(), 3);
30-
for (int i = 0; i < face_tuples.size(); ++i) {
31-
const auto vs = m_mesh.oriented_tri_vids(face_tuples[i]);
32-
for (int j = 0; j < 3; j++) {
33-
F(i, j) = vs[j];
34-
}
35-
}
36-
}
37-
Eigen::MatrixXd V;
38-
std::vector<MatrixXd> VA; // vertex attributes
39-
std::vector<std::string> VA_names;
40-
{
41-
const std::vector<Tuple> vertex_tuples = m_mesh.get_vertices();
42-
43-
if (vertex_tuples.empty()) {
44-
return false; // cannot print mesh without vertices
45-
}
46-
47-
// init VA
48-
VA_names = m_mesh.serialize_vertex_attributes_names();
49-
{
50-
VA.resize(VA_names.size());
51-
const std::vector<VectorXd> v0_attrs = m_mesh.serialize_vertex_attributes(0);
52-
assert(VA_names.size() == v0_attrs.size());
53-
for (int i = 0; i < v0_attrs.size(); ++i) {
54-
VA[i].resize(vertex_tuples.size(), v0_attrs[i].size());
55-
}
56-
}
57-
58-
// init V
59-
V.resize(vertex_tuples.size(), m_mesh.position(0).size());
60-
61-
for (const Tuple& v : vertex_tuples) {
62-
const auto vid = v.vid(m_mesh);
63-
V.row(vid) = m_mesh.position(vid);
64-
65-
const std::vector<VectorXd> attrs = m_mesh.serialize_vertex_attributes(vid);
66-
for (int i = 0; i < attrs.size(); ++i) {
67-
VA[i].row(vid) = attrs[i];
68-
}
69-
}
70-
}
19+
void add_vertex_positions(const std::function<VectorXd(const int)>& f);
7120

72-
paraviewo::VTUWriter writer;
73-
for (int i = 0; i < VA.size(); ++i) {
74-
writer.add_field(VA_names[i], VA[i]);
75-
}
21+
void add_vertex_attribute(const std::string& name, const std::function<VectorXd(const int)>& f);
22+
23+
void add_triangle_attribute(
24+
const std::string& name,
25+
const std::function<VectorXd(const int)>& f);
7626

77-
for (const auto& [name, attr] : m_vertex_attributes) {
78-
writer.add_field(name, attr);
27+
bool write_triangles(const std::filesystem::path& filename);
28+
29+
private:
30+
MeshT& m_mesh;
31+
32+
MatrixXd m_V; // vertex positions
33+
MatrixXi m_E; // edge - vids
34+
MatrixXi m_F; // face - vids
35+
MatrixXi m_T; // tet - vids
36+
37+
std::map<std::string, MatrixXd> m_V_attributes;
38+
std::map<std::string, MatrixXd> m_F_attributes;
39+
};
40+
41+
template <typename MeshT>
42+
inline VTUWriter<MeshT>::VTUWriter(MeshT& mesh)
43+
: m_mesh(mesh)
44+
{
45+
const std::vector<Tuple> face_tuples = m_mesh.get_faces();
46+
47+
if (face_tuples.empty()) {
48+
log_and_throw_error("Cannot print mesh without faces.");
49+
}
50+
51+
m_F.resize(face_tuples.size(), 3);
52+
for (int i = 0; i < face_tuples.size(); ++i) {
53+
const auto vs = m_mesh.oriented_tri_vids(face_tuples[i]);
54+
for (int j = 0; j < 3; j++) {
55+
m_F(i, j) = vs[j];
7956
}
57+
}
58+
}
8059

81-
bool r = writer.write_mesh(filename.string(), V, F);
82-
return r;
60+
template <typename MeshT>
61+
inline bool VTUWriter<MeshT>::write_triangles(const std::filesystem::path& filename)
62+
{
63+
assert(m_F.size() > 0);
64+
65+
if (m_V.size() == 0) {
66+
log_and_throw_error("Must set vertex positions before writing");
8367
}
8468

85-
void add_vertex_attribute(const std::string& name, const std::function<VectorXd(const int)>& f)
86-
{
87-
const std::vector<Tuple> vertex_tuples = m_mesh.get_vertices();
69+
paraviewo::VTUWriter writer;
70+
for (const auto& [name, attr] : m_V_attributes) {
71+
writer.add_field(name, attr);
72+
}
73+
for (const auto& [name, attr] : m_F_attributes) {
74+
writer.add_cell_field(name, attr);
75+
}
8876

89-
if (vertex_tuples.empty()) {
90-
logger().warn("Cannot print mesh without vertices.");
91-
return;
92-
}
77+
bool r = writer.write_mesh(filename.string(), m_V, m_F);
78+
return r;
79+
}
9380

94-
MatrixXd attr;
95-
attr.resize(vertex_tuples.size(), f(0).size());
81+
template <typename MeshT>
82+
inline void VTUWriter<MeshT>::add_vertex_positions(const std::function<VectorXd(const int)>& f)
83+
{
84+
int max_id = m_mesh.vert_capacity();
9685

97-
// init V
98-
for (const Tuple& v : vertex_tuples) {
99-
const auto vid = v.vid(m_mesh);
100-
attr.row(vid) = f(vid);
101-
}
86+
if (max_id < 0) {
87+
log_and_throw_error("Cannot print mesh without vertices.");
88+
}
89+
90+
// init V
91+
m_V.resize(max_id, f(0).size());
10292

103-
m_vertex_attributes[name] = attr;
93+
for (int i = 0; i < max_id; ++i) {
94+
m_V.row(i) = f(i);
10495
}
96+
}
10597

106-
private:
107-
MeshT& m_mesh;
98+
template <typename MeshT>
99+
inline void VTUWriter<MeshT>::add_vertex_attribute(
100+
const std::string& name,
101+
const std::function<VectorXd(const int)>& f)
102+
{
103+
int max_id = m_mesh.vert_capacity();
108104

109-
std::map<std::string, MatrixXd> m_vertex_attributes;
110-
};
105+
if (max_id < 0) {
106+
logger().warn("Cannot add attribute {}.", name);
107+
return;
108+
}
109+
110+
MatrixXd attr;
111+
attr.resize(max_id, f(0).size());
112+
113+
for (int i = 0; i < max_id; ++i) {
114+
attr.row(i) = f(i);
115+
}
116+
117+
m_V_attributes[name] = attr;
118+
}
111119

112120
template <typename MeshT>
113-
inline VTUWriter<MeshT>::VTUWriter(MeshT& mesh)
114-
: m_mesh(mesh)
115-
{}
121+
inline void VTUWriter<MeshT>::add_triangle_attribute(
122+
const std::string& name,
123+
const std::function<VectorXd(const int)>& f)
124+
{
125+
int max_id = m_mesh.tri_capacity();
126+
127+
if (max_id < 0) {
128+
logger().warn("Cannot add attribute {}.", name);
129+
return;
130+
}
131+
132+
MatrixXd attr;
133+
attr.resize(max_id, f(0).size());
134+
135+
for (int i = 0; i < max_id; ++i) {
136+
attr.row(i) = f(i);
137+
}
138+
139+
m_F_attributes[name] = attr;
140+
}
116141

117142
} // namespace wmtk::io

0 commit comments

Comments
 (0)