17 #ifndef ITK_ConnectomicsNetworkToConnectivityMatrixImageFilter_CPP
18 #define ITK_ConnectomicsNetworkToConnectivityMatrixImageFilter_CPP
20 #include <itkImageRegionIterator.h>
25 : m_BinaryConnectivity(false)
26 , m_RescaleConnectivity(false)
27 , m_InputNetwork(NULL)
39 this->AllocateOutputs();
43 if(m_InputNetwork.IsNull())
45 MITK_ERROR <<
"Failed to generate data, no valid network was set.";
50 typedef boost::graph_traits< NetworkType >::vertex_descriptor DescriptorType;
51 typedef boost::graph_traits< NetworkType >::vertex_iterator IteratorType;
54 std::vector< std::vector< int > > connectivityMatrix;
55 int numberOfVertices = m_InputNetwork->GetNumberOfVertices();
57 connectivityMatrix.resize( numberOfVertices );
58 for(
int index(0); index < numberOfVertices; index++ )
60 connectivityMatrix[ index ].resize( numberOfVertices );
64 std::map< std::string, DescriptorType > labelToIndexMap;
66 NetworkType boostGraph = *(m_InputNetwork->GetBoostGraph());
69 IteratorType iterator, end;
70 boost::tie(iterator, end) = boost::vertices( boostGraph );
72 for ( ; iterator != end; ++iterator)
74 labelToIndexMap.insert(
75 std::pair< std::string, DescriptorType >(
76 boostGraph[ *iterator ].label, *iterator )
80 std::vector< std::string > indexToLabel;
83 indexToLabel.resize( numberOfVertices );
85 boost::tie(iterator, end) = boost::vertices( boostGraph );
87 for ( ; iterator != end; ++iterator)
89 indexToLabel[ *iterator ] = boostGraph[ *iterator ].label;
93 std::vector< std::string > positionToLabelVector;
94 positionToLabelVector = indexToLabel;
96 std::sort ( positionToLabelVector.begin(), positionToLabelVector.end() );
98 for(
int outerLoop( 0 ); outerLoop < numberOfVertices; outerLoop++ )
100 DescriptorType fromVertexDescriptor = labelToIndexMap.find( positionToLabelVector[ outerLoop ] )->second;
101 for(
int innerLoop( outerLoop + 1 ); innerLoop < numberOfVertices; innerLoop++ )
103 DescriptorType toVertexDescriptor = labelToIndexMap.find( positionToLabelVector[ innerLoop ] )->second;
108 if( boost::edge(toVertexDescriptor, fromVertexDescriptor, boostGraph ).second )
110 weight = m_InputNetwork->GetEdge( fromVertexDescriptor , toVertexDescriptor ).weight;;
112 connectivityMatrix[outerLoop][innerLoop] = weight;
113 connectivityMatrix[innerLoop][outerLoop] = weight;
118 OutputImageType::SpacingType spacing;
124 OutputImageType::IndexType index;
127 OutputImageType::SizeType size;
128 size[0] = numberOfVertices;
129 size[1] = numberOfVertices;
130 OutputImageType::RegionType region;
131 region.SetIndex( index );
132 region.SetSize( size );
134 output->SetSpacing( spacing );
135 output->SetOrigin( origin );
136 output->SetRegions( region );
138 output->FillBuffer(0);
141 itk::ImageRegionIterator< OutputImageType > it_connect(output, output->GetLargestPossibleRegion());
145 double rescaleFactor( 1.0 );
146 double rescaleMax( 255.0 );
148 if( m_RescaleConnectivity )
150 rescaleFactor = rescaleMax / m_InputNetwork->GetMaximumWeight();
154 if( m_BinaryConnectivity )
157 while( !it_connect.IsAtEnd() )
159 if( connectivityMatrix[ ( counter - counter % numberOfVertices ) / numberOfVertices][ counter % numberOfVertices ] )
174 while( !it_connect.IsAtEnd() )
176 it_connect.Set( (
unsigned short ) rescaleFactor *
177 connectivityMatrix[ ( counter - counter % numberOfVertices ) / numberOfVertices][ counter % numberOfVertices ]
itk::SmartPointer< Self > Pointer
ConnectomicsNetworkToConnectivityMatrixImageFilter()
virtual ~ConnectomicsNetworkToConnectivityMatrixImageFilter()
void GenerateData() override
boost::adjacency_list< boost::vecS, boost::vecS, boost::undirectedS, NetworkNode, NetworkEdge > NetworkType