Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions include/lean_vtk.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <iostream>
#include <string>
#include <vector>
#include <tuple>

namespace leanvtk {
inline int index(int N, int i, int j) {
Expand Down Expand Up @@ -82,6 +83,14 @@ template <typename T> class VTKDataNode {

class VTUWriter {
public:

void add_field_data(const std::string& type,
const std::string& name,
const int& value);
void add_field_data(const std::string& type,
const std::string& name,
const double& value);

/**
* Write surface mesh to a file
* const string& path filename to store vtk mesh (ending with .vtu)
Expand Down Expand Up @@ -341,6 +350,9 @@ class VTUWriter {
void clear();

private:
typedef std::tuple<std::string,std::string,int,double> FieldDataTuple;

std::vector<FieldDataTuple> field_data_;
std::vector<VTKDataNode<double>> point_data_;
std::vector<VTKDataNode<double>> cell_data_;
std::string current_scalar_point_data_;
Expand All @@ -356,6 +368,8 @@ class VTUWriter {
void write_header(const int n_vertices, const int n_elements,
std::ostream &os);

void write_field_data(std::ostream &os);

void write_footer(std::ostream &os);

bool write_mesh(std::ostream &os, const int dim, const int cell_size,
Expand Down
53 changes: 53 additions & 0 deletions src/lean_vtk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,62 @@ void VTUWriter::write_header(const int n_vertices, const int n_elements,
std::ostream &os) {
os << "<VTKFile type=\"UnstructuredGrid\" version=\"1.0\">\n";
os << "<UnstructuredGrid>\n";
write_field_data(os);
os << "<Piece NumberOfPoints=\"" << n_vertices << "\" NumberOfCells=\""
<< n_elements << "\">\n";
}

//
// per the VTK wiki, at https://vtk.org/Wiki/VTK/Tutorials/DataStorage
//
// "Arrays attached to the FieldData of a dataset describe global properties of
// the data. That is, if you want to save the time at which the data were
// recorded, you would put that value in the FieldData. If you wanted to name
// the data set, you would also put that in the FieldData. There are no
// restrictions about the length of arrays that are added to the FieldData."
//
void VTUWriter::add_field_data(const std::string& type,
const std::string& name,
const int& value) {
VTUWriter::FieldDataTuple t;
std::get<0>(t)=type;
std::get<1>(t)=name;
std::get<2>(t)=value;
field_data_.push_back(t);
}
void VTUWriter::add_field_data(const std::string& type,
const std::string& name,
const double& value) {
VTUWriter::FieldDataTuple t;
std::get<0>(t)=type;
std::get<1>(t)=name;
std::get<3>(t)=value;
field_data_.push_back(t);
}
void VTUWriter::write_field_data(std::ostream &os) {
if (field_data_.size()==0) {
return;
}
os << "<FieldData>\n";
for (auto t: field_data_) {
os << "<DataArray ";
std::string type=std::get<0>(t);
os << "type=\"" << type << "\" ";
os << "Name=\"" << std::get<1>(t) << "\" ";
os << "NumberOfTuples=\"1\" format=\"ascii\">\n";
// the data itself
if (type.find("Int")!=std::string::npos) {
os << std::get<2>(t) << "\n";
} else if (type.find("Float")!=std::string::npos) {
os << std::get<3>(t) << "\n";
} else {
os << "ERROR: field data type does not contain \"Int\" or \"Float\"\n";
}
os << "</DataArray>\n";
}
os << "</FieldData>\n";
}

void VTUWriter::write_footer(std::ostream &os) {
os << "</Piece>\n";
os << "</UnstructuredGrid>\n";
Expand Down Expand Up @@ -180,6 +232,7 @@ void VTUWriter::write_cell_data(std::ostream &os) {
os << "</CellData>\n";
}
void VTUWriter::clear() {
field_data_.clear();
point_data_.clear();
cell_data_.clear();
}
Expand Down
6 changes: 5 additions & 1 deletion tests/test_lean_vtk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ TEST_CASE("Single elements", "[single]"){
int cell_size;
std::string filename;
bool is_volume_mesh = false;

VTUWriter writer;

SECTION("Triangle 2D"){
points = {
1., 1.,
Expand Down Expand Up @@ -112,6 +115,8 @@ TEST_CASE("Single elements", "[single]"){
filename = "single_quad.vtu";
}
SECTION("Hex element"){
writer.add_field_data("Int32","CYCLE",123);
writer.add_field_data("Float64","TIME",4.56);
points = {
1., 1., 1.,
1., -1., 1.,
Expand Down Expand Up @@ -156,7 +161,6 @@ TEST_CASE("Single elements", "[single]"){
filename = "single_tet.vtu";
is_volume_mesh = true;
}
VTUWriter writer;

writer.add_scalar_field("scalar_field", scalar_field);
writer.add_vector_field("vector_field", vector_field, dim);
Expand Down