14 #ifndef NANOVDB_GRIDCHECKSUM_H_HAS_BEEN_INCLUDED 15 #define NANOVDB_GRIDCHECKSUM_H_HAS_BEEN_INCLUDED 23 #include <type_traits> 25 #include "../NanoVDB.h" 43 template <
typename ValueT>
51 template <
typename ValueT>
58 template <
typename ValueT>
64 inline std::uint_fast32_t
crc32(
const void *data,
size_t size);
70 template <
typename IterT>
71 std::uint_fast32_t
crc32(IterT begin, IterT end);
76 using ByteT = std::uint_fast8_t;
77 using HashT = std::uint_fast32_t;
80 static std::array<HashT, 256> INIT()
83 auto kernel = [&n]()->HashT{
85 for (
int i = 0; i < 8; ++i) checksum = (checksum >> 1) ^ ((checksum & 0x1u) ? HashT{0xEDB88320uL} : 0);
88 std::array<HashT, 256> LUT{};
89 std::generate(LUT.begin(), LUT.end(), kernel);
95 static const HashT EMPTY = ~HashT{0} & HashT{0xFFFFFFFFuL};
99 void reset() { mChecksum = EMPTY; }
101 HashT
checksum()
const {
return HashT{0xFFFFFFFFuL} & ~mChecksum; }
103 template <
typename IterT>
106 static const auto LUT = INIT();
107 auto kernel = [](HashT
checksum, ByteT
value){
return LUT[(checksum ^
value) & 0xFFu] ^ (checksum >> 8);};
113 const ByteT *begin =
static_cast<const ByteT*
>(data);
114 this->operator()<
const ByteT*>(begin, begin + byteSize);
117 template <
typename T>
121 inline std::uint_fast32_t
crc32(
const void *data,
size_t byteSize)
128 template <
typename IterT>
129 inline std::uint_fast32_t
crc32(IterT begin, IterT end)
140 union {uint32_t mCRC[2]; uint64_t mChecksum; };
157 uint32_t
crc32(
int i)
const {assert(i==0 || i==1);
return mCRC[i]; }
161 bool isEmpty()
const {
return mChecksum == EMPTY; }
169 template <
typename ValueT>
177 template <
typename ValueT>
185 #else// the static asserts below generate compiler warnings 186 static_assert(offsetof(
GridData, mMagic) == 0,
"Unexpected offset to magic number");
187 static_assert(offsetof(
GridData, mChecksum) == 8,
"Unexpected offset to checksum");
188 static_assert(offsetof(
GridData, mVersion) == 16,
"Unexpected offset to version number");
190 static const size_t offset = 16;
196 const auto &tree = grid.
tree();
197 const auto &root = tree.root();
201 const uint8_t *begin =
reinterpret_cast<const uint8_t*
>(&grid);
202 const uint8_t *end = begin + grid.
memUsage() + tree.memUsage() + root.memUsage();
203 crc(begin + offset, end);
210 auto *nodeMgr = nodeMgrHandle.template mgr<ValueT>();
212 const auto nodeCount = tree.nodeCount(0) + tree.nodeCount(1) + tree.nodeCount(2);
213 std::vector<std::uint_fast32_t> checksums(nodeCount, 0);
216 auto kernel2 = [&](
const Range1D &r) {
218 std::uint_fast32_t *p = checksums.data() + r.begin();
219 for (
auto i = r.begin(); i != r.end(); ++i) {
220 const auto &node = nodeMgr->upper(static_cast<uint32_t>(i));
228 auto kernel1 = [&](
const Range1D &r) {
230 std::uint_fast32_t *p = checksums.data() + r.begin() + tree.nodeCount(2);
231 for (
auto i = r.begin(); i != r.end(); ++i) {
232 const auto &node = nodeMgr->lower(static_cast<uint32_t>(i));
240 auto kernel0 = [&](
const Range1D &r) {
242 std::uint_fast32_t *p = checksums.data() + r.begin() + tree.nodeCount(1) + tree.nodeCount(2);
243 for (
auto i = r.begin(); i != r.end(); ++i) {
244 const auto &leaf = nodeMgr->leaf(static_cast<uint32_t>(i));
251 forEach(0, tree.nodeCount(2), 1, kernel2);
252 forEach(0, tree.nodeCount(1), 1, kernel1);
253 forEach(0, tree.nodeCount(0), 8, kernel0);
256 crc(checksums.data(),
sizeof(std::uint_fast32_t)*checksums.size() );
260 template <
typename ValueT>
268 template <
typename ValueT>
272 cs2(grid, cs1.mode() );
276 template <
typename ValueT>
286 #endif // NANOVDB_GRIDCHECKSUM_H_HAS_BEEN_INCLUDED
Highest level of the data structure. Contains a tree and a world->index transform (that currently onl...
Definition: NanoVDB.h:2555
Class that computes the Cyclic Redundancy Check (CRC)
Definition: GridChecksum.h:74
#define NANOVDB_OFFSETOF(CLASS, MEMBER)
Definition: NanoVDB.h:206
std::uint_fast32_t crc32(const void *data, size_t size)
Return the CRC32 checksum of the raw data of size.
Definition: GridChecksum.h:121
A unified wrapper for tbb::parallel_for and a naive std::thread fallback.
ChecksumMode
List of different modes for computing for a checksum.
Definition: GridChecksum.h:33
Defines two classes, a GridRegister the defines the value type (e.g. Double, Float etc) of a NanoVDB ...
Class that encapsulates two CRC32 checksums, one for the Grid, Tree and Root node meta data and one f...
Definition: GridChecksum.h:138
void operator()(const NanoGrid< ValueT > &grid, ChecksumMode mode=ChecksumMode::Full)
Definition: GridChecksum.h:178
const TreeT & tree() const
Return a const reference to the tree.
Definition: NanoVDB.h:2598
void forEach(RangeT range, const FuncT &func)
simple wrapper for tbb::parallel_for with a naive std fallback
Definition: ForEach.h:40
bool operator!=(const GridChecksum &rhs) const
Definition: GridChecksum.h:173
HashT checksum() const
Definition: GridChecksum.h:101
Definition: NanoVDB.h:208
bool operator==(const GridChecksum &rhs) const
Definition: GridChecksum.h:172
uint64_t checksum(const NanoGrid< ValueT > &grid, ChecksumMode mode=ChecksumMode::Default)
Return the (2 x CRC32) checksum of the specified grid.
Definition: GridChecksum.h:261
static const HashT EMPTY
Definition: GridChecksum.h:95
uint64_t mChecksum
Definition: GridChecksum.h:140
GridChecksum(uint32_t head, uint32_t tail)
Definition: GridChecksum.h:148
ValueT value
Definition: GridBuilder.h:1290
bool isEmpty() const
Definition: GridChecksum.h:161
void updateChecksum(NanoGrid< ValueT > &grid, ChecksumMode mode=ChecksumMode::Default)
Updates the checksum of a grid.
Definition: GridChecksum.h:277
bool isFull() const
Definition: GridChecksum.h:159
bool validateChecksum(const NanoGrid< ValueT > &grid, ChecksumMode mode=ChecksumMode::Default)
Return true if the checksum of the grid matches the expected value already encoded into the grid's me...
Definition: GridChecksum.h:269
DataType * data()
Definition: NanoVDB.h:2575
uint32_t crc32(int i) const
Definition: GridChecksum.h:157
#define NANOVDB_ASSERT(x)
Definition: NanoVDB.h:173
void operator()(const T &data)
Definition: GridChecksum.h:118
PromoteType< ValueT >::Highest accumulate(const PointDataTreeT &points, const std::string &attribute, const FilterT &filter=NullFilter())
Evaluates the total value of a point attribute.
Definition: PointStatistics.h:729
CRC32()
Definition: GridChecksum.h:97
uint64_t checksum() const
Return checksum of the grid buffer.
Definition: NanoVDB.h:2715
void operator()(const void *data, size_t byteSize)
Definition: GridChecksum.h:111
Struct with all the member data of the Grid (useful during serialization of an openvdb grid) ...
Definition: NanoVDB.h:2431
GridChecksum(uint64_t checksum, ChecksumMode mode=ChecksumMode::Full)
Definition: GridChecksum.h:150
NodeManagerHandle< BufferT > createNodeManager(const NanoGrid< BuildT > &grid, const BufferT &buffer=BufferT())
brief Construct a NodeManager and return its handle
Definition: NodeManager.h:289
static uint64_t memUsage()
Return memory usage in bytes for this class only.
Definition: NanoVDB.h:2580
void reset()
Definition: GridChecksum.h:99
GridChecksum()
Definition: GridChecksum.h:146
static bool isValid(const void *p)
return true if the specified pointer is aligned and not NULL
Definition: NanoVDB.h:504
ChecksumMode mode() const
Definition: GridChecksum.h:163
void operator()(IterT begin, IterT end)
Definition: GridChecksum.h:104
uint64_t checksum() const
Definition: GridChecksum.h:155