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{};
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();
108 mChecksum = std::accumulate(begin, end, mChecksum,
kernel);
113 const ByteT *begin =
static_cast<const ByteT*
>(data);
117 template <
typename T>
125 return crc.checksum();
128template <
typename IterT>
129inline std::uint_fast32_t
crc32(IterT begin, IterT end)
133 return crc.checksum();
169 template <
typename ValueT>
177template <
typename ValueT>
186 static_assert(
offsetof(
GridData, mMagic) == 0,
"Unexpected offset to magic number");
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();
202 const uint8_t *end = begin + grid.memUsage() + tree.memUsage() + root.memUsage();
203 crc(begin + offset, end);
212 const auto nodeCount = tree.nodeCount(0) + tree.nodeCount(1) + tree.nodeCount(2);
213 std::vector<std::uint_fast32_t>
checksums(nodeCount, 0);
219 for (
auto i = r.begin(); i != r.end(); ++i) {
230 std::uint_fast32_t *
p =
checksums.
data() + r.begin() + tree.nodeCount(2);
231 for (
auto i = r.begin(); i != r.end(); ++i) {
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) {
260template <
typename ValueT>
265 return cs.checksum();
268template <
typename ValueT>
276template <
typename ValueT>
281 grid.
data()->mChecksum =
cs.checksum();
A unified wrapper for tbb::parallel_for and a naive std::thread fallback.
ValueT value
Definition GridBuilder.h:1290
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:173
#define NANOVDB_OFFSETOF(CLASS, MEMBER)
Definition NanoVDB.h:206
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
void operator()(const T &data)
Definition GridChecksum.h:118
HashT checksum() const
Definition GridChecksum.h:101
void reset()
Definition GridChecksum.h:99
CRC32()
Definition GridChecksum.h:97
Definition DenseGrid.h:402
uint64_t size() const
Returns the size in bytes of the raw memory buffer managed by this DenseGridHandle's allocator.
Definition DenseGrid.h:440
uint8_t * data()
Returns a non-const pointer to the data.
Definition DenseGrid.h:432
void reset()
Definition DenseGrid.h:424
Class that encapsulates two CRC32 checksums, one for the Grid, Tree and Root node meta data and one f...
Definition GridChecksum.h:139
ChecksumMode mode() const
Definition GridChecksum.h:163
uint32_t mCRC[2]
Definition GridChecksum.h:140
GridChecksum(uint64_t checksum, ChecksumMode mode=ChecksumMode::Full)
Definition GridChecksum.h:150
GridChecksum(uint32_t head, uint32_t tail)
Definition GridChecksum.h:148
GridChecksum()
Definition GridChecksum.h:146
uint32_t crc32(int i) const
Definition GridChecksum.h:157
uint64_t checksum() const
Definition GridChecksum.h:155
void operator()(const NanoGrid< ValueT > &grid, ChecksumMode mode=ChecksumMode::Full)
Definition GridChecksum.h:178
uint64_t mChecksum
Definition GridChecksum.h:140
bool operator==(const GridChecksum &rhs) const
Definition GridChecksum.h:172
static const uint64_t EMPTY
Definition GridChecksum.h:144
bool isEmpty() const
Definition GridChecksum.h:161
bool operator!=(const GridChecksum &rhs) const
Definition GridChecksum.h:173
bool isFull() const
Definition GridChecksum.h:159
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
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
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
void updateChecksum(NanoGrid< ValueT > &grid, ChecksumMode mode=ChecksumMode::Default)
Updates the checksum of a grid.
Definition GridChecksum.h:277
ChecksumMode
List of different modes for computing for a checksum.
Definition GridChecksum.h:33
void forEach(RangeT range, const FuncT &func)
simple wrapper for tbb::parallel_for with a naive std fallback
Definition ForEach.h:40
NodeManagerHandle< BufferT > createNodeManager(const NanoGrid< BuildT > &grid, const BufferT &buffer=BufferT())
brief Construct a NodeManager and return its handle
Definition NodeManager.h:289
static bool isValid(const void *p)
return true if the specified pointer is aligned and not NULL
Definition NanoVDB.h:504
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:2432