10#ifndef NANOVDB_DENSEGRID_H_HAS_BEEN_INCLUDED
11#define NANOVDB_DENSEGRID_H_HAS_BEEN_INCLUDED
23#define LOG2_TILE_SIZE 2
28template<
typename BufferT = HostBuffer>
31#define DENSE_MAGIC_NUMBER 0x42445665736e6544UL
48 template<
typename Vec3T>
50 template<
typename Vec3T>
52 template<
typename Vec3T>
54 template<
typename Vec3T>
56 template<
typename Vec3T>
59 template<
typename Vec3T>
61 template<
typename Vec3T>
63 template<
typename Vec3T>
65 template<
typename Vec3T>
67 template<
typename Vec3T>
73template<
typename ValueT>
77 static constexpr uint32_t TileLog2 =
LOG2_TILE_SIZE, TileMask = (1 << TileLog2) - 1, TileDim = 1 << (3*TileLog2);
79 using DenseData = DenseData;
84 template<
typename BufferT = HostBuffer>
90 const BufferT& allocator = BufferT());
112 template<
typename Vec3T>
116 template<
typename Vec3T>
121 template<
typename Vec3T>
126 template<
typename Vec3T>
131 template<
typename Vec3T>
135 template<
typename Vec3T>
139 template<
typename Vec3T>
144 template<
typename Vec3T>
149 template<
typename Vec3T>
154 template<
typename Vec3T>
173 __hostdev__ const DenseData*
data()
const {
return reinterpret_cast<const DenseData*
>(
this); }
182template<
typename ValueT>
183template<
typename BufferT>
184DenseGridHandle<BufferT>
193 throw std::runtime_error(
"GridBuilder: voxel size is zero or negative");
197#if LOG2_TILE_SIZE > 0
198 const uint64_t dim[3] = {(
uint64_t(max[0] - min[0]) + TileMask) >> TileLog2,
199 (
uint64_t(max[1] - min[1]) + TileMask) >> TileLog2,
200 (
uint64_t(max[2] - min[2]) + TileMask) >> TileLog2};
213 const double mat[4][4] = {
219 const double invMat[4][4] = {
220 {1 /
dx, 0.0, 0.0, 0.0},
221 {0.0, 1 /
dx, 0.0, 0.0},
222 {0.0, 0.0, 1 /
dx, 0.0},
226 grid->mMap.set(mat, invMat, 1.0);
227 for (
int i=0; i<3; ++i) {
228 grid->mIndexBBox[0][i] = min[i];
229 grid->mIndexBBox[1][i] = max[i] - 1;
231 grid->mWorldBBox[0] =
grid->mWorldBBox[1] =
grid->mMap.applyMap(
Vec3d(min[0], min[1], min[2]));
232 grid->mWorldBBox.expand(
grid->mMap.applyMap(
Vec3d(min[0], min[1], max[2])));
233 grid->mWorldBBox.expand(
grid->mMap.applyMap(
Vec3d(min[0], max[1], min[2])));
234 grid->mWorldBBox.expand(
grid->mMap.applyMap(
Vec3d(max[0], min[1], min[2])));
235 grid->mWorldBBox.expand(
grid->mMap.applyMap(
Vec3d(max[0], max[1], min[2])));
236 grid->mWorldBBox.expand(
grid->mMap.applyMap(
Vec3d(max[0], min[1], max[2])));
237 grid->mWorldBBox.expand(
grid->mMap.applyMap(
Vec3d(min[0], max[1], max[2])));
238 grid->mWorldBBox.expand(
grid->mMap.applyMap(
Vec3d(max[0], max[1], max[2])));
241 throw std::runtime_error(
"Level sets are expected to be floating point types");
243 throw std::runtime_error(
"Fog volumes are expected to be floating point types");
244 grid->mGridClass = gridClass;
247 grid->mX = dim[2] * dim[1];
251template<
typename ValueT>
254 return (ijk[0]>=mIndexBBox[0][0]) && (ijk[0]<=mIndexBBox[1][0]) &&
255 (ijk[1]>=mIndexBBox[0][1]) && (ijk[1]<=mIndexBBox[1][1]) &&
256 (ijk[2]>=mIndexBBox[0][2]) && (ijk[2]<=mIndexBBox[1][2]);
259template<
typename ValueT>
263#if LOG2_TILE_SIZE > 0
264 const uint32_t x = ijk[0] - mIndexBBox[0][0];
265 const uint32_t y = ijk[1] - mIndexBBox[0][1];
266 const uint32_t z = ijk[2] - mIndexBBox[0][2];
267 return ((mX*(x>>TileLog2) + mY*(y>>TileLog2) + (z>>TileLog2))<<(3*TileLog2)) +
268 ((x&TileMask)<<(2*TileLog2)) + ((y&TileMask)<<TileLog2) + (z&TileMask);
270 return uint64_t(ijk[0]-mIndexBBox[0][0])*mX +
271 uint64_t(ijk[1]-mIndexBBox[0][1])*mY +
276template<
typename ValueT>
279 return this->values()[this->coordToOffset(ijk)];
282template<
typename ValueT>
285 this->values()[this->coordToOffset(ijk)] =
value;
288template<
typename ValueT>
291 return std::is_same<float, ValueT>::value ? mGridType ==
GridType::Float :
false;
298template<
typename ValueT>
301 std::ofstream
os(
fileName, std::ios::out | std::ios::binary | std::ios::trunc);
303 throw std::runtime_error(
"Unable to open file for output");
306 os.write(
reinterpret_cast<const char*
>(
tmp), 2*
sizeof(
uint64_t));
307 os.write(
reinterpret_cast<const char*
>(&grid),
tmp[1]);
310template<
typename BufferT>
313 std::ofstream
os(
fileName, std::ios::out | std::ios::binary | std::ios::trunc);
315 throw std::runtime_error(
"Unable to open file for output");
318 os.write(
reinterpret_cast<const char*
>(
tmp), 2*
sizeof(
uint64_t));
322template<
typename BufferT = HostBuffer>
326 std::ifstream
is(
fileName, std::ios::in | std::ios::binary);
328 throw std::runtime_error(
"Unable to open file for input");
333 throw std::runtime_error(
"This is not a dense NanoVDB file!");
343template<
typename Gr
idT,
typename BufferT = HostBuffer>
346 using ValueT =
typename GridT::ValueType;
348 const Coord min = grid.indexBBox().min(), max = grid.indexBBox().max() +
Coord(1,1,1);
349#if LOG2_TILE_SIZE > 0
350 static constexpr uint32_t TileLog2 =
LOG2_TILE_SIZE, TileMask = (1 << TileLog2) - 1, TileDim = 1 << (3*TileLog2);
351 const uint64_t dim[3] = {(
uint64_t(max[0] - min[0]) + TileMask) >> TileLog2,
352 (
uint64_t(max[1] - min[1]) + TileMask) >> TileLog2,
353 (
uint64_t(max[2] - min[2]) + TileMask) >> TileLog2};
354 const uint64_t size =
sizeof(DenseT) +
sizeof(ValueT)*TileDim*dim[0]*dim[1]*dim[2];
359 const uint64_t size =
sizeof(DenseT) +
sizeof(ValueT)*dim[0]*dim[1]*dim[2];
367 data->mMap = grid.map();
368 data->mIndexBBox = grid.indexBBox();
369 data->mWorldBBox = grid.worldBBox();
370 data->mVoxelSize = grid.voxelSize();
371 data->mGridClass = grid.gridClass();
372 data->mGridType = grid.gridType();
374 data->mX = dim[2] * dim[1];
379 auto acc = grid.getAccessor();
381 for (ijk[0] = r.begin(); ijk[0] < r.end(); ++ijk[0]) {
382 for (ijk[1] = min[1]; ijk[1] < max[1]; ++ijk[1]) {
383 for (ijk[2] = min[2]; ijk[2] < max[2]; ++ijk[2]) {
384 dense->setValue(ijk, acc.getValue(ijk));
400template<
typename BufferT>
416 mBuffer = std::move(
other.mBuffer);
427 const BufferT&
buffer()
const {
return mBuffer; }
446 template<
typename ValueT>
450 GridT*
grid =
reinterpret_cast<GridT*
>(mBuffer.data());
454 template<
typename ValueT>
458 GridT*
grid =
reinterpret_cast<GridT*
>(mBuffer.data());
462 template<
typename ValueT,
typename U = BufferT>
463 typename std::enable_if<BufferTraits<U>::hasDeviceDual,
const DenseGrid<ValueT>*>::type
467 bool isValidType =
reinterpret_cast<GridT*
>(mBuffer.data())->isValidType();
468 GridT*
grid =
reinterpret_cast<GridT*
>(mBuffer.deviceData());
469 return (
grid && isValidType) ?
grid :
nullptr;
472 template<
typename U = BufferT>
473 typename std::enable_if<BufferTraits<U>::hasDeviceDual,
void>::type
478 template<
typename U = BufferT>
479 typename std::enable_if<BufferTraits<U>::hasDeviceDual,
void>::type
#define DENSE_MAGIC_NUMBER
Definition DenseGrid.h:31
#define LOG2_TILE_SIZE
Definition DenseGrid.h:23
A unified wrapper for tbb::parallel_for and a naive std::thread fallback.
ValueT value
Definition GridBuilder.h:1290
HostBuffer - a buffer that contains a shared or private bump pool to either externally or internally ...
Implements a light-weight self-contained VDB data-structure in a single file! In other words,...
#define __hostdev__
Definition NanoVDB.h:192
Signed (i, j, k) 32-bit integer coordinate class, similar to openvdb::math::Coord.
Definition NanoVDB.h:967
Definition DenseGrid.h:402
DenseGridHandle(const DenseGridHandle &)=delete
Disallow copy-construction.
DenseGridHandle(DenseGridHandle &&other) noexcept
Move copy-constructor.
Definition DenseGrid.h:420
std::enable_if< BufferTraits< U >::hasDeviceDual, void >::type deviceUpload(void *stream=nullptr, bool sync=true)
Definition DenseGrid.h:474
const uint8_t * data() const
Returns a const pointer to the data.
Definition DenseGrid.h:437
DenseGridHandle(BufferT &&resources)
Definition DenseGrid.h:406
BufferT & buffer()
Definition DenseGrid.h:426
std::enable_if< BufferTraits< U >::hasDeviceDual, void >::type deviceDownload(void *stream=nullptr, bool sync=true)
Definition DenseGrid.h:480
DenseGridHandle()=default
DenseGridHandle & operator=(DenseGridHandle &&other) noexcept
Move copy assignment operation.
Definition DenseGrid.h:414
~DenseGridHandle()
Default destructor.
Definition DenseGrid.h:422
std::enable_if< BufferTraits< U >::hasDeviceDual, constDenseGrid< ValueT > * >::type deviceGrid() const
Definition DenseGrid.h:464
uint64_t size() const
Returns the size in bytes of the raw memory buffer managed by this DenseGridHandle's allocator.
Definition DenseGrid.h:440
const BufferT & buffer() const
Definition DenseGrid.h:427
uint8_t * data()
Returns a non-const pointer to the data.
Definition DenseGrid.h:432
DenseGrid< ValueT > * grid()
Definition DenseGrid.h:455
const DenseGrid< ValueT > * grid() const
Returns a const pointer to the NanoVDB grid encoded in the DenseGridHandle.
Definition DenseGrid.h:447
void reset()
Definition DenseGrid.h:424
DenseGridHandle & operator=(const DenseGridHandle &)=delete
Disallow copy assignment operation.
Simple dense grid class.
Definition DenseGrid.h:75
__hostdev__ ValueT * values()
Definition DenseGrid.h:175
__hostdev__ uint64_t size() const
Definition DenseGrid.h:96
__hostdev__ const GridClass & gridClass() const
Definition DenseGrid.h:170
__hostdev__ const BBox< Vec3R > & worldBBox() const
Computes a AABB of active values in world space.
Definition DenseGrid.h:158
__hostdev__ void setValue(const Coord &ijk, const ValueT &v)
Definition DenseGrid.h:283
__hostdev__ Vec3T worldToIndexF(const Vec3T &xyz) const
world to index space transformation
Definition DenseGrid.h:136
__hostdev__ const Map & map() const
Return a const reference to the Map for this grid.
Definition DenseGrid.h:106
__hostdev__ const ValueT * values() const
Definition DenseGrid.h:176
__hostdev__ DenseData * data()
Definition DenseGrid.h:172
__hostdev__ const ValueT & getValue(const Coord &ijk) const
Definition DenseGrid.h:277
static DenseGridHandle< BufferT > create(Coord min, Coord max, double dx=1.0, const Vec3d &p0=Vec3d(0.0), GridClass gridClass=GridClass::Unknown, const BufferT &allocator=BufferT())
Definition DenseGrid.h:185
__hostdev__ const DenseData * data() const
Definition DenseGrid.h:173
__hostdev__ Vec3T worldToIndexDir(const Vec3T &dir) const
transformation from world space direction to index space direction
Definition DenseGrid.h:127
ValueT ValueType
Definition DenseGrid.h:82
__hostdev__ const GridType & gridType() const
Definition DenseGrid.h:169
__hostdev__ uint64_t gridSize() const
Definition DenseGrid.h:100
__hostdev__ bool test(const Coord &ijk) const
Definition DenseGrid.h:252
__hostdev__ Vec3T indexToWorldGrad(const Vec3T &grad) const
transform the gradient from index space to world space.
Definition DenseGrid.h:132
__hostdev__ Vec3T indexToWorldGradF(const Vec3T &grad) const
Transforms the gradient from index space to world space.
Definition DenseGrid.h:155
__hostdev__ uint64_t coordToOffset(const Coord &ijk) const
Definition DenseGrid.h:260
__hostdev__ Vec3T indexToWorldDirF(const Vec3T &dir) const
transformation from index space direction to world space direction
Definition DenseGrid.h:145
__hostdev__ const CoordBBox & indexBBox() const
Computes a AABB of active values in index space.
Definition DenseGrid.h:167
__hostdev__ const Coord & max() const
Definition DenseGrid.h:102
__hostdev__ Vec3T indexToWorldDir(const Vec3T &dir) const
transformation from index space direction to world space direction
Definition DenseGrid.h:122
__hostdev__ Vec3T indexToWorld(const Vec3T &xyz) const
world to index space transformation
Definition DenseGrid.h:117
__hostdev__ DenseGrid & operator=(const DenseGrid &)=delete
__hostdev__ const Vec3R & voxelSize() const
Definition DenseGrid.h:109
__hostdev__ const Coord & min() const
Definition DenseGrid.h:101
__hostdev__ bool isLevelSet() const
Definition DenseGrid.h:160
__hostdev__ DenseGrid(const DenseGrid &)=delete
__hostdev__ uint64_t memUsage() const
Definition DenseGrid.h:99
__hostdev__ bool isValidType() const
Definition DenseGrid.h:289
__hostdev__ Vec3T worldToIndex(const Vec3T &xyz) const
world to index space transformation
Definition DenseGrid.h:113
__hostdev__ Vec3T worldToIndexDirF(const Vec3T &dir) const
transformation from world space direction to index space direction
Definition DenseGrid.h:150
__hostdev__ Vec3T indexToWorldF(const Vec3T &xyz) const
index to world space transformation
Definition DenseGrid.h:140
__hostdev__ ~DenseGrid()=delete
__hostdev__ bool isFogVolume() const
Definition DenseGrid.h:161
void writeDense(const DenseGrid< ValueT > &grid, const char *fileName)
Definition DenseGrid.h:299
DenseGridHandle< BufferT > readDense(const char *fileName, const BufferT &allocator=BufferT())
Definition DenseGrid.h:324
GridClass
Classes (defined in OpenVDB) that are currently supported by NanoVDB.
Definition NanoVDB.h:281
GridType
List of types that are currently supported by NanoVDB.
Definition NanoVDB.h:243
DenseGridHandle< BufferT > convertToDense(const GridT &grid, const BufferT &allocator=BufferT())
Converts a NanoVDB grid to a DenseGrid.
Definition DenseGrid.h:344
Vec3< double > Vec3d
Definition NanoVDB.h:1288
void forEach(RangeT range, const FuncT &func)
simple wrapper for tbb::parallel_for with a naive std fallback
Definition ForEach.h:40
Definition DenseGrid.h:35
CoordBBox mIndexBBox
Definition DenseGrid.h:37
__hostdev__ Vec3T applyInverseJacobian(const Vec3T &xyz) const
Definition DenseGrid.h:55
GridType mGridType
Definition DenseGrid.h:41
uint64_t mY
Definition DenseGrid.h:42
uint64_t mSize
Definition DenseGrid.h:43
__hostdev__ Vec3T applyIJTF(const Vec3T &xyz) const
Definition DenseGrid.h:68
GridClass mGridClass
Definition DenseGrid.h:40
BBox< Vec3R > mWorldBBox
Definition DenseGrid.h:38
__hostdev__ Vec3T applyInverseJacobianF(const Vec3T &xyz) const
Definition DenseGrid.h:66
__hostdev__ Vec3T applyInverseMapF(const Vec3T &xyz) const
Definition DenseGrid.h:62
uint64_t mX
Definition DenseGrid.h:42
__hostdev__ Vec3T applyIJT(const Vec3T &xyz) const
Definition DenseGrid.h:57
Vec3R mVoxelSize
Definition DenseGrid.h:39
__hostdev__ Vec3T applyMap(const Vec3T &xyz) const
Definition DenseGrid.h:49
__hostdev__ Vec3T applyJacobianF(const Vec3T &xyz) const
Definition DenseGrid.h:64
__hostdev__ Vec3T applyInverseMap(const Vec3T &xyz) const
Definition DenseGrid.h:51
Map mMap
Definition DenseGrid.h:36
__hostdev__ Vec3T applyJacobian(const Vec3T &xyz) const
Definition DenseGrid.h:53
__hostdev__ Coord dim() const
Definition DenseGrid.h:45
__hostdev__ Vec3T applyMapF(const Vec3T &xyz) const
Definition DenseGrid.h:60
Defines an affine transform and its inverse represented as a 3x3 matrix and a vec3 translation.
Definition NanoVDB.h:2224
Vec3T applyInverseJacobianF(const Vec3T &xyz) const
Definition NanoVDB.h:2270
Vec3T applyIJTF(const Vec3T &xyz) const
Definition NanoVDB.h:2275
Vec3T applyIJT(const Vec3T &xyz) const
Definition NanoVDB.h:2273
Vec3T applyJacobianF(const Vec3T &xyz) const
Definition NanoVDB.h:2254
Vec3T applyInverseJacobian(const Vec3T &xyz) const
Definition NanoVDB.h:2268
Vec3T applyMapF(const Vec3T &xyz) const
Definition NanoVDB.h:2249
Vec3T applyInverseMap(const Vec3T &xyz) const
Definition NanoVDB.h:2257
Vec3T applyJacobian(const Vec3T &xyz) const
Definition NanoVDB.h:2252
Vec3T applyMap(const Vec3T &xyz) const
Definition NanoVDB.h:2247
Vec3T applyInverseMapF(const Vec3T &xyz) const
Definition NanoVDB.h:2262
C++11 implementation of std::is_floating_point.
Definition NanoVDB.h:414