20#ifndef NANOVDB_TOOLS_GRID_VALIDATOR_H_HAS_BEEN_INCLUDED
21#define NANOVDB_TOOLS_GRID_VALIDATOR_H_HAS_BEEN_INCLUDED
38template <
typename ValueT>
47template <
typename ValueT>
48[[deprecated(
"Use isValue(const NanoGrid<ValueT>*, CheckMode, bool) instead.")]]
57template<
typename ValueT>
64 if (grid ==
nullptr) {
65 return util::sprint(error,
"Invalid pointer: Grid is NULL");
67 return util::sprint(error,
"Invalid pointer: Grid is misaligned");
87 auto &tree = grid->
tree();
88 if (
auto *p = tree.getRoot()) {
91 return util::strcpy(error,
"Invalid pointer: Root is NULL");
95 auto &root = tree.root();
96 auto *rootData = root.data();
97 if (rootData ==
nullptr) {
98 return util::strcpy(error,
"Invalid pointer: Root is NULL");
99 }
else if (!
isAligned((
const void*)rootData)) {
100 return util::strcpy(error,
"Invalid pointer: Root is misaligned");
101 }
else if ( (
const uint8_t*)(rootData) < (
const uint8_t*)(&tree+1)) {
102 return util::strcpy(error,
"Invalid root pointer (should be located after the Grid and Tree)");
103 }
else if ( (
const void*)(rootData) >
util::PtrAdd(rootData, root.memUsage())) {
104 return util::strcpy(error,
"Invalid root pointer (appears to be located after the end of the buffer)");
106 const void *bounds[2] = {rootData + 1,
util::PtrAdd(rootData, root.memUsage())};
107 for (uint32_t i = 0; i<rootData->mTableSize; ++i) {
108 const void *tile = rootData->tile(i);
109 if ( tile < bounds[0] ) {
110 return util::strcpy(error,
"Invalid root tile pointer (below lower bound");
111 }
else if (tile >= bounds[1]) {
112 return util::strcpy(error,
"Invalid root tile pointer (above higher bound");
120 auto *n0 = tree.template getFirstNode<0>();
121 auto *n1 = tree.template getFirstNode<1>();
122 auto *n2 = tree.template getFirstNode<2>();
125 auto check = [&](
const void *ptr,
int level) ->
bool {
127 util::strcpy(error,
"Invalid node pointer: node is NULL");
129 util::strcpy(error,
"Invalid node pointer: node is misaligned");
130 }
else if (test && level == 0 && (
const void*)(n0++) != ptr) {
131 util::strcpy(error,
"Leaf node is not stored breadth-first");
132 }
else if (test && level == 1 && (
const void*)(n1++) != ptr) {
133 util::strcpy(error,
"Lower node is not stored breadth-first");
134 }
else if (test && level == 2 && (
const void*)(n2++) != ptr) {
135 util::strcpy(error,
"Upper node is not stored breadth-first");
136 }
else if ( ptr < bounds[level][0] ) {
137 util::strcpy(error,
"Invalid node pointer: below lower bound");
138 }
else if ( ptr >= bounds[level][1] ) {
139 util::strcpy(error,
"Invalid node pointer: above higher bound");
144 for (
auto it2 = root.cbeginChild(); it2; ++it2) {
145 if (check(&*it2, 2))
return error;
146 for (
auto it1 = it2->cbeginChild(); it1; ++it1) {
147 if (check(&*it1, 1))
return error;
148 for (
auto it0 = it1->cbeginChild(); it0; ++it0)
if (check(&*it0, 0))
return error;
157template <
typename ValueT>
160 std::unique_ptr<char[]> strUP(
new char[100]);
161 char *str = strUP.get();
166 if (verbose && !
util::empty(str)) std::cerr <<
"Validation failed: " << str << std::endl;
174 template <
typename BuildT>
183 std::cerr <<
"Unsupported GridType: \"" <<
toStr(str, gridData->
mGridType) <<
"\"\n" << std::endl;
196template <
typename Gr
idHandleT>
201 }
else if (gridID >= handle.gridCount()) {
202 if (verbose) std::cerr <<
"grid index " << gridID <<
" exceeds available grid count " << handle.gridCount() << std::endl;
216template <
typename Gr
idHandleT>
220 for (uint32_t gridID=0; gridID<handle.gridCount(); ++gridID) {
221 if (!
validateGrid(handle, gridID, mode, verbose))
return false;
228template<
typename ValueT>
229[[deprecated(
"Use nanovdb:tools::checkGrid instead.")]]
235template <
typename ValueT>
236[[deprecated(
"Use nanovdb:tools::isValid instead.")]]
Implements a light-weight self-contained VDB data-structure in a single file! In other words,...
#define NANOVDB_MAGIC_GRID
Definition NanoVDB.h:140
#define NANOVDB_MAGIC_NUMB
Definition NanoVDB.h:139
bool isBreadthFirst() const
Definition NanoVDB.h:2326
uint64_t gridSize() const
Return memory usage in bytes for this class only.
Definition NanoVDB.h:2213
const TreeT & tree() const
Return a const reference to the tree.
Definition NanoVDB.h:2236
bool isCompatible() const
Definition NanoVDB.h:760
#define __hostdev__
Definition SampleFromVoxels.h:29
static DstT * PtrAdd(void *p, int64_t offset)
Adds a byte offset to a non-const pointer to produce another non-const pointer.
Definition Util.h:524
char * strcpy(char *dst, const char *src)
Copy characters from src to dst.
Definition Util.h:178
bool empty(const char *str)
tests if a c-string str is empty, that is its first value is '\0'
Definition Util.h:156
char * sprint(char *dst, T var1, Types... var2)
prints a variable number of string and/or numbers to a destination string
Definition Util.h:298
Defines a simple memory pool used to call cub functions that use dynamic temporary storage.
Definition GridHandle.h:31
auto callNanoGrid(GridDataT *gridData, ArgsT &&... args)
Below is an example of the struct used for generic programming with callNanoGrid.
Definition NanoVDB.h:4846
GridType toGridType()
Maps from a templated build type to a GridType enum.
Definition NanoVDB.h:851
Grid< NanoTree< BuildT > > NanoGrid
Definition NanoVDB.h:4742
@ End
Definition NanoVDB.h:299
@ End
Definition NanoVDB.h:246
CheckMode
List of different modes for computing for a checksum.
Definition NanoVDB.h:1846
@ Full
Definition NanoVDB.h:1851
@ Disable
Definition NanoVDB.h:1846
@ Half
Definition NanoVDB.h:1848
MagicType toMagic(uint64_t magic)
maps 64 bits of magic number to enum
Definition NanoVDB.h:366
char * toStr(char *dst, GridType gridType)
Maps a GridType to a c-string.
Definition NanoVDB.h:253
__hostdev__ char * checkGrid(const NanoGrid< ValueT > *grid, char *error, CheckMode mode=CheckMode::Full)
Definition GridValidator.h:230
static bool isAligned(const void *p)
return true if the specified pointer is 32 byte aligned
Definition NanoVDB.h:600
bool isValid(GridType gridType, GridClass gridClass)
return true if the combination of GridType and GridClass is valid.
Definition NanoVDB.h:664
Struct with all the member data of the Grid (useful during serialization of an openvdb grid)
Definition NanoVDB.h:1977
Version mVersion
Definition NanoVDB.h:1981
GridType mGridType
Definition NanoVDB.h:1991
uint64_t mMagic
Definition NanoVDB.h:1979
GridClass mGridClass
Definition NanoVDB.h:1990
uint32_t mGridCount
Definition NanoVDB.h:1984
uint32_t mGridIndex
Definition NanoVDB.h:1983