Chaste  Build::
VesselNetworkGraphCalculator.cpp
1 /*
2 
3 Copyright (c) 2005-2016, University of Oxford.
4 All rights reserved.
5 
6 University of Oxford means the Chancellor, Masters and Scholars of the
7 University of Oxford, having an administrative office at Wellington
8 Square, Oxford OX1 2JD, UK.
9 
10 This file is part of Chaste.
11 
12 Redistribution and use in source and binary forms, with or without
13 modification, are permitted provided that the following conditions are met:
14  * Redistributions of source code must retain the above copyright notice,
15  this list of conditions and the following disclaimer.
16  * Redistributions in binary form must reproduce the above copyright notice,
17  this list of conditions and the following disclaimer in the documentation
18  and/or other materials provided with the distribution.
19  * Neither the name of the University of Oxford nor the names of its
20  contributors may be used to endorse or promote products derived from this
21  software without specific prior written permission.
22 
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 
34  */
35 
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"
44 
48 template<typename TimeMap> class bfs_time_visitor : public boost::default_bfs_visitor
49 {
53  typedef typename boost::property_traits<TimeMap>::value_type T;
54 
55 public:
56 
60  TimeMap m_timemap;
61 
65  T& m_time;
66 
72  bfs_time_visitor(TimeMap tmap, T& t)
73  :m_timemap(tmap),
74  m_time(t)
75  {
76  }
77 
83  template<typename Vertex, typename Graph>
84  void discover_vertex(Vertex u, const Graph& g) const
85  {
86  put(m_timemap, u, m_time++);
87  }
88 };
89 
90 template <unsigned DIM>
92  mpVesselNetwork()
93 {
94 
95 }
96 
97 template <unsigned DIM>
99 {
100 
101 }
102 
103 template <unsigned DIM>
104 boost::shared_ptr<VesselNetworkGraphCalculator<DIM> > VesselNetworkGraphCalculator<DIM>::Create()
105 {
106  MAKE_PTR(VesselNetworkGraphCalculator<DIM>, pSelf);
107  return pSelf;
108 }
109 
110 template <unsigned DIM>
112 {
113  if(!mpVesselNetwork)
114  {
115  EXCEPTION("Vessel network not set in graph calculator");
116  }
117 
118  std::vector<boost::shared_ptr<VesselNode<DIM> > > nodes = mpVesselNetwork->GetVesselEndNodes();
119  std::vector<boost::shared_ptr<Vessel<DIM> > > vessels = mpVesselNetwork->GetVessels();
120  std::vector<std::vector<unsigned> > node_vessel_connectivity = GetNodeVesselConnectivity();
121 
122  std::vector<std::vector<unsigned> > connectivity;
123  for (unsigned node_index = 0; node_index < nodes.size(); node_index++)
124  {
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++)
129  {
130  boost::shared_ptr<Vessel<DIM> > p_vessel = vessels[node_vessel_connectivity[node_index][vessel_index]];
131 
132  // Get the node at the other end of the vessel
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);
137  }
138  connectivity.push_back(node_indexes);
139  }
140  return connectivity;
141 }
142 
143 template <unsigned DIM>
145 {
146  if(!mpVesselNetwork)
147  {
148  EXCEPTION("Vessel network not set in graph calculator");
149  }
150 
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++)
156  {
157  boost::shared_ptr<VesselNode<DIM> > p_node = nodes[node_index];
158  std::vector<unsigned> vessel_indexes;
159 
160  unsigned num_segments_on_node = p_node->GetNumberOfSegments();
161  for (unsigned segment_index = 0; segment_index < num_segments_on_node; segment_index++)
162  {
163  boost::shared_ptr<Vessel<DIM> > p_vessel = p_node->GetSegments()[segment_index]->GetVessel();
164 
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);
169  }
170  connectivity.push_back(vessel_indexes);
171  }
172  return connectivity;
173 }
174 
175 template <unsigned DIM>
176 bool VesselNetworkGraphCalculator<DIM>::IsConnected(boost::shared_ptr<VesselNode<DIM> > pSourceNode, boost::shared_ptr<VesselNode<DIM> > pQueryNode)
177 {
178  if(!mpVesselNetwork)
179  {
180  EXCEPTION("Vessel network not set in graph calculator");
181  }
182 
183  if (!mpVesselNetwork->NodeIsInNetwork(pSourceNode))
184  {
185  EXCEPTION("Source node is not in network.");
186  }
187  if (!mpVesselNetwork->NodeIsInNetwork(pQueryNode))
188  {
189  EXCEPTION("Query node is not in network.");
190  }
191 
192  if (pSourceNode == pQueryNode)
193  {
194  return true;
195  }
196 
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();
199 
200  if (p_source_vessel == p_query_vessel || p_source_vessel->IsConnectedTo(p_query_vessel))
201  {
202  return true;
203  }
204 
205  // Assign the vessel nodes unique IDs
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++)
210  {
211  (*node_iter)->SetId(counter);
212  counter ++;
213  }
214 
215  // Get the start nodes of the containing and query vessels
216  boost::shared_ptr<VesselNode<DIM> > pEquivalentSourceNode = p_source_vessel->GetStartNode();
217  boost::shared_ptr<VesselNode<DIM> > pEquivalentQueryNode = p_query_vessel->GetStartNode();
218 
219  // construct graph representation of vessel network
220  typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS> Graph;
221 
222  Graph G;
223 
224  std::vector<boost::shared_ptr<Vessel<DIM> > > vessels = mpVesselNetwork->GetVessels();
225  for (unsigned i = 0; i < vessels.size(); i++)
226  {
227  add_edge(vessels[i]->GetStartNode()->GetId(), vessels[i]->GetEndNode()->GetId(), G);
228  }
229 
230  // typedefs
231  typedef boost::graph_traits<Graph>::vertices_size_type Size;
232 
233  // a vector to hold the discover time property for each vertex
234  std::vector<Size> dtime(num_vertices(G));
235 
236  Size time = 0;
237  bfs_time_visitor<Size*>vis(&dtime[0], time);
238 
239  // use breadth first search to establish discovery time of all nodes from node1
240  // this assigns a discovery time to dTime for each node (index relates to nodeID)
241  // dTime is zero for node1 and all other nodes that are not connected to node1
242  // dTime is nonzero for all nodes that are connected to node1 (except node 1 itself)
243  breadth_first_search(G,vertex(pEquivalentSourceNode->GetId(),G), boost::visitor(vis));
244 
245  return (dtime[pEquivalentQueryNode->GetId()] > 0);
246 }
247 
248 template <unsigned DIM>
249 std::vector<bool > VesselNetworkGraphCalculator<DIM>::IsConnected(std::vector<boost::shared_ptr<VesselNode<DIM> > > sourceNodes,
250  std::vector<boost::shared_ptr<VesselNode<DIM> > > queryNodes)
251 {
252  if(!mpVesselNetwork)
253  {
254  EXCEPTION("Vessel network not set in graph calculator");
255  }
256 
257  // Assign the vessel nodes unique IDs
258  std::vector<boost::shared_ptr<VesselNode<DIM> > > vessel_nodes = mpVesselNetwork->GetVesselEndNodes();
259 
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++)
263  {
264  (*node_iter)->SetId(counter);
265  counter ++;
266  }
267 
268  // construct graph representation of vessel network
269  typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS> Graph;
270  typedef boost::graph_traits<Graph>::vertices_size_type Size;
271 
272  Graph G;
273 
274  std::vector<boost::shared_ptr<Vessel<DIM> > > vessels = mpVesselNetwork->GetVessels();
275  for (unsigned i = 0; i < vessels.size(); i++)
276  {
277  add_edge(vessels[i]->GetStartNode()->GetId(), vessels[i]->GetEndNode()->GetId(), G);
278  }
279 
280  std::vector<bool > connected(queryNodes.size(),false);
281 
282  for(unsigned i=0; i<sourceNodes.size(); i++)
283  {
284  if (!mpVesselNetwork->NodeIsInNetwork(sourceNodes[i]))
285  {
286  EXCEPTION("Source node is not in network.");
287  }
288 
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();
292 
293  // a vector to hold the discover time property for each vertex
294  std::vector<Size> dtime(num_vertices(G));
295 
296  Size time = 0;
297  bfs_time_visitor<Size*>vis(&dtime[0], time);
298 
299  breadth_first_search(G,vertex(pEquivalentSourceNode->GetId(),G), boost::visitor(vis));
300 
301  for (unsigned j=0; j<queryNodes.size(); j++)
302  {
303  if (!mpVesselNetwork->NodeIsInNetwork(queryNodes[j]))
304  {
305  EXCEPTION("Query node is not in network.");
306  }
307 
308  boost::shared_ptr<VesselNode<DIM> > pQueryNode = queryNodes[j];
309 
310  if (pSourceNode == pQueryNode)
311  {
312  connected[j] = true;
313  continue;
314  }
315 
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))
318  {
319  connected[j] = true;
320  continue;
321  }
322 
323  boost::shared_ptr<VesselNode<DIM> > pEquivalentQueryNode = p_query_vessel->GetStartNode();
324 
325  if (dtime[pEquivalentQueryNode->GetId()] > 0)
326  {
327  connected[j] = true;
328  }
329  }
330  }
331 
332  return connected;
333 }
334 
335 template <unsigned DIM>
336 void VesselNetworkGraphCalculator<DIM>::WriteConnectivity(const std::string& output_filename)
337 {
338  if(!mpVesselNetwork)
339  {
340  EXCEPTION("Vessel network not set in graph calculator");
341  }
342 
343  // construct graph representation of vessel network
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();
347 
348  for (node_iterator = nodes.begin(); node_iterator != nodes.end(); node_iterator++)
349  {
350  if ((*node_iterator)->GetSegments().size() > 1)
351  {
352  for (unsigned j = 1; j < (*node_iterator)->GetSegments().size(); j++)
353  {
354  add_edge(mpVesselNetwork->GetVesselIndex((*node_iterator)->GetSegments()[0]->GetVessel()),
355  mpVesselNetwork->GetVesselIndex((*node_iterator)->GetSegments()[j]->GetVessel()), G);
356  }
357  }
358  }
359 
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++)
363  {
364  if ((*vessel_iterator)->GetStartNode()->GetNumberOfSegments() == 1 && (*vessel_iterator)->GetEndNode()->GetNumberOfSegments() == 1)
365  {
366  add_edge(mpVesselNetwork->GetVesselIndex((*vessel_iterator)),
367  mpVesselNetwork->GetVesselIndex((*vessel_iterator)), G);
368  }
369  }
370 
371  if(PetscTools::AmMaster())
372  {
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);
377  }
378 }
379 
380 template <unsigned DIM>
382 {
383  mpVesselNetwork = pVesselNetwork;
384 }
385 
386 // Explicit instantiation
387 template class VesselNetworkGraphCalculator<2>;
388 template class VesselNetworkGraphCalculator<3>;
389 
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)
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)