-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomputeLowestNeighbor.cpp
More file actions
165 lines (155 loc) · 5.99 KB
/
computeLowestNeighbor.cpp
File metadata and controls
165 lines (155 loc) · 5.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include <iostream>
#include <vector>
#include "computeLowestNeighbor.h"
using namespace std;
int hashMinNeighbors(vector<int> &resList) {
int hashing;
if (resList.size() == 1) {
if (resList[0] == 0) hashing = 0;
else if (resList[0] == 1) hashing = 4;
else if (resList[0] == 2) hashing = 5;
else if (resList[0] == 3) hashing = 6;
else if (resList[0] == 4) hashing = 7;
} else if (resList.size() == 2) {
if (resList[0] == 1 && resList[1] == 2) hashing = 8;
else if (resList[0] == 2 && resList[1] == 3) hashing = 9;
else if (resList[0] == 3 && resList[1] == 4) hashing = 10;
else if (resList[0] == 1 && resList[1] == 4) hashing = 11;
else if (resList[0] == 1 && resList[1] == 3) hashing = 2;
else if (resList[0] == 2 && resList[1] == 4) hashing = 3;
} else if (resList.size() == 3) {
if (resList[0] == 2 && resList[1] == 3 && resList[2] == 4) hashing = 12;
else if (resList[0] == 1 && resList[1] == 3 && resList[2] == 4) hashing = 13;
else if (resList[0] == 1 && resList[1] == 2 && resList[2] == 4) hashing = 14;
else if (resList[0] == 1 && resList[1] == 2 && resList[2] == 3) hashing = 15;
} else if (resList.size() == 4) {
hashing = 16;
}
return hashing;
}
void computeMinNeighbors(int **eleMat, int r, int c, vector<int> &neighbors, vector<int> &minNeighbors) {
int min = eleMat[r][c]; // Initialize the min elevation at center point
int center = eleMat[r][c];
int tie = 1;
int i = 0;
int item;
for (vector<int>::iterator it = neighbors.begin() ; it != neighbors.end(); ++it) {
switch (*it) {
case 1: // compare with upper pixel, compare(min, center, cntTie, eleMat[r-1][c]);
if (min > eleMat[r-1][c]) {
min = eleMat[r-1][c];
tie = 1;
minNeighbors.clear();
minNeighbors.push_back(1);
} else if (min == eleMat[r-1][c] && min != center) {
// TIE: If min == center, no need to change, no tricle
tie++;
minNeighbors.push_back (1);
}
break;
case 2: // compare with right, compare(min, center, tie, eleMat[r][c+1]);
if (min > eleMat[r][c+1]) {
min = eleMat[r][c+1];
tie = 1;
minNeighbors.clear();
minNeighbors.push_back(2);
} else if (min == eleMat[r][c+1] && min != center) {
tie++;
minNeighbors.push_back(2);
}
break;
case 3: // compare with lower, compare(min, center, tie, eleMat[r+1][c]);
if (min > eleMat[r+1][c]) {
min = eleMat[r+1][c];
tie = 1;
minNeighbors.clear();
minNeighbors.push_back(3);
} else if (min == eleMat[r+1][c] && min != center) {
tie++;
minNeighbors.push_back(3);
}
break;
case 4: // compare with left, compare(min, center, tie, eleMat[r][c-1]);
if (min > eleMat[r][c-1]) {
min = eleMat[r][c-1];
tie = 1;
minNeighbors.clear();
minNeighbors.push_back(4);
} else if (min == eleMat[r][c-1] && min != center) {
tie++;
minNeighbors.push_back(4);
}
break;
default: break;
}
}
// check minneighbors
if (minNeighbors.size() == 0) {
minNeighbors.push_back(0);
}
return;
}
void computeNeighbors(int r, int c, int N, vector<int> &neighbors) {
if (r == 0 && c == 0) {
neighbors.push_back(2);
neighbors.push_back(3);
} else if (r == 0 && c == N-1) {
neighbors.push_back(3);
neighbors.push_back(4);
} else if (r == N-1 && c == 0) {
neighbors.push_back(1);
neighbors.push_back(2);
} else if (r == N-1 && c== N-1) {
neighbors.push_back(1);
neighbors.push_back(4);
} else if (r == 0) {
neighbors.push_back(2);
neighbors.push_back(3);
neighbors.push_back(4);
} else if (r == N-1) {
neighbors.push_back(1);
neighbors.push_back(2);
neighbors.push_back(4);
} else if (c == 0) {
neighbors.push_back(1);
neighbors.push_back(2);
neighbors.push_back(3);
} else if (c == N-1) {
neighbors.push_back(1);
neighbors.push_back(3);
neighbors.push_back(4);
} else {
neighbors.push_back(1);
neighbors.push_back(2);
neighbors.push_back(3);
neighbors.push_back(4);
}
// cout vector neighbors
return;
}
void computeLowestNeighbor(int **eleMat, int **lowestMat, int N) {
// int** lowestMat pass in success!
int hashingValue = 0;
vector<int> neighbors;
vector<int> minNeighbors;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
computeNeighbors(i, j, N, neighbors);
computeMinNeighbors(eleMat, i, j, neighbors, minNeighbors);
hashingValue = hashMinNeighbors(minNeighbors);
lowestMat[i][j] = hashingValue;
/* cout << "check computeNeighbors results at " << i << ", " << j << ": " << endl;
for (vector<int>::iterator it = neighbors.begin() ; it != neighbors.end(); ++it) {
cout << *it << " ";
}
cout << "\n" << "check computeMinNeighbors results: " << endl;
for (vector<int>::iterator it = minNeighbors.begin() ; it != minNeighbors.end(); ++it) {
cout << *it << " ";
}
cout << "\n" << "hashing value: " << hashingValue << endl; */
neighbors.clear();
minNeighbors.clear();
}
}
return;
}