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 ss <<
"Incorrect magic number: Expected " <<
NANOVDB_MAGIC_NUMBER <<
", but read " << data->mMagic;
64 errorStr.assign(
"Mis-matching checksum");
68 }
else if (data->mVersion <
Version(29,0,0) && data->mVersion.
id() != 28u) {
69 ss <<
"Invalid old major version number: Expected 28 or newer, but read " << data->mVersion.id();
72 errorStr.assign(
"Invalid GridClass");
74 errorStr.assign(
"Invalid GridType");
75 }
else if (data->mGridType != mapToGridType<ValueT>()) {
76 errorStr.assign(
"Invalid combination of ValueType and GridType");
77 }
else if (!
isValid(data->mGridType, data->mGridClass)) {
78 errorStr.assign(
"Invalid combination of GridType and GridClass");
79 }
else if ( (
const uint8_t*)(&(grid.
tree())) != (
const uint8_t*)(&grid+1) ) {
80 errorStr.assign(
"Invalid Tree pointer");
82 checkTree(grid, errorStr, detailed);
89 template<
typename ValueT>
92 if ( (
const uint8_t*)(&grid.
tree().root()) < (
const uint8_t*)(&grid.
tree()+1)) {
93 errorStr.assign(
"Invalid root pointer (should be located after the Grid and Tree)");
94 }
else if ( (
const uint8_t*)(&grid.
tree().root()) > (
const uint8_t*)(&grid) + grid.
gridSize() -
sizeof(grid.
tree().root()) ) {
95 errorStr.assign(
"Invalid root pointer (appears to be located after the end of the buffer)");
97 checkRoot(grid, errorStr, detailed);
103 template<
typename ValueT>
106 auto &root = grid.
tree().root();
107 auto *data = root.data();
108 const uint8_t *minPtr = (
const uint8_t*)(&root + 1);
109 const uint8_t *maxPtr = (
const uint8_t*)(&root) + root.memUsage();
110 for (uint32_t i = 0; errorStr.empty() && i<data->mTableSize; ++i) {
111 const auto *tile = data->tile(i);
112 if ( (
const uint8_t *) tile < minPtr ) {
113 errorStr.assign(
"Invalid root tile pointer (below lower bound");
114 }
else if ( (
const uint8_t *) tile > maxPtr -
sizeof(*tile) ) {
115 errorStr.assign(
"Invalid root tile pointer (above higher bound");
118 if (detailed && errorStr.empty()) {
119 checkNodes(grid, errorStr);
124 template<
typename ValueT>
127 auto &root = grid.
tree().root();
128 const uint8_t *minPtr = (
const uint8_t*)(&root) + root.memUsage();
129 const uint8_t *maxPtr = (
const uint8_t*)(&grid) + grid.
gridSize();
131 auto check = [&](
const void * ptr,
size_t ptrSize) ->
bool {
132 if ( (
const uint8_t *) ptr < minPtr ) {
133 errorStr.assign(
"Invalid node pointer: below lower bound");
134 }
else if ( (
const uint8_t *) ptr > maxPtr - ptrSize ) {
135 errorStr.assign(
"Invalid node pointer: above higher bound");
137 return errorStr.empty();
140 auto *data3 = grid.
tree().root().data();
141 for (uint32_t i=0, size=data3->mTableSize; i<size; ++i) {
142 auto *tile = data3->tile(i);
143 if (!tile->isChild())
continue;
144 auto *node2 = data3->getChild(tile);
145 if (!
check(node2,
sizeof(*node2)))
return;
146 auto *data2 = node2->data();
147 for (
auto it2 = data2->mChildMask.beginOn(); it2; ++it2) {
148 auto *node1 = data2->getChild(*it2);
149 if (!
check(node1,
sizeof(*node1)))
return;
150 auto *data1 = node1->data();
151 for (
auto it1 = data1->mChildMask.beginOn(); it1; ++it1) {
152 auto *leaf = data1->getChild(*it1);
153 if (!
check(leaf,
sizeof(*leaf)))
return;
162 template <
typename ValueT>
166 if (verbose && !str.empty()) std::cerr <<
"Validation failed: " << str << std::endl;
172 #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:2307
const TreeT & tree() const
Return a const reference to the tree.
Definition: NanoVDB.h:2344
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:540
Definition: NanoVDB.h:184
#define NANOVDB_MAJOR_VERSION_NUMBER
Definition: NanoVDB.h:104
uint32_t id() const
Definition: NanoVDB.h:561
#define NANOVDB_MAGIC_NUMBER
Definition: NanoVDB.h:102
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
uint32_t getMajor() const
Definition: NanoVDB.h:562
bool isValid(GridType gridType, GridClass gridClass)
return true if the combination of GridType and GridClass is valid.
Definition: NanoVDB.h:520
Struct with all the member data of the Grid (useful during serialization of an openvdb grid) ...
Definition: NanoVDB.h:2184
uint64_t gridSize() const
Return the memory footprint of the entire grid, i.e. including all nodes and blind data...
Definition: NanoVDB.h:2335
Allows for the construction of NanoVDB grids without any dependecy.
Definition: GridValidator.h:32