ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
Graph.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 // Local
11 #include "Common.h"
12 
13 // Boost
14 #include <boost/graph/adjacency_list.hpp>
15 #include <boost/graph/graph_traits.hpp>
16 #include <boost/graph/properties.hpp>
17 #include <boost/property_map/property_map.hpp>
18 
19 namespace CP {
20 typedef boost::graph_traits<
21  boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS>>::
22  edge_descriptor EdgeDescriptor;
23 
24 template <typename T>
26  typedef T calc_type;
27 
28  VertexAttribute(uint32_t dim = 1, T weight = 1.)
29  : weight(weight),
30  observation(dim, 0.),
31  value(dim, 0.),
32  color(-1),
33  isBorder(false),
34  in_component(0) {}
35 
36  T weight; // weight of the observation
37  std::vector<T> observation; // observed value
38  std::vector<T> value; // current value
39  uint32_t color; // field use for the graph cut
40  bool isBorder; // is the node part of an activated edge
41  uint32_t in_component; // index of the component in which the node belong
42 };
43 
44 template <typename T>
45 struct EdgeAttribute {
46  typedef T calc_type;
47 
48  EdgeAttribute(T weight = 1., uint32_t eIndex = 0, bool real = true)
49  : index(eIndex),
50  weight(weight),
53  isActive(!real),
54  realEdge(real) {}
55 
56  uint32_t index; // index of the edge (necessary for graph cuts)
57  EdgeDescriptor edge_reverse; // pointer to the reverse edge, also necessary
58  // for graph cuts
59  T weight; // weight of the edge
60  T capacity; // capacity in the flow graph
61  T residualCapacity; // necessary for graph cuts
62  bool isActive; // is the edge in the support of the values
63  bool realEdge; // is the edge between real nodes or link to source/sink
64 };
65 
66 template <typename T>
67 using Graph = typename boost::adjacency_list<boost::vecS,
68  boost::vecS,
69  boost::directedS,
72 
73 template <typename T>
75  typename boost::graph_traits<CP::Graph<T>>::vertex_descriptor;
76 template <typename T>
77 using VertexIndex =
78  typename boost::graph_traits<CP::Graph<T>>::vertices_size_type;
79 template <typename T>
80 using EdgeIndex = typename boost::graph_traits<CP::Graph<T>>::edges_size_type;
81 template <typename T>
82 using VertexIterator = typename boost::graph_traits<Graph<T>>::vertex_iterator;
83 template <typename T>
84 using EdgeIterator = typename boost::graph_traits<Graph<T>>::edge_iterator;
85 
86 template <typename T>
88  boost::vec_adj_list_vertex_property_map<Graph<T>,
89  Graph<T>*,
92  boost::vertex_bundle_t>;
93 template <typename T>
95  boost::adj_list_edge_property_map<boost::directed_tag,
98  uint64_t,
100  boost::edge_bundle_t>;
101 template <typename T>
103  typename boost::property_map<Graph<T>, boost::vertex_index_t>::type;
104 template <typename T>
106  typename boost::property_map<Graph<T>,
107  uint32_t EdgeAttribute<T>::*>::type;
108 
109 template <typename T>
111  const VertexDescriptor<T>& source,
112  const VertexDescriptor<T>& target,
113  const T weight,
114  uint32_t eIndex,
115  EdgeAttributeMap<T>& edge_attribute_map,
116  bool real = true) {
117  // Add edges between two vertices. We have to create the edge and the
118  // reverse edge, then add the edge_reverse as the corresponding reverse edge
119  // to 'edge', and then add 'edge' as the corresponding reverse edge to
120  // 'edge_reverse'
121 
122  EdgeDescriptor edge, edge_reverse;
123  std::pair<EdgeDescriptor, bool> edge_added =
124  boost::add_edge(source, target, g);
125  if (edge_added.second) {
126  edge = edge_added.first;
127  edge_reverse = boost::add_edge(target, source, g).first;
128  EdgeAttribute<T> attrib_edge(weight, eIndex, real);
129  EdgeAttribute<T> attrib_edge_reverse(weight, eIndex + 1, real);
130  attrib_edge.edge_reverse = edge_reverse;
131  attrib_edge_reverse.edge_reverse = edge;
132  edge_attribute_map(edge) = attrib_edge;
133  edge_attribute_map(edge_reverse) = attrib_edge_reverse;
134  return true;
135  } else {
136  return false;
137  }
138 }
139 } // namespace CP
char type
Definition: API.h:123
typename boost::graph_traits< CP::Graph< T > >::vertices_size_type VertexIndex
Definition: Graph.h:78
bool addDoubledge(Graph< T > &g, const VertexDescriptor< T > &source, const VertexDescriptor< T > &target, const T weight, uint32_t eIndex, EdgeAttributeMap< T > &edge_attribute_map, bool real=true)
Definition: Graph.h:110
boost::vec_adj_list_vertex_property_map< Graph< T >, Graph< T > *, VertexAttribute< T >, VertexAttribute< T > &, boost::vertex_bundle_t > VertexAttributeMap
Definition: Graph.h:92
typename boost::graph_traits< Graph< T > >::edge_iterator EdgeIterator
Definition: Graph.h:84
typename boost::graph_traits< Graph< T > >::vertex_iterator VertexIterator
Definition: Graph.h:82
boost::graph_traits< boost::adjacency_list< boost::vecS, boost::vecS, boost::directedS > >::edge_descriptor EdgeDescriptor
Definition: Graph.h:22
typename boost::property_map< Graph< T >, uint32_t EdgeAttribute< T >::* >::type EdgeIndexMap
Definition: Graph.h:107
typename boost::graph_traits< CP::Graph< T > >::vertex_descriptor VertexDescriptor
Definition: Graph.h:75
typename boost::adjacency_list< boost::vecS, boost::vecS, boost::directedS, VertexAttribute< T >, EdgeAttribute< T > > Graph
Definition: Graph.h:71
typename boost::property_map< Graph< T >, boost::vertex_index_t >::type VertexIndexMap
Definition: Graph.h:103
boost::adj_list_edge_property_map< boost::directed_tag, EdgeAttribute< T >, EdgeAttribute< T > &, uint64_t, CP::EdgeAttribute< T >, boost::edge_bundle_t > EdgeAttributeMap
Definition: Graph.h:100
bool realEdge
Definition: Graph.h:63
bool isActive
Definition: Graph.h:62
EdgeAttribute(T weight=1., uint32_t eIndex=0, bool real=true)
Definition: Graph.h:48
EdgeDescriptor edge_reverse
Definition: Graph.h:57
uint32_t index
Definition: Graph.h:56
T residualCapacity
Definition: Graph.h:61
std::vector< T > observation
Definition: Graph.h:37
VertexAttribute(uint32_t dim=1, T weight=1.)
Definition: Graph.h:28
uint32_t color
Definition: Graph.h:39
std::vector< T > value
Definition: Graph.h:38
uint32_t in_component
Definition: Graph.h:41