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  m_id = 0;
124  curElement = meshStart;
125  m_isGood = ( meshEnd.array() != meshStart.array() ).all() ;
126  if (m_isGood)
127  update();
128  }
129 
132  {
133  gsVector<unsigned, D> curr_index(d);
134  for (int i = 0; i < d; ++i)
135  curr_index[i] = curElement[i] - breaks[i].begin();
136  return curr_index;
137  }
138 
139  void getVertices(gsMatrix<T>& result)
140  {
141  result.resize( D, 1 << D);
142 
143  gsVector<T,D> v, l, u;
144  l.setZero();
145  u.setOnes();
146  v.setZero();
147  int r = 0;
148  do {
149  for ( int i = 0; i< D; ++i)
150  result(i,r) = ( v[i] ? upper[i] : lower[i] );
151  }
152  while ( nextCubeVertex(v, l, u) );
153  }
154 
155  const gsVector<T> & lowerCorner() const
156  { return lower; }
157 
158  const gsVector<T> & upperCorner() const
159  { return upper; }
160 
161  bool isBoundaryElement() const
162  {
163  for (int i = 0; i< D; ++i)
164  if ((lower[i]-*meshStart[i]==0) ||
165  (*meshEnd[0]-upper[0] ==0) )
166  return true;
167  return false;
168  }
169 
170  index_t domainDim() const {return d;}
171 
172 private:
173 
177  void update()
178  {
179  for (int i = 0; i < d; ++i)
180  {
181  lower[i] = *curElement[i];
182  upper[i] = *(curElement[i]+1);
183  center[i] = (T)(0.5) * (lower[i] + upper[i]);
184  }
185  }
186 
187 // size_t numElements() const
188 // {
189 //
190 // }
191 
192 // Data members
193 public:
195 
196 protected:
200 
201 private:
202  // the dimension of the parameter space
203  int d;
204 
205  // coordinates of the grid cell boundaries
206  std::vector< std::vector<T> > breaks;
207 
208  // Extent of the tensor grid
209  gsVector<uiter, D> meshStart, meshEnd;
210 
211  // Current element as pointers to it's supporting mesh-lines
212  gsVector<uiter, D> curElement;
213 
214  // parameter coordinates of current grid cell
215  gsVector<T> lower, upper;
216 
217 public:
218 # define Eigen gsEigen
219  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
220 # undef Eigen
221 }; // class gsTensorDomainIterator
222 
223 
224 } // 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:177
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:158
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:131
const gsVector< T > & lowerCorner() const
Returns the lower corner of the current element.
Definition: gsTensorDomainIterator.h:155
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