-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQTree.cpp
More file actions
303 lines (236 loc) · 7.25 KB
/
Copy pathQTree.cpp
File metadata and controls
303 lines (236 loc) · 7.25 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/**
*
* Balanced Quad Tree (pa3)
*
* This file will be used for grading.
*
*/
#include "QTree.h"
// Return the biggest power of 2 less than or equal to n
int biggestPow2(int n) {
if( n < 1 ) return 0;
int v = 1;
while( v <= n ) v <<= 1;
return v >> 1;
}
QTree::Node::Node(PNG & im, pair<int,int> ul, int sz, Node *par)
:upLeft(ul),size(sz),parent(par),nw(NULL),ne(NULL),sw(NULL),se(NULL)
{
var = varAndAvg(im,ul,size,avg);
}
QTree::~QTree(){
clear();
}
QTree::QTree(const QTree & other) {
copy(other);
}
QTree & QTree::operator=(const QTree & rhs){
if (this != &rhs) {
clear();
copy(rhs);
}
return *this;
}
QTree::QTree(PNG & imIn, int leafB, RGBAPixel frameC, bool bal)
: leafBound(leafB), balanced(bal), drawFrame(true), frameColor(frameC)
{
/* YOUR CODE HERE */
im = imIn;
size = biggestPow2(min((int)imIn.height(), (int)imIn.width()));
root = new Node(im, pair<int, int>(0, 0), size, NULL);
numLeaf = 1;
Q.push(root);
while (numLeaf < leafBound) {
Node* temp = Q.top();
Q.pop();
split(temp);
}
}
QTree::QTree(PNG & imIn, int leafB, bool bal)
: leafBound(leafB), balanced(bal), drawFrame(false)
{
/* YOUR CODE HERE */
frameColor = NULL;
im = imIn;
size = biggestPow2(min((int)imIn.height(), (int)imIn.width()));
root = new Node(im, pair<int, int>(0, 0), size, NULL);
numLeaf = 1;
Q.push(root);
while (numLeaf < leafBound) {
Node* temp = Q.top();
Q.pop();
split(temp);
}
}
bool QTree::isLeaf( Node *t ) {
/* YOUR CODE HERE */
if (t == NULL) return false;
return t->ne == NULL && t->nw == NULL && t->se == NULL && t->sw == NULL;
}
void QTree::split( Node *t ) {
/* YOUR CODE HERE */
// FOR BALANCED QTREES-------------------------------------------------
// A split might cause one or two nbrs of the parent of t to split
// to maintain balance. Note that these two nbrs exist (unless they're
// not in the image region) because the current set of leaves are
// balanced.
// if( t is a NW (or NE or SW or SE) child ) then we need to check that
// the North and West (or North and East or South and West or
// South and East) nbrs of t->parent have children. If they don't
// we need to split them.
if (!isLeaf(t)) return;
if (t == NULL) return;
if (balanced == true && t->parent != NULL) {
if (t == t->parent->nw) {
if (isLeaf(NNbr(t->parent))) split(NNbr(t->parent));
if (isLeaf(WNbr(t->parent))) split(WNbr(t->parent));
} else if (t == t->parent->ne) {
if (isLeaf(NNbr(t->parent))) split(NNbr(t->parent));
if (isLeaf(ENbr(t->parent))) split(ENbr(t->parent));
} else if (t == t->parent->sw) {
if (isLeaf(SNbr(t->parent))) split(SNbr(t->parent));
if (isLeaf(WNbr(t->parent))) split(WNbr(t->parent));
} else if (t == t->parent->se) {
if (isLeaf(SNbr(t->parent))) split(SNbr(t->parent));
if (isLeaf(ENbr(t->parent))) split(ENbr(t->parent));
}
}
int splitSize = t->size / 2;
t->nw = new Node(im, t->upLeft, splitSize, t);
t->ne = new Node(im, pair<int, int> ((t->upLeft).first + splitSize, (t->upLeft).second),splitSize, t);
t->sw = new Node(im, pair<int, int>((t->upLeft).first, (t->upLeft.second) + splitSize), splitSize, t);
t->se = new Node(im, pair<int, int>((t->upLeft).first + splitSize, (t->upLeft).second + splitSize), splitSize, t);
if (t->nw->size > 0) { Q.push(t->nw); }
if (t->ne->size > 0) { Q.push(t->ne); numLeaf++; }
if (t->sw->size > 0) { Q.push(t->sw); numLeaf++; }
if (t->se->size > 0) { Q.push(t->se); numLeaf++; }
}
/* NNbr(t)
* return the same-sized quad tree node that is north of Node t.
* return NULL if this node is not in the QTree.
*/
QTree::Node * QTree::NNbr(Node *t) {
/* YOUR CODE HERE */
if (t == NULL) return NULL;
if (t->parent == NULL) return NULL;
if (t == t->parent->sw) return t->parent->nw;
if (t == t->parent->se) return t->parent->ne;
Node* parentN = NNbr(t->parent);
if (parentN == NULL) return NULL;
if (isLeaf(parentN) && (parentN->size = 2 * t->size)) return parentN;
if (t == t->parent->nw) return parentN->sw;
if (t == t->parent->ne) return parentN->se;
return NULL;
}
/* SNbr(t)
* return the same-sized quad tree node that is south of Node t.
* return NULL if this node is not in the QTree.
*/
QTree::Node * QTree::SNbr(Node *t) {
/* YOUR CODE HERE */
if (t == NULL) return NULL;
if (t->parent == NULL) return NULL;
if (t == t->parent->nw) return t->parent->sw;
if (t == t->parent->ne) return t->parent->se;
Node* parentN = SNbr(t->parent);
if (parentN == NULL) return NULL;
if (isLeaf(parentN) && (parentN->size = 2 * t->size)) return parentN;
if (t == t->parent->sw) return parentN->nw;
if (t == t->parent->se) return parentN->ne;
return NULL;
}
/* ENbr(t)
* return the same-sized quad tree node that is east of Node t.
* return NULL if this node is not in the QTree.
*/
QTree::Node * QTree::ENbr(Node *t) {
/* YOUR CODE HERE */
if (t == NULL) return NULL;
if (t->parent == NULL) return NULL;
if (t == t->parent->nw) return t->parent->ne;
if (t == t->parent->sw) return t->parent->se;
Node* parentN = ENbr(t->parent);
if (parentN == NULL) return NULL;
if (isLeaf(parentN) && (parentN->size = 2 * t->size)) return parentN;
if (t == t->parent->ne) return parentN->nw;
if (t == t->parent->se) return parentN->sw;
return NULL;
}
/* WNbr(t)
* return the same-sized quad tree node that is west of Node t.
* return NULL if this node is not in the QTree.
*/
QTree::Node * QTree::WNbr(Node *t) {
/* YOUR CODE HERE */
if (t == NULL) return NULL;
if (t->parent == NULL) return NULL;
if (t == t->parent->ne) return t->parent->nw;
if (t == t->parent->se) return t->parent->sw;
Node* parentN = WNbr(t->parent);
if (parentN == NULL) return NULL;
if (isLeaf(parentN) && (parentN->size = 2 * t->size)) return parentN;
if (t == t->parent->nw) return parentN->ne;
if (t == t->parent->sw) return parentN->se;
return NULL;
}
bool QTree::write(string const & fileName){
/* YOUR CODE HERE */
PNG img(size, size);
writeHelper(root, img);
// include the following line to write the image to file.
return(img.writeToFile(fileName));
}
void QTree::writeHelper(Node * n, PNG & img) {
if (isLeaf(n)) {
int x = (n->upLeft).first;
int y = (n->upLeft).second;
for (int i = 0; i < n->size; i++) {
for (int j = 0; j < n->size; j++) {
RGBAPixel* p = img.getPixel(x + i, y + j);
if ((drawFrame == true) &&
((i == 0) || (i + 1 == n->size) || (j == 0) || (j + 1 == n->size))) {
*p = frameColor;
}
else {
*p = n->avg;
}
}
}
}
else {
writeHelper(n->nw, img);
writeHelper(n->ne, img);
writeHelper(n->sw, img);
writeHelper(n->se, img);
}
}
void QTree::clear() {
/* YOUR CODE HERE */
clearHelper(root);
}
void QTree::clearHelper(Node* n) {
if (n == NULL) return;
clearHelper(n->nw);
clearHelper(n->ne);
clearHelper(n->sw);
clearHelper(n->se);
delete n;
}
void QTree::copy(const QTree & orig) {
/* YOUR CODE HERE */
leafBound = orig.leafBound;
im = orig.im;
balanced = orig.balanced;
drawFrame = orig.drawFrame;
frameColor = orig.frameColor;
Q = priority_queue < Node*, vector<Node*>, compare>();
size = biggestPow2(min((int)orig.im.height(), (int)orig.im.width()));
root = new Node(im, pair<int, int>(0, 0), size, NULL);
numLeaf = 1;
Q.push(root);
while (numLeaf < leafBound) {
Node* temp = Q.top();
Q.pop();
split(temp);
}
}