Commit cdc5490f authored by schurade's avatar schurade

[ADD] function that returns a 27 neighbourhood for a voxel

parent 8bc05e46
...@@ -547,6 +547,134 @@ std::vector< size_t > WGridRegular3D::getNeighbours( size_t id ) const ...@@ -547,6 +547,134 @@ std::vector< size_t > WGridRegular3D::getNeighbours( size_t id ) const
return neighbours; return neighbours;
} }
std::vector< size_t > WGridRegular3D::getNeighbours27( size_t id ) const
{
std::vector< size_t > neighbours;
size_t x = id % m_nbPosX;
size_t y = ( id / m_nbPosX ) % m_nbPosY;
size_t z = id / ( m_nbPosX * m_nbPosY );
if( x >= m_nbPosX || y >= m_nbPosY || z >= m_nbPosZ )
{
std::stringstream ss;
ss << "This point: " << id << " is not part of this grid: ";
ss << " nbPosX: " << m_nbPosX;
ss << " nbPosY: " << m_nbPosY;
ss << " nbPosZ: " << m_nbPosZ;
throw WOutOfBounds( ss.str() );
}
// for every neighbour we must check if its not on the boundary, it will be skipped otherwise
if( x > 0 )
{
neighbours.push_back( id - 1 );
if( y > 0 )
{
neighbours.push_back( id - m_nbPosX - 1 );
}
if( y < m_nbPosY - 1 )
{
neighbours.push_back( id + m_nbPosX - 1 );
}
}
if( x < m_nbPosX - 1 )
{
neighbours.push_back( id + 1 );
if( y > 0 )
{
neighbours.push_back( id - m_nbPosX + 1 );
}
if( y < m_nbPosY - 1 )
{
neighbours.push_back( id + m_nbPosX + 1 );
}
}
if( y > 0 )
{
neighbours.push_back( id - m_nbPosX );
}
if( y < m_nbPosY - 1 )
{
neighbours.push_back( id + m_nbPosX );
}
if( z > 0 )
{
int tempId = id - ( m_nbPosX * m_nbPosY );
neighbours.push_back( tempId );
if( x > 0 )
{
neighbours.push_back( tempId - 1 );
if( y > 0 )
{
neighbours.push_back( tempId - m_nbPosX - 1 );
}
if( y < m_nbPosY - 1 )
{
neighbours.push_back( tempId + m_nbPosX - 1 );
}
}
if( x < m_nbPosX - 1 )
{
neighbours.push_back( tempId + 1 );
if( y > 0 )
{
neighbours.push_back( tempId - m_nbPosX + 1 );
}
if( y < m_nbPosY - 1 )
{
neighbours.push_back( tempId + m_nbPosX + 1 );
}
}
if( y > 0 )
{
neighbours.push_back( tempId - m_nbPosX );
}
if( y < m_nbPosY - 1 )
{
neighbours.push_back( tempId + m_nbPosX );
}
}
if( z < m_nbPosZ - 1 )
{
int tempId = id + ( m_nbPosX * m_nbPosY );
neighbours.push_back( tempId );
if( x > 0 )
{
neighbours.push_back( tempId - 1 );
if( y > 0 )
{
neighbours.push_back( tempId - m_nbPosX - 1 );
}
if( y < m_nbPosY - 1 )
{
neighbours.push_back( tempId + m_nbPosX - 1 );
}
}
if( x < m_nbPosX - 1 )
{
neighbours.push_back( tempId + 1 );
if( y > 0 )
{
neighbours.push_back( tempId - m_nbPosX + 1 );
}
if( y < m_nbPosY - 1 )
{
neighbours.push_back( tempId + m_nbPosX + 1 );
}
}
if( y > 0 )
{
neighbours.push_back( tempId - m_nbPosX );
}
if( y < m_nbPosY - 1 )
{
neighbours.push_back( tempId + m_nbPosX );
}
}
return neighbours;
}
bool WGridRegular3D::encloses( const wmath::WPosition& pos ) const bool WGridRegular3D::encloses( const wmath::WPosition& pos ) const
{ {
return getVoxelNum( pos ) != -1; // note this is an integer comparision, hence it should be numerical stable! return getVoxelNum( pos ) != -1; // note this is an integer comparision, hence it should be numerical stable!
......
...@@ -406,6 +406,18 @@ public: ...@@ -406,6 +406,18 @@ public:
*/ */
std::vector< size_t > getNeighbours( size_t id ) const; std::vector< size_t > getNeighbours( size_t id ) const;
/**
* Return the list of all neighbour voxels.
*
* \throw WOutOfBounds If the voxel id is outside of the grid.
*
* \param id Number of the voxel for which the neighbours should be computed
*
* \return Vector of voxel ids which are all neighboured
*/
std::vector< size_t > getNeighbours27( size_t id ) const;
/** /**
* Decides whether a certain position is inside this grid or not. * Decides whether a certain position is inside this grid or not.
* *
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment