#ifndef itkConnectivity_txx #define itkConnectivity_txx #include "itkConnectivity.h" namespace itk { template int Connectivity ::m_GlobalDefaultCellDimension = VDimension - 1; template Connectivity ::Connectivity() { this->SetCellDimension( m_GlobalDefaultCellDimension ); } template bool Connectivity ::AreNeighbors(IndexType const & p1, IndexType const & p2) const { OffsetType diff; for(unsigned int i=0; iIsInNeighborhood( diff ); } template bool Connectivity ::IsInNeighborhood(OffsetType const & o) const { typename OffsetContainerType::const_iterator iterator = std::find(m_Neighbors.begin(), m_Neighbors.end(), o); return (iterator != m_Neighbors.end()); } template const typename Connectivity::OffsetContainerType & Connectivity ::GetNeighbors() const { return m_Neighbors; } template int Connectivity ::GetNeighborhoodSize() { return static_cast(vcl_pow( 3.0, static_cast(VDimension) ) ); } template int Connectivity ::GetNumberOfNeighbors() const { return m_Neighbors.size(); } template void Connectivity ::SetNumberOfNeighbors( int nb ) { // and search a matching cell dimension. It should be quite // fast with usual dimensions. bool found = false; for( int i=0; i<=VDimension && !found; i++ ) { if( ComputeNumberOfNeighbors( i ) == nb ) { this->SetCellDimension( i ); found = true; } } if( !found ) { // send an exception itkExceptionMacro( << nb << " is not a valid number of neighbors for dimension " << VDimension << "."); } } template int Connectivity ::ComputeNumberOfNeighbors( int cellDimension ) { int numberOfNeighbors = 0; for(unsigned int i = cellDimension; i <= VDimension-1; ++i) { numberOfNeighbors += factorial(VDimension)/(factorial(VDimension-i)*factorial(i)) * 1<<(VDimension-i); } return numberOfNeighbors; } template void Connectivity ::SetCellDimension( int dim ) { // check the validity of the requested cell dimension if( dim < 0 || dim > VDimension ) { itkExceptionMacro( << dim << " is not a valid cell dimension for dimension " << VDimension << "." ); } m_CellDimension = dim; m_Neighbors.clear(); m_Neighbors.reserve( ComputeNumberOfNeighbors( dim ) ); int neighborhoodSize = this->GetNeighborhoodSize(); for(int i=0; i< neighborhoodSize; ++i) { OffsetType const offset = IntToOffset( i ); unsigned int numberOfZeros = 0; for( int j=0; j= m_CellDimension) { m_Neighbors.push_back( offset ); } } } template const int & Connectivity ::GetCellDimension() const { return m_CellDimension; } template void Connectivity ::SetFullyConnected( bool value ) { if( value ) { this->SetCellDimension( 0 ); } else { this->SetCellDimension( Dimension - 1 ); } } template bool Connectivity ::GetFullyConnected() const { if( m_CellDimension == 0 ) { return true; } // not really true, but return false otherwise return false; } template typename Connectivity::OffsetType Connectivity ::IntToOffset( int i ) { OffsetType o; for( int d=0; d int Connectivity ::OffsetToInt( const OffsetType & o ) { int i=0; int factor=1; for(unsigned int d=0; d int Connectivity ::factorial( int n ) { if( n<=1 ) { return 1; } return n * factorial( n-1 ); } template void Connectivity ::SetGlobalDefaultCellDimension( int dim ) { // check the validity of the requested cell dimension if( dim < 0 || dim > VDimension ) { itkGenericExceptionMacro( << dim << " is not a valid cell dimension for dimension " << VDimension << "." ); } m_GlobalDefaultCellDimension = dim; } template const int & Connectivity ::GetGlobalDefaultCellDimension() { return m_GlobalDefaultCellDimension; } template void Connectivity ::SetGlobalDefaultFullyConnected( bool value ) { if( value ) { m_GlobalDefaultCellDimension = 0; } else { m_GlobalDefaultCellDimension = Dimension - 1; } } template bool Connectivity ::GetGlobalDefaultFullyConnected() { if( m_GlobalDefaultCellDimension == 0 ) { return true; } // not really true, but return false otherwise return false; } template int Connectivity ::GetGlobalDefaultNumberOfNeighbors() { return ComputeNumberOfNeighbors( m_GlobalDefaultCellDimension ); } template void Connectivity ::SetGlobalDefaultNumberOfNeighbors( int nb ) { // and search a matching cell dimension. It should be quite // fast with usual dimensions. bool found = false; for( int i=0; i<=VDimension && !found; i++ ) { if( ComputeNumberOfNeighbors( i ) == nb ) { SetGlobalDefaultCellDimension( i ); found = true; } } if( !found ) { // send an exception itkGenericExceptionMacro( << nb << " is not a valid number of neighbors for dimension " << VDimension << "."); } } template void Connectivity ::PrintSelf(std::ostream& os, Indent indent) const { Superclass::PrintSelf( os, indent ); os << indent << "CellDimension: " << m_CellDimension << std::endl; os << indent << "NumberOfNeighbors: " << this->GetNumberOfNeighbors() << std::endl; os << indent << "FullyConnected: " << this->GetFullyConnected() << std::endl; os << indent << "Neighbors: [ "; for( int i=0; i