5 #include <Core/RaCore.hpp>
16 template <
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;
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 ); }
167 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
206 inline const T&
at( uint idx )
const;
207 inline T&
at( uint idx );
225 inline Iterator
begin()
const;
229 inline Iterator
end()
const;
243 template <
typename T, u
int D>
248 for ( uint i = 0; i < D; ++i ) {
249 result[i] = linIdx % size[i];
250 linIdx = linIdx / size[i];
255 template <
typename T, u
int D>
260 for ( uint i = 0; i < D; ++i ) {
261 result += vecIdx[i] * dimProd;
272 template <
typename T, u
int D>
274 m_size( size ), m_data( size.prod(), val ) {}
276 template <
typename T, u
int D>
278 m_size( size ), m_data( size.prod(), values ) {}
283 template <
typename T, u
int D>
285 CORE_ASSERT( m_data.size() == m_size.prod(),
"Inconsistent grid size" );
286 return m_data.size();
289 template <
typename T, u
int D>
291 CORE_ASSERT( m_data.size() == m_size.prod(),
"Inconsistent grid size" );
295 template <
typename T, u
int D>
297 CORE_ASSERT( m_data.size() == m_size.prod(),
"Inconsistent grid size" );
298 return m_data.empty();
301 template <
typename T, u
int D>
304 m_size = IdxVector::Zero();
305 CORE_ASSERT( empty(),
"Inconsistent grid" );
308 template <
typename T, u
int D>
310 return m_data.data();
313 template <
typename T, u
int D>
315 return m_data.data();
322 template <
typename T, u
int D>
324 const uint linIdx = idxVectorToLinear<T, D>( idx, sizeVector() );
325 CORE_ASSERT( linIdx < m_data.size(),
"Invalid vector index" );
326 return m_data[linIdx];
329 template <
typename T, u
int D>
331 const uint linIdx = idxVectorToLinear<T, D>( idx, sizeVector() );
332 CORE_ASSERT( linIdx < m_data.size(),
"Invalid vector index" );
333 return m_data[linIdx];
336 template <
typename T, u
int D>
338 CORE_ASSERT( idx < m_data.size(),
"Invalid vector index" );
342 template <
typename T, u
int D>
344 CORE_ASSERT( idx < m_data.size(),
"Invalid vector index" );
348 template <
typename T, u
int D>
350 CORE_ASSERT( it.
getGridSize() == m_size,
"Incompatible iterator" );
354 template <
typename T, u
int D>
356 CORE_ASSERT( it.getGridSize() == m_size,
"Incompatible iterator" );
357 return at( it.getLinear() );
364 template <
typename T, u
int D>
369 template <
typename T, u
int D>
371 return Iterator( *
this );
374 template <
typename T, u
int D>
379 template <
typename T, u
int D>
381 return Iterator( *
this, size() );
388 template <
typename T, u
int D>
391 setFromLinear( startIdx );
394 template <
typename T, u
int D>
398 setFromVector( startIdx );
401 template <
typename T, u
int D>
403 m_sizes( grid.sizeVector() ) {
407 template <
typename T, u
int D>
418 template <
typename T, u
int D>
423 template <
typename T, u
int D>
425 m_index = idxVectorToLinear<T, D>( idx, m_sizes );
428 template <
typename T, u
int D>
433 template <
typename T, u
int D>
435 return linearToIdxVector<T, D>( m_index, m_sizes );
438 template <
typename T, u
int D>
443 template <
typename T, u
int D>
445 return m_index < m_sizes.prod();
452 template <
typename T, u
int D>
458 template <
typename T, u
int D>
464 template <
typename T, u
int D>
471 template <
typename T, u
int D>
473 Iterator copy( *
this );
478 template <
typename T, u
int D>
484 template <
typename T, u
int D>
490 template <
typename T, u
int D>
493 CORE_ASSERT( isValidOffset( idx.template cast<int>() ),
"Invalid offset vector." );
494 setFromVector( getVector() + idx );
498 template <
typename T, u
int D>
501 CORE_ASSERT( isValidOffset( -( idx.template cast<int>() ) ),
"Invalid offset vector." );
502 setFromVector( getVector() - idx );
506 template <
typename T, u
int D>
509 CORE_ASSERT( isValidOffset( idx ),
"Invalid offset vector" );
510 setFromVector( ( getVector().
template cast<int>() + idx ).
template cast<uint>() );
514 template <
typename T, u
int D>
515 bool Grid<T, D>::Iterator::operator==(
const typename Grid<T, D>::Iterator& other )
const {
516 CORE_ASSERT( m_sizes == other.m_sizes,
"Comparing unrelated grid iterators" );
517 return m_index == other.m_index;
520 template <
typename T, u
int D>
521 bool Grid<T, D>::Iterator::operator<(
const typename Grid<T, D>::Iterator& other )
const {
522 CORE_ASSERT( m_sizes == other.m_sizes,
"Comparing unrelated grid iterators" );
523 return m_index < other.m_index;
526 template <
typename T, u
int D>
531 template <
typename T, u
int D>
532 template <
typename T2>
537 template <
typename T, u
int D>
539 OffsetVector pos = getVector().template cast<int>() + idx;
540 return !( ( pos.array() < 0 ).any() ||
541 ( 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.
void clear()
Erases all data and makes the grid empty.
EIGEN_MAKE_ALIGNED_OPERATOR_NEW Grid(const IdxVector &size=IdxVector::Zero(), const T &val=T())
Construct a grid of a given size and fill it with the given value.
const IdxVector & sizeVector() const
Returns the size vector (a D-dimensional vector with the size along each dimension).
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).
Eigen::Matrix< uint, D, 1 > IdxVector
Dimension of our grid.
const T & at(const IdxVector &idx) const
Access an element with a D-dimensional index.
std::vector< T > m_data
Storage for the grid data.
uint size() const
Returns the number of elements stored.
const T & at(const Iterator &it) const
Access an element with an iterator.
Iterator end()
Get an iterator on this grid past the last element.