22 #ifndef NANOVDB_NANOTOOPENVDB_H_HAS_BEEN_INCLUDED 23 #define NANOVDB_NANOTOOPENVDB_H_HAS_BEEN_INCLUDED 31 template<
typename ValueT>
36 template<
typename BufferT>
41 template<
typename ValueType>
52 using DstNode0 = openvdb::tree::LeafNode<ValueT, SrcNode0::LOG2DIM>;
53 using DstNode1 = openvdb::tree::InternalNode<DstNode0, SrcNode1::LOG2DIM>;
54 using DstNode2 = openvdb::tree::InternalNode<DstNode1, SrcNode2::LOG2DIM>;
55 using DstRootT = openvdb::tree::RootNode<DstNode2>;
56 using DstTreeT = openvdb::tree::Tree<DstRootT>;
68 template<
typename SrcNodeT,
typename DstNodeT>
69 DstNodeT* processNode(
const SrcNodeT*);
71 DstNode2* process(
const SrcNode2* node) {
return this->
template processNode<SrcNode2, DstNode2>(node);}
72 DstNode1* process(
const SrcNode1* node) {
return this->
template processNode<SrcNode1, DstNode1>(node);}
73 DstNode0* process(
const SrcNode0* node);
85 using Type = openvdb::math::Vec3<T>;
91 using Type = openvdb::math::Vec4<T>;
100 auto dstGrid = openvdb::createGrid<DstGridT>(srcGrid->
tree().background());
101 dstGrid->setName(srcGrid->
gridName());
113 throw std::runtime_error(
"NanoToOpenVDB does not yet support PointIndexGrids");
115 throw std::runtime_error(
"NanoToOpenVDB does not yet support PointDataGrids");
117 throw std::runtime_error(
"NanoToOpenVDB does not yet support Mask (or Topology) Grids");
123 auto mat = openvdb::math::Mat4<double>::identity();
124 mat.setMat3(openvdb::math::Mat3<double>(nanoMap.
mMatD));
126 mat.setTranslation(openvdb::math::Vec3<double>(nanoMap.
mVecD));
127 dstGrid->setTransform(openvdb::math::Transform::createLinearTransform(mat));
130 auto &root = dstGrid->tree().root();
131 auto *data = srcGrid->
tree().root().data();
132 for (uint32_t i=0; i<data->mTableSize; ++i) {
133 auto *tile = data->tile(i);
134 if (tile->isChild()) {
135 #if OPENVDB_ABI_VERSION_NUMBER >= 7 136 root.addChild( this->process( data->getChild(tile)) );
137 #else// hack since RootNode::addChild is not available in older versions 138 root.addTile(tile->origin(), root.background(),
false);
139 auto it = root.beginChildAll();
140 while(it.getCoord() != tile->origin()) ++it;
141 it.setChild(this->process( data->getChild(tile)) );
144 root.addTile(tile->origin(), tile->value, tile->state);
152 template<
typename SrcNodeT,
typename DstNodeT>
156 DstNodeT *dstNode =
new DstNodeT();
157 dstNode->setOrigin(srcNode->origin());
158 const auto& childMask = srcNode->childMask();
159 const_cast<typename DstNodeT::NodeMaskType&
>(dstNode->getValueMask()) = srcNode->valueMask();
160 const_cast<typename DstNodeT::NodeMaskType&
>(dstNode->getChildMask()) = childMask;
161 auto* dstTable =
const_cast<typename DstNodeT::UnionType*
>(dstNode->getTable());
162 auto* srcData = srcNode->data();
163 std::vector<std::pair<uint32_t, const typename SrcNodeT::ChildNodeType*>> childNodes;
164 const auto childCount = childMask.countOn();
165 childNodes.reserve(childCount);
166 for (uint32_t n = 0; n < DstNodeT::NUM_VALUES; ++n) {
167 if (childMask.isOn(n)) {
168 childNodes.emplace_back(n, srcData->getChild(n));
170 dstTable[n].setValue(srcData->mTable[n].value);
173 auto kernel = [&](
const auto& r) {
174 for (
auto i = r.begin(); i != r.end(); ++i) {
175 auto &p = childNodes[i];
176 dstTable[p.first].setChild( this->process(p.second) );
181 kernel(
Range1D(0, childCount));
183 forEach(0, childCount, 1, kernel);
189 typename NanoToOpenVDB<T>::DstNode0*
192 DstNode0* dstNode =
new DstNode0();
193 dstNode->setOrigin(srcNode->
origin());
194 dstNode->setValueMask(srcNode->
valueMask());
197 for (ValueT *dst = dstNode->buffer().data(), *end = dst + DstNode0::SIZE; dst != end; dst += 4, src += 4) {
207 template<
typename ValueT>
212 return tmp(grid, verbose);
215 template<
typename BufferT>
219 if (
auto grid = handle.template grid<float>()) {
221 }
else if (
auto grid = handle.template grid<double>()) {
223 }
else if (
auto grid = handle.template grid<int32_t>()) {
225 }
else if (
auto grid = handle.template grid<int64_t>()) {
227 }
else if (
auto grid = handle.template grid<nanovdb::Vec3f>()) {
229 }
else if (
auto grid = handle.template grid<nanovdb::Vec3d>()) {
231 }
else if (
auto grid = handle.template grid<nanovdb::Vec4f>()) {
233 }
else if (
auto grid = handle.template grid<nanovdb::Vec4d>()) {
242 #endif // NANOVDB_NANOTOOPENVDB_H_HAS_BEEN_INCLUDED
VDB Tree, which is a thin wrapper around a RootNode.
Definition: NanoVDB.h:2542
Highest level of the data structure. Contains a tree and a world->index transform (that currently onl...
Definition: NanoVDB.h:2307
SharedPtr< Grid > Ptr
Definition: Grid.h:579
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:74
double mVecD[3]
Definition: NanoVDB.h:2005
A unified wrapper for tbb::parallel_for and a naive std::thread fallback.
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
A simple vector class with three double components, similar to openvdb::math::Vec4.
Definition: NanoVDB.h:1188
const TreeT & tree() const
Return a const reference to the tree.
Definition: NanoVDB.h:2344
void forEach(RangeT range, const FuncT &func)
simple wrapper for tbb::parallel_for with a naive std fallback
Definition: ForEach.h:40
Definition: NanoToOpenVDB.h:28
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...
Definition: NanoVDB.h:184
Top-most node of the VDB tree structure.
Definition: NanoVDB.h:2798
DataType * data()
Definition: NanoVDB.h:3709
double mMatD[9]
Definition: NanoVDB.h:2003
ValueType mValues[1u<< 3 *LOG2DIM]
Definition: NanoVDB.h:3373
DstGridT::Ptr operator()(const NanoGrid< ValueType > &grid, int verbose=0)
Return a shared pointer to a NanoVDB grid constructed from the specified OpenVDB grid.
Definition: NanoToOpenVDB.h:96
NanoToOpenVDB()
Construction from an existing const OpenVDB Grid.
Definition: NanoToOpenVDB.h:61
openvdb::math::Vec3< T > Type
Definition: NanoToOpenVDB.h:85
openvdb::math::Vec4< T > Type
Definition: NanoToOpenVDB.h:91
Definition: Exceptions.h:63
This class will serialize an OpenVDB grid into a NanoVDB grid managed by a GridHandle.
Definition: NanoToOpenVDB.h:42
Range< 1, size_t > Range1D
Definition: Range.h:30
Container class that associates a tree with a transform and metadata.
Definition: Grid.h:28
const char * gridName() const
Return a c-string with the name of this grid.
Definition: NanoVDB.h:2445
Defines an affine transform and its inverse represented as a 3x3 matrix and a vec3 translation...
Definition: NanoVDB.h:1997
const GridClass & gridClass() const
Definition: NanoVDB.h:2419
const MaskType< LOG2DIM > & valueMask() const
Return a const reference to the bit mask of active voxels in this leaf node.
Definition: NanoVDB.h:3714
Internal nodes of a VDB treedim(),.
Definition: NanoVDB.h:3120
T Type
Definition: NanoToOpenVDB.h:79
Struct with all the member data of the Grid (useful during serialization of an openvdb grid) ...
Definition: NanoVDB.h:2184
CoordT origin() const
Return the origin in index space of this leaf node.
Definition: NanoVDB.h:3734
Leaf nodes of the VDB tree. (defaults to 8x8x8 = 512 voxels)
Definition: NanoVDB.h:3683
openvdb::Grid< typename openvdb::tree::Tree4< typename ConvertTrait< ValueT >::Type >::Type >::Ptr nanoToOpenVDB(const NanoGrid< ValueT > &grid, int verbose=0)
Forward declaration of free-standing function that de-serializes a typed NanoVDB grid into an OpenVDB...
Definition: NanoToOpenVDB.h:209