-
Notifications
You must be signed in to change notification settings - Fork 4
/
vdbvolume.cpp
executable file
·184 lines (151 loc) · 7.03 KB
/
vdbvolume.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
* OpenVDB Data Source for Mitsuba Render
* Copyright (C) 2014 Bo Zhou<[email protected]>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "vdbvolume.h"
#include <openvdb/openvdb.h>
#include <openvdb/math/BBox.h>
#include <openvdb/math/Ray.h>
#include <openvdb/tools/Interpolation.h>
struct VdbGridSet
{
std::map<std::string, openvdb::GridBase::Ptr > m_grids;
};
struct VdbGridPool
{
std::map<std::string, VdbGridSet > m_gridSets;
};
static VdbGridPool s_vdbGridPool ;
MTS_NAMESPACE_BEGIN
VdbDataSource::VdbDataSource(const Properties &props)
:
VolumeDataSource(props),
m_customStepSize(0.0f) {
openvdb::initialize();
//
m_filename = props.getString("filename");
m_fieldname = props.getString("fieldname");
if( props.hasProperty("customStepSize") ) {
m_customStepSize = props.getFloat("customStepSize");
}
std::map<std::string, VdbGridSet>::iterator gridSetItr = s_vdbGridPool.m_gridSets.find(m_filename);
if (gridSetItr == s_vdbGridPool.m_gridSets.end()) {
if (open()) {
gridSetItr = s_vdbGridPool.m_gridSets.find(m_filename);
std::map<std::string, openvdb::GridBase::Ptr >::iterator gridItr = gridSetItr->second.m_grids.find(m_fieldname);
if (gridItr == gridSetItr->second.m_grids.end()) {
Log(EError, "Can't get the specific field [%s] from [%s] to read.", m_fieldname.c_str(), m_filename.c_str());
}
}
else {
Log(EError, "Can't open the file [%s].", m_filename.c_str());
}
}
else if (gridSetItr->second.m_grids.find(m_fieldname) == gridSetItr->second.m_grids.end()) {
Log(EError, "Opened the file [%s] but can't get the field [%s].", m_filename.c_str(), m_fieldname.c_str());
}
}
VdbDataSource::VdbDataSource(Stream *stream, InstanceManager *manager)
:
VolumeDataSource(stream, manager) {
m_filename = stream->readString();
m_fieldname = stream->readString();
m_customStepSize = stream->readFloat();
}
VdbDataSource::~VdbDataSource() {
}
bool VdbDataSource::supportsFloatLookups() const {
return true;
}
Float VdbDataSource::lookupFloat(const Point &p) const {
Float data = 0.0f;
std::map<std::string, VdbGridSet >::const_iterator gridSetItr = s_vdbGridPool.m_gridSets.find(m_filename);
if (gridSetItr != s_vdbGridPool.m_gridSets.end()) {
std::map<std::string, openvdb::GridBase::Ptr >::const_iterator gridItr = gridSetItr->second.m_grids.find(m_fieldname);
if (gridItr != gridSetItr->second.m_grids.end()) {
const openvdb::FloatGrid::Ptr &floatGrid = openvdb::gridPtrCast<openvdb::FloatGrid>(gridItr->second);
openvdb::tools::GridSampler<openvdb::FloatTree, openvdb::tools::BoxSampler> interpolator(floatGrid->constTree() , floatGrid->transform());
data = interpolator.wsSample(openvdb::math::Vec3d(p.x, p.y, p.z));
}
}
return data;
}
bool VdbDataSource::supportsVectorLookups() const {
return false;
}
void VdbDataSource::serialize(Stream *stream, InstanceManager *manager) const {
VolumeDataSource::serialize(stream, manager);
stream->writeString(m_filename);
stream->writeString(m_fieldname);
stream->writeFloat(m_customStepSize);
}
Float VdbDataSource::getStepSize() const {
Float stepSize = 1.0f;
if ( m_customStepSize > 0.0f ) {
stepSize = m_customStepSize;
}
std::map<std::string, VdbGridSet >::const_iterator gridSetItr = s_vdbGridPool.m_gridSets.find(m_filename);
if (gridSetItr != s_vdbGridPool.m_gridSets.end()) {
std::map<std::string, openvdb::GridBase::Ptr >::const_iterator gridItr = gridSetItr->second.m_grids.find(m_fieldname);
if (gridItr != gridSetItr->second.m_grids.end()) {
const openvdb::math::Vec3d &voxelSize = gridItr->second->constTransform().voxelSize();
stepSize = std::min(std::min(voxelSize.x(), voxelSize.y()) , voxelSize.z()) * 0.5;
}
}
return stepSize;
}
Float VdbDataSource::getMaximumFloatValue() const {
Float valMax = - 1.0f;
std::map<std::string, VdbGridSet >::const_iterator gridSetItr = s_vdbGridPool.m_gridSets.find(m_filename);
if (gridSetItr != s_vdbGridPool.m_gridSets.end()) {
std::map<std::string, openvdb::GridBase::Ptr >::const_iterator gridItr = gridSetItr->second.m_grids.find(m_fieldname);
if (gridItr != gridSetItr->second.m_grids.end()) {
const openvdb::FloatGrid::Ptr &floatGrid = openvdb::gridPtrCast<openvdb::FloatGrid>(gridItr->second);
Float valMin = 0.0f;
floatGrid->evalMinMax(valMin, valMax);
}
}
return valMax;
}
bool VdbDataSource::open() {
bool success = false ;
try {
boost::scoped_ptr< openvdb::io::File > file(new openvdb::io::File(m_filename));
if (file->open()) {
s_vdbGridPool.m_gridSets.insert(std::make_pair(m_filename, VdbGridSet()));
std::map<std::string, VdbGridSet>::iterator gridSetItr = s_vdbGridPool.m_gridSets.find(m_filename);
//
openvdb::math::Vec3d bbMin = openvdb::math::Vec3d( std::numeric_limits< double >::max());
openvdb::math::Vec3d bbMax = openvdb::math::Vec3d(- std::numeric_limits< double >::max());
openvdb::GridPtrVecPtr grids = file->readAllGridMetadata() ;
for (openvdb::GridPtrVec::const_iterator itr = grids->begin(); itr != grids->end(); ++ itr) {
const openvdb::Vec3i & bbMinI = (*itr)->metaValue<openvdb::Vec3i>(openvdb::GridBase::META_FILE_BBOX_MIN);
const openvdb::Vec3i & bbMaxI = (*itr)->metaValue<openvdb::Vec3i>(openvdb::GridBase::META_FILE_BBOX_MAX);
bbMin = openvdb::math::minComponent(bbMin , (*itr)->indexToWorld(bbMinI));
bbMax = openvdb::math::maxComponent(bbMax , (*itr)->indexToWorld(bbMaxI));
const std::string &fieldname = (*itr)->getName();
gridSetItr->second.m_grids.insert(std::make_pair(fieldname, file->readGrid((*itr)->getName())));
}
m_aabb.min = Point(bbMin.x(), bbMin.y(), bbMin.z());
m_aabb.max = Point(bbMax.x(), bbMax.y(), bbMax.z());
success = true;
}
}
catch(const std::exception &e) {
std::cerr << e.what() << std::endl;
}
catch(...) {
}
return success ;
}
MTS_NAMESPACE_END