33 template<
typename Z,
int mode,
short_t d,
bool>
class gsGridIterator
34 {
using Z::GISMO_ERROR_gsGridIterator_has_invalid_template_arguments;};
69 template<
class Z,
int mode,
short_t d>
70 class gsGridIterator<Z,mode,d,true>
82 GISMO_STATIC_ASSERT(std::numeric_limits<Z>::is_integer,
"The template parameter needs to be an integer type.");
83 GISMO_STATIC_ASSERT(mode > -1 && mode < 3,
"The mode of gsGridIterator needs to be 0, 1 or 2.");
94 { reset(a, b, open); }
103 { reset(point::Zero(b.size()), b, open); }
112 { reset(ab.col(0), ab.col(1), open); }
123 GISMO_ASSERT(a.rows() == b.rows(),
"Invalid endpoint dimensions");
125 if (open) m_upp = b.array() - 1;
else m_upp = b;
126 m_valid = ( (m_low.array() <= m_upp.array()).all() ?
true : false );
140 operator bool()
const {
return m_valid;}
142 const point &
operator*()
const {
return m_cur;}
144 const point * operator->()
const {
return &m_cur;}
146 inline gsGridIterator & operator++()
153 for (
index_t i = 0; i != m_cur.size(); ++i)
154 if ( m_cur[i] != m_upp[i] )
156 if (0==mode) ++m_cur[i];
else m_cur[i] = m_upp[i];
166 for (
index_t i = 0; i != m_cur.size(); ++i)
168 if ( m_cur[i] != m_upp[i] )
170 if ( m_cur[i] == m_low[i] && (i+1!=m_cur.size() || 1==m_cur.size()) )
173 for (
index_t j = c; j!=m_cur.size(); ++j)
174 if ( (m_cur[j] == m_low[j]) ||
175 (m_cur[j] == m_upp[j]) )
186 m_cur.head(i) = m_low.head(i);
199 inline bool isFloor(
int i)
const {
return m_cur[i] == m_low[i];}
205 {
return mode ? m_cur[i] == m_upp[i] : m_cur[i] + 1 == m_upp[i];}
212 return (m_cur.array() == m_low.array()).any() ||
213 (m_cur.array() == m_upp.array()).any() ;
223 case 0:
return ((m_upp-m_low).array() + 1).prod();
224 case 1:
return ((m_upp-m_low).array() + 1).prod() - ((m_upp-m_low).array()-1).prod();
225 case 2:
return ( 1 << m_low.rows() );
264 point res(m_low.rows());
266 for (
index_t i = 1; i != res.size(); ++i )
267 res[i] = res[i-1] * ( m_upp[i-1] - m_low[i-1] + 1 );
307 template<
class T,
int mode,
short_t d>
308 class gsGridIterator<T,mode,d,false>
314 typedef gsGridIterator<index_t, mode, d> integer_iterator;
316 typedef typename integer_iterator::point point_index;
330 point_index
const & np)
334 GISMO_STATIC_ASSERT(mode > -1 && mode < 3,
"The mode of gsGridIterator needs to be 0, 1 or 2.");
346 point_index
const & np)
349 reset(ab.col(0), ab.col(1));
362 : m_low(ab.col(0)), m_upp(ab.col(1))
366 const double h = std::pow(L.prod()/numPoints, 1.0 / m_low.rows());
367 const point_index npts = (L/h).unaryExpr((
double(*)(double))std::ceil).
template cast<index_t>();
368 m_iter = integer_iterator(npts, 1);
369 reset(ab.col(0), ab.col(1));
376 void reset() { m_cur = m_low; m_iter.reset(); }
389 m_step = (b-a).array() / (m_iter.numPointsCwise().array() - 1)
390 .matrix().cwiseMax(1).template cast<T>().array() ;
397 integer_iterator iter = m_iter;
400 for(
index_t c = 0; iter; ++iter, ++c)
401 update(*iter,res.col(c).data());
411 operator bool()
const {
return m_iter;}
413 const gsMatrix<T> &
operator*()
const {
return m_cur;}
415 const gsMatrix<T> * operator->()
const {
return &m_cur;}
417 inline gsGridIterator & operator++()
419 if ( ++m_iter ) update(*m_iter, m_cur.data());
426 inline bool isFloor(
int i)
const {
return m_iter.isFloor(i);}
431 inline bool isCeil (
int i)
const {
return m_iter.isCeil(i);}
436 inline bool isBoundary()
const {
return m_iter.isBoundary();}
489 inline const gsMatrix<T> operator [] (
const point_index & ti)
const
492 update(ti, res.data());
499 inline void update(
const point_index & ti, T * pt)
const
501 for (
index_t i = 0; i != m_low.size(); ++i)
502 if ( ti[i] == m_iter.lower()[i] )
504 else if ( ti[i] == m_iter.upper()[i] )
507 *(pt++) = m_low[i] + static_cast<T>(ti[i]) * m_step[i];
538 template<
class T,
short_t d>
539 class gsGridIterator<T,
CWISE,d,false>
543 typedef gsGridIterator<index_t, 0, d> integer_iterator;
545 typedef typename integer_iterator::point point_index;
557 template<
class CwiseContainer>
559 : m_cwise(cwise.size()), m_cur(m_cwise.size(),1)
561 point_index npts(m_cwise.size());
562 for (
index_t i = 0; i != npts.size(); ++i)
564 m_cwise[i] = cwise[i].data();
565 npts[i] = cwise[i].size() - 1;
566 GISMO_ASSERT(cwise[i].cols()==1 || cwise[i].rows()==1,
"Invalid input");
568 m_iter = integer_iterator(npts, 0);
573 update(*m_iter, m_cur.data());
576 template<
class Matrix_t>
577 explicit gsGridIterator(
const std::vector<Matrix_t*> & cwise)
578 : m_cwise(cwise.size()), m_cur(m_cwise.size(),1)
580 point_index npts(m_cwise.size());
581 for (
index_t i = 0; i != npts.size(); ++i)
583 m_cwise[i] = cwise[i]->data();
584 npts[i] = cwise[i]->size() - 1;
585 GISMO_ASSERT(cwise[i]->cols()==1 || cwise[i]->rows()==1,
"Invalid input");
587 m_iter = integer_iterator(npts, 0);
588 update(*m_iter, m_cur.data());
595 void reset() { m_iter.reset(); update(*m_iter, m_cur.data());}
601 operator bool()
const {
return m_iter;}
603 const gsMatrix<T> &
operator*()
const {
return m_cur;}
605 const gsMatrix<T> * operator->()
const {
return &m_cur;}
607 inline gsGridIterator & operator++()
609 if (++m_iter) update(*m_iter, m_cur.data());
616 inline bool isFloor(
int i)
const {
return m_iter.isFloor(i);}
621 inline bool isCeil (
int i)
const {
return m_iter.isCeil(i);}
626 inline bool isBoundary()
const {
return m_iter.isBoundary();}
664 inline const gsMatrix<T> operator [] (
const point_index & ti)
const
667 update(ti, res.data());
674 inline void update(
const point_index & ti, T * pt)
676 for (
index_t i = 0; i != m_cur.rows(); ++i)
677 *(pt++) = m_cwise[i][ti[i]];
Boundary mode iterates over boundary lattice points only.
Definition: gsGridIterator.h:27
gsGridIterator()
Empty constructor.
Definition: gsGridIterator.h:80
bool isCeil(int i) const
Returns true if the i-th coordinate has maximal value.
Definition: gsGridIterator.h:204
Coordinate-wise mode iterates over a grid given by coordinate vectors.
Definition: gsGridIterator.h:29
integer_iterator m_iter
Underlying integer lattice iterator.
Definition: gsGridIterator.h:514
const point_index & tensorIndex() const
Returns the tensor index of the current point.
Definition: gsGridIterator.h:462
const point & step() const
Returns the coordinate-wise stepping between the samples.
Definition: gsGridIterator.h:472
Vertex mode iterates over cube vertices only.
Definition: gsGridIterator.h:28
CwiseData m_cwise
List of coordinate-wise values.
Definition: gsGridIterator.h:682
index_t numPoints() const
Returns the total number of points that are iterated.
Definition: gsGridIterator.h:219
const point & lower() const
Returns the first point in the iteration.
Definition: gsGridIterator.h:452
void reset()
Resets the iterator, so that a new iteration over the points may start.
Definition: gsGridIterator.h:595
gsGridIterator(point const &a, point const &b, point_index const &np)
Constructor using lower and upper limits.
Definition: gsGridIterator.h:328
gsMatrix< T > m_cur
Current point pointed at by the iterator.
Definition: gsGridIterator.h:515
integer_iterator m_iter
Underlying integer lattice iterator.
Definition: gsGridIterator.h:684
#define index_t
Definition: gsConfig.h:32
gsGridIterator(gsMatrix< T, d, 2 > const &ab, point_index const &np)
Constructor using lower and upper limits.
Definition: gsGridIterator.h:345
A matrix with arbitrary coefficient type and fixed or dynamic size.
Definition: gsMatrix.h:38
bool isFloor(int i) const
Returns true if the i-th coordinate has minimal value.
Definition: gsGridIterator.h:199
#define GISMO_ASSERT(cond, message)
Definition: gsDebug.h:89
gsGridIteratorMode
Specifies aliases describing the modes for gismo::gsGridIterator.
Definition: gsGridIterator.h:24
index_t index(const index_t i) const
Returns the i-th index of the current point.
Definition: gsGridIterator.h:647
bool isCeil(int i) const
Returns true if the i-th coordinate has maximal value.
Definition: gsGridIterator.h:621
const point & upper() const
Returns the last point in the iteration.
Definition: gsGridIterator.h:457
gsGridIterator(gsMatrix< Z, d, 2 > const &ab, bool open=true)
Constructor using lower and upper limits.
Definition: gsGridIterator.h:111
gsGridIterator(gsMatrix< T, d, 2 > const &ab, unsigned numPoints)
Constructor using lower and upper limits.
Definition: gsGridIterator.h:361
point m_upp
Iteration lower and upper limits.
Definition: gsGridIterator.h:273
index_t numPoints() const
Returns the total number of points that are iterated.
Definition: gsGridIterator.h:441
void reset()
Resets the iterator, so that a new iteration over the points may start.
Definition: gsGridIterator.h:133
void reset(point const &a, point const &b, bool open=true)
Resets the iterator using new lower and upper limits.
Definition: gsGridIterator.h:121
bool isBoundary() const
Returns true if the current point lies on a boundary.
Definition: gsGridIterator.h:210
point_index numPointsCwise() const
Returns the total number of points per coordinate which are iterated.
Definition: gsGridIterator.h:447
gsGridIterator(const CwiseContainer &cwise)
Constructor using references to the coordinate vectors.
Definition: gsGridIterator.h:558
EIGEN_STRONG_INLINE mult_expr< E1, E2 > const operator*(_expr< E1 > const &u, _expr< E2 > const &v)
Multiplication operator for expressions.
Definition: gsExpressions.h:4559
point m_step
Iteration lower and upper limits and stepsize.
Definition: gsGridIterator.h:512
void reset(point const &a, point const &b)
Resets the iterator using new lower and upper limits.
Definition: gsGridIterator.h:384
index_t index(const index_t i) const
Returns the i-th index of the current point.
Definition: gsGridIterator.h:467
bool m_valid
Indicates the state of the iterator.
Definition: gsGridIterator.h:275
gsGridIterator(point const &a, point const &b, bool open=true)
Constructor using lower and upper limits.
Definition: gsGridIterator.h:93
bool isBoundary() const
Returns true if the current point lies on a boundary.
Definition: gsGridIterator.h:436
bool isFloor(int i) const
Returns true if the i-th coordinate has minimal value.
Definition: gsGridIterator.h:616
const point_index & tensorIndex() const
Returns the tensor index of the current point.
Definition: gsGridIterator.h:642
void reset()
Resets the iterator, so that a new iteration over the points may start.
Definition: gsGridIterator.h:376
Cube mode iterates over all lattice points inside a cube.
Definition: gsGridIterator.h:26
bool isBoundary() const
Returns true if the current point lies on a boundary.
Definition: gsGridIterator.h:626
gsMatrix< T > m_cur
Current point pointed at by the iterator.
Definition: gsGridIterator.h:685
point numPointsCwise() const
Returns the total number of points per coordinate which are iterated.
Definition: gsGridIterator.h:234
point_index numPointsCwise() const
Returns the total number of points per coordinate which are iterated.
Definition: gsGridIterator.h:637
#define GISMO_ERROR(message)
Definition: gsDebug.h:118
bool isFloor(int i) const
Returns true if the i-th coordinate has minimal value.
Definition: gsGridIterator.h:426
bool isCeil(int i) const
Returns true if the i-th coordinate has maximal value.
Definition: gsGridIterator.h:431
const point & lower() const
Returns the first point in the iteration.
Definition: gsGridIterator.h:239
index_t numPoints() const
Returns the total number of points that are iterated.
Definition: gsGridIterator.h:631
const integer_iterator & index_iterator() const
Returns a reference to the underlying integer lattice iterator.
Definition: gsGridIterator.h:478
const point & upper() const
Returns the last point in the iteration.
Definition: gsGridIterator.h:244
const integer_iterator & index_iterator() const
Returns a reference to the underlying integer lattice iterator.
Definition: gsGridIterator.h:653
point strides() const
Utility function which returns the vector of strides.
Definition: gsGridIterator.h:262
gsGridIterator(point const &b, bool open=true)
Constructor using upper limit. The iteration starts from zero.
Definition: gsGridIterator.h:102
point m_cur
Current point pointed at by the iterator.
Definition: gsGridIterator.h:274