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