24#ifndef NANOVDB_TOOLS_NANOTOOPENVDB_H_HAS_BEEN_INCLUDED
25#define NANOVDB_TOOLS_NANOTOOPENVDB_H_HAS_BEEN_INCLUDED
46using OpenTree =
typename openvdb::tree::Tree4<trait::MapToOpenT<T>, 5, 4, 3>::Type;
62template<
typename NanoBuildT>
72template<
typename NanoBuildT,
typename NanoValueT>
75 const NanoValueT *sideCar,
77 const char *gridName =
nullptr);
85template<
typename BufferT>
102template<
typename T, u
int32_t LEVEL>
107struct OpenNode<T, 0> {
using type = openvdb::tree::LeafNode<MapToOpenT<T>, 3>;};
110struct OpenNode<T, 1> {
using type = openvdb::tree::InternalNode<typename OpenNode<T, 0>::type, 4>;};
113struct OpenNode<T, 2> {
using type = openvdb::tree::InternalNode<typename OpenNode<T, 1>::type, 5>;};
115template<
typename T, u
int32_t LEVEL>
131 throw std::runtime_error(
"NanoToOpenVDB does not yet support PointIndexGrids");
133 throw std::runtime_error(
"NanoToOpenVDB does not yet support PointDataGrids");
143template<u
int32_t LOG2DIM>
145static auto mapMask(
const Mask<LOG2DIM> &m){
return reinterpret_cast<const openvdb::util::NodeMask<LOG2DIM>&
>(m);}
161 template<
typename NanoBuildT>
170 template<
typename NanoIndexT>
181 template<
typename NanoIndexT,
typename NanoValueT>
184 const NanoValueT *sideCar,
186 const char *name =
nullptr);
190 template<
int LEVEL,
typename NanoBuildT>
194 template<
int LEVEL,
typename NanoIndexT,
typename NanoValueT>
198 template<
int LEVEL,
typename NanoBuildT>
202 template<
int LEVEL,
typename NanoIndexT,
typename NanoValueT>
210template<
typename NanoBuildT>
216 dstGrid->setName(srcGrid.
gridName());
220 const Map& nanoMap =
reinterpret_cast<const GridData&
>(srcGrid).mMap;
221 auto mat = openvdb::math::Mat4<double>::identity();
222 mat.setMat3(openvdb::math::Mat3<double>(nanoMap.
mMatD));
223 mat = mat.transpose();
224 mat.setTranslation(openvdb::math::Vec3<double>(nanoMap.
mVecD));
225 dstGrid->setTransform(openvdb::math::Transform::createLinearTransform(mat));
228 auto &root = dstGrid->tree().root();
229 auto *data = srcGrid.
tree().root().data();
230 for (uint32_t i=0; i<data->mTableSize; ++i) {
231 auto *tile = data->tile(i);
232 if (tile->isChild()) {
234 this->
template process<2, NanoBuildT>( dstChild, data->getChild(tile) );
235 root.addChild( dstChild );
244template<
typename NanoIndexT,
typename NanoValueT>
247 const NanoValueT *sideCar,
253 if (name) dstGrid->setName(name);
257 const Map& nanoMap =
reinterpret_cast<const GridData&
>(indexGrid).mMap;
258 auto mat = openvdb::math::Mat4<double>::identity();
259 mat.setMat3(openvdb::math::Mat3<double>(nanoMap.
mMatD));
260 mat = mat.transpose();
261 mat.setTranslation(openvdb::math::Vec3<double>(nanoMap.
mVecD));
262 dstGrid->setTransform(openvdb::math::Transform::createLinearTransform(mat));
265 auto &root = dstGrid->tree().root();
266 auto *data = indexGrid.
tree().root().data();
267 for (uint32_t i=0; i<data->mTableSize; ++i) {
268 auto *tile = data->tile(i);
269 if (tile->isChild()) {
271 this->
template process<2, NanoIndexT, NanoValueT>( dstChild, data->getChild(tile), sideCar );
272 root.addChild( dstChild );
285template<
typename NanoIndexT>
290 if (blindDataID >= 0 &&
int(i) != blindDataID)
continue;
293 idxGrid.
valueCount() != metaData.mValueCount)
continue;
296 switch (metaData.mDataType){
299 return (*
this)(idxGrid, idxGrid.template getBlindData<float>(i), gridClass);
302 return (*
this)(idxGrid, idxGrid.template getBlindData<double>(i), gridClass);
305 return (*
this)(idxGrid, idxGrid.template getBlindData<int32_t>(i), gridClass);
308 return (*
this)(idxGrid, idxGrid.template getBlindData<Vec3f>(i), gridClass);
318template<
int LEVEL,
typename NanoBuildT>
328 const_cast<typename DstNodeT::NodeMaskType&
>(dstNode->getValueMask()) = valueMask;
329 const_cast<typename DstNodeT::NodeMaskType&
>(dstNode->getChildMask()) = childMask;
330 auto* dstTable =
const_cast<typename DstNodeT::UnionType*
>(dstNode->getTable());
331 auto* srcData = srcNode->data();
332 std::vector<std::pair<uint32_t, const typename SrcNodeT::ChildNodeType*>> childNodes;
333 const auto childCount = childMask.countOn();
334 childNodes.reserve(childCount);
335 for (uint32_t n = 0; n < DstNodeT::NUM_VALUES; ++n) {
336 if (childMask.isOn(n)) {
337 childNodes.emplace_back(n, srcData->getChild(n));
339 dstTable[n].setValue(*
trait::mapPtr(&srcData->mTable[n].value));
343 using DstChildNodeType =
typename DstNodeT::ChildNodeType;
344 auto kernel = [&](
const auto& r) {
345 for (
auto i = r.begin(); i != r.end(); ++i) {
346 auto &p = childNodes[i];
347 auto* dstChild =
new DstChildNodeType();
348 this->
template process<LEVEL-1, NanoBuildT>(dstChild, p.second);
349 dstTable[p.first].setChild( dstChild );
354 kernel(
Range1D(0, childCount));
360template<
int LEVEL,
typename NanoIndexT,
typename NanoValueT>
361util::enable_if_t<BuildTraits<NanoIndexT>::is_index && LEVEL != 0>
364 const NanoValueT* sideCar)
371 const_cast<typename DstNodeT::NodeMaskType&
>(dstNode->getValueMask()) = valueMask;
372 const_cast<typename DstNodeT::NodeMaskType&
>(dstNode->getChildMask()) = childMask;
373 auto* dstTable =
const_cast<typename DstNodeT::UnionType*
>(dstNode->getTable());
374 auto* srcData = srcNode->data();
375 std::vector<std::pair<uint32_t, const typename SrcNodeT::ChildNodeType*>> childNodes;
376 const auto childCount = childMask.countOn();
377 childNodes.reserve(childCount);
378 for (uint32_t n = 0; n < DstNodeT::NUM_VALUES; ++n) {
379 if (childMask.isOn(n)) {
380 childNodes.emplace_back(n, srcData->getChild(n));
382 dstTable[n].setValue(*
trait::mapPtr(sideCar + srcData->mTable[n].value));
386 using DstChildNodeType =
typename DstNodeT::ChildNodeType;
387 auto kernel = [&](
const auto& r) {
388 for (
auto i = r.begin(); i != r.end(); ++i) {
389 auto &p = childNodes[i];
390 auto *dstChild =
new DstChildNodeType();
391 this->
template process<LEVEL-1, NanoIndexT, NanoValueT>(dstChild, p.second, sideCar);
392 dstTable[p.first].setChild( dstChild );
397 kernel(
Range1D(0, childCount));
405template<
int LEVEL,
typename NanoBuildT>
406util::disable_if_t<BuildTraits<NanoBuildT>::is_index || LEVEL != 0>
416 std::copy(src, src + 512, dstLeaf->buffer().data());
418 for (
auto *dst = dstLeaf->buffer().data(), *end = dst + 512; dst != end; dst += 4, src += 4) {
426 float *dst = dstLeaf->buffer().data();
427 for (
int i=0; i!=512; i+=4) {
428 *dst++ = srcLeaf->getValue(i);
429 *dst++ = srcLeaf->getValue(i+1);
430 *dst++ = srcLeaf->getValue(i+2);
431 *dst++ = srcLeaf->getValue(i+3);
434 reinterpret_cast<openvdb::util::NodeMask<3>&
>(dstLeaf->buffer()) =
trait::mapMask(srcLeaf->data()->mValues);
436 OPENVDB_THROW(openvdb::RuntimeError,
"Unsupported NanoBuildT in NanoToOpenVDB::process(NanoLeaf<NanoBuildT>)");
440template<
int LEVEL,
typename NanoIndexT,
typename NanoValueT>
441util::enable_if_t<BuildTraits<NanoIndexT>::is_index && LEVEL == 0>
444 const NanoValueT *sideCar)
451 const auto *src =
trait::mapPtr(sideCar + srcLeaf->data()->mOffset);
453 std::copy(src, src + 512, dstLeaf->buffer().data());
455 for (
auto *dst = dstLeaf->buffer().data(), *end = dst + 512; dst != end; dst += 4, src += 4) {
464 auto *dst = dstLeaf->buffer().data();
465 for (
int i = 0; i < 512; i += 4) {
466 *dst++ = src[srcLeaf->getValue(i )];
467 *dst++ = src[srcLeaf->getValue(i+1)];
468 *dst++ = src[srcLeaf->getValue(i+2)];
469 *dst++ = src[srcLeaf->getValue(i+3)];
473 OPENVDB_THROW(openvdb::RuntimeError,
"Unsupported NanoValueT in NanoToOpenVDB::process(NanoLeaf<NanoIndexT>, NanoValueT*)");
479template<
typename NanoBuildT>
486template<
typename NanoBuildT,
typename NanoValueT>
489 const NanoValueT *sideCar,
491 const char *gridName)
494 return tmp(grid, sideCar, gridClass, gridName);
497template<
typename BufferT>
498openvdb::GridBase::Ptr
501 if (
auto grid = handle.template grid<float>(n)) {
503 }
else if (
auto grid = handle.template grid<double>(n)) {
505 }
else if (
auto grid = handle.template grid<int32_t>(n)) {
507 }
else if (
auto grid = handle.template grid<int64_t>(n)) {
509 }
else if (
auto grid = handle.template grid<bool>(n)) {
511 }
else if (
auto grid = handle.template grid<Fp4>(n)) {
513 }
else if (
auto grid = handle.template grid<Fp8>(n)) {
515 }
else if (
auto grid = handle.template grid<Fp16>(n)) {
517 }
else if (
auto grid = handle.template grid<FpN>(n)) {
519 }
else if (
auto grid = handle.template grid<ValueMask>(n)) {
521 }
else if (
auto grid = handle.template grid<Vec3f>(n)) {
523 }
else if (
auto grid = handle.template grid<Vec3d>(n)) {
525 }
else if (
auto grid = handle.template grid<Vec4f>(n)) {
527 }
else if (
auto grid = handle.template grid<Vec4d>(n)) {
529 }
else if (
auto grid = handle.template grid<ValueOnIndex>(n)) {
531 }
else if (
auto grid = handle.template grid<ValueIndex>(n)) {
541template<
typename NanoBuildT>
542[[deprecated(
"Use nanovdb::tools::nanoToOpenVDB instead.")]]
549template<
typename BufferT>
550[[deprecated(
"Use nanovdb::tools::nanoToOpenVDB instead.")]]
A unified wrapper for tbb::parallel_for and a naive std::thread fallback.
Defines GridHandle, which manages a memory buffer containing one or more NanoVDB grids: host-resident...
Implements a light-weight self-contained VDB data-structure in a single file! In other words,...
Propagate the signs of distance values from the active voxels in the narrow band to the inactive valu...
This class serves to manage a buffer containing one or more NanoVDB Grids.
Definition GridHandle.h:109
uint32_t blindDataCount() const
Return true if this grid is empty, i.e. contains no values or nodes.
Definition NanoVDB.h:2354
const GridClass & gridClass() const
Definition NanoVDB.h:2311
const char * gridName() const
Return a c-string with the name of this grid.
Definition NanoVDB.h:2342
const GridBlindMetaData & blindMetaData(uint32_t n) const
Definition NanoVDB.h:2388
const TreeT & tree() const
Return a const reference to the tree.
Definition NanoVDB.h:2236
util::enable_if< BuildTraits< T >::is_index, constuint64_t & >::type valueCount() const
Return the total number of values indexed by this IndexGrid.
Definition NanoVDB.h:2226
Bit-mask to encode active states and facilitate sequential iterators and a fast codec for I/O compres...
Definition NanoVDB.h:1068
Dummy type for a voxel whose value equals its binary active state.
Definition NanoVDB.h:183
Signed (i, j, k) 32-bit integer coordinate class, similar to openvdb::math::Coord.
Definition Math.h:346
Container class that associates a tree with a transform and metadata.
Definition Grid.h:571
SharedPtr< Grid > Ptr
Definition Grid.h:573
Definition Exceptions.h:63
Signed (x, y, z) 32-bit integer coordinates.
Definition Coord.h:26
Definition DitherLUT.h:19
void forEach(RangeT range, const FuncT &func)
simple wrapper for tbb::parallel_for with a naive std fallback
Definition ForEach.h:42
typename disable_if< Test, T >::type disable_if_t
Definition Util.h:370
Range< 1, size_t > Range1D
Definition Range.h:33
static constexpr bool is_same_v
Definition Util.h:338
typename enable_if< Test, T >::type enable_if_t
Definition Util.h:359
Defines a simple memory pool used to call cub functions that use dynamic temporary storage.
Definition GridHandle.h:31
Grid< NanoTree< BuildT > > NanoGrid
Definition NanoVDB.h:4742
tools::trait::OpenGridPtr< NanoBuildT > nanoToOpenVDB(const NanoGrid< NanoBuildT > &grid)
Forward declaration of free-standing function that de-serializes a typed NanoVDB grid into an OpenVDB...
Definition NanoToOpenVDB.h:543
math::Vec3< float > Vec3f
Definition Math.h:2230
GridClass
Classes (superset of OpenVDB) that are currently supported by NanoVDB.
Definition NanoVDB.h:288
@ FogVolume
Definition NanoVDB.h:290
@ Unknown
Definition NanoVDB.h:288
@ PointIndex
Definition NanoVDB.h:292
@ PointData
Definition NanoVDB.h:293
@ Staggered
Definition NanoVDB.h:291
@ LevelSet
Definition NanoVDB.h:289
@ Float
Definition NanoVDB.h:220
@ Int32
Definition NanoVDB.h:223
@ Double
Definition NanoVDB.h:221
@ Vec3f
Definition NanoVDB.h:225
typename NanoNode< BuildT, LEVEL >::type NanoNodeT
Definition NanoVDB.h:4775
@ ChannelArray
Definition NanoVDB.h:414
GridClass toGridClass(GridBlindDataSemantic semantics, GridClass defaultClass=GridClass::Unknown)
Maps from GridBlindDataSemantic to GridClass.
Definition NanoVDB.h:448
GridClass
Definition Types.h:524
@ GRID_FOG_VOLUME
Definition Types.h:527
@ GRID_STAGGERED
Definition Types.h:528
@ GRID_LEVEL_SET
Definition Types.h:526
@ GRID_UNKNOWN
Definition Types.h:525
GridType::Ptr createGrid(const typename GridType::ValueType &background)
Create a new grid of type GridType with a given background value.
Definition Grid.h:1749
#define NANOVDB_ASSERT(x)
Definition Util.h:53
#define OPENVDB_THROW(exception, message)
Definition Exceptions.h:74
T type
Definition NanoVDB.h:528
static constexpr bool is_Fp
Definition NanoVDB.h:514
static constexpr bool is_special
Definition NanoVDB.h:518
Struct with all the member data of the Grid (useful during serialization of an openvdb grid)
Definition NanoVDB.h:1977
Defines an affine transform and its inverse represented as a 3x3 matrix and a vec3 translation.
Definition NanoVDB.h:1415
double mVecD[3]
Definition NanoVDB.h:1422
double mMatD[9]
Definition NanoVDB.h:1420