15 #ifndef NANOVDB_GRID_HANDLE_H_HAS_BEEN_INCLUDED 16 #define NANOVDB_GRID_HANDLE_H_HAS_BEEN_INCLUDED 21 #include <initializer_list> 25 #include <nanovdb/tools/GridChecksum.h> 36 template<
typename BufferT = HostBuffer>
39 std::vector<GridHandleMetaData> mMetaData;
43 static T* no_const(
const T* ptr) {
return const_cast<T*
>(ptr); }
51 template<typename T = BufferT, typename util::enable_if<BufferTraits<T>::hasDeviceDual,
int>::type = 0>
57 template<typename T = BufferT, typename util::disable_if<BufferTraits<T>::hasDeviceDual,
int>::type = 0>
68 mBuffer = std::move(other.mBuffer);
69 mMetaData = std::move(other.mMetaData);
83 mBuffer = std::move(other.mBuffer);
84 mMetaData = std::move(other.mMetaData);
92 template <
typename OtherBufferT = HostBuffer>
96 BufferT&
buffer() {
return mBuffer; }
99 const BufferT&
buffer()
const {
return mBuffer; }
103 void*
data() {
return mBuffer.data(); }
107 const void*
data()
const {
return mBuffer.data(); }
109 template<
typename U = BufferT>
112 template<
typename U = BufferT>
117 uint64_t
size()
const {
return mBuffer.size(); }
133 template<
typename ValueT>
141 template<
typename ValueT>
149 template<
typename ValueT,
typename U = BufferT>
151 deviceGrid(uint32_t n=0)
const;
159 template<
typename ValueT,
typename U = BufferT>
160 typename util::enable_if<BufferTraits<U>::hasDeviceDual,
NanoGrid<ValueT>*>::type
165 template<
typename U = BufferT>
166 typename util::enable_if<BufferTraits<U>::hasDeviceDual,
void>::type
167 deviceUpload(
void* stream =
nullptr,
bool sync =
true) { mBuffer.deviceUpload(stream, sync); }
171 template<
typename U = BufferT>
172 typename util::enable_if<BufferTraits<U>::hasDeviceDual,
void>::type
173 deviceDownload(
void* stream =
nullptr,
bool sync =
true) { mBuffer.deviceDownload(stream, sync); }
177 bool isPadded()
const {
return mMetaData.empty() ?
false : mMetaData.back().offset + mMetaData.back().size != mBuffer.size();}
180 uint32_t
gridCount()
const {
return static_cast<uint32_t
>(mMetaData.size());}
185 uint64_t
gridSize(uint32_t n = 0)
const {
return mMetaData[n].size; }
195 const GridData* gridData(uint32_t n = 0)
const;
205 void write(std::ostream& os, uint32_t n)
const {
206 if (
const GridData* data = this->gridData(n)) {
207 os.write((
const char*)data, data->mGridSize);
209 throw std::runtime_error(
"GridHandle does not contain a #" + std::to_string(n) +
" grid");
215 void write(std::ostream& os)
const {
216 for (uint32_t n=0; n<this->gridCount(); ++n) this->write(os, n);
221 void write(
const std::string &fileName)
const {
222 std::ofstream os(fileName, std::ios::out | std::ios::binary | std::ios::trunc);
223 if (!os.is_open())
throw std::ios_base::failure(
"Unable to open file named \"" + fileName +
"\" for output");
230 void write(
const std::string &fileName, uint32_t n)
const {
231 std::ofstream os(fileName, std::ios::out | std::ios::binary | std::ios::trunc);
232 if (!os.is_open())
throw std::ios_base::failure(
"Unable to open file named \"" + fileName +
"\" for output");
240 void read(std::istream& is,
const BufferT& pool = BufferT());
247 void read(std::istream& is, uint32_t n,
const BufferT& pool = BufferT());
254 void read(std::istream& is,
const std::string &gridName,
const BufferT& pool = BufferT());
259 void read(
const std::string &fileName,
const BufferT& pool = BufferT()) {
260 std::ifstream is(fileName, std::ios::in | std::ios::binary);
261 if (!is.is_open())
throw std::ios_base::failure(
"Unable to open file named \"" + fileName +
"\" for input");
262 this->read(is, pool);
271 void read(
const std::string &fileName, uint32_t n,
const BufferT& pool = BufferT()) {
272 std::ifstream is(fileName, std::ios::in | std::ios::binary);
273 if (!is.is_open())
throw std::ios_base::failure(
"Unable to open file named \"" + fileName +
"\" for input");
274 this->read(is, n, pool);
283 void read(
const std::string &fileName,
const std::string &gridName,
const BufferT& pool = BufferT()) {
284 std::ifstream is(fileName, std::ios::in | std::ios::binary);
285 if (!is.is_open())
throw std::ios_base::failure(
"Unable to open file named \"" + fileName +
"\" for input");
286 this->read(is, gridName, pool);
292 template<
typename BufferT>
295 const void *data = this->data();
296 if (data ==
nullptr || n >= mMetaData.size())
return nullptr;
297 return util::PtrAdd<GridData>(data, mMetaData[n].offset);
300 template<
typename BufferT>
303 const auto *data = this->data();
304 if (data ==
nullptr || n >= mMetaData.size())
return nullptr;
305 return util::PtrAdd<GridMetaData>(data, mMetaData[n].offset);
311 for (
auto *p=meta, *q=p+data->
mGridCount; p!=q; ++p) {
314 data = util::PtrAdd<GridData>(data, p->size);
318 template<
typename BufferT>
319 template<typename T, typename util::disable_if<BufferTraits<T>::hasDeviceDual,
int>::type>
323 mBuffer = std::move(buffer);
324 if (
auto *data = reinterpret_cast<const GridData*>(mBuffer.data())) {
325 if (!data->isValid())
throw std::runtime_error(
"GridHandle was constructed with an invalid host buffer");
326 mMetaData.resize(data->mGridCount);
331 template<
typename BufferT>
332 template <
typename OtherBufferT>
336 auto buffer = OtherBufferT::create(mBuffer.size(), &other);
337 std::memcpy(buffer.data(), mBuffer.data(), mBuffer.size());
341 template<
typename BufferT>
342 template<
typename ValueT>
345 const void *data = mBuffer.data();
346 if (data ==
nullptr || n >= mMetaData.size() || mMetaData[n].gridType != toGridType<ValueT>())
return nullptr;
347 return util::PtrAdd<NanoGrid<ValueT>>(data, mMetaData[n].offset);
350 template<
typename BufferT>
351 template<
typename ValueT,
typename U>
352 inline typename util::enable_if<BufferTraits<U>::hasDeviceDual,
const NanoGrid<ValueT>*>::type
355 const void *data = mBuffer.deviceData();
356 if (data ==
nullptr || n >= mMetaData.size() || mMetaData[n].gridType != toGridType<ValueT>())
return nullptr;
357 return util::PtrAdd<NanoGrid<ValueT>>(data, mMetaData[n].offset);
360 template<
typename BufferT>
364 is.read((
char*)&data,
sizeof(
GridData));
369 is.read((
char*)&data,
sizeof(
GridData));
372 auto buffer = BufferT::create(size + sum, &pool);
373 is.seekg(-int64_t(sum +
sizeof(
GridData)), std::ios::cur);
374 is.read((
char*)(buffer.data()), buffer.size());
377 is.seekg(-
sizeof(
GridData), std::ios::cur);
378 throw std::logic_error(
"This stream does not contain a valid raw grid buffer");
382 template<
typename BufferT>
386 is.read((
char*)&data,
sizeof(
GridData));
388 if (n>=data.
mGridCount)
throw std::runtime_error(
"stream does not contain a #" + std::to_string(n) +
" grid");
391 is.read((
char*)&data,
sizeof(
GridData));
393 auto buffer = BufferT::create(data.
mGridSize, &pool);
394 is.seekg(-
sizeof(
GridData), std::ios::cur);
395 is.read((
char*)(buffer.data()), data.
mGridSize);
396 tools::updateGridCount((
GridData*)buffer.data(), 0u, 1u);
399 is.seekg(-
sizeof(
GridData), std::ios::cur);
400 throw std::logic_error(
"This file does not contain a valid raw buffer");
404 template<
typename BufferT>
407 static const std::streamsize byteSize =
sizeof(
GridData);
409 is.read((
char*)&data, byteSize);
410 is.seekg(-byteSize, std::ios::cur);
415 is.read((
char*)&data, byteSize);
416 is.seekg(-byteSize, std::ios::cur);
418 if (n>data.
mGridCount)
throw std::runtime_error(
"No raw grid named \""+gridName+
"\"");
419 auto buffer = BufferT::create(data.
mGridSize, &pool);
420 is.read((
char*)(buffer.data()), data.
mGridSize);
421 tools::updateGridCount((
GridData*)buffer.data(), 0u, 1u);
424 throw std::logic_error(
"This file does not contain a valid raw buffer");
435 template<
typename BufferT,
template <
class,
class...>
class VectorT = std::vector>
436 inline VectorT<GridHandle<BufferT>>
440 const void *ptr = handle.
data();
441 if (ptr ==
nullptr)
return VectorT<HandleT>();
442 VectorT<HandleT> handles(handle.
gridCount());
443 for (
auto &h : handles) {
446 auto buffer = BufferT::create(src->
mGridSize, other);
449 tools::updateGridCount(dst, 0u, 1u);
450 h = HandleT(std::move(buffer));
453 return std::move(handles);
461 template<
typename BufferT,
template <
class,
class...>
class VectorT>
466 uint32_t counter = 0u, gridCount = 0u;
467 for (
auto &h : handles) {
468 gridCount += h.gridCount();
469 for (uint32_t n=0; n<h.gridCount(); ++n) size += h.gridSize(n);
471 auto buffer = BufferT::create(size, pool);
472 void *dst = buffer.data();
473 for (
auto &h : handles) {
474 const void *src = h.data();
475 for (uint32_t n=0; n<h.gridCount(); ++n) {
476 std::memcpy(dst, src, h.gridSize(n));
479 tools::updateGridCount(data, counter++, gridCount);
489 #if defined(__CUDACC__) 490 #include <nanovdb/cuda/GridHandle.cuh> 491 #endif// defined(__CUDACC__) 493 #endif // NANOVDB_GRID_HANDLE_H_HAS_BEEN_INCLUDED C++11 implementation of std::enable_if.
Definition: Util.h:335
uint64_t size() const
Returns the size in bytes of the raw memory buffer managed by this GridHandle.
Definition: GridHandle.h:117
Highest level of the data structure. Contains a tree and a world->index transform (that currently onl...
Definition: NanoVDB.h:2011
void write(const std::string &fileName, uint32_t n) const
Write a specific grid to file.
Definition: GridHandle.h:230
char mGridName[MaxNameSize]
Definition: NanoVDB.h:1820
HostBuffer - a buffer that contains a shared or private bump pool to either externally or internally ...
NanoGrid< ValueT > * grid(uint32_t n=0)
Returns a host pointer to the n'th NanoVDB grid encoded in this GridHandle.
Definition: GridHandle.h:142
uint64_t mGridSize
Definition: NanoVDB.h:1819
void reset()
clear this GridHandle to an empty handle
Definition: GridHandle.h:73
uint32_t mGridCount
Definition: NanoVDB.h:1818
__hostdev__ bool isValid() const
return true if the magic number and the version are both valid
Definition: NanoVDB.h:1864
#define __hostdev__
Definition: Util.h:73
util::enable_if< BufferTraits< U >::hasDeviceDual, void * >::type deviceData()
Definition: GridHandle.h:114
This class serves to manage a buffer containing one or more NanoVDB Grids.
Definition: GridHandle.h:37
GridHandle< OtherBufferT > copy(const OtherBufferT &buffer=OtherBufferT()) const
Performs a deep copy of the GridHandle, possibly templated on a different buffer type.
Definition: GridHandle.h:333
BufferT & buffer()
Return a reference to the buffer.
Definition: GridHandle.h:96
Implements a light-weight self-contained VDB data-structure in a single file! In other words...
util::enable_if< BufferTraits< U >::hasDeviceDual, NanoGrid< ValueT > * >::type deviceGrid(uint32_t n=0)
Return a const pointer to the n'th grid encoded in this GridHandle on the device, e...
Definition: GridHandle.h:161
Definition: GridHandle.h:27
GridType mGridType
Definition: NanoVDB.h:1825
void read(std::istream &is, const BufferT &pool=BufferT())
Read an entire raw grid buffer from an input stream.
Definition: GridHandle.h:361
bool empty() const
Return true if this handle is empty, i.e. has no allocated memory.
Definition: GridHandle.h:121
VectorT< GridHandle< BufferT > > splitGrids(const GridHandle< BufferT > &handle, const BufferT *other=nullptr)
Split all grids in a single GridHandle into a vector of multiple GridHandles each with a single grid...
Definition: GridHandle.h:437
GridType gridType(uint32_t n=0) const
Return the GridType of the n'th grid in this GridHandle.
Definition: GridHandle.h:190
util::enable_if< BufferTraits< U >::hasDeviceDual, void >::type deviceDownload(void *stream=nullptr, bool sync=true)
Download the grid to from the device, e.g. from GPU to CPU.
Definition: GridHandle.h:173
GridHandle()=default
Constructs an empty GridHandle.
bool isPadded() const
Check if the buffer is this handle has any padding, i.e. if the buffer is larger than the combined si...
Definition: GridHandle.h:177
void read(const std::string &fileName, const std::string &gridName, const BufferT &pool=BufferT())
Read a specific grid from a file containing a raw grid buffer.
Definition: GridHandle.h:283
uint32_t gridCount() const
Return the total number of grids contained in this buffer.
Definition: GridHandle.h:180
void read(const std::string &fileName, const BufferT &pool=BufferT())
Read a raw grid buffer from a file.
Definition: GridHandle.h:259
const NanoGrid< ValueT > * grid(uint32_t n=0) const
Returns a const host pointer to the n'th NanoVDB grid encoded in this GridHandle. ...
Definition: GridHandle.h:343
void write(std::ostream &os) const
Write the entire grid buffer to an output stream.
Definition: GridHandle.h:215
bool empty(const char *str)
tests if a c-string str is empty, that is its first value is '\0'
Definition: Util.h:144
GridHandle< BufferT > mergeGrids(const VectorT< GridHandle< BufferT >> &handles, const BufferT *pool=nullptr)
Combines (or merges) multiple GridHandles into a single GridHandle containing all grids...
Definition: GridHandle.h:463
uint64_t gridSize(uint32_t n=0) const
Return the grid size of the n'th grid in this GridHandle.
Definition: GridHandle.h:185
util::enable_if< BufferTraits< U >::hasDeviceDual, const void * >::type deviceData() const
Definition: GridHandle.h:111
void read(const std::string &fileName, uint32_t n, const BufferT &pool=BufferT())
Read a specific grid from a file containing a raw grid buffer.
Definition: GridHandle.h:271
uint32_t mGridIndex
Definition: NanoVDB.h:1817
void write(const std::string &fileName) const
Write this entire grid buffer to a file.
Definition: GridHandle.h:221
BufferT BufferType
Definition: GridHandle.h:46
const BufferT & buffer() const
Return a const reference to the buffer.
Definition: GridHandle.h:99
bool isEmpty() const
Return true if this handle is empty, i.e. has no allocated memory.
Definition: GridHandle.h:122
#define NANOVDB_ASSERT(x)
Definition: Util.h:50
GridType
List of types that are currently supported by NanoVDB.
Definition: NanoVDB.h:219
util::enable_if< BufferTraits< U >::hasDeviceDual, const NanoGrid< ValueT > * >::type deviceGrid(uint32_t n=0) const
Return a const pointer to the n'th grid encoded in this GridHandle on the device, e...
Definition: GridHandle.h:353
util::enable_if< BufferTraits< U >::hasDeviceDual, void >::type deviceUpload(void *stream=nullptr, bool sync=true)
Upload the grid to the device, e.g. from CPU to GPU.
Definition: GridHandle.h:167
static DstT * PtrAdd(void *p, int64_t offset)
Adds a byte offset to a non-const pointer to produce another non-const pointer.
Definition: Util.h:478
const GridData * gridData(uint32_t n=0) const
Access to the GridData of the n'th grid in the current handle.
Definition: GridHandle.h:293
GridHandle & operator=(GridHandle &&other) noexcept
Move copy assignment operation.
Definition: GridHandle.h:82
void write(std::ostream &os, uint32_t n) const
Write a specific grid in this buffer to an output stream.
Definition: GridHandle.h:205
C++11 implementation of std::is_same.
Definition: Util.h:314
Struct with all the member data of the Grid (useful during serialization of an openvdb grid) ...
Definition: NanoVDB.h:1810
const void * data() const
Returns a const pointer to the data.
Definition: GridHandle.h:107
const GridMetaData * gridMetaData(uint32_t n=0) const
Returns a const point to the n'th grid meta data.
Definition: GridHandle.h:301
GridHandle(GridHandle &&other) noexcept
Move copy-constructor.
Definition: GridHandle.h:67
__hostdev__ void cpyGridHandleMeta(const GridData *data, GridHandleMetaData *meta)
Definition: GridHandle.h:308
void * data()
Returns a non-const pointer to the data.
Definition: GridHandle.h:103