Wildmeshing Toolkit
ParaviewWriter.cpp
Go to the documentation of this file.
1 #include "ParaviewWriter.hpp"
2 
3 
4 #include <wmtk/Mesh.hpp>
5 #include <wmtk/utils/Logger.hpp>
7 
8 #include <paraviewo/VTUWriter.hpp>
9 // #include <paraviewo/HDF5VTUWriter.hpp>
10 
11 #include <sstream>
12 
13 namespace wmtk::io {
14 
16 {
17  m_paraview_file = std::make_shared<paraviewo::VTUWriter>();
18  // m_paraview_file = std::make_shared<paraviewo::HDF5VTUWriter>();
19 }
20 
22  const std::filesystem::path& filename,
23  const std::string& vertices_name,
24  const Eigen::MatrixXi& elements,
25  const bool enabled)
26 {
27  m_vertices_name = vertices_name;
28  m_elements = elements;
29 
30  m_filename = filename;
31  m_enabled = enabled;
32 }
33 
35 {
36  if (m_enabled) m_paraview_file->write_mesh(m_filename.string(), m_vertices, m_elements);
37 }
38 
39 
41  const std::string& name,
42  const int64_t stride,
43  const std::vector<double>& val)
44 {
45  Eigen::MatrixXd tmp =
46  Eigen::Map<const Eigen::MatrixXd>(&val[0], stride, val.size() / stride).transpose();
47 
48  if (stride == 1 || stride == 2 || stride == 3) {
49  m_paraview_file->add_cell_field(name, tmp);
50  } else if (stride % 3 == 0) {
51  for (int64_t i = 0; i < stride; i += 3) {
52  m_paraview_file->add_cell_field(
53  name + "_" + std::to_string(i / 3),
54  tmp.block(0, i, tmp.rows(), 3));
55  }
56  } else if (stride % 2 == 0) {
57  for (int64_t i = 0; i < stride; i += 2) {
58  m_paraview_file->add_cell_field(
59  name + "_" + std::to_string(i / 2),
60  tmp.block(0, i, tmp.rows(), 2));
61  }
62  } else {
63  for (int64_t i = 0; i < stride; ++i) {
64  m_paraview_file->add_cell_field(name + "_" + std::to_string(i), tmp.col(i));
65  }
66  }
67 }
68 
70  const std::filesystem::path& filename,
71  const std::string& vertices_name,
72  const Mesh& mesh,
73  bool write_points,
74  bool write_edges,
75  bool write_faces,
76  bool write_tetrahedra)
77  : m_vertices_name(vertices_name)
78 {
79  m_enabled[0] = write_points;
80  m_enabled[1] = write_edges;
81  m_enabled[2] = write_faces;
82  m_enabled[3] = write_tetrahedra;
83 
84  std::array<Eigen::MatrixXi, 4> cells;
85 
86  for (size_t i = 0; i < 4; ++i) {
87  const auto pt = PrimitiveType(i);
88  if (m_enabled[i]) {
89  // include deleted tuples so that attributes are aligned
90  const auto tuples = mesh.get_all(pt, true);
91  cells[i].resize(tuples.size(), i + 1);
92 
93  for (size_t j = 0; j < tuples.size(); ++j) {
94  const auto& t = tuples[j];
95  if (t.is_null()) {
96  for (size_t d = 0; d < cells[i].cols(); ++d) {
97  cells[i](j, d) = 0;
98  }
99  } else {
100  int64_t vid = mesh.id(t, PrimitiveType::Vertex);
101  cells[i](j, 0) = vid;
102  if (i > 0) {
103  auto t1 = mesh.switch_tuple(t, PrimitiveType::Vertex);
104 
105  cells[i](j, 1) = mesh.id(t1, PrimitiveType::Vertex);
106  }
107  if (i > 1) {
108  auto t1 = mesh.switch_tuple(t, PrimitiveType::Edge);
109  auto t2 = mesh.switch_tuple(t1, PrimitiveType::Vertex);
110 
111  cells[i](j, 2) = mesh.id(t2, PrimitiveType::Vertex);
112  }
113  if (i > 2) {
114  auto t1 = mesh.switch_tuple(t, PrimitiveType::Triangle);
115  auto t2 = mesh.switch_tuple(t1, PrimitiveType::Edge);
116  auto t3 = mesh.switch_tuple(t2, PrimitiveType::Vertex);
117 
118  cells[i](j, 3) = mesh.id(t3, PrimitiveType::Vertex);
119  }
120  }
121  }
122  }
123  }
124 
125  m_writers[0].init(filename.string() + "_verts.vtu", vertices_name, cells[0], m_enabled[0]);
126  m_writers[1].init(filename.string() + "_edges.vtu", vertices_name, cells[1], m_enabled[1]);
127  m_writers[2].init(filename.string() + "_faces.vtu", vertices_name, cells[2], m_enabled[2]);
128  m_writers[3].init(filename.string() + "_tets.vtu", vertices_name, cells[3], m_enabled[3]);
129 }
130 
132  const std::string& name,
133  const int64_t type,
134  const int64_t stride,
135  const std::vector<int64_t>& val,
136  const int64_t default_val)
137 {
138  std::vector<double> tmp;
139  tmp.reserve(val.size());
140  for (const auto& v : val) tmp.push_back(v);
141 
142  write_internal(name, type, stride, tmp);
143 }
144 
146  const std::string& name,
147  const int64_t type,
148  const int64_t stride,
149  const std::vector<double>& val,
150  const double default_val)
151 {
152  write_internal(name, type, stride, val);
153 }
154 
156  const std::string& name,
157  const int64_t type,
158  const int64_t stride,
159  const std::vector<char>& val,
160  const char default_val)
161 {
162  std::vector<double> tmp;
163  tmp.reserve(val.size());
164  for (const auto& v : val) tmp.push_back(v);
165 
166  write_internal(name, type, stride, tmp);
167 }
168 
169 
171  const std::string& name,
172  const int64_t type,
173  const int64_t stride,
174  const std::vector<Rational>& val,
175  const Rational& default_val)
176 {
177  std::vector<double> tmp;
178  tmp.reserve(val.size());
179  for (const auto& v : val) tmp.push_back(double(v));
180 
181  write_internal(name, type, stride, tmp);
182 }
183 
185  const std::string& name,
186  const int64_t type,
187  const int64_t stride,
188  const std::vector<double>& val)
189 {
190  if (!m_write) return;
191 
192  if (name == m_vertices_name) {
193  assert(stride == 2 || stride == 3);
194 
195  Eigen::MatrixXd V =
196  Eigen::Map<const Eigen::MatrixXd>(&val[0], stride, val.size() / stride).transpose();
197 
198  for (int i = 0; i < m_writers.size(); ++i) {
199  if (m_enabled[i]) m_writers[i].vertices() = V;
200  }
201 
202  } else if (m_enabled[type])
203  m_writers[type].write(name, stride, val);
204 }
205 
206 
207 } // namespace wmtk::io
int64_t id(const Tuple &tuple, PrimitiveType type) const
return the global id of the Tuple of the given dimension
Definition: Mesh.hpp:1020
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
void write(const std::string &name, const int64_t stride, const std::vector< double > &val)
std::shared_ptr< paraviewo::ParaviewWriter > m_paraview_file
void init(const std::filesystem::path &filename, const std::string &vertices_name, const Eigen::MatrixXi &elements, const bool enabled)
ParaviewWriter(const std::filesystem::path &filename, const std::string &vertices_name, const Mesh &mesh, bool write_points=true, bool write_edges=true, bool write_faces=true, bool write_tetrahedra=true)
std::array< ParaviewInternalWriter, 4 > m_writers
bool write(const int dim) override
std::array< bool, 4 > m_enabled
void write_internal(const std::string &name, const int64_t type, const int64_t stride, const std::vector< double > &val)