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);
118 inline std::uint_fast32_t
crc32(
const void *data,
size_t byteSize)
125 template <
typename IterT>
126 inline std::uint_fast32_t
crc32(IterT begin, IterT end)
137 union {uint32_t mCRC[2]; uint64_t mChecksum; };
154 uint32_t
crc32(
int i)
const {assert(i==0 || i==1);
return mCRC[i]; }
158 bool isEmpty()
const {
return mChecksum == EMPTY; }
166 template <
typename ValueT>
174 template <
typename ValueT>
182 #else// the static asserts below generate compiler warnings 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() );
255 template <
typename ValueT>
263 template <
typename ValueT>
267 cs2(grid, cs1.mode() );
271 template <
typename ValueT>
281 #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:2307
Class that computes the Cyclic Redundancy Check (CRC)
Definition: GridChecksum.h:74
#define NANOVDB_OFFSETOF(CLASS, MEMBER)
Definition: NanoVDB.h:182
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
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 ...
NodeManager< GridT > createNodeMgr(GridT &grid)
creates a NodeManager from a grid. Move semantics is used.
Definition: NodeManager.h:252
Class that encapsulates two CRC32 checksums, one for the Grid, Tree and Root node meta data and one f...
Definition: GridChecksum.h:135
void operator()(const NanoGrid< ValueT > &grid, ChecksumMode mode=ChecksumMode::Full)
Definition: GridChecksum.h:175
const TreeT & tree() const
Return a const reference to the tree.
Definition: NanoVDB.h:2344
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:170
HashT checksum() const
Definition: GridChecksum.h:101
Definition: NanoVDB.h:184
bool operator==(const GridChecksum &rhs) const
Definition: GridChecksum.h:169
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
static const HashT EMPTY
Definition: GridChecksum.h:95
uint64_t mChecksum
Definition: GridChecksum.h:137
GridChecksum(uint32_t head, uint32_t tail)
Definition: GridChecksum.h:145
ValueT value
Definition: GridBuilder.h:1287
bool isEmpty() const
Definition: GridChecksum.h:158
void updateChecksum(NanoGrid< ValueT > &grid, ChecksumMode mode=ChecksumMode::Default)
Updates the checksum of a grid.
Definition: GridChecksum.h:272
bool isFull() const
Definition: GridChecksum.h:156
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
DataType * data()
Definition: NanoVDB.h:2327
uint32_t crc32(int i) const
Definition: GridChecksum.h:154
#define NANOVDB_ASSERT(x)
Definition: NanoVDB.h:149
CRC32()
Definition: GridChecksum.h:97
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
uint64_t checksum() const
Return checksum of the grid buffer.
Definition: NanoVDB.h:2459
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:2184
GridChecksum(uint64_t checksum, ChecksumMode mode=ChecksumMode::Full)
Definition: GridChecksum.h:147
static uint64_t memUsage()
Return memory usage in bytes for this class only.
Definition: NanoVDB.h:2332
void reset()
Definition: GridChecksum.h:99
GridChecksum()
Definition: GridChecksum.h:143
ChecksumMode mode() const
Definition: GridChecksum.h:160
void operator()(IterT begin, IterT end)
Definition: GridChecksum.h:104
uint64_t checksum() const
Definition: GridChecksum.h:152