#ifndef Connectivity_txx #define Connectivity_txx #include "Connectivity.h" namespace itk { template Connectivity const * Connectivity ::m_Instance = 0; template Connectivity const & Connectivity ::GetInstance() { if(m_Instance == 0) { m_Instance = new Connectivity; } return (*m_Instance) ; } template Connectivity ::Connectivity() : m_NeighborhoodSize(static_cast(std::pow(3.0, static_cast(VDim)))), m_NumberOfNeighbors( ComputeNumberOfNeighbors() ), m_NeighborsPoints(new Point[m_NumberOfNeighbors] ), m_NeighborsOffsets(new Offset[m_NumberOfNeighbors] ) { int currentNbNeighbors = 0; for(int i=0; i< std::pow( float(VDim), 3); ++i) { Point const p = OffsetToPoint(i); unsigned int const numberOfZeros = std::count(p.begin(), p.end(), 0); if( numberOfZeros!=VDim && numberOfZeros >= VCellDim) { { Point & neighbor = *(m_NeighborsPoints+currentNbNeighbors); neighbor = p; } { Offset & neighbor = *(m_NeighborsOffsets+currentNbNeighbors); neighbor = i; } ++currentNbNeighbors; } } } template Connectivity ::~Connectivity() { delete[] m_NeighborsPoints; delete[] m_NeighborsOffsets; } template bool Connectivity ::AreNeighbors(Point const & p1, Point const & p2) const { Point difference; for(unsigned int i=0; i bool Connectivity ::AreNeighbors(Offset const & o1, Point const & o2) const { /// @todo assert(false && "not implemented"); return false; } template bool Connectivity ::IsInNeighborhood(Point const & p) const { Point* const iterator = std::find(m_NeighborsPoints, m_NeighborsPoints+m_NumberOfNeighbors, p); return ( iterator != m_NeighborsPoints+m_NumberOfNeighbors ); } template bool Connectivity ::IsInNeighborhood(Offset const & o) const { Offset* const iterator = std::find(m_NeighborsOffsets, m_NeighborsOffsets+m_NumberOfNeighbors, o); return (iterator != m_NeighborsOffsets+m_NumberOfNeighbors); } template typename Connectivity::Point Connectivity ::OffsetToPoint(Offset const offset) const { Offset remainder = offset; Point p; for(unsigned int i=0; i typename Connectivity::Offset Connectivity ::PointToOffset(Point const p) const { Offset offset=0; Offset factor=1; for(unsigned int i=0; i int Connectivity ::ComputeNumberOfNeighbors() { int numberOfNeighbors = 0; for(unsigned int i = VCellDim; i <= VDim-1; ++i) { numberOfNeighbors += factorial(VDim)/(factorial(VDim-i)*factorial(i)) * 1<<(VDim-i); } return numberOfNeighbors; } } #endif // Connectivity_txx