G+Smo  24.08.0
Geometry + Simulation Modules
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
gsHDomainLeafIter.h
Go to the documentation of this file.
1 
14 #pragma once
15 
16 #include <gsHSplines/gsKdNode.h>
17 #include <gsCore/gsTemplateTools.h>
18 
19 namespace gismo
20 {
21 
28 template<typename node, bool isconst = false>
30 {
31 public:
32  //typedef kdnode<d, index_t> node;
33  typedef typename util::conditional<isconst, const node&, node&>::type reference;
34  typedef typename util::conditional<isconst, const node*, node*>::type pointer;
35 
36  typedef typename node::point point;
37 
38 public:
39  reference operator*() const { return *curNode; }
40  pointer operator->() const { return curNode; }
41 
42 public:
43 
44  gsHDomainLeafIter() : curNode(0)
45  { }
46 
47  explicit gsHDomainLeafIter( node * const root_node, index_t index_level)
48  : m_index_level(index_level)
49  {
50  m_stack.push(root_node);
51 
52  // Go to the first leaf
53  next();
54  }
55 
56  // Next leaf
57  bool next()
58  {
59  while ( ! m_stack.empty() )
60  {
61  curNode = m_stack.top();
62  m_stack.pop();
63 
64  if ( curNode->isLeaf() )
65  {
66  return true;
67  }
68  else // this is a split-node
69  {
70  m_stack.push(curNode->left );
71  m_stack.push(curNode->right);
72  }
73  }
74 
75  // Leaves exhausted
76  curNode = NULL;
77  return false;
78  }
79 
81  bool good() const { return curNode != 0; }
82 
84  void startFrom( node * const root_node)
85  {
86  m_stack.clear();
87  m_stack.push(root_node);
88  }
89 
90  int level() const { return curNode->level; }
91 
92  point lowerCorner() const
93  {
94  point result = curNode->box->first;
95  const int lvl = curNode->level;
96 
97  //result = result.array() / (1>> (m_index_level-lvl)) ;
98  for ( index_t i = 0; i!= result.size(); ++i )
99  result[i] = result[i] >> (m_index_level-lvl) ;
100 
101  return result;
102  }
103 
104  point upperCorner() const
105  {
106  point result = curNode->box->second;
107  const int lvl = curNode->level;
108 
109  for ( index_t i = 0; i!=result.size(); ++i )
110  result[i] = result[i] >> (m_index_level-lvl) ;
111 
112  return result;
113  }
114 
115  index_t indexLevel() const {return m_index_level;}
116 
117  bool isAligned() const
118  {
119  const unsigned h = 1 << (m_index_level - curNode->level);
120 
121  for ( index_t i = 0; i!=curNode->box->first.size(); ++i )
122  {
123  if (curNode->box->second[i] % h != 0 ||
124  curNode->box->first[i] % h != 0 )
125  return false;
126  }
127  return true;
128  }
129 
130 private:
131 
132  // current node
133  node * curNode;
134 
137 
138  // stack of pointers to tree nodes, used in next()
139  std::stack<node*> m_stack; // to do: change type to std::vector
140 };
141 
142 
143 
144 } // end namespace gismo
Iterates over the leaves of an gsHDomain (tree).
Definition: gsHDomainLeafIter.h:29
#define index_t
Definition: gsConfig.h:32
void startFrom(node *const root_node)
The iteration is done in the sub-tree hanging from node root_node.
Definition: gsHDomainLeafIter.h:84
index_t m_index_level
The level of the box representation.
Definition: gsHDomainLeafIter.h:136
Utilities related to template programming.
Provides declaration of the tree node.
bool good() const
Returns true iff we are still pointing at a valid leaf.
Definition: gsHDomainLeafIter.h:81