Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkUndirectedGraph.txx
Go to the documentation of this file.
1 /*===================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 #ifndef _mitkUndirectedGraph_txx
18 #define _mitkUndirectedGraph_txx
19 
20 namespace mitk
21 {
22  template <class TVertex, class TEdge>
23  UndirectedGraph<TVertex, TEdge>::UndirectedGraph() : m_Graph(NULL)
24  {
25  }
26 
27  template <class TVertex, class TEdge>
28  UndirectedGraph<TVertex, TEdge>::UndirectedGraph(const UndirectedGraph<TVertex, TEdge> &graph)
29  : m_Graph(graph.m_Graph)
30  {
31  }
32 
33  template <class TVertex, class TEdge>
34  UndirectedGraph<TVertex, TEdge>::~UndirectedGraph()
35  {
36  }
37 
38  template <class TVertex, class TEdge>
39  typename UndirectedGraph<TVertex, TEdge>::VertexDescriptorType UndirectedGraph<TVertex, TEdge>::AddVertex(
40  const typename UndirectedGraph<TVertex, TEdge>::VertexType &vertexData)
41  {
42  VertexDescriptorType vertex = boost::add_vertex(m_Graph);
43  this->properties(vertex) = vertexData;
44 
45  this->Modified();
46 
47  return vertex;
48  }
49 
50  template <class TVertex, class TEdge>
51  void UndirectedGraph<TVertex, TEdge>::RemoveVertex(
52  const typename UndirectedGraph<TVertex, TEdge>::VertexDescriptorType &vertex)
53  {
54  boost::clear_vertex(vertex, m_Graph);
55  boost::remove_vertex(vertex, m_Graph);
56  this->Modified();
57  }
58 
59  template <class TVertex, class TEdge>
60  typename UndirectedGraph<TVertex, TEdge>::VertexType UndirectedGraph<TVertex, TEdge>::GetVertex(
61  const typename UndirectedGraph<TVertex, TEdge>::VertexDescriptorType &vertex)
62  {
63  return this->properties(vertex);
64  }
65 
66  template <class TVertex, class TEdge>
67  void UndirectedGraph<TVertex, TEdge>::SetVertex(
68  const typename UndirectedGraph<TVertex, TEdge>::VertexDescriptorType &vertex,
69  const typename UndirectedGraph<TVertex, TEdge>::VertexType &vertexData)
70  {
71  this->properties(vertex) = vertexData;
72 
73  this->Modified();
74  }
75 
76  template <class TVertex, class TEdge>
77  typename UndirectedGraph<TVertex, TEdge>::VertexDescriptorType UndirectedGraph<TVertex, TEdge>::GetVertexDescriptor(
78  const typename UndirectedGraph<TVertex, TEdge>::VertexType &vertexData) const
79  {
80  typename UndirectedGraph<TVertex, TEdge>::VertexIteratorType iterator, end;
81 
82  boost::tie(iterator, end) = boost::vertices(m_Graph);
83 
84  for (; iterator != end; ++iterator)
85  {
86  typename UndirectedGraph<TVertex, TEdge>::VertexType tempVertex;
87 
88  // the value of an iterator is a descriptor
89  tempVertex = this->properties(*iterator);
90 
91  if (vertexData == tempVertex)
92  return *iterator;
93  }
94  MITK_ERROR << "Error in mitkUndirectedGraph::GetVertexDescriptor(...): vertex could not be found." << std::endl;
95  throw std::runtime_error(
96  "Exception thrown in mitkUndirectedGraph::GetVertexDescriptor(...): vertex could not be found.");
97  }
98 
99  template <class TVertex, class TEdge>
100  typename UndirectedGraph<TVertex, TEdge>::EdgeDescriptorType UndirectedGraph<TVertex, TEdge>::AddEdge(
101  const typename UndirectedGraph<TVertex, TEdge>::VertexDescriptorType &vertexA,
102  const typename UndirectedGraph<TVertex, TEdge>::VertexDescriptorType &vertexB,
103  const typename UndirectedGraph<TVertex, TEdge>::EdgeType &edgeData)
104  {
105  EdgeDescriptorType edge;
106  bool inserted = false;
107 
108  boost::tie(edge, inserted) = boost::add_edge(vertexA, vertexB, m_Graph);
109 
110  if (!inserted)
111  {
112  MITK_ERROR << "Error in mitkUndirectedGraph::addEdge(...): edge could not be inserted." << std::endl;
113  throw std::runtime_error("Exception thrown in mitkUndirectedGraph::addEdge(...): edge could not be inserted.");
114  }
115  this->properties(edge) = edgeData;
116 
117  this->Modified();
118 
119  return edge;
120  }
121 
122  template <class TVertex, class TEdge>
123  void UndirectedGraph<TVertex, TEdge>::RemoveEdge(
124  const typename UndirectedGraph<TVertex, TEdge>::EdgeDescriptorType &edge)
125  {
126  boost::remove_edge(edge, m_Graph);
127  this->Modified();
128  }
129 
130  template <class TVertex, class TEdge>
131  typename UndirectedGraph<TVertex, TEdge>::EdgeType UndirectedGraph<TVertex, TEdge>::GetEdge(
132  const typename UndirectedGraph<TVertex, TEdge>::EdgeDescriptorType &edge)
133  {
134  return this->properties(edge);
135  }
136 
137  template <class TVertex, class TEdge>
138  void UndirectedGraph<TVertex, TEdge>::SetEdge(
139  const typename UndirectedGraph<TVertex, TEdge>::EdgeDescriptorType &edge,
140  const typename UndirectedGraph<TVertex, TEdge>::EdgeType &edgeData)
141  {
142  this->properties(edge) = edgeData;
143 
144  this->Modified();
145  }
146 
147  template <class TVertex, class TEdge>
148  typename UndirectedGraph<TVertex, TEdge>::EdgeDescriptorType UndirectedGraph<TVertex, TEdge>::GetEdgeDescriptor(
149  const typename UndirectedGraph<TVertex, TEdge>::EdgeType &edgeData) const
150  {
151  typename UndirectedGraph<TVertex, TEdge>::EdgeIteratorType iterator, end;
152  boost::tie(iterator, end) = boost::edges(m_Graph);
153 
154  for (; iterator != end; ++iterator)
155  {
156  typename UndirectedGraph<TVertex, TEdge>::EdgeType tempEdge;
157 
158  // the value of an iterator is a descriptor
159  tempEdge = this->properties(*iterator);
160 
161  if (edgeData == tempEdge)
162  return iterator.dereference();
163  }
164  // if no edge match, throw error
165  MITK_ERROR << "Error in mitkUndirectedGraph::GetEdgeDescriptor(...): edge could not be found." << std::endl;
166  throw std::runtime_error(
167  "Exception thrown in mitkUndirectedGraph::GetEdgeDescriptor(...): edge could not be found.");
168  }
169 
170  template <class TVertex, class TEdge>
171  std::pair<typename UndirectedGraph<TVertex, TEdge>::VertexType, typename UndirectedGraph<TVertex, TEdge>::VertexType>
172  UndirectedGraph<TVertex, TEdge>::GetVerticesOfAnEdge(
173  const typename UndirectedGraph<TVertex, TEdge>::EdgeDescriptorType &edge) const
174  {
175  typename UndirectedGraph<TVertex, TEdge>::VertexType sourceNode, targetNode;
176 
177  sourceNode = this->properties(boost::source(edge, m_Graph));
178  targetNode = this->properties(boost::target(edge, m_Graph));
179  std::pair<typename UndirectedGraph<TVertex, TEdge>::VertexType,
180  typename UndirectedGraph<TVertex, TEdge>::VertexType>
181  nodePair(sourceNode, targetNode);
182 
183  return nodePair;
184  }
185 
186  template <class TVertex, class TEdge>
187  typename UndirectedGraph<TVertex, TEdge>::EdgeDescriptorType
188  UndirectedGraph<TVertex, TEdge>::GetEdgeDescriptorByVerices(
189  const typename UndirectedGraph<TVertex, TEdge>::VertexDescriptorType &vertexA,
190  const typename UndirectedGraph<TVertex, TEdge>::VertexDescriptorType &vertexB) const
191  {
192  typename UndirectedGraph<TVertex, TEdge>::EdgeDescriptorType edge;
193  bool edgeExists(false);
194  boost::tie(edge, edgeExists) = boost::edge(vertexA, vertexB, m_Graph);
195  if (edgeExists)
196  return edge;
197  else
198  {
199  MITK_ERROR << "Error in mitkUndirectedGraph::GetEdgeDescriptorByVerices(...): edge could not be found."
200  << std::endl;
201  throw std::runtime_error(
202  "Exception thrown in mitkUndirectedGraph::GetEdgeDescriptorByVerices(...): edge could not be found.");
203  }
204  }
205 
206  template <class TVertex, class TEdge>
207  std::vector<typename UndirectedGraph<TVertex, TEdge>::EdgeType> UndirectedGraph<TVertex, TEdge>::GetAllEdgesOfAVertex(
208  const typename UndirectedGraph<TVertex, TEdge>::VertexDescriptorType &vertex) const
209  {
210  typename UndirectedGraph<TVertex, TEdge>::OutEdgeIteratorType iterator, end;
211  boost::tie(iterator, end) = boost::out_edges(vertex, m_Graph);
212 
213  std::vector<typename UndirectedGraph<TVertex, TEdge>::EdgeType> vectorOfEdges;
214  for (; iterator != end; ++iterator)
215  {
216  typename UndirectedGraph<TVertex, TEdge>::EdgeType tempEdge;
217 
218  // the value of an iterator is a descriptor
219  tempEdge = this->properties(*iterator);
220 
221  vectorOfEdges.push_back(tempEdge);
222  }
223 
224  return vectorOfEdges;
225  }
226 
227  template <class TVertex, class TEdge>
228  int UndirectedGraph<TVertex, TEdge>::GetNumberOfVertices() const
229  {
230  // Returns the number of vertices in the graph
231  return boost::num_vertices(m_Graph);
232  }
233 
234  template <class TVertex, class TEdge>
235  int UndirectedGraph<TVertex, TEdge>::GetNumberOfEdges() const
236  {
237  // Returns the number of edges in the graph
238  return boost::num_edges(m_Graph);
239  }
240 
241  template <class TVertex, class TEdge>
242  std::vector<typename UndirectedGraph<TVertex, TEdge>::VertexType>
243  UndirectedGraph<TVertex, TEdge>::GetVectorOfAllVertices() const
244  {
245  typename UndirectedGraph<TVertex, TEdge>::VertexIteratorType iterator, end;
246 
247  // sets iterator to start end end to end
248  boost::tie(iterator, end) = boost::vertices(m_Graph);
249 
250  std::vector<typename UndirectedGraph<TVertex, TEdge>::VertexType> vectorOfNodes;
251 
252  for (; iterator != end; ++iterator)
253  {
254  typename UndirectedGraph<TVertex, TEdge>::VertexType tempVertex;
255 
256  // the value of an iterator is a descriptor
257  tempVertex = this->properties(*iterator);
258 
259  vectorOfNodes.push_back(tempVertex);
260  }
261 
262  return vectorOfNodes;
263  }
264 
265  template <class TVertex, class TEdge>
266  std::vector<typename UndirectedGraph<TVertex, TEdge>::EdgeType> UndirectedGraph<TVertex, TEdge>::GetVectorOfAllEdges()
267  const
268  {
269  typename UndirectedGraph<TVertex, TEdge>::EdgeIteratorType iterator, end;
270 
271  // sets iterator to start end end to end
272  boost::tie(iterator, end) = boost::edges(m_Graph);
273 
274  std::vector<typename UndirectedGraph<TVertex, TEdge>::EdgeType> vectorOfEdges;
275 
276  for (; iterator != end; ++iterator)
277  {
278  typename UndirectedGraph<TVertex, TEdge>::EdgeType tempEdge;
279 
280  // the value of an iterator is a descriptor
281  tempEdge = this->properties(*iterator);
282 
283  vectorOfEdges.push_back(tempEdge);
284  }
285 
286  return vectorOfEdges;
287  }
288 
289  template <class TVertex, class TEdge>
290  void UndirectedGraph<TVertex, TEdge>::Clear()
291  {
292  m_Graph.clear();
293  this->Modified();
294  }
295 
296  template <class TVertex, class TEdge>
297  const typename UndirectedGraph<TVertex, TEdge>::GraphType &UndirectedGraph<TVertex, TEdge>::GetGraph() const
298  {
299  return m_Graph;
300  }
301 
302  /* operators */
303  template <class TVertex, class TEdge>
304  UndirectedGraph<TVertex, TEdge> &UndirectedGraph<TVertex, TEdge>::operator=(
305  const UndirectedGraph<TVertex, TEdge> &rhs)
306  {
307  m_Graph = rhs.m_Graph;
308  return *this;
309  }
310 
311  template <class TVertex, class TEdge>
312  TVertex &UndirectedGraph<TVertex, TEdge>::properties(
313  const typename UndirectedGraph<TVertex, TEdge>::VertexDescriptorType &vertex)
314  {
315  typename boost::property_map<GraphType, vertex_properties_t>::type param = boost::get(vertex_properties, m_Graph);
316  return param[vertex];
317  }
318 
319  template <class TVertex, class TEdge>
320  const TVertex &UndirectedGraph<TVertex, TEdge>::properties(
321  const typename UndirectedGraph<TVertex, TEdge>::VertexDescriptorType &vertex) const
322  {
323  typename boost::property_map<GraphType, vertex_properties_t>::const_type param =
324  boost::get(vertex_properties, m_Graph);
325  return param[vertex];
326  }
327 
328  template <class TVertex, class TEdge>
329  TEdge &UndirectedGraph<TVertex, TEdge>::properties(
330  const typename UndirectedGraph<TVertex, TEdge>::EdgeDescriptorType &edge)
331  {
332  typename boost::property_map<GraphType, edge_properties_t>::type param = boost::get(edge_properties, m_Graph);
333  return param[edge];
334  }
335 
336  template <class TVertex, class TEdge>
337  const TEdge &UndirectedGraph<TVertex, TEdge>::properties(
338  const typename UndirectedGraph<TVertex, TEdge>::EdgeDescriptorType &edge) const
339  {
340  typename boost::property_map<GraphType, edge_properties_t>::const_type param = boost::get(edge_properties, m_Graph);
341  return param[edge];
342  }
343 
344 } // namespace mitk
345 
346 #endif //_mitkUndirectedGraph_txx