Bembel
 
Loading...
Searching...
No Matches
VTKPointExport.hpp
1// This file is part of Bembel, the higher order C++ boundary element library.
2//
3// Copyright (C) 2023 see <http://www.bembel.eu>
4//
5// It was written as part of a cooperation of J. Doelz, H. Harbrecht, S. Kurz,
6// M. Multerer, S. Schoeps, and F. Wolf at Technische Universitaet Darmstadt,
7// Universitaet Basel, and Universita della Svizzera italiana, Lugano. This
8// source code is subject to the GNU General Public License version 3 and
9// provided WITHOUT ANY WARRANTY, see <http://www.bembel.eu> for further
10// information.
11
12#ifndef BEMBEL_SRC_IO_VTKPOINTEXPORT_HPP_
13#define BEMBEL_SRC_IO_VTKPOINTEXPORT_HPP_
14
15namespace Bembel {
16
17// This class provides the possibility to generate a vtk-visualization.
19 public:
26 const Eigen::Matrix<double, Eigen::Dynamic, 3> &points) {
27 init_VTKPointExport(points);
28 }
29
30 inline void init_VTKPointExport(
31 const Eigen::Matrix<double, Eigen::Dynamic, 3> &points) {
32 points_ = points;
33 point_number_ = Eigen::VectorXi(points_.rows());
34 for (int i = 0; i < points_.rows(); ++i) point_number_(i) = i;
35 return;
36 }
37
38 // This routine turns the data of the above DataSet-routines into a string and
39 // stores it.
40 inline void addDataSet(const std::string &name, const Eigen::MatrixXd &mat) {
41 assert(mat.cols() == 1 || mat.cols() == 3);
42
43 const int cols = mat.cols();
44 std::string data_ascii = "<DataArray type=\"Float32\" Name=\"" + name +
45 "\" NumberOfComponents=\"" +
46 std::to_string(mat.cols()) +
47 "\" format=\"ascii\">\n";
48 for (int i = 0; i < mat.rows(); ++i) {
49 for (int j = 0; j < cols; ++j) {
50 data_ascii.append(std::to_string(mat(i, j)) + " ");
51 }
52 data_ascii.append("\n");
53 }
54 data_ascii.append("</DataArray>\n");
55 additionalData_.push_back(data_ascii);
56 }
57 inline void addComplexDataSet(const std::string &name,
58 const Eigen::MatrixXcd &mat) {
59 addDataSet(name + std::string("_real"), mat.real());
60 addDataSet(name + std::string("_imag"), mat.imag());
61 }
62
63 inline void writeToFile(const std::string &filename) {
64 std::ofstream output;
65 output.open(filename);
66 output << "<VTKFile type=\"PolyData\" version=\"0.1\" "
67 "byte_order=\"LittleEndian\">\n"
68 "<PolyData>\n"
69 "<Piece NumberOfPoints=\" "
70 << points_.rows()
71 << "\" NumberOfVerts=\"0\" NumberOfLines=\"0\" NumberOfStrips=\"0\" "
72 "NumberOfPolys=\"0\">\n"
73 "<Points>\n"
74 "<DataArray type=\"Float32\" NumberOfComponents=\"3\" "
75 "format=\"ascii\">\n";
76 output << points_;
77 output << "\n</DataArray>\n</Points>\n";
78 output << "<PointData Scalars=\"number\">\n"
79 "<DataArray type=\"Int32\" Name=\"number\" "
80 "format=\"ascii\">\n";
81 output << point_number_;
82 output << "</DataArray>\n";
83 for (auto data : additionalData_) {
84 output << data;
85 }
86 output << "</PointData>\n"
87 "</Piece>\n"
88 "</PolyData>\n"
89 "</VTKFile>";
90 output.close();
91 return;
92 }
93
94 // can be used to clear the additional Data.
95 inline void clearData() {
96 additionalData_ = {};
97 additionalData_.shrink_to_fit();
98 }
99
100 private:
101 Eigen::Matrix<double, Eigen::Dynamic, 3> points_;
102 Eigen::VectorXi point_number_;
103 std::vector<std::string> additionalData_;
104}; // namespace Bembel
105
106} // namespace Bembel
107
108#endif // BEMBEL_SRC_IO_VTKPOINTEXPORT_HPP_
VTKPointExport(const Eigen::Matrix< double, Eigen::Dynamic, 3 > &points)
Provides export routines to the VTK file format.
Routines for the evalutation of pointwise errors.
constexpr int getFunctionSpaceOutputDimension()