G+Smo  24.08.0
Geometry + Simulation Modules
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
gsTensorDomainIterator.h
Go to the documentation of this file.
1 
14 #pragma once
15 
17 
19 
21 
22 namespace gismo
23 {
24 // Documentation in gsDomainIterator.h
25 // Class which enables iteration over all elements of a tensor product parameter domain
26 
34 template<class T, int D>
36 {
37 private:
38  typedef typename std::vector<T>::const_iterator uiter;
39 
40 public:
41 
42  gsTensorDomainIterator(const std::vector< std::vector<T> > & breaks_)
43  : d( breaks_.size() ),
44  lower ( gsVector<T, D>::Zero(d) ),
45  upper ( gsVector<T, D>::Zero(d) )
46  {
48 
49  // compute breaks and mesh size
50  meshStart.resize(d);
51  meshEnd.resize(d);
52  curElement.resize(d);
53  breaks = breaks_;
54  for (int i=0; i < d; ++i)
55  {
56  meshEnd[i] = breaks[i].end() - 1;
57  curElement[i] = meshStart[i] = breaks[i].begin();
58 
59  if (meshEnd[i] == meshStart[i])
60  m_isGood = false;
61  }
62 
63  if (m_isGood)
64  update();
65  }
66 
67 
68  // note: this assumes that b is a tensor product basis
69  gsTensorDomainIterator(const gsBasis<T>& b) // to do: change to gsTensorBasis
70  : gsDomainIterator<T>( b ),
71  d( m_basis->dim() ),
72  lower ( gsVector<T, D>::Zero(d) ),
73  upper ( gsVector<T, D>::Zero(d) )
74  {
75  // compute breaks and mesh size
76  meshStart.resize(d);
77  meshEnd.resize(d);
78  curElement.resize(d);
79  breaks.reserve(d);
80  for (int i=0; i < d; ++i)
81  {
82  breaks.push_back( m_basis->component(i).domain()->breaks() );
83  // for n breaks, we have n-1 elements (spans)
84  meshEnd[i] = breaks[i].end() - 1;
85  curElement[i] = meshStart[i] = breaks[i].begin();
86 
87  if (meshEnd[i] == meshStart[i])
88  m_isGood = false;
89  }
90 
91  if (m_isGood)
92  update();
93  }
94 
95  // Documentation in gsDomainIterator.h
96  bool next()
97  {
98  m_isGood = m_isGood && nextLexicographic(curElement, meshStart, meshEnd);
99  if (m_isGood)
100  {
101  update();
102  ++m_id; //increment id
103  }
104  return m_isGood;
105  }
106 
107  // Documentation in gsDomainIterator.h
108  bool next(index_t increment)
109  {
110  for (index_t i = 0; i < increment; i++)
111  m_isGood = m_isGood && nextLexicographic(curElement, meshStart, meshEnd);
112  if (m_isGood)
113  {
114  update();
115  m_id += increment; //increment id
116  }
117  return m_isGood;
118  }
119 
120  // Documentation in gsDomainIterator.h
121  void reset()
122  {
123  curElement = meshStart;
124  m_isGood = ( meshEnd.array() != meshStart.array() ).all() ;
125  if (m_isGood)
126  update();
127  }
128 
131  {
132  gsVector<unsigned, D> curr_index(d);
133  for (int i = 0; i < d; ++i)
134  curr_index[i] = curElement[i] - breaks[i].begin();
135  return curr_index;
136  }
137 
138  void getVertices(gsMatrix<T>& result)
139  {
140  result.resize( D, 1 << D);
141 
142  gsVector<T,D> v, l, u;
143  l.setZero();
144  u.setOnes();
145  v.setZero();
146  int r = 0;
147  do {
148  for ( int i = 0; i< D; ++i)
149  result(i,r) = ( v[i] ? upper[i] : lower[i] );
150  }
151  while ( nextCubeVertex(v, l, u) );
152  }
153 
154  const gsVector<T> & lowerCorner() const
155  { return lower; }
156 
157  const gsVector<T> & upperCorner() const
158  { return upper; }
159 
160  bool isBoundaryElement() const
161  {
162  for (int i = 0; i< D; ++i)
163  if ((lower[i]-*meshStart[i]==0) ||
164  (*meshEnd[0]-upper[0] ==0) )
165  return true;
166  return false;
167  }
168 
169  index_t domainDim() const {return d;}
170 
171 private:
172 
176  void update()
177  {
178  for (int i = 0; i < d; ++i)
179  {
180  lower[i] = *curElement[i];
181  upper[i] = *(curElement[i]+1);
182  center[i] = (T)(0.5) * (lower[i] + upper[i]);
183  }
184  }
185 
186 // size_t numElements() const
187 // {
188 //
189 // }
190 
191 // Data members
192 public:
194 
195 protected:
199 
200 private:
201  // the dimension of the parameter space
202  int d;
203 
204  // coordinates of the grid cell boundaries
205  std::vector< std::vector<T> > breaks;
206 
207  // Extent of the tensor grid
208  gsVector<uiter, D> meshStart, meshEnd;
209 
210  // Current element as pointers to it's supporting mesh-lines
211  gsVector<uiter, D> curElement;
212 
213  // parameter coordinates of current grid cell
214  gsVector<T> lower, upper;
215 
216 public:
217 # define Eigen gsEigen
218  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
219 # undef Eigen
220 }; // class gsTensorDomainIterator
221 
222 
223 } // namespace gismo
Re-implements gsDomainIterator for iteration over all elements of a tensor product parameter domain...
Definition: gsTensorDomainIterator.h:35
Provides the Gauss-Legendre quadrature rule.
void reset()
Resets the iterator so that it points to the first element.
Definition: gsTensorDomainIterator.h:121
#define index_t
Definition: gsConfig.h:32
bool next()
Proceeds to the next element.
Definition: gsTensorDomainIterator.h:96
Provides combinatorial unitilies.
void update()
Definition: gsTensorDomainIterator.h:176
A vector with arbitrary coefficient type and fixed or dynamic size.
Definition: gsVector.h:35
Class which enables iteration over all elements of a parameter domain.
Definition: gsDomainIterator.h:67
Provides declaration of DomainIterator abstract interface.
const gsVector< T > & upperCorner() const
Returns the upper corner of the current element.
Definition: gsTensorDomainIterator.h:157
bool next(index_t increment)
Proceeds to the next element (skipping increment elements).
Definition: gsTensorDomainIterator.h:108
bool m_isGood
Definition: gsDomainIterator.h:223
bool nextLexicographic(Vec &cur, const Vec &size)
Iterates through a tensor lattice with the given size. Updates cur and returns true if another entry ...
Definition: gsCombinatorics.h:196
const gsBasis< T > * m_basis
The basis on which the domain iterator is defined.
Definition: gsDomainIterator.h:219
gsVector< T > center
Coordinates of a central point in the element (in the parameter domain).
Definition: gsDomainIterator.h:215
gsVector< unsigned, D > index() const
return the tensor index of the current element
Definition: gsTensorDomainIterator.h:130
const gsVector< T > & lowerCorner() const
Returns the lower corner of the current element.
Definition: gsTensorDomainIterator.h:154
A basis represents a family of scalar basis functions defined over a common parameter domain...
Definition: gsBasis.h:78
bool nextCubeVertex(Vec &cur, const Vec &start, const Vec &end)
Iterate in lexicographic order through the vertices of the cube [start,end]. Updates cur with the cur...
Definition: gsCombinatorics.h:252