OpenVDB  8.0.1
Iterator.h
Go to the documentation of this file.
1 // Copyright Contributors to the OpenVDB Project
2 // SPDX-License-Identifier: MPL-2.0
3 //
7 
8 #ifndef OPENVDB_TREE_ITERATOR_HAS_BEEN_INCLUDED
9 #define OPENVDB_TREE_ITERATOR_HAS_BEEN_INCLUDED
10 
11 #include <sstream>
12 #include <type_traits>
13 #include <openvdb/util/NodeMasks.h>
14 #include <openvdb/Exceptions.h>
15 
16 namespace openvdb {
18 namespace OPENVDB_VERSION_NAME {
19 namespace tree {
20 
28 template<typename MaskIterT, typename NodeT>
30 {
31 public:
32  IteratorBase(): mParentNode(nullptr) {}
33  IteratorBase(const MaskIterT& iter, NodeT* parent): mParentNode(parent), mMaskIter(iter) {}
34  IteratorBase(const IteratorBase&) = default;
35  IteratorBase& operator=(const IteratorBase&) = default;
36 
37  bool operator==(const IteratorBase& other) const
38  {
39  return (mParentNode == other.mParentNode) && (mMaskIter == other.mMaskIter);
40  }
41  bool operator!=(const IteratorBase& other) const
42  {
43  return !(*this == other);
44  }
45 
47  NodeT* getParentNode() const { return mParentNode; }
50  NodeT& parent() const
51  {
52  if (!mParentNode) OPENVDB_THROW(ValueError, "iterator references a null node");
53  return *mParentNode;
54  }
55 
57  Index offset() const { return mMaskIter.offset(); }
58 
60  Index pos() const { return mMaskIter.offset(); }
61 
63  bool test() const { return mMaskIter.test(); }
65  operator bool() const { return this->test(); }
66 
68  bool next() { return mMaskIter.next(); }
70  void increment() { mMaskIter.increment(); }
72  IteratorBase& operator++() { this->increment(); return *this; }
74  void increment(Index n) { mMaskIter.increment(n); }
75 
78  bool isValueOn() const { return parent().isValueMaskOn(this->pos()); }
81  void setValueOn(bool on = true) const { parent().setValueMask(this->pos(), on); }
86  void setValueOff() const { parent().mValueMask.setOff(this->pos()); }
87 
89  Coord getCoord() const { return parent().offsetToGlobalCoord(this->pos()); }
91  void getCoord(Coord& xyz) const { xyz = this->getCoord(); }
92 
93 private:
100  mutable NodeT* mParentNode;
101  MaskIterT mMaskIter;
102 }; // class IteratorBase
103 
104 
106 
107 
109 template<
110  typename MaskIterT, // mask iterator type (OnIterator, OffIterator, etc.)
111  typename IterT, // SparseIteratorBase subclass (the "Curiously Recurring Template Pattern")
112  typename NodeT, // type of node over which to iterate
113  typename ItemT> // type of value to which this iterator points
114 struct SparseIteratorBase: public IteratorBase<MaskIterT, NodeT>
115 {
116  using NodeType = NodeT;
117  using ValueType = ItemT;
118  using NonConstNodeType = typename std::remove_const<NodeT>::type;
119  using NonConstValueType = typename std::remove_const<ItemT>::type;
120  static const bool IsSparseIterator = true, IsDenseIterator = false;
121 
123  SparseIteratorBase(const MaskIterT& iter, NodeT* parent):
124  IteratorBase<MaskIterT, NodeT>(iter, parent) {}
125 
128  ItemT& getItem(Index) const;
131  void setItem(Index, const ItemT&) const;
132 
134  ItemT& operator*() const { return this->getValue(); }
136  ItemT* operator->() const { return &(this->operator*()); }
137 
139  ItemT& getValue() const
140  {
141  return static_cast<const IterT*>(this)->getItem(this->pos()); // static polymorphism
142  }
145  void setValue(const ItemT& value) const
146  {
147  static_assert(!std::is_const<NodeT>::value, "setValue() not allowed for const iterators");
148  static_cast<const IterT*>(this)->setItem(this->pos(), value); // static polymorphism
149  }
155  template<typename ModifyOp>
156  void modifyValue(const ModifyOp& op) const
157  {
158  static_assert(!std::is_const<NodeT>::value,
159  "modifyValue() not allowed for const iterators");
160  static_cast<const IterT*>(this)->modifyItem(this->pos(), op); // static polymorphism
161  }
162 }; // class SparseIteratorBase
163 
164 
166 
167 
172 template<
173  typename MaskIterT, // mask iterator type (typically a DenseIterator)
174  typename IterT, // DenseIteratorBase subclass (the "Curiously Recurring Template Pattern")
175  typename NodeT, // type of node over which to iterate
176  typename SetItemT, // type of set value (ChildNodeType, for non-leaf nodes)
177  typename UnsetItemT> // type of unset value (ValueType, usually)
178 struct DenseIteratorBase: public IteratorBase<MaskIterT, NodeT>
179 {
180  using NodeType = NodeT;
181  using ValueType = UnsetItemT;
182  using ChildNodeType = SetItemT;
183  using NonConstNodeType = typename std::remove_const<NodeT>::type;
184  using NonConstValueType = typename std::remove_const<UnsetItemT>::type;
185  using NonConstChildNodeType = typename std::remove_const<SetItemT>::type;
186  static const bool IsSparseIterator = false, IsDenseIterator = true;
187 
189  DenseIteratorBase(const MaskIterT& iter, NodeT* parent):
190  IteratorBase<MaskIterT, NodeT>(iter, parent) {}
191 
196  bool getItem(Index, SetItemT*& child, NonConstValueType& value) const;
199  void setItem(Index, SetItemT*) const;
202  void unsetItem(Index, const UnsetItemT&) const;
203 
205  bool isChildNode() const { return this->parent().isChildMaskOn(this->pos()); }
206 
209  SetItemT* probeChild(NonConstValueType& value) const
210  {
211  SetItemT* child = nullptr;
212  static_cast<const IterT*>(this)->getItem(this->pos(), child, value); // static polymorphism
213  return child;
214  }
218  bool probeChild(SetItemT*& child, NonConstValueType& value) const
219  {
220  child = probeChild(value);
221  return (child != nullptr);
222  }
223 
226  bool probeValue(NonConstValueType& value) const
227  {
228  SetItemT* child = nullptr;
229  const bool isChild = static_cast<const IterT*>(this)-> // static polymorphism
230  getItem(this->pos(), child, value);
231  return !isChild;
232  }
233 
236  void setChild(SetItemT* child) const
237  {
238  static_cast<const IterT*>(this)->setItem(this->pos(), child); // static polymorphism
239  }
240 
243  void setValue(const UnsetItemT& value) const
244  {
245  static_cast<const IterT*>(this)->unsetItem(this->pos(), value); // static polymorphism
246  }
247 }; // struct DenseIteratorBase
248 
249 } // namespace tree
250 } // namespace OPENVDB_VERSION_NAME
251 } // namespace openvdb
252 
253 #endif // OPENVDB_TREE_ITERATOR_HAS_BEEN_INCLUDED
openvdb::v8_0::tree::DenseIteratorBase::isChildNode
bool isChildNode() const
Return true if this iterator is pointing to a child node.
Definition: Iterator.h:205
openvdb::v8_0::tree::DenseIteratorBase< MaskDenseIterator, DenseIter< NodeT, ChildT, ValueT, TagT >, NodeT, ChildT, ValueT >::NodeType
NodeT NodeType
Definition: Iterator.h:180
openvdb::v8_0::Index
Index32 Index
Definition: openvdb/Types.h:32
openvdb::v8_0::tree::IteratorBase::IteratorBase
IteratorBase()
Definition: Iterator.h:32
openvdb::v8_0::tree::DenseIteratorBase< MaskDenseIterator, DenseIter< NodeT, ChildT, ValueT, TagT >, NodeT, ChildT, ValueT >::NonConstValueType
typename std::remove_const< ValueT >::type NonConstValueType
Definition: Iterator.h:184
openvdb::v8_0::tree::IteratorBase::IteratorBase
IteratorBase(const MaskIterT &iter, NodeT *parent)
Definition: Iterator.h:33
NodeMasks.h
openvdb::v8_0::tree::SparseIteratorBase::operator->
ItemT * operator->() const
Return a pointer to the item to which this iterator is pointing.
Definition: Iterator.h:136
openvdb::v8_0::tree::IteratorBase::increment
void increment()
Advance to the next item in the parent node's table.
Definition: Iterator.h:70
openvdb::v8_0::tree::IteratorBase::pos
Index pos() const
Identical to offset.
Definition: Iterator.h:60
openvdb::v8_0::tree::IteratorBase::setValueOn
void setValueOn(bool on=true) const
If this iterator is pointing to a value, set the value's active state. Otherwise, do nothing.
Definition: Iterator.h:81
openvdb::v8_0::math::Coord
Signed (x, y, z) 32-bit integer coordinates.
Definition: Coord.h:26
openvdb::v8_0::tree::IteratorBase::getParentNode
NodeT * getParentNode() const
Return a pointer to the node (if any) over which this iterator is iterating.
Definition: Iterator.h:47
openvdb::v8_0::tree::IteratorBase::next
bool next()
Advance to the next item in the parent node's table.
Definition: Iterator.h:68
openvdb::v8_0::tree::IteratorBase::operator!=
bool operator!=(const IteratorBase &other) const
Definition: Iterator.h:41
openvdb::v8_0::tree::SparseIteratorBase::modifyValue
void modifyValue(const ModifyOp &op) const
Apply a functor to the item to which this iterator is pointing. (Not valid for const iterators....
Definition: Iterator.h:156
openvdb::v8_0::tree::DenseIteratorBase< MaskDenseIterator, DenseIter< NodeT, ChildT, ValueT, TagT >, NodeT, ChildT, ValueT >::NonConstNodeType
typename std::remove_const< NodeT >::type NonConstNodeType
Definition: Iterator.h:183
openvdb::v8_0::tree::SparseIteratorBase::getValue
ItemT & getValue() const
Return the item to which this iterator is pointing.
Definition: Iterator.h:139
openvdb::v8_0::tree::SparseIteratorBase
Base class for sparse iterators over internal and leaf nodes.
Definition: Iterator.h:115
OPENVDB_THROW
#define OPENVDB_THROW(exception, message)
Definition: openvdb/Exceptions.h:74
openvdb::v8_0::tree::DenseIteratorBase< MaskDenseIterator, DenseIter< NodeT, ChildT, ValueT, TagT >, NodeT, ChildT, ValueT >::NonConstChildNodeType
typename std::remove_const< ChildT >::type NonConstChildNodeType
Definition: Iterator.h:185
openvdb::v8_0::tree::IteratorBase::increment
void increment(Index n)
Advance n items in the parent node's table.
Definition: Iterator.h:74
openvdb::v8_0::tree::IteratorBase
Base class for iterators over internal and leaf nodes.
Definition: Iterator.h:30
openvdb::v8_0::tree::SparseIteratorBase::SparseIteratorBase
SparseIteratorBase()
Definition: Iterator.h:122
openvdb::v8_0::tree::IteratorBase::test
bool test() const
Return true if this iterator is not yet exhausted.
Definition: Iterator.h:63
openvdb::v8_0::math::operator*
Mat3< typename promote< T0, T1 >::type > operator*(const Mat3< T0 > &m0, const Mat3< T1 > &m1)
Multiply m0 by m1 and return the resulting matrix.
Definition: Mat3.h:611
openvdb::v8_0::tree::SparseIteratorBase::setValue
void setValue(const ItemT &value) const
Set the value of the item to which this iterator is pointing. (Not valid for const iterators....
Definition: Iterator.h:145
openvdb::v8_0::tree::SparseIteratorBase< MaskIterT, ValueIter< MaskIterT, NodeT, ValueT, TagT >, NodeT, ValueT >::ValueType
ValueT ValueType
Definition: Iterator.h:117
openvdb::v8_0::tree::SparseIteratorBase< MaskIterT, ValueIter< MaskIterT, NodeT, ValueT, TagT >, NodeT, ValueT >::NonConstNodeType
typename std::remove_const< NodeT >::type NonConstNodeType
Definition: Iterator.h:118
openvdb::v8_0::tree::DenseIteratorBase::DenseIteratorBase
DenseIteratorBase()
Definition: Iterator.h:188
openvdb::v8_0::ValueError
Definition: openvdb/Exceptions.h:65
openvdb::v8_0::tree::DenseIteratorBase::setValue
void setValue(const UnsetItemT &value) const
Replace with the given value the item in the parent node's table to which this iterator is pointing.
Definition: Iterator.h:243
openvdb::v8_0::tree::IteratorBase::operator++
IteratorBase & operator++()
Advance to the next item in the parent node's table.
Definition: Iterator.h:72
openvdb::v8_0::tree::IteratorBase::operator=
IteratorBase & operator=(const IteratorBase &)=default
openvdb::v8_0::tree::DenseIteratorBase::probeValue
bool probeValue(NonConstValueType &value) const
Return true if this iterator is pointing to a value and return the value in value....
Definition: Iterator.h:226
openvdb::v8_0::tree::SparseIteratorBase::operator*
ItemT & operator*() const
Return a reference to the item to which this iterator is pointing.
Definition: Iterator.h:134
openvdb::v8_0::tree::IteratorBase::getCoord
Coord getCoord() const
Return the coordinates of the item to which this iterator is pointing.
Definition: Iterator.h:89
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:153
openvdb::v8_0::tree::SparseIteratorBase::getItem
ItemT & getItem(Index) const
Return the item at the given index in the parent node's table.
openvdb::v8_0::tree::DenseIteratorBase::probeChild
SetItemT * probeChild(NonConstValueType &value) const
If this iterator is pointing to a child node, return a pointer to the node. Otherwise,...
Definition: Iterator.h:209
openvdb::v8_0::tree::SparseIteratorBase::setItem
void setItem(Index, const ItemT &) const
Set the value of the item at the given index in the parent node's table.
openvdb::v8_0::tree::SparseIteratorBase::SparseIteratorBase
SparseIteratorBase(const MaskIterT &iter, NodeT *parent)
Definition: Iterator.h:123
openvdb::v8_0::tree::IteratorBase::operator==
bool operator==(const IteratorBase &other) const
Definition: Iterator.h:37
openvdb::v8_0::tree::IteratorBase::offset
Index offset() const
Return this iterator's position as an index into the parent node's table.
Definition: Iterator.h:57
openvdb::v8_0::tree::SparseIteratorBase< MaskIterT, ValueIter< MaskIterT, NodeT, ValueT, TagT >, NodeT, ValueT >::NodeType
NodeT NodeType
Definition: Iterator.h:116
openvdb::v8_0::tree::DenseIteratorBase::setChild
void setChild(SetItemT *child) const
Replace with the given child node the item in the parent node's table to which this iterator is point...
Definition: Iterator.h:236
openvdb::v8_0::tree::DenseIteratorBase::probeChild
bool probeChild(SetItemT *&child, NonConstValueType &value) const
If this iterator is pointing to a child node, return true and return a pointer to the child node in c...
Definition: Iterator.h:218
openvdb::v8_0::tree::IteratorBase::parent
NodeT & parent() const
Return a reference to the node over which this iterator is iterating.
Definition: Iterator.h:50
openvdb::v8_0::tree::IteratorBase::setValueOff
void setValueOff() const
If this iterator is pointing to a value, mark the value as inactive.
Definition: Iterator.h:86
openvdb::v8_0::tree::DenseIteratorBase::setItem
void setItem(Index, SetItemT *) const
Set the value of the item at the given index in the parent node's table.
openvdb::v8_0::tree::DenseIteratorBase< MaskDenseIterator, DenseIter< NodeT, ChildT, ValueT, TagT >, NodeT, ChildT, ValueT >::ValueType
ValueT ValueType
Definition: Iterator.h:181
openvdb::v8_0::tree::DenseIteratorBase
Base class for dense iterators over internal and leaf nodes.
Definition: Iterator.h:179
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:101
openvdb::v8_0::tree::IteratorBase::isValueOn
bool isValueOn() const
Return true if this iterator is pointing to an active value. Return false if it is pointing to either...
Definition: Iterator.h:78
openvdb::v8_0::tree::IteratorBase::IteratorBase
IteratorBase(const IteratorBase &)=default
openvdb::v8_0::tree::DenseIteratorBase< MaskDenseIterator, DenseIter< NodeT, ChildT, ValueT, TagT >, NodeT, ChildT, ValueT >::ChildNodeType
ChildT ChildNodeType
Definition: Iterator.h:182
openvdb::v8_0::tree::DenseIteratorBase::DenseIteratorBase
DenseIteratorBase(const MaskIterT &iter, NodeT *parent)
Definition: Iterator.h:189
openvdb::v8_0::tree::DenseIteratorBase::getItem
bool getItem(Index, SetItemT *&child, NonConstValueType &value) const
Return true if the item at the given index in the parent node's table is a set value and return eithe...
openvdb
Definition: openvdb/Exceptions.h:13
openvdb::v8_0::tree::DenseIteratorBase::unsetItem
void unsetItem(Index, const UnsetItemT &) const
"Unset" the value of the item at the given index in the parent node's table.
Exceptions.h
openvdb::v8_0::tree::SparseIteratorBase< MaskIterT, ValueIter< MaskIterT, NodeT, ValueT, TagT >, NodeT, ValueT >::NonConstValueType
typename std::remove_const< ValueT >::type NonConstValueType
Definition: Iterator.h:119
openvdb::v8_0::tree::IteratorBase::getCoord
void getCoord(Coord &xyz) const
Return in xyz the coordinates of the item to which this iterator is pointing.
Definition: Iterator.h:91