forked from smufa/diplomska
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
129 lines (116 loc) · 4.39 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include <openvdb/math/Coord.h>
#include <openvdb/openvdb.h>
#include <openvdb/tools/Composite.h>
#include <openvdb/tools/ChangeBackground.h>
#include <openvdb/tools/ValueTransformer.h>
#include <iostream>
#include <sstream>
#include <string>
#include <filesystem>
#include <boost/algorithm/string.hpp>
#include <vector>
#include <algorithm>
#include <thread>
#include <fstream>
namespace fs = std::filesystem;
using std::string, std::vector;
openvdb::FloatGrid::Ptr swapValueCopy(openvdb::FloatGrid::Ptr input, float scale)
{
// struct Local {
// float scale;
// Local(float neki): scale(neki) {}
// inline void op(const openvdb::FloatGrid::ValueAllIter& iter) {
// iter.setValue(*iter * scale);
// }
// };
openvdb::FloatGrid::Ptr output = input->deepCopy();
for (auto i = output->beginValueOn(); i; ++i) {
i.setValue(*i * scale);
}
return output;
}
openvdb::FloatGrid::Ptr combineGrids(vector<openvdb::FloatGrid::Ptr> grids)
{
return std::accumulate(
grids.begin() + 1,
grids.end(),
(*grids.begin())->deepCopy(),
[](auto a, auto b)
{
return openvdb::tools::csgUnionCopy(*a,*b);
});
}
vector<float> getModelColors(string path)
{
using boost::split, boost::is_any_of;
vector<string> strs;
split(strs, path, is_any_of("-"));
path = strs[1].substr(0, strs[1].size()-4);
split(strs, path, is_any_of(","));
vector<float> color(strs.size());
std::transform(strs.begin(), strs.end(), color.begin(), [](string i) {return std::stof(i);});
return color;
}
void vdb2Raw(openvdb::FloatGrid::Ptr grid, string output)
{
openvdb::FloatGrid::ConstAccessor accessor = grid->getConstAccessor();
auto bound_box = grid->evalActiveVoxelBoundingBox();
std::ofstream outputFile(output + std::to_string(bound_box.dim().x()) + "," +
std::to_string(bound_box.dim().y()) + "," +
std::to_string(bound_box.dim().z()) + ".raw", std::ios::binary);
if (!outputFile)
{
std::cout << "Can't write to file" << std::endl;
return;
}
for (auto i = grid->evalActiveVoxelBoundingBox().beginXYZ(); i; ++i)
{
uint8_t value = static_cast<char>(accessor.getValue(*i) * 255);
outputFile << value;
}
}
int main()
{
// Initialize the OpenVDB library
openvdb::initialize();
string model_dir = "./vdb";
string output_dir = "./raw/";
fs::create_directory(output_dir);
int frame = 0;
for (const auto & entry : fs::directory_iterator(model_dir))
{
if (!entry.is_directory()) continue;
std::cout << "Parsing frame " << entry.path().filename() << std::endl;
vector<openvdb::FloatGrid::Ptr> red_grids;
vector<openvdb::FloatGrid::Ptr> blue_grids;
vector<openvdb::FloatGrid::Ptr> green_grids;
for (const auto & frame_dir : fs::directory_iterator(entry.path()))
{
// Parse filename to get color
vector<float> color = getModelColors(frame_dir.path());
// Read file and make r, g, and b grids
openvdb::io::File file(frame_dir.path());
file.open();
openvdb::GridBase::Ptr grid = file.readGrid("density");
file.close();
openvdb::FloatGrid::Ptr volume = openvdb::gridPtrCast<openvdb::FloatGrid>(grid);
// std::cout << color[0] << "," << color[1] << "," << color[2] << std::endl;
// std::stringstream name("./test");
// name << frame_dir.path().filename() << volume->evalActiveVoxelBoundingBox().dim() << ".raw";
// vdb2Raw(volume, name.str());
// red_grids.push_back(volume);
red_grids.push_back(swapValueCopy(volume, color[0]));
green_grids.push_back(swapValueCopy(volume, color[1]));
blue_grids.push_back(swapValueCopy(volume, color[2]));
}
// Combine grids into accumulator
openvdb::FloatGrid::Ptr red = combineGrids(red_grids);
openvdb::FloatGrid::Ptr green = combineGrids(green_grids);
openvdb::FloatGrid::Ptr blue = combineGrids(blue_grids);
string frame_dir = output_dir + entry.path().filename().string();
fs::create_directory(frame_dir);
vdb2Raw(red, frame_dir + "/red");
vdb2Raw(green, frame_dir + "/green");
vdb2Raw(blue, frame_dir + "/blue");
}
}