@@ -16,102 +16,127 @@ class VTUWriter
1616public:
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
112120template <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