Guest User

Untitled

a guest
Oct 20th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.09 KB | None | 0 0
  1. // Written by Nadav Benedek 2017
  2.  
  3.  
  4. #include "SparseConnectedComponents.h"
  5. #include <iostream>
  6. #include "opencv2/opencv.hpp"
  7.  
  8. SparseConnectedComponents::SparseConnectedComponents() {
  9. }
  10.  
  11. SparseConnectedComponents::~SparseConnectedComponents() {
  12. }
  13.  
  14. void SparseConnectedComponents::Reset() {
  15. hashGraphUnionFind.clear();
  16. }
  17.  
  18. node* SparseConnectedComponents::MakeSet(int x, int y) {
  19. node n;
  20.  
  21. n.rank = 0;
  22. n.totalX = x;
  23. n.totalY = y;
  24. n.numPixels = 1;
  25.  
  26. hashGraphUnionFind.emplace(x * MAX_COL + y, n);
  27.  
  28. node * nodePointer = &hashGraphUnionFind[x*MAX_COL + y];
  29.  
  30. nodePointer->parentNode = nodePointer;
  31.  
  32. return nodePointer; // Must return a pointer to the memory inside the hash, not to our local stack
  33. }
  34.  
  35. node* SparseConnectedComponents::Find(node* a) { // with path compression
  36. if (a->parentNode == a) return a;
  37. else {
  38. a->parentNode = Find(a->parentNode);
  39. return a->parentNode;
  40. }
  41. }
  42.  
  43.  
  44. node* SparseConnectedComponents::getNode(int x, int y) { // with path compression
  45.  
  46.  
  47. if (hashGraphUnionFind.find(x*MAX_COL+y) != hashGraphUnionFind.end()) { // if key is found
  48. return &hashGraphUnionFind[x*MAX_COL + y];
  49. } else {
  50. return 0;
  51. }
  52. }
  53.  
  54.  
  55.  
  56. void SparseConnectedComponents::Union(node* a0, node* a1) { // union by rank
  57. if (a0 == a1) return;
  58.  
  59. node *a2 = Find(a0);
  60. node *a3 = Find(a1);
  61.  
  62. if (a2 == a3) return;
  63.  
  64. if (a2->rank < a3->rank) {
  65. a2->parentNode = a3;
  66. a3->numPixels += a2->numPixels;
  67. a3->totalX += a2->totalX;
  68. a3->totalY += a2->totalY;
  69. } else if (a3->rank < a2->rank) {
  70. a3->parentNode = a2;
  71. a2->numPixels += a3->numPixels;
  72. a2->totalX += a3->totalX;
  73. a2->totalY += a3->totalY;
  74. } else {
  75. a2->parentNode = a3;
  76. a3->rank++;
  77. a3->numPixels += a2->numPixels;
  78. a3->totalX += a2->totalX;
  79. a3->totalY += a2->totalY;
  80. }
  81. }
  82.  
  83.  
  84. void SparseConnectedComponents::processListOf2DPoints(std::vector<cv::Point> & listOfPointsToProcess) {
  85. for (cv::Point & point : listOfPointsToProcess) {
  86. node * n = MakeSet(point.x, point.y);
  87.  
  88. if (hashGraphUnionFind.find((point.x - 1) *MAX_COL + point.y) != hashGraphUnionFind.end()) { // if key is found on left
  89. Union(&hashGraphUnionFind[(point.x - 1) *MAX_COL + point.y], n);
  90. }
  91.  
  92. if (hashGraphUnionFind.find((point.x - 1) *MAX_COL + (point.y-1)) != hashGraphUnionFind.end()) { // if key is found on up-left
  93. Union(&hashGraphUnionFind[(point.x - 1) * MAX_COL + (point.y-1)], n);
  94. }
  95.  
  96. if (hashGraphUnionFind.find((point.x) * MAX_COL + (point.y - 1)) != hashGraphUnionFind.end()) { // if key is found on top
  97. Union(&hashGraphUnionFind[(point.x) * MAX_COL + (point.y - 1)], n);
  98. }
  99.  
  100. if (hashGraphUnionFind.find((point.x + 1) * MAX_COL + (point.y - 1)) != hashGraphUnionFind.end()) { // if key is found on top-right
  101. Union(&hashGraphUnionFind[(point.x + 1) * MAX_COL + (point.y - 1)], n);
  102. }
  103. }
  104. }
  105.  
  106. void testSparseConnectedComponents() {
  107. SparseConnectedComponents scc;
  108. node * n1 = scc.MakeSet(100, 100);
  109.  
  110. std::cout << n1->totalX << " " << n1->isRoot() << std::endl;
  111.  
  112. node * n2 = scc.MakeSet(200, 200);
  113.  
  114. std::cout << "Test: " << scc.getNode(200,200)->totalX << std::endl;
  115.  
  116. std::cout << n2->totalX << " " << n2->isRoot() << std::endl;
  117. scc.Union(n1, n2);
  118. std::cout << "n1: " << n1->isRoot() << " totalx: " << n1->totalX << " centerX: " << n1->getCenterX() << std::endl;
  119. std::cout << "n2: " << n2->isRoot() << " totalx: " << n2->totalX << " centerX: " << n2->getCenterX() << std::endl;
  120.  
  121. node * n3 = scc.MakeSet(300, 300);
  122.  
  123. std::cout << n3->totalX << " " << n3->isRoot() << std::endl;
  124. scc.Union(n1, n3);
  125.  
  126. std::cout << "n1: " << n1->isRoot() << " totalx: " << n1->totalX << " centerX: " << n1->getCenterX() << std::endl;
  127. std::cout << "n2: " << n2->isRoot() << " totalx: " << n2->totalX << " centerX: " << n2->getCenterX() << std::endl;
  128. std::cout << "n3: " << n3->isRoot() << " totalx: " << n3->totalX << " centerX: " << n3->getCenterX() << std::endl;
  129.  
  130. std::cout << "iterate" << std::endl;
  131.  
  132. for (std::pair<const int, node> & pair : scc.hashGraphUnionFind) {
  133.  
  134. int x = pair.first / scc.MAX_COL;
  135. int y = pair.first % scc.MAX_COL;
  136.  
  137. std::cout << "key: " << x << "," << y << " value: " << pair.second.getCenterX() << "," << pair.second.getCenterY() << std::endl;
  138. }
  139. }
Add Comment
Please, Sign In to add comment