36#include <gtest/gtest.h>
52static std::string makeTempFilename(
const std::string &prefix)
55 std::ostringstream
ss;
58 tempDir = std::filesystem::temp_directory_path().string();
64 tempDir.push_back(std::filesystem::path::preferred_separator);
67 auto now = std::chrono::high_resolution_clock::now().time_since_epoch().count();
68 auto tidHash = std::hash<std::thread::id>{}(std::this_thread::get_id());
74static void writeVtiAscii(
const std::string &filename,
int nx,
int ny,
int nz,
const std::vector<float> &data)
76 std::ofstream
out(filename.c_str(), std::ios::binary);
79 std::ostringstream
xml;
80 xml <<
"<?xml version=\"1.0\"?>\n";
81 xml <<
"<VTKFile type=\"ImageData\" version=\"0.1\" byte_order=\"LittleEndian\">\n";
82 xml <<
"<ImageData WholeExtent=\"0 " << (nx - 1) <<
" 0 " << (ny - 1) <<
" 0 " << (nz - 1)
83 <<
"\" Origin=\"0 0 0\" Spacing=\"1 1 1\">\n";
84 xml <<
" <Piece Extent=\"0 " << (nx - 1) <<
" 0 " << (ny - 1) <<
" 0 " << (nz - 1) <<
"\">\n";
85 xml <<
" <PointData Scalars=\"scalars\">\n";
86 xml <<
" <DataArray type=\"Float32\" Name=\"scalars\" format=\"ascii\">\n";
87 for (
size_t i = 0;
i < data.size(); ++
i) {
89 if (
i + 1 < data.size())
xml <<
" ";
91 xml <<
"\n </DataArray>\n";
92 xml <<
" </PointData>\n";
93 xml <<
" <CellData/>\n";
95 xml <<
"</ImageData>\n";
96 xml <<
"</VTKFile>\n";
102static void writeVtiAppended(
const std::string &filename,
int nx,
int ny,
int nz,
const std::vector<float> &data)
104 std::ofstream
out(filename.c_str(), std::ios::binary);
107 std::ostringstream
xml;
108 xml <<
"<?xml version=\"1.0\"?>\n";
109 xml <<
"<VTKFile type=\"ImageData\" version=\"0.1\" byte_order=\"LittleEndian\">\n";
110 xml <<
"<ImageData WholeExtent=\"0 " << (nx - 1) <<
" 0 " << (ny - 1) <<
" 0 " << (nz - 1)
111 <<
"\" Origin=\"0 0 0\" Spacing=\"1 1 1\">\n";
112 xml <<
" <Piece Extent=\"0 " << (nx - 1) <<
" 0 " << (ny - 1) <<
" 0 " << (nz - 1) <<
"\">\n";
113 xml <<
" <PointData Scalars=\"scalars\">\n";
115 xml <<
" <DataArray type=\"Float32\" Name=\"scalars\" format=\"appended\" offset=\"0\"/>\n";
116 xml <<
" </PointData>\n";
117 xml <<
" <CellData/>\n";
118 xml <<
" </Piece>\n";
119 xml <<
"</ImageData>\n";
120 xml <<
"<AppendedData encoding=\"raw\">\n";
135 out.write(
reinterpret_cast<const char *
>(
sizeBytes), 4);
138 for (
size_t i = 0;
i < data.size(); ++
i) {
140 out.write(
reinterpret_cast<const char *
>(&v),
sizeof(
float));
143 out <<
"\n</AppendedData>\n";
144 out <<
"</VTKFile>\n";
150 const int nx = 2, ny = 2, nz = 1;
152 std::vector<float> data = {0.5f, 2.0f, 2.0f, 2.0f};
154 std::string fname = makeTempFilename(
"test_ascii_vti");
155 writeVtiAscii(fname, nx, ny, nz, data);
167 std::remove(fname.c_str());
172 const int nx = 2, ny = 2, nz = 1;
173 std::vector<float> data = {0.5f, 2.0f, 2.0f, 2.0f};
175 std::string fname = makeTempFilename(
"test_appended_vti");
176 writeVtiAppended(fname, nx, ny, nz, data);
188 std::remove(fname.c_str());
void setThreshold(double lowerThreshold, double upperThreshold)
std::shared_ptr< T > SPtr
void readMatrixFromVtiASCIIFile(std::string filename)
Reads a VTI file in ASCII format and fills the voxel matrix applying the thresholds....
void readMatrixFromVtiAppendedFile(std::string filename)
Reads a VTI file in appended binary format and fills the voxel matrix applying the thresholds....
TEST(GbVoxelMatrix3DVtiTest, ReadMatrixFromVtiASCIIFile)