14#ifndef NANOVDB_GRIDCHECKSUM_H_HAS_BEEN_INCLUDED
15#define NANOVDB_GRIDCHECKSUM_H_HAS_BEEN_INCLUDED
25#include "../NanoVDB.h"
43template <
typename ValueT>
51template <
typename ValueT>
58template <
typename ValueT>
64inline std::uint_fast32_t
crc32(
const void *data,
size_t size);
70template <
typename IterT>
71std::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{
88 std::array<HashT, 256> LUT{};
89 std::generate(LUT.begin(), LUT.end(), kernel);
95 static const HashT
EMPTY = ~HashT{0} & HashT{0xFFFFFFFFuL};
101 HashT
checksum()
const {
return HashT{0xFFFFFFFFuL} & ~mChecksum; }
103 template <
typename IterT>
106 static const auto LUT = INIT();
113 const ByteT *begin =
static_cast<const ByteT*
>(data);
114 this->operator()<
const ByteT*>(begin, begin + byteSize);
118inline std::uint_fast32_t
crc32(
const void *data,
size_t byteSize)
125template <
typename IterT>
126inline std::uint_fast32_t
crc32(IterT begin, IterT end)
154 uint32_t
crc32(
int i)
const {assert(i==0 || i==1);
return mCRC[i]; }
166 template <
typename ValueT>
174template <
typename ValueT>
183 static_assert(offsetof(
GridData, mMagic) == 0,
"Unexpected offset to magic number");
184 static_assert(offsetof(
GridData,
mChecksum) == 8,
"Unexpected offset to checksum");
185 static_assert(offsetof(
GridData, mVersion) == 16,
"Unexpected offset to version number");
187 static const size_t offset = 16;
193 const auto &tree = grid.
tree();
194 const auto &root = tree.root();
198 const uint8_t *begin =
reinterpret_cast<const uint8_t*
>(&grid);
199 const uint8_t *end = begin + grid.
memUsage() + tree.memUsage() + root.memUsage();
200 crc(begin + offset, end);
207 const auto nodeCount = tree.nodeCount(0) + tree.nodeCount(1) + tree.nodeCount(2);
208 std::vector<std::uint_fast32_t> checksums(nodeCount, 0);
211 auto kernel2 = [&](
const Range1D &r) {
213 std::uint_fast32_t *p = checksums.data() + r.begin();
214 for (
auto i = r.begin(); i != r.end(); ++i) {
215 const auto *node = mgr.upper(i);
216 local(node,
sizeof(*node) );
223 auto kernel1 = [&](
const Range1D &r) {
225 std::uint_fast32_t *p = checksums.data() + r.begin() + tree.nodeCount(2);
226 for (
auto i = r.begin(); i != r.end(); ++i) {
227 const auto *node = mgr.lower(i);
228 local(node,
sizeof(*node) );
235 auto kernel0 = [&](
const Range1D &r) {
237 std::uint_fast32_t *p = checksums.data() + r.begin() + tree.nodeCount(1) + tree.nodeCount(2);
238 for (
auto i = r.begin(); i != r.end(); ++i) {
239 const auto *leaf = mgr.leaf(i);
240 local(leaf,
sizeof(*leaf) );
246 forEach(0, tree.nodeCount(2), 1, kernel2);
247 forEach(0, tree.nodeCount(1), 1, kernel1);
248 forEach(0, tree.nodeCount(0), 8, kernel0);
251 crc(checksums.data(),
sizeof(std::uint_fast32_t)*checksums.size() );
255template <
typename ValueT>
263template <
typename ValueT>
267 cs2(grid, cs1.mode() );
271template <
typename ValueT>
A unified wrapper for tbb::parallel_for and a naive std::thread fallback.
ValueT value
Definition: GridBuilder.h:1287
Defines two classes, a GridRegister the defines the value type (e.g. Double, Float etc) of a NanoVDB ...
#define NANOVDB_ASSERT(x)
Definition: NanoVDB.h:149
#define NANOVDB_OFFSETOF(CLASS, MEMBER)
Definition: NanoVDB.h:182
Class that computes the Cyclic Redundancy Check (CRC)
Definition: GridChecksum.h:75
static const HashT EMPTY
Definition: GridChecksum.h:95
void operator()(const void *data, size_t byteSize)
Definition: GridChecksum.h:111
void operator()(IterT begin, IterT end)
Definition: GridChecksum.h:104
HashT checksum() const
Definition: GridChecksum.h:101
void reset()
Definition: GridChecksum.h:99
CRC32()
Definition: GridChecksum.h:97
Class that encapsulates two CRC32 checksums, one for the Grid, Tree and Root node meta data and one f...
Definition: GridChecksum.h:136
ChecksumMode mode() const
Definition: GridChecksum.h:160
uint32_t mCRC[2]
Definition: GridChecksum.h:137
GridChecksum(uint64_t checksum, ChecksumMode mode=ChecksumMode::Full)
Definition: GridChecksum.h:147
GridChecksum(uint32_t head, uint32_t tail)
Definition: GridChecksum.h:145
GridChecksum()
Definition: GridChecksum.h:143
uint32_t crc32(int i) const
Definition: GridChecksum.h:154
uint64_t checksum() const
Definition: GridChecksum.h:152
void operator()(const NanoGrid< ValueT > &grid, ChecksumMode mode=ChecksumMode::Full)
Definition: GridChecksum.h:175
uint64_t mChecksum
Definition: GridChecksum.h:137
bool operator==(const GridChecksum &rhs) const
Definition: GridChecksum.h:169
static const uint64_t EMPTY
Definition: GridChecksum.h:141
bool isEmpty() const
Definition: GridChecksum.h:158
bool operator!=(const GridChecksum &rhs) const
Definition: GridChecksum.h:170
bool isFull() const
Definition: GridChecksum.h:156
Highest level of the data structure. Contains a tree and a world->index transform (that currently onl...
Definition: NanoVDB.h:2308
DataType * data()
Definition: NanoVDB.h:2327
uint64_t checksum() const
Return checksum of the grid buffer.
Definition: NanoVDB.h:2459
const TreeT & tree() const
Return a const reference to the tree.
Definition: NanoVDB.h:2344
static uint64_t memUsage()
Return memory usage in bytes for this class only.
Definition: NanoVDB.h:2332
uint64_t checksum(const NanoGrid< ValueT > &grid, ChecksumMode mode=ChecksumMode::Default)
Return the (2 x CRC32) checksum of the specified grid.
Definition: GridChecksum.h:256
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:264
std::uint_fast32_t crc32(const void *data, size_t size)
Return the CRC32 checksum of the raw data of size.
Definition: GridChecksum.h:118
NodeManager< GridT > createNodeMgr(GridT &grid)
creates a NodeManager from a grid. Move semantics is used.
Definition: NodeManager.h:252
void updateChecksum(NanoGrid< ValueT > &grid, ChecksumMode mode=ChecksumMode::Default)
Updates the checksum of a grid.
Definition: GridChecksum.h:272
void forEach(RangeT range, const FuncT &func)
simple wrapper for tbb::parallel_for with a naive std fallback
Definition: ForEach.h:40
ChecksumMode
List of different modes for computing for a checksum.
Definition: GridChecksum.h:33
NodeManager produces linear arrays of all tree nodes allowing for efficient threading and bottom-up p...
Struct with all the member data of the Grid (useful during serialization of an openvdb grid)
Definition: NanoVDB.h:2185
uint64_t mChecksum
Definition: NanoVDB.h:2188