/
floatgrid.cpp
126 lines (111 loc) · 3.04 KB
/
floatgrid.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
// Ariel: FLIP Fluid Simulator
// Written by Yining Karl Li
//
// File: floatgrid.cpp
// Implements floatgrid.hpp
#include "floatgrid.hpp"
using namespace fluidCore;
using namespace utilityCore;
floatgrid::floatgrid(){
openvdb::initialize();
vdbgrid = openvdb::FloatGrid::create(0.0f);
type = VDB;
}
floatgrid::floatgrid(const gridtype& type, const vec3& dimensions, const int& background){
this->type = type;
this->dimensions = dimensions;
this->background = background;
if(type==VDB){
openvdb::initialize();
vdbgrid = openvdb::FloatGrid::create(background);
}else if(type==RAW){
rawgrid = createGrid<float>(dimensions.x+1, dimensions.y+1, dimensions.z+1);
#pragma omp parallel for
for(int i=0; i<(int)dimensions.x+1; i++){
for(int j=0; j<(int)dimensions.y+1; j++){
for(int k=0; k<(int)dimensions.z+1; k++){
rawgrid[i][j][k] = background;
}
}
}
}
}
floatgrid::floatgrid(openvdb::FloatGrid::Ptr newgrid){
vdbgrid = newgrid;
type = VDB;
}
floatgrid::~floatgrid(){
if(type==RAW){
deleteGrid<float>(rawgrid, dimensions.x+1, dimensions.y+1, dimensions.z+1);
}else if(type==VDB){
vdbgrid->clear();
vdbgrid.reset();
}
}
float floatgrid::getCell(const vec3& index){
return getCell((int)index.x, (int)index.y, (int)index.z);
}
float floatgrid::getCell(const int& x, const int& y, const int& z){
float cell = background;
if(type==VDB){
openvdb::Coord coord = openvdb::Coord(x,y,z);
openvdb::FloatGrid::Accessor accessor = vdbgrid->getAccessor();
cell = accessor.getValue(coord);
}else if(type==RAW){
cell = rawgrid[x][y][z];
}
return cell;
}
void floatgrid::setCell(const vec3& index, const float& value){
setCell((int)index.x, (int)index.y, (int)index.z, value);
}
void floatgrid::setCell(const int& x, const int& y, const int& z, const float& value){
if(type==VDB){
#pragma omp critical
{
openvdb::Coord coord = openvdb::Coord(x,y,z);
openvdb::FloatGrid::Accessor accessor = vdbgrid->getAccessor();
accessor.setValue(coord, value);
}
}else if(type==RAW){
rawgrid[x][y][z] = value;
}
}
float floatgrid::getInterpolatedCell(const vec3& index){
return getInterpolatedCell(index.x, index.y, index.z);
}
float floatgrid::getInterpolatedCell(const float& x, const float& y, const float& z){
float value;
#pragma omp critical
{
openvdb::Vec3f p(x,y,z);
openvdb::tools::GridSampler<openvdb::FloatTree, openvdb::tools::BoxSampler> interpolator(
vdbgrid->constTree(), vdbgrid->transform());
value = interpolator.wsSample(p);
}
return value;
}
openvdb::FloatGrid::Ptr& floatgrid::getVDBGrid(){
return vdbgrid;
}
void floatgrid::writeVDBGridToFile(string filename){
openvdb::io::File file(filename);
openvdb::GridPtrVec grids;
grids.push_back(vdbgrid);
file.write(grids);
file.close();
}
void floatgrid::clear(){
if(type==VDB){
vdbgrid->clear();
}else if(type==RAW){
#pragma omp parallel for
for(int i=0; i<(int)dimensions.x+1; i++){
for(int j=0; j<(int)dimensions.y+1; j++){
for(int k=0; k<(int)dimensions.z+1; k++){
rawgrid[i][j][k] = background;
}
}
}
}
}