OpenVDB  12.0.1
GridHandle.h
Go to the documentation of this file.
1 // Copyright Contributors to the OpenVDB Project
2 // SPDX-License-Identifier: Apache-2.0
3 
4 /*!
5  \file nanovdb/GridHandle.h
6 
7  \author Ken Museth
8 
9  \date January 8, 2020
10 
11  \brief Defines GridHandle, which manages a host, and possibly a device,
12  memory buffer containing one or more NanoVDB grids.
13 */
14 
15 #ifndef NANOVDB_GRID_HANDLE_H_HAS_BEEN_INCLUDED
16 #define NANOVDB_GRID_HANDLE_H_HAS_BEEN_INCLUDED
17 
18 #include <fstream> // for std::ifstream
19 #include <iostream> // for std::cerr/cout
20 #include <vector>
21 #include <initializer_list>
22 
23 #include <nanovdb/NanoVDB.h>// for toGridType
24 #include <nanovdb/HostBuffer.h>
25 #include <nanovdb/tools/GridChecksum.h>// for updateGridCount
26 
27 namespace nanovdb {
28 
29 // --------------------------> GridHandle <------------------------------------
30 
32 
33 /// @brief This class serves to manage a buffer containing one or more NanoVDB Grids.
34 ///
35 /// @note It is important to note that this class does NOT depend on OpenVDB.
36 template<typename BufferT = HostBuffer>
38 {
39  std::vector<GridHandleMetaData> mMetaData;
40  BufferT mBuffer;
41 
42  template <typename T>
43  static T* no_const(const T* ptr) { return const_cast<T*>(ptr); }
44 
45 public:
46  using BufferType = BufferT;
47 
48  /// @brief Move constructor from a dual host-device buffer
49  /// @param buffer buffer containing one or more NanoGrids that will be moved into this GridHandle
50  /// @throw Will throw and error with the buffer does not contain a valid NanoGrid!
51  /// @note The implementation of this template specialization is in nanovdb/cuda/GridHandle.cuh since it requires CUDA
52  template<typename T = BufferT, typename util::enable_if<BufferTraits<T>::hasDeviceDual, int>::type = 0>
53  GridHandle(T&& buffer);
54 
55  /// @brief Move constructor from a host buffer
56  /// @param buffer buffer containing one or more NanoGrids that will be moved into this GridHandle
57  /// @throw Will throw and error with the buffer does not contain a valid NanoGrid!
58  template<typename T = BufferT, typename util::disable_if<BufferTraits<T>::hasDeviceDual, int>::type = 0>
59  GridHandle(T&& buffer);
60 
61  /// @brief Constructs an empty GridHandle
62  GridHandle() = default;
63 
64  /// @brief Disallow copy-construction
65  GridHandle(const GridHandle&) = delete;
66 
67  /// @brief Move copy-constructor
68  GridHandle(GridHandle&& other) noexcept {
69  mBuffer = std::move(other.mBuffer);
70  mMetaData = std::move(other.mMetaData);
71  }
72 
73  /// @brief clear this GridHandle to an empty handle
74  void reset() {
75  mBuffer.clear();
76  mMetaData.clear();
77  }
78 
79  /// @brief Disallow copy assignment operation
80  GridHandle& operator=(const GridHandle&) = delete;
81 
82  /// @brief Move copy assignment operation
83  GridHandle& operator=(GridHandle&& other) noexcept {
84  mBuffer = std::move(other.mBuffer);
85  mMetaData = std::move(other.mMetaData);
86  return *this;
87  }
88 
89  /// @brief Performs a deep copy of the GridHandle, possibly templated on a different buffer type
90  /// @tparam OtherBufferT Buffer type of the deep copy
91  /// @param buffer optional buffer used for allocation
92  /// @return A new handle of the specified buffer type that contains a deep copy of the current handle
93  template <typename OtherBufferT = HostBuffer>
94  GridHandle<OtherBufferT> copy(const OtherBufferT& buffer = OtherBufferT()) const;
95 
96  /// @brief Return a reference to the buffer
97  BufferT& buffer() { return mBuffer; }
98 
99  /// @brief Return a const reference to the buffer
100  const BufferT& buffer() const { return mBuffer; }
101 
102  /// @brief Returns a non-const pointer to the data.
103  /// @warning Note that the return pointer can be NULL if the GridHandle was not initialized
104  void* data() { return mBuffer.data(); }
105 
106  /// @brief Returns a const pointer to the data.
107  /// @warning Note that the return pointer can be NULL if the GridHandle was not initialized
108  const void* data() const { return mBuffer.data(); }
109 
110  template<typename U = BufferT>
111  typename util::enable_if<BufferTraits<U>::hasDeviceDual, const void*>::type
112  deviceData() const { return mBuffer.deviceData(); }
113  template<typename U = BufferT>
115  deviceData() { return mBuffer.deviceData(); }
116  template<typename U = BufferT>
118  deviceData(int device) { return mBuffer.deviceData(device); }
119 
120  //@{
121  /// @brief Returns the size in bytes of the raw memory buffer managed by this GridHandle.
122  [[deprecated("Use GridHandle::bufferSize instead.")]] uint64_t size() const { return mBuffer.size(); }
123  uint64_t bufferSize() const { return mBuffer.size(); }
124  //@}
125 
126  //@{
127  /// @brief Return true if this handle is empty, i.e. has no allocated memory
128  bool empty() const { return mBuffer.size() == 0; }
129  bool isEmpty() const { return mBuffer.size() == 0; }
130  //@}
131 
132  /// @brief Return true if this handle is not empty, i.e. contains at least one grid
133  operator bool() const { return !this->empty(); }
134 
135  /// @brief Returns a const host pointer to the @a n'th NanoVDB grid encoded in this GridHandle.
136  /// @tparam ValueT Value type of the grid point to be returned
137  /// @param n Index of the (host) grid pointer to be returned
138  /// @warning Note that the return pointer can be NULL if the GridHandle no host grid, @a n is invalid
139  /// or if the template parameter does not match the specified grid!
140  template<typename ValueT>
141  const NanoGrid<ValueT>* grid(uint32_t n = 0) const;
142 
143  /// @brief Returns a host pointer to the @a n'th NanoVDB grid encoded in this GridHandle.
144  /// @tparam ValueT Value type of the grid point to be returned
145  /// @param n Index of the (host) grid pointer to be returned
146  /// @warning Note that the return pointer can be NULL if the GridHandle no host grid, @a n is invalid
147  /// or if the template parameter does not match the specified grid!
148  template<typename ValueT>
149  NanoGrid<ValueT>* grid(uint32_t n = 0) {return const_cast<NanoGrid<ValueT>*>(static_cast<const GridHandle*>(this)->template grid<ValueT>(n));}
150 
151  /// @brief Return a const pointer to the @a n'th grid encoded in this GridHandle on the device, e.g. GPU
152  /// @tparam ValueT Value type of the grid point to be returned
153  /// @param n Index of the (device) grid pointer to be returned
154  /// @warning Note that the return pointer can be NULL if the GridHandle has no device grid, @a n is invalid,
155  /// or if the template parameter does not match the specified grid.
156  template<typename ValueT, typename U = BufferT>
158  deviceGrid(uint32_t n=0) const;
159 
160  /// @brief Return a const pointer to the @a n'th grid encoded in this GridHandle on the device, e.g. GPU
161  /// @tparam ValueT Value type of the grid point to be returned
162  /// @param n Index of the grid pointer to be returned
163  /// @param verbose if non-zero error messages will be printed in case something failed
164  /// @warning Note that the return pointer can be NULL if the GridHandle was not initialized, @a n is invalid,
165  /// or if the template parameter does not match the specified grid.
166  template<typename ValueT, typename U = BufferT>
167  typename util::enable_if<BufferTraits<U>::hasDeviceDual, NanoGrid<ValueT>*>::type
168  deviceGrid(uint32_t n=0){return const_cast<NanoGrid<ValueT>*>(static_cast<const GridHandle*>(this)->template deviceGrid<ValueT>(n));}
169 
170  /// @brief Upload the grid to the device, e.g. from CPU to GPU
171  /// @note This method is only available if the buffer supports devices
172  template<typename U = BufferT>
173  typename util::enable_if<BufferTraits<U>::hasDeviceDual, void>::type
174  deviceUpload(void* stream, bool sync = true) { mBuffer.deviceUpload(stream, sync); }
175 
176  /// @brief Upload the host buffer to a specefic device buffer. It device buffer doesn't exist it's created first
177  /// @param device Device to upload host data to
178  /// @param stream cuda stream
179  /// @param sync if false the memory copy is asynchronous
180  template<typename U = BufferT>
181  typename util::enable_if<BufferTraits<U>::hasDeviceDual, void>::type
182  deviceUpload(int device = 0, void* stream = nullptr, bool sync = true) { mBuffer.deviceUpload(device, stream, sync); }
183 
184  /// @brief Download the grid to from the device, e.g. from GPU to CPU
185  /// @note This method is only available if the buffer supports devices
186  template<typename U = BufferT>
187  typename util::enable_if<BufferTraits<U>::hasDeviceDual, void>::type
188  deviceDownload(void* stream, bool sync = true) { mBuffer.deviceDownload(stream, sync); }
189 
190  template<typename U = BufferT>
191  typename util::enable_if<BufferTraits<U>::hasDeviceDual, void>::type
192  deviceDownload(int device = 0, void* stream = nullptr, bool sync = true) { mBuffer.deviceDownload(device, stream, sync); }
193 
194  /// @brief Check if the buffer is this handle has any padding, i.e. if the buffer is larger than the combined size of all its grids
195  /// @return true is the combined size of all grid is smaller than the buffer size
196  bool isPadded() const {return mMetaData.empty() ? false : mMetaData.back().offset + mMetaData.back().size != mBuffer.size();}
197 
198  /// @brief Return the total number of grids contained in this buffer
199  uint32_t gridCount() const {return static_cast<uint32_t>(mMetaData.size());}
200 
201  /// @brief Return the grid size of the @a n'th grid in this GridHandle
202  /// @param n index of the grid (assumed to be less than gridCount())
203  /// @return Return the byte size of the specified grid
204  uint64_t gridSize(uint32_t n = 0) const {return mMetaData[n].size; }
205 
206  /// @brief compute the total sum of memory footprints of all the grids in this buffer
207  /// @return the number of bytes occupied by all grids associated with this buffer
208  uint64_t totalGridSize() const {
209  uint64_t sum = 0;
210  for (auto &m : mMetaData) sum += m.size;
211  NANOVDB_ASSERT(sum <= mBuffer.size());
212  return sum;
213  }
214 
215  /// @brief compute the size of unusedstorage in this buffer
216  /// @return the number of unused bytes in this buffer.
217  uint64_t freeSize() const {return mBuffer.size() - this->totalGridSize();}
218 
219  /// @brief Test if this buffer has any unused storage left, i.e. memory not occupied by grids
220  /// @return true if there is no extra storage left in this buffer, i.e. empty or fully occupied with grids
221  bool isFull() const { return this->totalGridSize() == mBuffer.size(); }
222 
223  /// @brief Return the GridType of the @a n'th grid in this GridHandle
224  /// @param n index of the grid (assumed to be less than gridCount())
225  /// @return Return the GridType of the specified grid
226  GridType gridType(uint32_t n = 0) const {return mMetaData[n].gridType; }
227 
228  /// @brief Access to the GridData of the n'th grid in the current handle
229  /// @param n zero-based ID of the grid
230  /// @return Const pointer to the n'th GridData in the current handle
231  const GridData* gridData(uint32_t n = 0) const;
232 
233  /// @brief Returns a const point to the @a n'th grid meta data
234  /// @param n zero-based ID of the grid
235  /// @warning Note that the return pointer can be NULL if the GridHandle was not initialized
236  const GridMetaData* gridMetaData(uint32_t n = 0) const;
237 
238  /// @brief Write a specific grid in this buffer to an output stream
239  /// @param os output stream that the buffer will be written to
240  /// @param n zero-based index of the grid to be written to stream
241  void write(std::ostream& os, uint32_t n) const {
242  if (const GridData* data = this->gridData(n)) {
243  os.write((const char*)data, data->mGridSize);
244  } else {
245  throw std::runtime_error("GridHandle does not contain a #" + std::to_string(n) + " grid");
246  }
247  }
248 
249  /// @brief Write the entire grid buffer to an output stream
250  /// @param os output stream that the buffer will be written to
251  void write(std::ostream& os) const {
252  for (uint32_t n=0; n<this->gridCount(); ++n) this->write(os, n);
253  }
254 
255  /// @brief Write this entire grid buffer to a file
256  /// @param fileName string name of the output file
257  void write(const std::string &fileName) const {
258  std::ofstream os(fileName, std::ios::out | std::ios::binary | std::ios::trunc);
259  if (!os.is_open()) throw std::ios_base::failure("Unable to open file named \"" + fileName + "\" for output");
260  this->write(os);
261  }
262 
263  /// @brief Write a specific grid to file
264  /// @param fileName string name of the output file
265  /// @param n zero-based index of the grid to be written to file
266  void write(const std::string &fileName, uint32_t n) const {
267  std::ofstream os(fileName, std::ios::out | std::ios::binary | std::ios::trunc);
268  if (!os.is_open()) throw std::ios_base::failure("Unable to open file named \"" + fileName + "\" for output");
269  this->write(os, n);
270  }
271 
272  /// @brief Read an entire raw grid buffer from an input stream
273  /// @param is input stream containing a raw grid buffer
274  /// @param pool optional pool from which to allocate the new grid buffer
275  /// @throw Will throw a std::logic_error if the stream does not contain a valid raw grid
276  void read(std::istream& is, const BufferT& pool = BufferT());
277 
278  /// @brief Read a specific grid from an input stream containing a raw grid buffer
279  /// @param is input stream containing a raw grid buffer
280  /// @param n zero-based index of the grid to be read
281  /// @param pool optional pool from which to allocate the new grid buffer
282  /// @throw Will throw a std::logic_error if the stream does not contain a valid raw grid
283  void read(std::istream& is, uint32_t n, const BufferT& pool = BufferT());
284 
285  /// @brief Read a specific grid from an input stream containing a raw grid buffer
286  /// @param is input stream containing a raw grid buffer
287  /// @param gridName string name of the grid to be read
288  /// @param pool optional pool from which to allocate the new grid buffer
289  /// @throw Will throw a std::logic_error if the stream does not contain a valid raw grid with the speficied name
290  void read(std::istream& is, const std::string &gridName, const BufferT& pool = BufferT());
291 
292  /// @brief Read a raw grid buffer from a file
293  /// @param filename string name of the input file containing a raw grid buffer
294  /// @param pool optional pool from which to allocate the new grid buffe
295  void read(const std::string &fileName, const BufferT& pool = BufferT()) {
296  std::ifstream is(fileName, std::ios::in | std::ios::binary);
297  if (!is.is_open()) throw std::ios_base::failure("Unable to open file named \"" + fileName + "\" for input");
298  this->read(is, pool);
299  }
300 
301  /// @brief Read a specific grid from a file containing a raw grid buffer
302  /// @param filename string name of the input file containing a raw grid buffer
303  /// @param n zero-based index of the grid to be read
304  /// @param pool optional pool from which to allocate the new grid buffer
305  /// @throw Will throw a std::ios_base::failure if the file does not exist and a
306  /// std::logic_error if the files does not contain a valid raw grid
307  void read(const std::string &fileName, uint32_t n, const BufferT& pool = BufferT()) {
308  std::ifstream is(fileName, std::ios::in | std::ios::binary);
309  if (!is.is_open()) throw std::ios_base::failure("Unable to open file named \"" + fileName + "\" for input");
310  this->read(is, n, pool);
311  }
312 
313  /// @brief Read a specific grid from a file containing a raw grid buffer
314  /// @param filename string name of the input file containing a raw grid buffer
315  /// @param gridName string name of the grid to be read
316  /// @param pool optional pool from which to allocate the new grid buffer
317  /// @throw Will throw a std::ios_base::failure if the file does not exist and a
318  /// std::logic_error if the files does not contain a valid raw grid withe the specified name
319  void read(const std::string &fileName, const std::string &gridName, const BufferT& pool = BufferT()) {
320  std::ifstream is(fileName, std::ios::in | std::ios::binary);
321  if (!is.is_open()) throw std::ios_base::failure("Unable to open file named \"" + fileName + "\" for input");
322  this->read(is, gridName, pool);
323  }
324 }; // GridHandle
325 
326 // --------------------------> Implementation of private methods in GridHandle <------------------------------------
327 
328 template<typename BufferT>
329 inline const GridData* GridHandle<BufferT>::gridData(uint32_t n) const
330 {
331  const void *data = this->data();
332  if (data == nullptr || n >= mMetaData.size()) return nullptr;
333  return util::PtrAdd<GridData>(data, mMetaData[n].offset);
334 }// const GridData* GridHandle<BufferT>::gridData(uint32_t n) const
335 
336 template<typename BufferT>
337 inline const GridMetaData* GridHandle<BufferT>::gridMetaData(uint32_t n) const
338 {
339  const auto *data = this->data();
340  if (data == nullptr || n >= mMetaData.size()) return nullptr;
341  return util::PtrAdd<GridMetaData>(data, mMetaData[n].offset);
342 }// const GridMetaData* GridHandle<BufferT>::gridMetaData(uint32_t n) const
343 
345 {
346  uint64_t offset = 0;
347  for (auto *p=meta, *q=p+data->mGridCount; p!=q; ++p) {
348  *p = {offset, data->mGridSize, data->mGridType};
349  offset += p->size;
350  data = util::PtrAdd<GridData>(data, p->size);
351  }
352 }// void cpyGridHandleMeta(const GridData *data, GridHandleMetaData *meta)
353 
354 // template specialization of move constructor from a host buffer
355 template<typename BufferT>
356 template<typename T, typename util::disable_if<BufferTraits<T>::hasDeviceDual, int>::type>
358 {
359  static_assert(util::is_same<T,BufferT>::value, "Expected U==BufferT");
360  mBuffer = std::move(buffer);
361  if (auto *data = reinterpret_cast<const GridData*>(mBuffer.data())) {
362  if (!data->isValid()) throw std::runtime_error("GridHandle was constructed with an invalid host buffer");
363  mMetaData.resize(data->mGridCount);
364  cpyGridHandleMeta(data, mMetaData.data());
365  }
366 }// GridHandle<BufferT>::GridHandle(T&& buffer)
367 
368 template<typename BufferT>
369 template <typename OtherBufferT>
370 inline GridHandle<OtherBufferT> GridHandle<BufferT>::copy(const OtherBufferT& other) const
371 {
372  if (mBuffer.isEmpty()) return GridHandle<OtherBufferT>();// return an empty handle
373  auto buffer = OtherBufferT::create(mBuffer.size(), &other);
374  std::memcpy(buffer.data(), mBuffer.data(), mBuffer.size());// deep copy of buffer
375  return GridHandle<OtherBufferT>(std::move(buffer));
376 }// GridHandle<OtherBufferT> GridHandle<BufferT>::copy(const OtherBufferT& other) const
377 
378 template<typename BufferT>
379 template<typename ValueT>
380 inline const NanoGrid<ValueT>* GridHandle<BufferT>::grid(uint32_t n) const
381 {
382  const void *data = mBuffer.data();
383  if (data == nullptr || n >= mMetaData.size() || mMetaData[n].gridType != toGridType<ValueT>()) return nullptr;
384  return util::PtrAdd<NanoGrid<ValueT>>(data, mMetaData[n].offset);
385 }// const NanoGrid<ValueT>* GridHandle<BufferT>::grid(uint32_t n) const
386 
387 template<typename BufferT>
388 template<typename ValueT, typename U>
389 inline typename util::enable_if<BufferTraits<U>::hasDeviceDual, const NanoGrid<ValueT>*>::type
391 {
392  const void *data = mBuffer.deviceData();
393  if (data == nullptr || n >= mMetaData.size() || mMetaData[n].gridType != toGridType<ValueT>()) return nullptr;
394  return util::PtrAdd<NanoGrid<ValueT>>(data, mMetaData[n].offset);
395 }// GridHandle<BufferT>::deviceGrid(uint32_t n) cons
396 
397 template<typename BufferT>
398 void GridHandle<BufferT>::read(std::istream& is, const BufferT& pool)
399 {
400  GridData data;
401  is.read((char*)&data, sizeof(GridData));
402  if (data.isValid()) {
403  uint64_t size = data.mGridSize, sum = 0u;
404  while(data.mGridIndex + 1u < data.mGridCount) {// loop over remaining raw grids in stream
405  is.seekg(data.mGridSize - sizeof(GridData), std::ios::cur);// skip grid
406  is.read((char*)&data, sizeof(GridData));
407  sum += data.mGridSize;
408  }
409  auto buffer = BufferT::create(size + sum, &pool);
410  is.seekg(-int64_t(sum + sizeof(GridData)), std::ios::cur);// rewind to start
411  is.read((char*)(buffer.data()), buffer.size());
412  *this = GridHandle(std::move(buffer));
413  } else {
414  is.seekg(-sizeof(GridData), std::ios::cur);// rewind
415  throw std::logic_error("This stream does not contain a valid raw grid buffer");
416  }
417 }// void GridHandle<BufferT>::read(std::istream& is, const BufferT& pool)
418 
419 template<typename BufferT>
420 void GridHandle<BufferT>::read(std::istream& is, uint32_t n, const BufferT& pool)
421 {
422  GridData data;
423  is.read((char*)&data, sizeof(GridData));
424  if (data.isValid()) {
425  if (n>=data.mGridCount) throw std::runtime_error("stream does not contain a #" + std::to_string(n) + " grid");
426  while(data.mGridIndex != n) {
427  is.seekg(data.mGridSize - sizeof(GridData), std::ios::cur);// skip grid
428  is.read((char*)&data, sizeof(GridData));
429  }
430  auto buffer = BufferT::create(data.mGridSize, &pool);
431  is.seekg(-sizeof(GridData), std::ios::cur);// rewind
432  is.read((char*)(buffer.data()), data.mGridSize);
433  tools::updateGridCount((GridData*)buffer.data(), 0u, 1u);
434  *this = GridHandle(std::move(buffer));
435  } else {
436  is.seekg(-sizeof(GridData), std::ios::cur);// rewind sizeof(GridData) bytes to undo initial read
437  throw std::logic_error("This file does not contain a valid raw buffer");
438  }
439 }// void GridHandle<BufferT>::read(std::istream& is, uint32_t n, const BufferT& pool)
440 
441 template<typename BufferT>
442 void GridHandle<BufferT>::read(std::istream& is, const std::string &gridName, const BufferT& pool)
443 {
444  static const std::streamsize byteSize = sizeof(GridData);
445  GridData data;
446  is.read((char*)&data, byteSize);
447  is.seekg(-byteSize, std::ios::cur);// rewind
448  if (data.isValid()) {
449  uint32_t n = 0;
450  while(data.mGridName != gridName && n++ < data.mGridCount) {
451  is.seekg(data.mGridSize, std::ios::cur);// skip grid
452  is.read((char*)&data, byteSize);// read sizeof(GridData) bytes
453  is.seekg(-byteSize, std::ios::cur);// rewind
454  }
455  if (n>data.mGridCount) throw std::runtime_error("No raw grid named \""+gridName+"\"");
456  auto buffer = BufferT::create(data.mGridSize, &pool);
457  is.read((char*)(buffer.data()), data.mGridSize);
458  tools::updateGridCount((GridData*)buffer.data(), 0u, 1u);
459  *this = GridHandle(std::move(buffer));
460  } else {
461  throw std::logic_error("This file does not contain a valid raw buffer");
462  }
463 }// void GridHandle<BufferT>::read(std::istream& is, const std::string &gridName n, const BufferT& pool)
464 
465 // --------------------------> free-standing functions <------------------------------------
466 
467 /// @brief Split all grids in a single GridHandle into a vector of multiple GridHandles each with a single grid
468 /// @tparam BufferT Type of the input and output grid buffers
469 /// @param handle GridHandle with grids that will be slip into individual GridHandles
470 /// @param pool optional pool used for allocation of output GridHandle
471 /// @return Vector of GridHandles each containing a single grid
472 template<typename BufferT, template <class, class...> class VectorT = std::vector>
473 inline VectorT<GridHandle<BufferT>>
474 splitGrids(const GridHandle<BufferT> &handle, const BufferT* other = nullptr)
475 {
476  using HandleT = GridHandle<BufferT>;
477  const void *ptr = handle.data();
478  if (ptr == nullptr) return VectorT<HandleT>();
479  VectorT<HandleT> handles(handle.gridCount());
480  for (auto &h : handles) {
481  const GridData *src = reinterpret_cast<const GridData*>(ptr);
482  NANOVDB_ASSERT(src->isValid());
483  auto buffer = BufferT::create(src->mGridSize, other);
484  GridData *dst = reinterpret_cast<GridData*>(buffer.data());
485  std::memcpy(dst, src, src->mGridSize);
486  tools::updateGridCount(dst, 0u, 1u);
487  h = HandleT(std::move(buffer));
488  ptr = util::PtrAdd(ptr, src->mGridSize);
489  }
490  return std::move(handles);
491 }// splitGrids
492 
493 /// @brief Combines (or merges) multiple GridHandles into a single GridHandle containing all grids
494 /// @tparam BufferT Type of the input and output grid buffers
495 /// @param handles Vector of GridHandles to be combined
496 /// @param pool optional pool used for allocation of output GridHandle
497 /// @return single GridHandle containing all input grids
498 template<typename BufferT, template <class, class...> class VectorT>
499 inline GridHandle<BufferT>
500 mergeGrids(const VectorT<GridHandle<BufferT>> &handles, const BufferT* pool = nullptr)
501 {
502  uint64_t size = 0u;
503  uint32_t counter = 0u, gridCount = 0u;
504  for (auto &h : handles) {
505  gridCount += h.gridCount();
506  for (uint32_t n=0; n<h.gridCount(); ++n) size += h.gridSize(n);
507  }
508  auto buffer = BufferT::create(size, pool);
509  void *dst = buffer.data();
510  for (auto &h : handles) {
511  const void *src = h.data();
512  for (uint32_t n=0; n<h.gridCount(); ++n) {
513  std::memcpy(dst, src, h.gridSize(n));
514  GridData *data = reinterpret_cast<GridData*>(dst);
515  NANOVDB_ASSERT(data->isValid());
516  tools::updateGridCount(data, counter++, gridCount);
517  dst = util::PtrAdd(dst, data->mGridSize);
518  src = util::PtrAdd(src, data->mGridSize);
519  }
520  }
521  return GridHandle<BufferT>(std::move(buffer));
522 }// mergeGrids
523 
524 } // namespace nanovdb
525 
526 #if defined(__CUDACC__)
527 #include <nanovdb/cuda/GridHandle.cuh>
528 #endif// defined(__CUDACC__)
529 
530 #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:122
Highest level of the data structure. Contains a tree and a world->index transform (that currently onl...
Definition: NanoVDB.h:2045
uint64_t freeSize() const
compute the size of unusedstorage in this buffer
Definition: GridHandle.h:217
void write(const std::string &fileName, uint32_t n) const
Write a specific grid to file.
Definition: GridHandle.h:266
char mGridName[MaxNameSize]
Definition: NanoVDB.h:1854
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&#39;th NanoVDB grid encoded in this GridHandle.
Definition: GridHandle.h:149
uint64_t mGridSize
Definition: NanoVDB.h:1853
void reset()
clear this GridHandle to an empty handle
Definition: GridHandle.h:74
uint32_t mGridCount
Definition: NanoVDB.h:1852
__hostdev__ bool isValid() const
return true if the magic number and the version are both valid
Definition: NanoVDB.h:1898
#define __hostdev__
Definition: Util.h:73
util::enable_if< BufferTraits< U >::hasDeviceDual, void * >::type deviceData()
Definition: GridHandle.h:115
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:370
BufferT & buffer()
Return a reference to the buffer.
Definition: GridHandle.h:97
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&#39;th grid encoded in this GridHandle on the device, e...
Definition: GridHandle.h:168
Definition: GridHandle.h:27
GridType mGridType
Definition: NanoVDB.h:1859
void read(std::istream &is, const BufferT &pool=BufferT())
Read an entire raw grid buffer from an input stream.
Definition: GridHandle.h:398
bool empty() const
Return true if this handle is empty, i.e. has no allocated memory.
Definition: GridHandle.h:128
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:474
GridType gridType(uint32_t n=0) const
Return the GridType of the n&#39;th grid in this GridHandle.
Definition: GridHandle.h:226
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:196
util::enable_if< BufferTraits< U >::hasDeviceDual, void >::type deviceDownload(void *stream, bool sync=true)
Download the grid to from the device, e.g. from GPU to CPU.
Definition: GridHandle.h:188
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:319
uint32_t gridCount() const
Return the total number of grids contained in this buffer.
Definition: GridHandle.h:199
void read(const std::string &fileName, const BufferT &pool=BufferT())
Read a raw grid buffer from a file.
Definition: GridHandle.h:295
const NanoGrid< ValueT > * grid(uint32_t n=0) const
Returns a const host pointer to the n&#39;th NanoVDB grid encoded in this GridHandle. ...
Definition: GridHandle.h:380
void write(std::ostream &os) const
Write the entire grid buffer to an output stream.
Definition: GridHandle.h:251
uint64_t bufferSize() const
Returns the size in bytes of the raw memory buffer managed by this GridHandle.
Definition: GridHandle.h:123
bool empty(const char *str)
tests if a c-string str is empty, that is its first value is &#39;\0&#39;
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:500
uint64_t gridSize(uint32_t n=0) const
Return the grid size of the n&#39;th grid in this GridHandle.
Definition: GridHandle.h:204
util::enable_if< BufferTraits< U >::hasDeviceDual, void * >::type deviceData(int device)
Definition: GridHandle.h:118
util::enable_if< BufferTraits< U >::hasDeviceDual, void >::type deviceUpload(void *stream, bool sync=true)
Upload the grid to the device, e.g. from CPU to GPU.
Definition: GridHandle.h:174
bool isFull() const
Test if this buffer has any unused storage left, i.e. memory not occupied by grids.
Definition: GridHandle.h:221
util::enable_if< BufferTraits< U >::hasDeviceDual, const void * >::type deviceData() const
Definition: GridHandle.h:112
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:307
util::enable_if< BufferTraits< U >::hasDeviceDual, void >::type deviceUpload(int device=0, void *stream=nullptr, bool sync=true)
Upload the host buffer to a specefic device buffer. It device buffer doesn&#39;t exist it&#39;s created first...
Definition: GridHandle.h:182
OutGridT const XformOp bool bool
Definition: ValueTransformer.h:609
uint32_t mGridIndex
Definition: NanoVDB.h:1851
void write(const std::string &fileName) const
Write this entire grid buffer to a file.
Definition: GridHandle.h:257
BufferT BufferType
Definition: GridHandle.h:46
const BufferT & buffer() const
Return a const reference to the buffer.
Definition: GridHandle.h:100
bool isEmpty() const
Return true if this handle is empty, i.e. has no allocated memory.
Definition: GridHandle.h:129
#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&#39;th grid encoded in this GridHandle on the device, e...
Definition: GridHandle.h:390
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&#39;th grid in the current handle.
Definition: GridHandle.h:329
GridHandle & operator=(GridHandle &&other) noexcept
Move copy assignment operation.
Definition: GridHandle.h:83
void write(std::ostream &os, uint32_t n) const
Write a specific grid in this buffer to an output stream.
Definition: GridHandle.h:241
C++11 implementation of std::is_same.
Definition: Util.h:314
Definition: GridHandle.h:31
Struct with all the member data of the Grid (useful during serialization of an openvdb grid) ...
Definition: NanoVDB.h:1844
const void * data() const
Returns a const pointer to the data.
Definition: GridHandle.h:108
const GridMetaData * gridMetaData(uint32_t n=0) const
Returns a const point to the n&#39;th grid meta data.
Definition: GridHandle.h:337
uint64_t totalGridSize() const
compute the total sum of memory footprints of all the grids in this buffer
Definition: GridHandle.h:208
GridType gridType
Definition: GridHandle.h:31
This is a convenient class that allows for access to grid meta-data that are independent of the value...
Definition: NanoVDB.h:5928
util::enable_if< BufferTraits< U >::hasDeviceDual, void >::type deviceDownload(int device=0, void *stream=nullptr, bool sync=true)
Definition: GridHandle.h:192
uint64_t offset
Definition: GridHandle.h:31
uint64_t size
Definition: GridHandle.h:31
GridHandle(GridHandle &&other) noexcept
Move copy-constructor.
Definition: GridHandle.h:68
__hostdev__ void cpyGridHandleMeta(const GridData *data, GridHandleMetaData *meta)
Definition: GridHandle.h:344
void * data()
Returns a non-const pointer to the data.
Definition: GridHandle.h:104