14 #ifndef NANOVDB_GRIDVALIDATOR_H_HAS_BEEN_INCLUDED 15 #define NANOVDB_GRIDVALIDATOR_H_HAS_BEEN_INCLUDED 17 #include "../NanoVDB.h" 27 template <
typename ValueT>
28 bool isValid(
const NanoGrid<ValueT> &grid,
bool detailed =
true,
bool verbose =
false);
31 template <
typename ValueT>
35 inline static void checkTree(
const GridT&, std::string&,
bool);
36 inline static void checkRoot(
const GridT&, std::string&,
bool);
37 inline static void checkNodes(
const GridT&, std::string&);
46 static std::string
check(
const GridT &grid,
bool detailed =
true);
52 template <
typename ValueT>
61 errorStr.assign(
"Grid is not 32B aligned");
63 ss <<
"Incorrect magic number: Expected " <<
NANOVDB_MAGIC_NUMBER <<
", but read " << data->mMagic;
66 errorStr.assign(
"Mis-matching checksum");
70 }
else if (data->mVersion <
Version(29,0,0) && data->mVersion.
id() != 28u) {
71 ss <<
"Invalid old major version number: Expected 28 or newer, but read " << data->mVersion.id();
74 errorStr.assign(
"Invalid GridClass");
76 errorStr.assign(
"Invalid GridType");
77 }
else if (data->mGridType != mapToGridType<ValueT>()) {
78 errorStr.assign(
"Invalid combination of ValueType and GridType");
79 }
else if (!
isValid(data->mGridType, data->mGridClass)) {
80 errorStr.assign(
"Invalid combination of GridType and GridClass");
81 }
else if ( (
const uint8_t*)(&(grid.
tree())) != (
const uint8_t*)(&grid+1) ) {
82 errorStr.assign(
"Invalid Tree pointer");
84 checkTree(grid, errorStr, detailed);
91 template<
typename ValueT>
95 errorStr.assign(
"Tree is not 32B aligned");
96 }
else if ( (
const uint8_t*)(&grid.
tree().root()) < (
const uint8_t*)(&grid.
tree()+1)) {
97 errorStr.assign(
"Invalid root pointer (should be located after the Grid and Tree)");
98 }
else if ( (
const uint8_t*)(&grid.
tree().root()) > (
const uint8_t*)(&grid) + grid.
gridSize() -
sizeof(grid.
tree().root()) ) {
99 errorStr.assign(
"Invalid root pointer (appears to be located after the end of the buffer)");
101 checkRoot(grid, errorStr, detailed);
107 template<
typename ValueT>
110 auto &root = grid.
tree().root();
111 auto *data = root.data();
113 errorStr.assign(
"Root is not 32B aligned");
115 const uint8_t *minPtr = (
const uint8_t*)(&root + 1);
116 const uint8_t *maxPtr = (
const uint8_t*)(&root) + root.memUsage();
117 for (uint32_t i = 0; errorStr.empty() && i<data->mTableSize; ++i) {
118 const auto *tile = data->tile(i);
119 if ( (
const uint8_t *) tile < minPtr ) {
120 errorStr.assign(
"Invalid root tile pointer (below lower bound");
121 }
else if ( (
const uint8_t *) tile > maxPtr -
sizeof(*tile) ) {
122 errorStr.assign(
"Invalid root tile pointer (above higher bound");
125 if (detailed && errorStr.empty()) {
126 checkNodes(grid, errorStr);
131 template<
typename ValueT>
134 auto &root = grid.
tree().root();
135 const uint8_t *minPtr = (
const uint8_t*)(&root) + root.memUsage();
136 const uint8_t *maxPtr = (
const uint8_t*)(&grid) + grid.
gridSize();
138 auto check = [&](
const void * ptr,
size_t ptrSize) ->
bool {
140 errorStr.assign(
"Invalid node pointer: not 32B aligned");
141 }
else if ( (
const uint8_t *) ptr < minPtr ) {
142 errorStr.assign(
"Invalid node pointer: below lower bound");
143 }
else if ( (
const uint8_t *) ptr > maxPtr - ptrSize ) {
144 errorStr.assign(
"Invalid node pointer: above higher bound");
146 return errorStr.empty();
149 for (
auto it2 = grid.
tree().root().beginChild(); it2; ++it2) {
151 if (!
check(&node2,
sizeof(node2)))
return;
152 for (
auto it1 = node2.beginChild(); it1; ++it1) {
154 if (!
check(&node1,
sizeof(node1)))
return;
155 for (
auto it0 = node1.beginChild(); it0; ++it0) {
157 if (!
check(&node2,
sizeof(node2)))
return;
167 template <
typename ValueT>
171 if (verbose && !str.empty()) std::cerr <<
"Validation failed: " << str << std::endl;
177 #endif // NANOVDB_GRIDVALIDATOR_H_HAS_BEEN_INCLUDED static std::string check(const GridT &grid, bool detailed=true)
Returns an error message (an empty string means no error)
Definition: GridValidator.h:53
Highest level of the data structure. Contains a tree and a world->index transform (that currently onl...
Definition: NanoVDB.h:2555
const TreeT & tree() const
Return a const reference to the tree.
Definition: NanoVDB.h:2598
Computes a pair of 32bit checksums, og a Grid, by means of Cyclic Redundancy Check (CRC) ...
Bit-compacted representation of all three version numbers.
Definition: NanoVDB.h:647
Definition: NanoVDB.h:208
#define NANOVDB_MAJOR_VERSION_NUMBER
Definition: NanoVDB.h:123
uint32_t id() const
Definition: NanoVDB.h:668
#define NANOVDB_MAGIC_NUMBER
Definition: NanoVDB.h:121
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
uint32_t getMajor() const
Definition: NanoVDB.h:669
Struct with all the member data of the Grid (useful during serialization of an openvdb grid) ...
Definition: NanoVDB.h:2431
static bool isValid(const void *p)
return true if the specified pointer is aligned and not NULL
Definition: NanoVDB.h:504
uint64_t gridSize() const
Return the memory footprint of the entire grid, i.e. including all nodes and blind data...
Definition: NanoVDB.h:2583
Allows for the construction of NanoVDB grids without any dependecy.
Definition: GridValidator.h:32