OpenVDB  9.1.1
GridValidator.h
Go to the documentation of this file.
1 // Copyright Contributors to the OpenVDB Project
2 // SPDX-License-Identifier: MPL-2.0
3 
4 /*!
5  \file GridValidator.h
6 
7  \author Ken Museth
8 
9  \date August 30, 2020
10 
11  \brief Checks the validity of an existing NanoVDB grid.
12 */
13 
14 #ifndef NANOVDB_GRIDVALIDATOR_H_HAS_BEEN_INCLUDED
15 #define NANOVDB_GRIDVALIDATOR_H_HAS_BEEN_INCLUDED
16 
17 #include "../NanoVDB.h"
18 #include "GridChecksum.h"
19 
20 namespace nanovdb {
21 
22 /// @brief Return true if the specified grid passes several validation tests.
23 ///
24 /// @param grid Grid to validate
25 /// @param detailed If true the validation test is detailed and relatively slow.
26 /// @param verbose If true information about the first failed test is printed to std::cerr
27 template <typename ValueT>
28 bool isValid(const NanoGrid<ValueT> &grid, bool detailed = true, bool verbose = false);
29 
30 /// @brief Allows for the construction of NanoVDB grids without any dependecy
31 template <typename ValueT>
33 {
34  using GridT = NanoGrid<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&);
38 
39 public:
40  /// @brief Returns an error message (an empty string means no error)
41  ///
42  /// @param grid NanoVDB grid to be tested
43  /// @param detailed If true the checksum is computed and validated as well as all the node pointers
44  ///
45  /// @note The validation is much slower if @c detailed == true!
46  static std::string check(const GridT &grid, bool detailed = true);
47 
48 };// GridValidator
49 
50 //================================================================================================
51 
52 template <typename ValueT>
53 std::string GridValidator<ValueT>::check(const GridT &grid, bool detailed)
54 {
55  std::string errorStr;
56 
57  // First check the Grid
58  auto *data = reinterpret_cast<const typename GridT::DataType*>(&grid);
59  std::stringstream ss;
60  if (data->mMagic != NANOVDB_MAGIC_NUMBER) {
61  ss << "Incorrect magic number: Expected " << NANOVDB_MAGIC_NUMBER << ", but read " << data->mMagic;
62  errorStr = ss.str();
63  } else if (!validateChecksum(grid, detailed ? ChecksumMode::Full : ChecksumMode::Partial)) {
64  errorStr.assign("Mis-matching checksum");
65  } else if (data->mVersion >= Version(29,0,0) && data->mVersion.getMajor() != NANOVDB_MAJOR_VERSION_NUMBER) {
66  ss << "Invalid major version number: Expected " << NANOVDB_MAJOR_VERSION_NUMBER << ", but read " << data->mVersion.c_str();
67  errorStr = ss.str();
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();
70  errorStr = ss.str();
71  } else if (data->mGridClass >= GridClass::End) {
72  errorStr.assign("Invalid GridClass");
73  } else if (data->mGridType >= GridType::End) {
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");
81  } else {
82  checkTree(grid, errorStr, detailed);
83  }
84  return errorStr;
85 }
86 
87 //================================================================================================
88 
89 template<typename ValueT>
90 void GridValidator<ValueT>::checkTree(const GridT &grid, std::string &errorStr, bool detailed)
91 {
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)");
96  } else {
97  checkRoot(grid, errorStr, detailed);
98  }
99 }// GridValidator::checkTree
100 
101 //================================================================================================
102 
103 template<typename ValueT>
104 void GridValidator<ValueT>::checkRoot(const GridT &grid, std::string &errorStr, bool detailed)
105 {
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");
116  }
117  }
118  if (detailed && errorStr.empty()) {
119  checkNodes(grid, errorStr);
120  }
121 }// GridValidator::processRoot
122 
123 //================================================================================================
124 template<typename ValueT>
125 void GridValidator<ValueT>::checkNodes(const GridT &grid, std::string &errorStr)
126 {
127  auto &root = grid.tree().root();// note, the root node was already checked
128  const uint8_t *minPtr = (const uint8_t*)(&root) + root.memUsage();
129  const uint8_t *maxPtr = (const uint8_t*)(&grid) + grid.gridSize();
130 
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");
136  }
137  return errorStr.empty();
138  };
139 
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);// note, we already checked the root
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;
154  }// loop over child nodes of the lower internal node
155  }// loop over child nodes of the upper internal node
156  }// loop over child nodes of the root node
157 } // GridValidator::processNodes
158 
159 
160 //================================================================================================
161 
162 template <typename ValueT>
163 bool isValid(const NanoGrid<ValueT> &grid, bool detailed, bool verbose)
164 {
165  const std::string str = GridValidator<ValueT>::check( grid, detailed );
166  if (verbose && !str.empty()) std::cerr << "Validation failed: " << str << std::endl;
167  return str.empty();
168 }
169 
170 } // namespace nanovdb
171 
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:2308
const TreeT & tree() const
Return a const reference to the tree.
Definition: NanoVDB.h:2345
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&#39;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:2185
uint64_t gridSize() const
Return the memory footprint of the entire grid, i.e. including all nodes and blind data...
Definition: NanoVDB.h:2336
Allows for the construction of NanoVDB grids without any dependecy.
Definition: GridValidator.h:32