22 #ifndef NANOVDB_NANOTOOPENVDB_H_HAS_BEEN_INCLUDED
23 #define NANOVDB_NANOTOOPENVDB_H_HAS_BEEN_INCLUDED
52 template<
typename NanoBuildT>
54 nanoToOpenVDB(
const NanoGrid<NanoBuildT>& grid,
int verbose = 0);
57 template<
typename BufferT>
58 openvdb::GridBase::Ptr
62 template<
typename NanoBuildT>
74 using OpenNode0 = openvdb::tree::LeafNode<OpenBuildT, NanoNode0::LOG2DIM>;
75 using OpenNode1 = openvdb::tree::InternalNode<OpenNode0, NanoNode1::LOG2DIM>;
76 using OpenNode2 = openvdb::tree::InternalNode<OpenNode1, NanoNode2::LOG2DIM>;
77 using OpenRootT = openvdb::tree::RootNode<OpenNode2>;
78 using OpenTreeT = openvdb::tree::Tree<OpenRootT>;
79 using OpenGridT = openvdb::Grid<OpenTreeT>;
91 template<
typename NanoNodeT,
typename OpenNodeT>
92 OpenNodeT* processNode(
const NanoNodeT*);
94 OpenNode2* process(
const NanoNode2* node) {
return this->
template processNode<NanoNode2, OpenNode2>(node);}
95 OpenNode1* process(
const NanoNode1* node) {
return this->
template processNode<NanoNode1, OpenNode1>(node);}
97 template <
typename NanoLeafT>
105 process(
const NanoLeafT* node);
107 template <
typename NanoLeafT>
113 process(
const NanoLeafT* node);
115 template <
typename NanoLeafT>
118 process(
const NanoLeafT* node);
120 template <
typename NanoLeafT>
123 process(
const NanoLeafT* node);
126 static const OpenValueT& Convert(
const NanoValueT &
v) {
return reinterpret_cast<const OpenValueT&
>(
v);}
127 static const OpenValueT* Convert(
const NanoValueT *
v) {
return reinterpret_cast<const OpenValueT*
>(
v);}
131 template<
typename NanoBuildT>
132 typename NanoToOpenVDB<NanoBuildT>::OpenGridT::Ptr
138 auto dstGrid = openvdb::createGrid<OpenGridT>(Convert(srcGrid->
tree().background()));
139 dstGrid->setName(srcGrid->
gridName());
151 throw std::runtime_error(
"NanoToOpenVDB does not yet support PointIndexGrids");
153 throw std::runtime_error(
"NanoToOpenVDB does not yet support PointDataGrids");
158 const nanovdb::Map& nanoMap =
reinterpret_cast<const GridData*
>(srcGrid)->mMap;
159 auto mat = openvdb::math::Mat4<double>::identity();
160 mat.setMat3(openvdb::math::Mat3<double>(nanoMap.
mMatD));
162 mat.setTranslation(openvdb::math::Vec3<double>(nanoMap.
mVecD));
163 dstGrid->setTransform(openvdb::math::Transform::createLinearTransform(mat));
166 auto &root = dstGrid->tree().root();
167 auto *
data = srcGrid->
tree().root().data();
168 for (uint32_t i=0; i<
data->mTableSize; ++i) {
169 auto *tile =
data->tile(i);
170 if (tile->isChild()) {
171 root.addChild( this->process(
data->getChild(tile)) );
173 root.addTile(tile->origin(), Convert(tile->value), tile->state);
181 template<
typename SrcNodeT,
typename DstNodeT>
185 DstNodeT *dstNode =
new DstNodeT();
186 dstNode->setOrigin(srcNode->origin());
187 const auto& childMask = srcNode->childMask();
188 const_cast<typename DstNodeT::NodeMaskType&
>(dstNode->getValueMask()) = srcNode->valueMask();
189 const_cast<typename DstNodeT::NodeMaskType&
>(dstNode->getChildMask()) = childMask;
190 auto* dstTable =
const_cast<typename DstNodeT::UnionType*
>(dstNode->getTable());
191 auto* srcData = srcNode->data();
192 std::vector<std::pair<uint32_t, const typename SrcNodeT::ChildNodeType*>> childNodes;
193 const auto childCount = childMask.countOn();
194 childNodes.reserve(childCount);
195 for (uint32_t
n = 0;
n < DstNodeT::NUM_VALUES; ++
n) {
196 if (childMask.isOn(
n)) {
197 childNodes.emplace_back(
n, srcData->getChild(
n));
199 dstTable[
n].setValue(Convert(srcData->mTable[
n].value));
202 auto kernel = [&](
const auto&
r) {
203 for (
auto i =
r.begin(); i !=
r.end(); ++i) {
204 auto &p = childNodes[i];
205 dstTable[p.first].setChild( this->process(p.second) );
210 kernel(
Range1D(0, childCount));
212 forEach(0, childCount, 1, kernel);
218 template <
typename NanoLeafT>
225 typename NanoToOpenVDB<T>::OpenNode0*>
::type
229 OpenNode0* dstNode =
new OpenNode0();
230 dstNode->setOrigin(srcNode->origin());
231 dstNode->setValueMask(srcNode->valueMask());
233 const auto*
src = Convert(srcNode->data()->mValues);
245 template <
typename NanoLeafT>
250 typename NanoToOpenVDB<T>::OpenNode0*>
::type
254 OpenNode0* dstNode =
new OpenNode0();
255 dstNode->setOrigin(srcNode->origin());
256 dstNode->setValueMask(srcNode->valueMask());
257 float *
dst = dstNode->buffer().data();
258 for (
int i=0; i!=512; i+=4) {
259 *dst++ = srcNode->getValue(i);
260 *dst++ = srcNode->getValue(i+1);
261 *dst++ = srcNode->getValue(i+2);
262 *dst++ = srcNode->getValue(i+3);
269 template <
typename NanoLeafT>
271 typename NanoToOpenVDB<T>::OpenNode0*>
::type
275 OpenNode0* dstNode =
new OpenNode0();
276 dstNode->setOrigin(srcNode->origin());
277 dstNode->setValueMask(srcNode->valueMask());
283 template <
typename NanoLeafT>
285 typename NanoToOpenVDB<T>::OpenNode0*>
::type
289 OpenNode0* dstNode =
new OpenNode0();
290 dstNode->setOrigin(srcNode->origin());
291 dstNode->setValueMask(srcNode->valueMask());
292 reinterpret_cast<openvdb::util::NodeMask<3>&
>(dstNode->buffer()) = srcNode->data()->mValues;
297 template<
typename NanoBuildT>
302 return tmp(grid, verbose);
305 template<
typename BufferT>
306 openvdb::GridBase::Ptr
309 if (
auto grid = handle.template grid<float>(n)) {
311 }
else if (
auto grid = handle.template grid<double>(n)) {
313 }
else if (
auto grid = handle.template grid<int32_t>(n)) {
315 }
else if (
auto grid = handle.template grid<int64_t>(n)) {
317 }
else if (
auto grid = handle.template grid<bool>(n)) {
319 }
else if (
auto grid = handle.template grid<nanovdb::Fp4>(n)) {
321 }
else if (
auto grid = handle.template grid<nanovdb::Fp8>(n)) {
323 }
else if (
auto grid = handle.template grid<nanovdb::Fp16>(n)) {
325 }
else if (
auto grid = handle.template grid<nanovdb::FpN>(n)) {
327 }
else if (
auto grid = handle.template grid<nanovdb::ValueMask>(n)) {
329 }
else if (
auto grid = handle.template grid<nanovdb::Vec3f>(n)) {
331 }
else if (
auto grid = handle.template grid<nanovdb::Vec3d>(n)) {
333 }
else if (
auto grid = handle.template grid<nanovdb::Vec4f>(n)) {
335 }
else if (
auto grid = handle.template grid<nanovdb::Vec4d>(n)) {
338 OPENVDB_THROW(openvdb::RuntimeError,
"Unsupported NanoVDB grid type!");
344 #endif // NANOVDB_NANOTOOPENVDB_H_HAS_BEEN_INCLUDED
OpenGridT::Ptr operator()(const NanoGrid< NanoBuildT > &grid, int verbose=0)
Return a shared pointer to a NanoVDB grid constructed from the specified OpenVDB grid.
__hostdev__ const TreeT & tree() const
Return a const reference to the tree.
NanoToOpenVDB()
Construction from an existing const OpenVDB Grid.
GLsizei const GLfloat * value
Leaf nodes of the VDB tree. (defaults to 8x8x8 = 512 voxels)
openvdb::math::Vec4< T > Type
Highest level of the data structure. Contains a tree and a world->index transform (that currently onl...
void forEach(RangeT range, const FuncT &func)
simple wrapper for tbb::parallel_for with a naive std fallback
Defines GridHandle, which manages a host, and possibly a device, memory buffer containing one or more...
This class serves to manage a buffer containing one or more NanoVDB Grids.
This class will serialize an OpenVDB grid into a NanoVDB grid managed by a GridHandle.
__hostdev__ const GridClass & gridClass() const
A unified wrapper for tbb::parallel_for and a naive std::thread fallback.
Internal nodes of a VDB treedim(),.
IMATH_NAMESPACE::V2f float
Implements a light-weight self-contained VDB data-structure in a single file! In other words...
Defines an affine transform and its inverse represented as a 3x3 matrix and a vec3 translation...
openvdb::Grid< typename openvdb::tree::Tree4< typename ConvertTrait< NanoBuildT >::Type >::Type >::Ptr nanoToOpenVDB(const NanoGrid< NanoBuildT > &grid, int verbose=0)
Forward declaration of free-standing function that de-serializes a typed NanoVDB grid into an OpenVDB...
VDB Tree, which is a thin wrapper around a RootNode.
Range< 1, size_t > Range1D
__hostdev__ const char * gridName() const
Return a c-string with the name of this grid.
Top-most node of the VDB tree structure.
openvdb::math::Vec3< T > Type
typename TreeT::ValueType ValueType
#define OPENVDB_THROW(exception, message)