forked from dkolom/GALA2D
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathQuadNode.cpp
More file actions
326 lines (286 loc) · 10.6 KB
/
QuadNode.cpp
File metadata and controls
326 lines (286 loc) · 10.6 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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*************************************************************************************************************************
*
* Class QuadNode
*
* Node of a tree
*
* Adapted from QuadtreeSimulator v1.21 by Frank Dockhorn
*
*************************************************************************************************************************/
#include <iostream>
#include <cassert>
#include <cmath>
#include <ctime>
#include "QuadNode.h"
#include "QuadTree.h"
#include "Solver.h"
#include "Point.h"
using namespace std;
/*-------------------------------------------------------------------------*\
\*-------------------------------------------------------------------------*/
QuadNode::QuadNode(QuadTree* ntree, QuadNode* nparent, unsigned int njx, unsigned int njy, unsigned char nl)
{
m_tree = ntree;
m_parent = nparent;
m_child = NULL;
m_jx = njx;
m_jy = njy;
m_l = nl;
// Initialize grid points
assert(m_tree->m_solver->m_lmax-nl-1 >= 0);
// Finest-level grid step size
double ph = m_tree->m_solver->m_size / double(m_tree->m_solver->m_jmax);
// Integer coordinates of the three points that correspond to the given cell (njx,njy,nl)
unsigned int powl = std::pow(2,int(m_tree->m_solver->m_lmax-nl-1));
unsigned int pxM0 = powl * (2*njx + 1);
unsigned int pyM0 = powl * (2*njy);
unsigned int px0M = powl * (2*njx);
unsigned int py0M = powl * (2*njy + 1);
unsigned int pxMM = powl * (2*njx + 1);
unsigned int pyMM = powl * (2*njy + 1);
// Create points
m_cpoint[0] = new Point(pxM0,pyM0,ph,NULL);
m_cpoint[1] = new Point(px0M,py0M,ph,NULL);
m_cpoint[2] = new Point(pxMM,pyMM,ph,NULL);
// Do not remove this point during update
m_hold = 1;
// printNode();
}
/*-------------------------------------------------------------------------*\
\*-------------------------------------------------------------------------*/
void QuadNode::allocateChildren()
{
m_child = new QuadNode*[4];
for (int jj=0; jj<4; jj++) {
m_child[jj] = NULL;
}
}
/*-------------------------------------------------------------------------*\
\*-------------------------------------------------------------------------*/
void QuadNode::printNode()
{
// Prind the position and the level
cout << m_jx << " " << m_jy << " " << int(m_l) << endl;
}
/*-------------------------------------------------------------------------*\
\*-------------------------------------------------------------------------*/
// Destructor recursively removes all branches
QuadNode::~QuadNode()
{
int i;
// Recursively remove children
for (i = 0; i < 4; i++) {
if (m_child != NULL) {
delete m_child[i];
m_child[i] = NULL;
}
}
delete[] m_child;
m_child = NULL;
// Delete cell points
for (i = 0; i < 3; i++) {
delete m_cpoint[i];
m_cpoint[i] = NULL;
}
// this = NULL;
}
/*-------------------------------------------------------------------------*\
\*-------------------------------------------------------------------------*/
QuadNode* QuadNode::findNode(unsigned int njx, unsigned int njy, unsigned char nl)
{
// Initial guess
QuadNode* fnode = this;
// Check if the current node is sought
if ( (m_jx!=njx) || (m_jy!=njy) || (m_l!=nl) )
{
// Search the branch
location cloc;
unsigned int cjx;
unsigned int cjy;
unsigned char cl;
// Search begins from 'this' node
unsigned int njmax = (unsigned int)(floor(pow(double(2),double(nl-m_l))+0.5));
for (cl = m_l+1; cl<=nl; cl++)
{
njmax /= 2;
cjx = njx / njmax;
cjy = njy / njmax;
// Check jx and jy bounds
unsigned int cjmax = (unsigned int)(floor(pow(double(2),double(cl))+0.5));
assert ( (cjx<cjmax) && (cjy<cjmax) );
// Find child position
if (cjx % 2 == 0) {
if (cjy % 2 == 0) {
cloc = QuadNode::SW;
} else {
cloc = QuadNode::NW;
}
} else {
if (cjy % 2 == 0) {
cloc = QuadNode::SE;
} else {
cloc = QuadNode::NE;
}
}
// Go to one step up the tree, if possible.
// Otherwise return NULL
if (fnode->m_child != NULL) {
fnode = fnode->m_child[cloc];
} else {
return NULL;
}
// Stop if cannot find
assert(fnode != NULL);
}
}
// Node found
return fnode;
}
/*-------------------------------------------------------------------------*\
\*-------------------------------------------------------------------------*/
// Returns a pointer to the point
// that is situated in a corner of this node,
// and its location is given by clocx, clocy
// clocx=0, clocy=0 is lower-left corner
// clocx=0, clocy=1 is upper-left corner etc.
Point* QuadNode::getPoint(unsigned int clocx, unsigned int clocy)
{
unsigned int px, py;
unsigned int powl = std::pow(2,int(m_tree->m_solver->m_lmax-m_l));
// Point coordinates (integer number relative to the finest-level grid)
px = ((m_jx+clocx) * powl) % m_tree->m_solver->m_jmax;
py = ((m_jy+clocy) * powl) % m_tree->m_solver->m_jmax;
// Find point if it's not the root point
// if ( (px > 0) || (py > 0) ) return m_tree->getRoot()->findPoint(px,py,2*m_tree->m_solver->m_jmax);
if ( (px > 0) || (py > 0) ) return m_tree->getRoot()->findPoint(px,py,m_tree->m_solver->m_jmax);
// If px == 0 and py == 0, return root point
return m_tree->m_rootpoint;
}
/*-------------------------------------------------------------------------*\
\*-------------------------------------------------------------------------*/
QuadNode* QuadNode::northNeighbor()
{
QuadNode* cnode;
// Check the parent cell's children
if (m_parent == NULL) return this; // 'this' if periodic, 'NULL' if not periodic
if (this == m_parent->m_child[QuadNode::SW]) return m_parent->m_child[QuadNode::NW];
if (this == m_parent->m_child[QuadNode::SE]) return m_parent->m_child[QuadNode::NE];
// Recursive search
cnode = m_parent->northNeighbor();
// Note that cnode cannot be NULL
if (cnode->isLeaf()) {
return cnode;
} else {
if (this == m_parent->m_child[QuadNode::NW]) {
return cnode->m_child[QuadNode::SW];
} else {
return cnode->m_child[QuadNode::SE];
}
}
}
/*-------------------------------------------------------------------------*\
\*-------------------------------------------------------------------------*/
QuadNode* QuadNode::southNeighbor()
{
QuadNode* cnode;
// Check the parent cell's children
if (m_parent == NULL) return this;
if (this == m_parent->m_child[QuadNode::NW]) return m_parent->m_child[QuadNode::SW];
if (this == m_parent->m_child[QuadNode::NE]) return m_parent->m_child[QuadNode::SE];
// Recursive search
cnode = m_parent->southNeighbor();
if (cnode->isLeaf()) {
return cnode;
} else {
if (this == m_parent->m_child[QuadNode::SW]) {
return cnode->m_child[QuadNode::NW];
} else {
return cnode->m_child[QuadNode::NE];
}
}
}
/*-------------------------------------------------------------------------*\
\*-------------------------------------------------------------------------*/
QuadNode* QuadNode::westNeighbor()
{
QuadNode* cnode;
// Check the parent cell's children
if (m_parent == NULL) return this;
if (this == m_parent->m_child[QuadNode::SE]) return m_parent->m_child[QuadNode::SW];
if (this == m_parent->m_child[QuadNode::NE]) return m_parent->m_child[QuadNode::NW];
// Recursive search
cnode = m_parent->westNeighbor();
if (cnode->isLeaf()) {
return cnode;
} else {
if (this == m_parent->m_child[QuadNode::SW]) {
return cnode->m_child[QuadNode::SE];
} else {
return cnode->m_child[QuadNode::NE];
}
}
}
/*-------------------------------------------------------------------------*\
\*-------------------------------------------------------------------------*/
QuadNode* QuadNode::eastNeighbor()
{
QuadNode* cnode;
// Check the parent cell's children
if (m_parent == NULL) return this;
if (this == m_parent->m_child[QuadNode::SW]) return m_parent->m_child[QuadNode::SE];
if (this == m_parent->m_child[QuadNode::NW]) return m_parent->m_child[QuadNode::NE];
// Recursive search
cnode = m_parent->eastNeighbor();
// Note that cnode cannot be NULL
if (cnode->isLeaf()) {
return cnode;
} else {
if (this == m_parent->m_child[QuadNode::SE]) {
return cnode->m_child[QuadNode::SW];
} else {
return cnode->m_child[QuadNode::NW];
}
}
}
/*-------------------------------------------------------------------------*\
\*-------------------------------------------------------------------------*/
bool QuadNode::needToBeSplit()
{
QuadNode* cnode;
// Check north
cnode = northNeighbor();
if (!cnode->isLeaf())
if ( (!cnode->m_child[SW]->isLeaf()) || (!cnode->m_child[SE]->isLeaf()) ) return true;
// Check west
cnode = westNeighbor();
if (!cnode->isLeaf())
if ( (!cnode->m_child[SE]->isLeaf()) || (!cnode->m_child[NE]->isLeaf()) ) return true;
// Check south
cnode = southNeighbor();
if (!cnode->isLeaf())
if ( (!cnode->m_child[NW]->isLeaf()) || (!cnode->m_child[NE]->isLeaf()) ) return true;
// Check east
cnode = eastNeighbor();
if (!cnode->isLeaf())
if ( (!cnode->m_child[NW]->isLeaf()) || (!cnode->m_child[SW]->isLeaf()) ) return true;
return false;
}
/*-------------------------------------------------------------------------*\
\*-------------------------------------------------------------------------*/
// Grid point search. Returns pointer to a node and indices plocx, ploy.
// Point 0: plocx = 1, plocy = 0; Point 1: plocx = 0, plocy = 1; Point 2: plocx = 1, plocy = 1.
Point* QuadNode::findPoint(unsigned int px, unsigned int py, unsigned int pow2)
{
// The grid step that corresponds to the current cell is determined by its level,
// and pow2 is the corresponding power of 2. However, in a recursive search algorithm ,
// one should be very careful where to divide it by 2. Here this is done before computing the position indices
assert(pow2 >= 2);
unsigned int pow = pow2 / 2;
unsigned int jpos = 2*( py >= m_jy * pow2 + pow ) + ( px >= m_jx * pow2 + pow );
// Check if this cells contains the point
for (unsigned int jj=0; jj<3; ++jj) if ( (m_cpoint[jj]->m_px == px) && (m_cpoint[jj]->m_py == py) ) return m_cpoint[jj];
// Recursively search for cells that might contain that point
assert(m_child != NULL);
assert(m_child[jpos] != NULL);
return m_child[jpos]->findPoint(px,py,pow);
}