ConnectivityChecker.cpp
Go to the documentation of this file.
1 
19 
20 namespace SceneModel {
21 
22  ConnectivityChecker::ConnectivityChecker(unsigned int pNumObjects): mNumObjects(pNumObjects)
23  {}
24 
26  { }
27 
28  bool ConnectivityChecker::isConnected(std::vector<bool> pBitvector)
29  {
30  if (pBitvector.size() != (mNumObjects - 1) * mNumObjects / 2)
31  {
32  std::cout << "Bitvector does not represent the relations properly." << std::endl;
33  return false;
34  }
35 
36  // from lib_ism:
37  unsigned int numOnesInVector = 0;
38  for (bool bit : pBitvector) numOnesInVector += bit;
39  if (numOnesInVector < mNumObjects - 1) return false; //A topology needs at least #objects - 1 relations to be connected.
40  if (numOnesInVector == pBitvector.size()) return true; // Fully meshed topology.
41 
42  // Do BFS to see whether each node can be reached
43  // For starting relation: find first one (exists because of check above)
44  /*
45  unsigned int firstOneIndex = 0;
46  while (!pBitvector[firstOneIndex]) firstOneIndex++;
47  */
48 
49  unsigned int firstObject = 0;
50  while (firstObject < mNumObjects - 1)
51  {
52  bool found = false;
53  for (unsigned int j = firstObject + 1; j < mNumObjects; j++)
54  {
55  if (pBitvector[firstObject * mNumObjects - firstObject * (firstObject + 1) / 2 + j])
56  {
57  found = true;
58  break;
59  }
60  }
61  if (found) break;
62  else firstObject++;
63  }
64  //firstOneIndex = firstObject;
65 
66  std::vector<unsigned int> nodesToVisit;
67  nodesToVisit.push_back(firstObject);
68  std::vector<unsigned int> seenNodes;
69  seenNodes.push_back(firstObject);
70  while (!nodesToVisit.empty())
71  {
72  unsigned int currentNode = nodesToVisit[0];
73  nodesToVisit.erase(nodesToVisit.begin());
74  for (unsigned int i = 0; i < mNumObjects; i++)
75  if (currentNode != i)
76  {
77  unsigned int indexMin = std::min(currentNode, i);
78  unsigned int indexMax = std::max(currentNode, i);
79  unsigned int bitvectorindex = indexMin * mNumObjects - indexMin * (indexMin + 1) / 2 + (indexMax - indexMin) - 1;
80  // if a relation exists from currentNode to i && i was not already found:
81  if (pBitvector[bitvectorindex] && (std::find(seenNodes.begin(), seenNodes.end(), i) == seenNodes.end()))
82  {
83  nodesToVisit.push_back(i);
84  seenNodes.push_back(i);
85  }
86  }
87  }
88  return (seenNodes.size() == mNumObjects); // Whether all nodes were found
89  }
90 }
bool isConnected(std::vector< bool > pBitvector)
ConnectivityChecker(unsigned int pNumObjects)


asr_relation_graph_generator
Author(s): Meißner Pascal
autogenerated on Fri Nov 15 2019 03:39:19