Version: 1.0
connectivity_mat.h
Go to the documentation of this file.
1 // Copyright (c) 2020, the GRAPHGEN contributors, as
2 // shown by the AUTHORS file. All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
6 
7 #ifndef GRAPHGEN_CONNECTIVITY_MAT_H_
8 #define GRAPHGEN_CONNECTIVITY_MAT_H_
9 
10 #include <algorithm>
11 #include <cassert>
12 #include <iostream>
13 #include <iterator>
14 #include <map>
15 #include <string>
16 #include <vector>
17 
18 /*
19 connectivity_mat stores a matrix which tells if two pixels/blocks are connected, as an intermediate
20 step to choose which actions should be performed during connected components labeling.
21 */
23  std::vector<std::vector<int>> data_; // connectivity matrix
24  std::map<std::string, size_t> pos_; // inverse lookup table (from pixel names to matrix indexes)
25  std::vector<std::string> names_; // list of pixel names (ordered as in the connectivity matrix)
26 
27  // A connectivity matrix is constructed from a list of pixel names
28  connectivity_mat(const std::vector<std::string> &names) : names_{ names }, data_{ names.size(), std::vector<int>(names.size(), 0) } {
29  auto N = data_.size();
30  for (size_t i = 0; i < N; ++i) {
31  data_[i][i] = 1; // by definition every pixel is connected to itself
32  pos_[names[i]] = i; // initialize the inverse look up table
33  }
34  }
35 
36  // tells if two pixels (specified by names) are connected
37  bool operator()(const std::string& row, const std::string& col) const {
38  size_t r = pos_.at(row);
39  size_t c = pos_.at(col);
40  return data_[r][c];
41  }
42 
43  // sets the connection of two pixels (specified by names)
44  void set(const std::string& row, const std::string& col, bool b) {
45  size_t r = pos_.at(row);
46  size_t c = pos_.at(col);
47  data_[r][c] = data_[c][r] = b;
48  }
49 
50  // gives back the name of a row/column
51  const std::string& GetHeader(size_t i) {
52  auto N = data_.size();
53  assert(i < N);
54  return names_[i];
55  }
56 
57  // prints an undelimited list of pixel names
58  void DisplayCondNames(std::ostream &os = std::cout) {
59  std::copy(std::begin(names_), std::end(names_), std::ostream_iterator<std::string>(os));
60  os << "\n";
61  }
62 
63  // prints a visual representation of the matrix
64  void DisplayMap(std::ostream & os = std::cout) {
65  auto N = data_.size();
66  for (size_t c = 0; c < N; ++c) {
67  os << "\t" << names_[c];
68  }
69  os << "\n";
70 
71  for (size_t r = 0; r < N; ++r) {
72  os << names_[r];
73  for (size_t c = 0; c < N; ++c) {
74  os << "\t" << data_[pos_.at(GetHeader(r))][pos_.at(GetHeader(c))];
75  }
76  os << "\n";
77  }
78  }
79 };
80 
81 #endif // !GRAPHGEN_CONNECTIVITY_MAT_H_
bool operator()(const std::string &row, const std::string &col) const
const std::string & GetHeader(size_t i)
connectivity_mat(const std::vector< std::string > &names)
void set(const std::string &row, const std::string &col, bool b)
void DisplayCondNames(std::ostream &os=std::cout)
std::vector< std::vector< int > > data_
void DisplayMap(std::ostream &os=std::cout)
std::vector< std::string > names_
std::map< std::string, size_t > pos_