-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree2d_ada.cpp
317 lines (288 loc) · 11.6 KB
/
tree2d_ada.cpp
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
/*
* File: tree2d_ada.cpp
* Author: euklid
*
* Created on August 28, 2015, 12:57 AM
*/
#include "tree2d_ada.h"
#if DEBUG
#include <iostream>
#endif
Tree2D_Ada::Tree2D_Ada()
: Tree2D()
{}
Tree2D_Ada::~Tree2D_Ada()
{
for (int i = 0; i<m_cells.size(); i++)
{
delete m_cells[i];
}
m_cells.clear();
}
void Tree2D_Ada::init_first_level_lists()
{
// initialize lists for all cells
unsigned int num_cells = m_cells.size();
for (unsigned int i = 0; i < num_cells; i++)
{
// list 1: direct neighbors,
// list 2: interaction list,
// list 3: non-direct cells of colleagues descendents if cell is leaf
// list 4: dual to list 3
// list 5: colleagues on same level
m_cells[i]->init_empty_lists(5);
}
if (m_min_level == 0)
{
m_root->add_to_list(m_root, 4);
return;
}
std::vector<unsigned int> & lvl_cells = m_lvl_ids[m_min_level];
std::vector<unsigned int> & lvl_father_cells = m_lvl_ids[m_min_level - 1];
std::vector<unsigned int >::const_iterator it = lvl_cells.begin();
std::vector<unsigned int >::const_iterator other_it;
for (; it != lvl_cells.end(); ++it)
{
Cell* cur_cell = m_cells[*it];
assert(cur_cell->get_id() == *it);
Cell* cur_cell_father = cur_cell->get_father();
Point cur_cell_grid_pos = lazy_get_set_cell_grid_pos(cur_cell, m_min_level, m_root);
Point cur_cell_father_grid_pos = lazy_get_set_cell_grid_pos(cur_cell_father, m_min_level - 1, m_root);
for (other_it = lvl_father_cells.begin(); other_it != lvl_father_cells.end(); ++other_it)
{
Cell* other_cell_father = m_cells[*other_it];
Point other_cell_father_grid_pos = lazy_get_set_cell_grid_pos(other_cell_father,
m_min_level - 1,
m_root);
if (Point::max_norm_dist(cur_cell_father_grid_pos, other_cell_father_grid_pos) > 1)
{
continue;
}
//otherwise we have neighboring father cells (or the own father)
//and want to retrieve the other cell's children
if (other_cell_father->is_leaf())
{
continue;
}
std::vector<Cell*> children = other_cell_father->get_children();
std::vector<Cell*>::const_iterator children_it = children.begin();
for (; children_it != children.end(); ++children_it)
{
Point children_cell_grid_pos = lazy_get_set_cell_grid_pos((*children_it),
m_min_level,
m_root);
if (Point::max_norm_dist(children_cell_grid_pos, cur_cell_grid_pos) > 1)
{
// well seperated on same level as cur_cell --> list 2
cur_cell->add_to_list(*children_it, 1);
}
else
{
if (cur_cell->is_leaf())
{
// the "neighbor" is cell itself --> add to list1
if (cur_cell == *children_it)
{
cur_cell->add_to_list(cur_cell, 0);
}
else
{
// have a look at neighbors of b (and their
// descendents to find out who belongs to list 1
// (direct neighbor) or list 3
generate_lists134(cur_cell, *children_it);
}
}
cur_cell->add_to_list(*children_it,4);
}
}
}
#ifdef DEBUG
std::cout << "Cell " << cur_cell->debug_info() << " lists are" << std::endl;
for (unsigned int list_nr = 0; list_nr < 5; list_nr++)
{
std::vector<unsigned int> list_ids = cur_cell->get_list_ids(list_nr);
std::cout << "list " << list_nr << " : " << std::endl;
for (unsigned int i = 0; i < list_ids.size(); i++)
{
std::cout << list_ids[i] << " ";
}
std::cout << std::endl;
}
std::cout << "\n" << std::endl;
#endif
}
}
void Tree2D_Ada::make_other_levels_lists()
{
for (unsigned int i = m_min_level+1; i<=m_max_level; i++)
{
std::vector<unsigned int> & lvl_cells = m_lvl_ids[i];
std::vector<unsigned int >::const_iterator it = lvl_cells.begin();
std::vector<Cell*>::const_iterator other_it;
for (; it!=lvl_cells.end(); ++it)
{
Cell* cur_cell = m_cells[*it];
assert(cur_cell->get_id() == *it);
Cell* cur_cell_father = cur_cell->get_father();
Point cur_cell_grid_pos = lazy_get_set_cell_grid_pos(cur_cell,i,m_root);
std::vector<Cell*> father_coll = cur_cell->get_father()->get_list(4);
for (other_it = father_coll.begin(); other_it != father_coll.end(); ++other_it)
{
Cell* other_cell_father = *other_it;
if (other_cell_father->is_leaf())
{
continue;
}
std::vector<Cell*> const & children = other_cell_father->get_children();
std::vector<Cell*>::const_iterator children_it = children.begin();
for(;children_it != children.end(); ++children_it)
{
Point children_cell_grid_pos = lazy_get_set_cell_grid_pos((*children_it),
i,
m_root);
if (Point::max_norm_dist(children_cell_grid_pos, cur_cell_grid_pos) > 1)
{
// well seperated on same level as cur_cell --> list 2
cur_cell->add_to_list(*children_it, 1);
}
else
{
if (cur_cell->is_leaf())
{
// the "neighbor" is cell itself --> add to list1
if (cur_cell == *children_it)
{
cur_cell->add_to_list(cur_cell, 0);
}
else
{
// have a look at neighbors of b (and their
// descendents to find out who belongs to list 1
// (direct neighbor) or list 3
generate_lists134(cur_cell, *children_it);
}
}
cur_cell->add_to_list(*children_it,4);
}
}
}
#ifdef DEBUG
std::cout << "Cell " << cur_cell->debug_info() << " lists are" << std::endl;
for(unsigned int list_nr = 0; list_nr < 5; list_nr++)
{
std::vector<unsigned int> list_ids = cur_cell->get_list_ids(list_nr);
std::cout << "list " << list_nr << " : " << std::endl;
for(unsigned int i = 0; i< list_ids.size(); i++)
{
std::cout << list_ids[i] << " ";
}
std::cout << std::endl;
}
std::cout << "\n" << std::endl;
#endif
}
}
}
void make_list4(Cell * const cell, Cell * const neighbor)
{
std::vector<Cell*> stack;
stack.push_back(neighbor);
Cell * cur_cell;
while(!stack.empty())
{
cur_cell = stack.back();
stack.pop_back();
if(cur_cell->is_leaf())
{
// the bigger cell cell is in list 4 of cur_cell
cur_cell->add_to_list(cell,3);
}
else
{
std::vector<Cell*> const & children = cur_cell->get_children();
stack.insert(stack.end(),children.begin(),children.end());
}
}
}
void Tree2D_Ada::generate_lists134(Cell * const cell, Cell * const neighbor)
{
if(neighbor->is_leaf())
{
// don't go any further, just add to direct list
cell->add_to_list(neighbor,0);
neighbor->add_to_list(cell,0);
return;
}
// the following two vectors determine the minimal and maximal positions
// in every direction in the level grid of the cell in relative depth to
// the cells depth(== level)
std::vector<Point> cell_min_pos;
std::vector<Point> cell_max_pos;
cell_min_pos.push_back(cell->get_level_grid_position());
cell_max_pos.push_back(cell->get_level_grid_position());
// start directly with neighbor's children
std::vector<Cell*> stack(neighbor->get_children());
while(!stack.empty())
{
Cell* cur_cell = stack.back();
stack.pop_back();
int cur_depth = cur_cell->get_level() - cell->get_level();
// reached deeper level on which cell's grid_positions need to be
// computed
if (cur_depth == cell_min_pos.size())
{
Point next_min_pos(cell_min_pos.at(cur_depth-1));
Point next_max_pos(cell_max_pos.at(cur_depth-1));
int cell_size = 1 << cur_depth;
for(int i = 0; i<m_dim; i++)
{
next_min_pos[i] = (int)(2*next_min_pos[i]);
next_max_pos[i] = next_min_pos[i] + cell_size - 1;
}
cell_min_pos.push_back(next_min_pos);
cell_max_pos.push_back(next_max_pos);
}
// get coordinates of cur_cell and find out whether it is a direct
// neighbor or not
Point cur_cell_pos = lazy_get_set_cell_grid_pos(cur_cell,
cur_cell->get_level(),
m_root);
// we have direct neighbor if in all dimensions we are within range
// of minimum position lowered by one everywhere and max position
// heightened by one everywhere
Point const & min_cell_pos = cell_min_pos[cur_depth];
Point const & max_cell_pos = cell_max_pos[cur_depth];
bool direct = true;
for(int i = 0; (i<m_dim) && direct; i++)
{
direct = (0 <= (cur_cell_pos[i] - min_cell_pos[i] + 1));
direct = direct && (0<= (max_cell_pos[i] - cur_cell_pos[i])+1);
}
if(direct)
{
// if we have a leaf that we are fine and add it to the direct list1
if (cur_cell->is_leaf())
{
//cell is direct neighbor of cur_cell
cur_cell->add_to_list(cell,0);
//and vice versa
cell->add_to_list(cur_cell,0);
}
else
{
//since it is direct but a leaf we need to examine its children
std::vector<Cell*> const & children = cur_cell->get_children();
stack.insert(stack.end(),children.begin(),children.end());
}
} else
{
// the cur_cell's father was a direct neighbor to cell, otherwise
// it wouldn't be in the stack. but cur_cell is not a direct
// neighbor to cell --> cur_cell is in list 3 of cell
cell->add_to_list(cur_cell,2);
// and all its descendents are in list 4
make_list4(cell,cur_cell);
}
}
}