5#include <Core/RaCore.hpp>
16template <
typename T, u
int D>
22 static const uint Dimension = D;
24 Eigen::Matrix<uint, D, 1>;
53 template <
typename T2>
126 inline bool operator==(
const Iterator& other )
const;
127 inline bool operator<(
const Iterator& other )
const;
134 friend Iterator operator+(
const Iterator& it, uint i ) {
138 friend Iterator operator-(
const Iterator& it, uint i ) {
156 inline bool operator!=(
const Iterator& rhs )
const {
return !( *
this == rhs ); }
157 inline bool operator>(
const Iterator& rhs )
const {
return rhs < *
this; }
158 inline bool operator<=(
const Iterator& rhs )
const {
return !( *
this > rhs ); }
159 inline bool operator>=(
const Iterator& rhs )
const {
return !( *
this < rhs ); }
207 inline const T&
at( uint idx )
const;
208 inline T&
at( uint idx );
226 inline Iterator
begin()
const;
230 inline Iterator
end()
const;
244template <
typename T, u
int D>
249 for ( uint i = 0; i < D; ++i ) {
250 result[i] = linIdx % size[i];
251 linIdx = linIdx / size[i];
256template <
typename T, u
int D>
261 for ( uint i = 0; i < D; ++i ) {
262 result += vecIdx[i] * dimProd;
272template <
typename T, u
int D>
274 CORE_ASSERT( m_data.size() == m_size.prod(),
"Inconsistent grid size" );
275 return m_data.size();
278template <
typename T, u
int D>
280 CORE_ASSERT( m_data.size() == m_size.prod(),
"Inconsistent grid size" );
284template <
typename T, u
int D>
286 CORE_ASSERT( m_data.size() == m_size.prod(),
"Inconsistent grid size" );
287 return m_data.empty();
290template <
typename T, u
int D>
293 m_size = IdxVector::Zero();
294 CORE_ASSERT( empty(),
"Inconsistent grid" );
297template <
typename T, u
int D>
299 return m_data.data();
302template <
typename T, u
int D>
304 return m_data.data();
311template <
typename T, u
int D>
313 const uint linIdx = idxVectorToLinear<T, D>( idx, sizeVector() );
314 CORE_ASSERT( linIdx < m_data.size(),
"Invalid vector index" );
315 return m_data[linIdx];
318template <
typename T, u
int D>
320 const uint linIdx = idxVectorToLinear<T, D>( idx, sizeVector() );
321 CORE_ASSERT( linIdx < m_data.size(),
"Invalid vector index" );
322 return m_data[linIdx];
325template <
typename T, u
int D>
327 CORE_ASSERT( idx < m_data.size(),
"Invalid vector index" );
331template <
typename T, u
int D>
333 CORE_ASSERT( idx < m_data.size(),
"Invalid vector index" );
337template <
typename T, u
int D>
339 CORE_ASSERT( it.
getGridSize() == m_size,
"Incompatible iterator" );
343template <
typename T, u
int D>
345 CORE_ASSERT( it.getGridSize() == m_size,
"Incompatible iterator" );
346 return at( it.getLinear() );
353template <
typename T, u
int D>
358template <
typename T, u
int D>
360 return Iterator( *
this );
363template <
typename T, u
int D>
368template <
typename T, u
int D>
370 return Iterator( *
this, size() );
377template <
typename T, u
int D>
380 setFromLinear( startIdx );
383template <
typename T, u
int D>
387 setFromVector( startIdx );
390template <
typename T, u
int D>
392 m_sizes( grid.sizeVector() ) {
396template <
typename T, u
int D>
407template <
typename T, u
int D>
412template <
typename T, u
int D>
414 m_index = idxVectorToLinear<T, D>( idx, m_sizes );
417template <
typename T, u
int D>
422template <
typename T, u
int D>
424 return linearToIdxVector<T, D>( m_index, m_sizes );
427template <
typename T, u
int D>
432template <
typename T, u
int D>
434 return m_index < m_sizes.prod();
441template <
typename T, u
int D>
447template <
typename T, u
int D>
453template <
typename T, u
int D>
460template <
typename T, u
int D>
462 Iterator copy( *
this );
467template <
typename T, u
int D>
473template <
typename T, u
int D>
479template <
typename T, u
int D>
482 CORE_ASSERT( isValidOffset( idx.template cast<int>() ),
"Invalid offset vector." );
483 setFromVector( getVector() + idx );
487template <
typename T, u
int D>
490 CORE_ASSERT( isValidOffset( -( idx.template cast<int>() ) ),
"Invalid offset vector." );
491 setFromVector( getVector() - idx );
495template <
typename T, u
int D>
498 CORE_ASSERT( isValidOffset( idx ),
"Invalid offset vector" );
499 setFromVector( ( getVector().
template cast<int>() + idx ).
template cast<uint>() );
503template <
typename T, u
int D>
504bool Grid<T, D>::Iterator::operator==(
const typename Grid<T, D>::Iterator& other )
const {
505 CORE_ASSERT( m_sizes == other.m_sizes,
"Comparing unrelated grid iterators" );
506 return m_index == other.m_index;
509template <
typename T, u
int D>
510bool Grid<T, D>::Iterator::operator<(
const typename Grid<T, D>::Iterator& other )
const {
511 CORE_ASSERT( m_sizes == other.m_sizes,
"Comparing unrelated grid iterators" );
512 return m_index < other.m_index;
515template <
typename T, u
int D>
520template <
typename T, u
int D>
521template <
typename T2>
526template <
typename T, u
int D>
528 OffsetVector pos = getVector().template cast<int>() + idx;
529 return !( ( pos.array() < 0 ).any() ||
530 ( pos.array() >= m_sizes.template cast<int>().array() ).any() );
A vector of signed offsets.
IdxVector getVector() const
Get the current vector index.
Iterator & operator++()
Advance the iterator to the next-element.
void setFromLinear(uint idx)
Set an existing iterator to a linear index value.
bool isOut() const
Returns true if the current index is out of the grid.
void setFromVector(const IdxVector &idx)
Set an existing iterator to a vector index value.
const IdxVector & getGridSize() const
Get the associated grid size.
bool isValidOffset(const OffsetVector &idx)
Iterator(const Iterator &other)=default
Default copy constructor and assignment operator.
Grid< T2, D >::Iterator cast() const
Cast to the an iterator in a different type grid.
Iterator & operator-=(uint i)
Rewind the iterator with an offset of i elements.
Iterator & operator+=(uint i)
Advance the iterator with an offset of i elements.
Iterator(const IdxVector &size, const IdxVector &startIdx)
Constructor from size and vector index.
Iterator(const IdxVector &size, uint startIdx=0)
Constructor from size and linear index.
bool isIn() const
Returns true if the current index is inside the grid.
Iterator & operator--()
Move the iterator back to the previous element.
Iterator(const Grid< T, D > &grid, const IdxVector &startIdx)
Constructor from grid and vector index.
uint getLinear() const
Get the current linear index.
Iterator begin()
Get an iterator on this grid at the first element.
IdxVector m_size
Indicate the extends of the grid along each dimension.
const T & at(uint idx) const
Access an element with a linear index.
Grid(const Grid< T, D > &other)=default
Copy constructor and assignment operator.
Grid(const IdxVector &size, const T *values)
Construct a grid of a given size with values in ()-major format.
T * data()
Read-write access to the underlying data.
Grid(const IdxVector &size=IdxVector::Zero(), const T &val=T())
Construct a grid of a given size and fill it with the given value.
void clear()
Erases all data and makes the grid empty.
Eigen::Matrix< uint, D, 1 > IdxVector
Dimension of our grid.
const IdxVector & sizeVector() const
Returns the size vector (a D-dimensional vector with the size along each dimension).
const T & at(const IdxVector &idx) const
Access an element with a D-dimensional index.
const T & at(const Iterator &it) const
Access an element with an iterator.
const T * data() const
Read only access to the underlying data.
Eigen::Matrix< int, D, 1 > OffsetVector
A vector of the size of the grid along each dimension.
bool empty() const
Returns true if the grid is empty (i.e. if size() ==0).
std::vector< T > m_data
Storage for the grid data.
uint size() const
Returns the number of elements stored.
Iterator end()
Get an iterator on this grid past the last element.
hepler function to manage enum as underlying types in VariableSet