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>
59 nanoToOpenVDB(
const GridHandle<BufferT>& handle,
int verbose = 0);
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>;
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>
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");
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);
234 for (
auto *dst = dstNode->buffer().data(), *end = dst + OpenNode0::SIZE; dst != end; dst += 4, src += 4) {
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>
309 if (
auto grid = handle.template grid<float>()) {
311 }
else if (
auto grid = handle.template grid<double>()) {
313 }
else if (
auto grid = handle.template grid<int32_t>()) {
315 }
else if (
auto grid = handle.template grid<int64_t>()) {
317 }
else if (
auto grid = handle.template grid<bool>()) {
319 }
else if (
auto grid = handle.template grid<nanovdb::Fp4>()) {
321 }
else if (
auto grid = handle.template grid<nanovdb::Fp8>()) {
323 }
else if (
auto grid = handle.template grid<nanovdb::Fp16>()) {
325 }
else if (
auto grid = handle.template grid<nanovdb::FpN>()) {
327 }
else if (
auto grid = handle.template grid<nanovdb::ValueMask>()) {
329 }
else if (
auto grid = handle.template grid<nanovdb::Vec3f>()) {
331 }
else if (
auto grid = handle.template grid<nanovdb::Vec3d>()) {
333 }
else if (
auto grid = handle.template grid<nanovdb::Vec4f>()) {
335 }
else if (
auto grid = handle.template grid<nanovdb::Vec4d>()) {
344 #endif // NANOVDB_NANOTOOPENVDB_H_HAS_BEEN_INCLUDED
T Type
Definition: NanoToOpenVDB.h:26
openvdb::math::Vec4< T > Type
Definition: NanoToOpenVDB.h:32
VDB Tree, which is a thin wrapper around a RootNode.
Definition: NanoVDB.h:2798
Highest level of the data structure. Contains a tree and a world->index transform (that currently onl...
Definition: NanoVDB.h:2555
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:74
double mVecD[3]
Definition: NanoVDB.h:2231
A unified wrapper for tbb::parallel_for and a naive std::thread fallback.
openvdb::GridBase::Ptr nanoToOpenVDB(const GridHandle< BufferT > &handle, int verbose=0)
Forward declaration of free-standing function that de-serializes a NanoVDB GridHandle into an OpenVDB...
Definition: NanoToOpenVDB.h:307
Definition: NanoToOpenVDB.h:26
Defines two classes, a GridRegister the defines the value type (e.g. Double, Float etc) of a NanoVDB ...
SharedPtr< GridBase > Ptr
Definition: Grid.h:80
typename _TreeType::ValueType ValueType
Definition: Grid.h:579
const TreeT & tree() const
Return a const reference to the tree.
Definition: NanoVDB.h:2598
void forEach(RangeT range, const FuncT &func)
simple wrapper for tbb::parallel_for with a naive std fallback
Definition: ForEach.h:40
This class serves to manage a raw memory buffer of a NanoVDB Grid.
Definition: GridHandle.h:70
Implements a light-weight self-contained VDB data-structure in a single file! In other words...
float Type
Definition: NanoToOpenVDB.h:38
Definition: NanoVDB.h:208
float Type
Definition: NanoToOpenVDB.h:44
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...
Definition: NanoToOpenVDB.h:299
Top-most node of the VDB tree structure.
Definition: NanoVDB.h:3073
float Type
Definition: NanoToOpenVDB.h:35
double mMatD[9]
Definition: NanoVDB.h:2229
openvdb::math::Vec3< T > Type
Definition: NanoToOpenVDB.h:29
NanoToOpenVDB()
Construction from an existing const OpenVDB Grid.
Definition: NanoToOpenVDB.h:84
Definition: Exceptions.h:63
typename TreeT::ValueType ValueType
Definition: NanoVDB.h:2561
ValueT value
Definition: GridBuilder.h:1290
This class will serialize an OpenVDB grid into a NanoVDB grid managed by a GridHandle.
Definition: NanoToOpenVDB.h:63
Range< 1, size_t > Range1D
Definition: Range.h:30
const char * gridName() const
Return a c-string with the name of this grid.
Definition: NanoVDB.h:2700
Defines an affine transform and its inverse represented as a 3x3 matrix and a vec3 translation...
Definition: NanoVDB.h:2223
SharedPtr< Grid > Ptr
Definition: Grid.h:573
const GridClass & gridClass() const
Definition: NanoVDB.h:2673
Internal nodes of a VDB treedim(),.
Definition: NanoVDB.h:3520
Struct with all the member data of the Grid (useful during serialization of an openvdb grid) ...
Definition: NanoVDB.h:2431
Container class that associates a tree with a transform and metadata.
Definition: Grid.h:28
Leaf nodes of the VDB tree. (defaults to 8x8x8 = 512 voxels)
Definition: NanoVDB.h:4251
float Type
Definition: NanoToOpenVDB.h:41