36 #include <boost/graph/graphviz.hpp> 37 #include <boost/graph/visitors.hpp> 38 #include <boost/graph/adjacency_list.hpp> 39 #include <boost/graph/breadth_first_search.hpp> 40 #include <boost/pending/indirect_cmp.hpp> 41 #include "Exception.hpp" 42 #include "VesselNetworkGraphCalculator.hpp" 43 #include "PetscTools.hpp" 53 typedef typename boost::property_traits<TimeMap>::value_type
T;
83 template<
typename Vertex,
typename Graph>
86 put(m_timemap, u, m_time++);
90 template <
unsigned DIM>
97 template <
unsigned DIM>
103 template <
unsigned DIM>
110 template <
unsigned DIM>
115 EXCEPTION(
"Vessel network not set in graph calculator");
118 std::vector<boost::shared_ptr<VesselNode<DIM> > > nodes =
mpVesselNetwork->GetVesselEndNodes();
119 std::vector<boost::shared_ptr<Vessel<DIM> > > vessels =
mpVesselNetwork->GetVessels();
122 std::vector<std::vector<unsigned> > connectivity;
123 for (
unsigned node_index = 0; node_index < nodes.size(); node_index++)
125 std::vector<unsigned> node_indexes;
126 boost::shared_ptr<VesselNode<DIM> > p_node = nodes[node_index];
127 unsigned num_branches = node_vessel_connectivity[node_index].size();
128 for (
unsigned vessel_index = 0; vessel_index < num_branches; vessel_index++)
130 boost::shared_ptr<Vessel<DIM> > p_vessel = vessels[node_vessel_connectivity[node_index][vessel_index]];
133 boost::shared_ptr<VesselNode<DIM> > p_other_node = p_vessel->GetNodeAtOppositeEnd(p_node);
134 typename std::vector<boost::shared_ptr<VesselNode<DIM> > >::iterator node_iter = std::find(nodes.begin(), nodes.end(), p_other_node);
135 unsigned other_node_index = std::distance(nodes.begin(), node_iter);
136 node_indexes.push_back(other_node_index);
138 connectivity.push_back(node_indexes);
143 template <
unsigned DIM>
148 EXCEPTION(
"Vessel network not set in graph calculator");
151 std::vector<boost::shared_ptr<VesselNode<DIM> > > nodes =
mpVesselNetwork->GetVesselEndNodes();
152 std::vector<boost::shared_ptr<Vessel<DIM> > > vessels =
mpVesselNetwork->GetVessels();
153 unsigned num_nodes = nodes.size();
154 std::vector<std::vector<unsigned> > connectivity;
155 for (
unsigned node_index = 0; node_index < num_nodes; node_index++)
157 boost::shared_ptr<VesselNode<DIM> > p_node = nodes[node_index];
158 std::vector<unsigned> vessel_indexes;
160 unsigned num_segments_on_node = p_node->GetNumberOfSegments();
161 for (
unsigned segment_index = 0; segment_index < num_segments_on_node; segment_index++)
163 boost::shared_ptr<Vessel<DIM> > p_vessel = p_node->GetSegments()[segment_index]->GetVessel();
165 typename std::vector<boost::shared_ptr<Vessel<DIM> > >::iterator vessel_iter =
166 std::find(vessels.begin(), vessels.end(), p_vessel);
167 unsigned vessel_index = std::distance(vessels.begin(), vessel_iter);
168 vessel_indexes.push_back(vessel_index);
170 connectivity.push_back(vessel_indexes);
175 template <
unsigned DIM>
180 EXCEPTION(
"Vessel network not set in graph calculator");
185 EXCEPTION(
"Source node is not in network.");
189 EXCEPTION(
"Query node is not in network.");
192 if (pSourceNode == pQueryNode)
197 boost::shared_ptr<Vessel<DIM> > p_source_vessel = pSourceNode->GetSegments()[0]->GetVessel();
198 boost::shared_ptr<Vessel<DIM> > p_query_vessel = pQueryNode->GetSegments()[0]->GetVessel();
200 if (p_source_vessel == p_query_vessel || p_source_vessel->IsConnectedTo(p_query_vessel))
206 std::vector<boost::shared_ptr<VesselNode<DIM> > > vessel_nodes =
mpVesselNetwork->GetVesselEndNodes();
207 typename std::vector<boost::shared_ptr<VesselNode<DIM> > >::iterator node_iter;
208 unsigned counter = 0;
209 for(node_iter = vessel_nodes.begin(); node_iter != vessel_nodes.end(); node_iter++)
211 (*node_iter)->SetId(counter);
216 boost::shared_ptr<VesselNode<DIM> > pEquivalentSourceNode = p_source_vessel->GetStartNode();
217 boost::shared_ptr<VesselNode<DIM> > pEquivalentQueryNode = p_query_vessel->GetStartNode();
220 typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS> Graph;
224 std::vector<boost::shared_ptr<Vessel<DIM> > > vessels =
mpVesselNetwork->GetVessels();
225 for (
unsigned i = 0; i < vessels.size(); i++)
227 add_edge(vessels[i]->GetStartNode()->GetId(), vessels[i]->GetEndNode()->GetId(), G);
231 typedef boost::graph_traits<Graph>::vertices_size_type Size;
234 std::vector<Size> dtime(num_vertices(G));
243 breadth_first_search(G,vertex(pEquivalentSourceNode->GetId(),G), boost::visitor(vis));
245 return (dtime[pEquivalentQueryNode->GetId()] > 0);
248 template <
unsigned DIM>
254 EXCEPTION(
"Vessel network not set in graph calculator");
258 std::vector<boost::shared_ptr<VesselNode<DIM> > > vessel_nodes =
mpVesselNetwork->GetVesselEndNodes();
260 typename std::vector<boost::shared_ptr<VesselNode<DIM> > >::iterator node_iter;
261 unsigned counter = 0;
262 for(node_iter = vessel_nodes.begin(); node_iter != vessel_nodes.end(); node_iter++)
264 (*node_iter)->SetId(counter);
269 typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS> Graph;
270 typedef boost::graph_traits<Graph>::vertices_size_type Size;
274 std::vector<boost::shared_ptr<Vessel<DIM> > > vessels =
mpVesselNetwork->GetVessels();
275 for (
unsigned i = 0; i < vessels.size(); i++)
277 add_edge(vessels[i]->GetStartNode()->GetId(), vessels[i]->GetEndNode()->GetId(), G);
280 std::vector<bool > connected(queryNodes.size(),
false);
282 for(
unsigned i=0; i<sourceNodes.size(); i++)
286 EXCEPTION(
"Source node is not in network.");
289 boost::shared_ptr<VesselNode<DIM> > pSourceNode = sourceNodes[i];
290 boost::shared_ptr<Vessel<DIM> > p_source_vessel = pSourceNode->GetSegments()[0]->GetVessel();
291 boost::shared_ptr<VesselNode<DIM> > pEquivalentSourceNode = p_source_vessel->GetStartNode();
294 std::vector<Size> dtime(num_vertices(G));
299 breadth_first_search(G,vertex(pEquivalentSourceNode->GetId(),G), boost::visitor(vis));
301 for (
unsigned j=0; j<queryNodes.size(); j++)
305 EXCEPTION(
"Query node is not in network.");
308 boost::shared_ptr<VesselNode<DIM> > pQueryNode = queryNodes[j];
310 if (pSourceNode == pQueryNode)
316 boost::shared_ptr<Vessel<DIM> > p_query_vessel = pQueryNode->GetSegments()[0]->GetVessel();
317 if (p_source_vessel == p_query_vessel || p_source_vessel->IsConnectedTo(p_query_vessel))
323 boost::shared_ptr<VesselNode<DIM> > pEquivalentQueryNode = p_query_vessel->GetStartNode();
325 if (dtime[pEquivalentQueryNode->GetId()] > 0)
335 template <
unsigned DIM>
340 EXCEPTION(
"Vessel network not set in graph calculator");
344 boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS> G;
345 typename std::vector<boost::shared_ptr<VesselNode<DIM> > >::iterator node_iterator;
346 std::vector<boost::shared_ptr<VesselNode<DIM> > > nodes =
mpVesselNetwork->GetVesselEndNodes();
348 for (node_iterator = nodes.begin(); node_iterator != nodes.end(); node_iterator++)
350 if ((*node_iterator)->GetSegments().size() > 1)
352 for (
unsigned j = 1; j < (*node_iterator)->GetSegments().size(); j++)
354 add_edge(
mpVesselNetwork->GetVesselIndex((*node_iterator)->GetSegments()[0]->GetVessel()),
355 mpVesselNetwork->GetVesselIndex((*node_iterator)->GetSegments()[j]->GetVessel()), G);
360 std::vector<boost::shared_ptr<Vessel<DIM> > > vessels =
mpVesselNetwork->GetVessels();
361 typename std::vector<boost::shared_ptr<Vessel<DIM> > >::iterator vessel_iterator;
362 for (vessel_iterator = vessels.begin(); vessel_iterator != vessels.end(); vessel_iterator++)
364 if ((*vessel_iterator)->GetStartNode()->GetNumberOfSegments() == 1 && (*vessel_iterator)->GetEndNode()->GetNumberOfSegments() == 1)
371 if(PetscTools::AmMaster())
373 std::ofstream outf(output_filename.c_str());
374 boost::dynamic_properties dp;
375 dp.property(
"node_id",
get(boost::vertex_index, G));
376 write_graphviz_dp(outf, G, dp);
380 template <
unsigned DIM>
~VesselNetworkGraphCalculator()
std::vector< std::vector< unsigned > > GetNodeVesselConnectivity()
void discover_vertex(Vertex u, const Graph &g) const
void WriteConnectivity(const std::string &rFilename)
boost::shared_ptr< VesselNetwork< DIM > > mpVesselNetwork
bool IsConnected(boost::shared_ptr< VesselNode< DIM > > pSourceNode, boost::shared_ptr< VesselNode< DIM > > pQueryNode)
VesselNetworkGraphCalculator()
static boost::shared_ptr< VesselNetworkGraphCalculator< DIM > > Create()
boost::property_traits< TimeMap >::value_type T
std::vector< std::vector< unsigned > > GetNodeNodeConnectivity()
bfs_time_visitor(TimeMap tmap, T &t)
void SetVesselNetwork(boost::shared_ptr< VesselNetwork< DIM > > pVesselNetwork)