Skip to content

Commit 8f5c36b

Browse files
committed
Add different way to write attributes to VTU.
1 parent 61e3618 commit 8f5c36b

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,8 @@ TEST_CASE("write", "")
2020
// m.write_mesh("simplicial_embedding_out.vtu");
2121

2222
io::VTUWriter writer(m);
23+
writer.add_vertex_attribute("position2", [&m](int i) -> VectorXd {
24+
return m.vertex_attrs[i].pos;
25+
});
2326
writer.write_triangles("simplicial_embedding_out.vtu");
2427
}

src/wmtk/io/VTUWriter.hpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#pragma once
22

33
#include <filesystem>
4+
#include <functional>
5+
#include <map>
46
#include <paraviewo/VTUWriter.hpp>
57
#include <wmtk/Types.hpp>
68

@@ -71,12 +73,40 @@ class VTUWriter
7173
for (int i = 0; i < VA.size(); ++i) {
7274
writer.add_field(VA_names[i], VA[i]);
7375
}
76+
77+
for (const auto& [name, attr] : m_vertex_attributes) {
78+
writer.add_field(name, attr);
79+
}
80+
7481
bool r = writer.write_mesh(filename.string(), V, F);
7582
return r;
7683
}
7784

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();
88+
89+
if (vertex_tuples.empty()) {
90+
logger().warn("Cannot print mesh without vertices.");
91+
return;
92+
}
93+
94+
MatrixXd attr;
95+
attr.resize(vertex_tuples.size(), f(0).size());
96+
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+
}
102+
103+
m_vertex_attributes[name] = attr;
104+
}
105+
78106
private:
79107
MeshT& m_mesh;
108+
109+
std::map<std::string, MatrixXd> m_vertex_attributes;
80110
};
81111

82112
template <typename MeshT>

0 commit comments

Comments
 (0)