-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.cpp
329 lines (299 loc) · 5.74 KB
/
node.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
318
319
320
321
322
323
324
325
326
327
328
#include <iostream>
#include <stdlib.h>
#include "vect.h"
#include "consts.h"
#include "bodies.h"
#include <fstream>
int NodeCounter = 0;
//Contructor
Node::Node(Vect _center, double _size)
{
center = _center;
size = _size;// + (_size * 0.1);
ne = se = sw = nw = NULL;
NodeCounter++;
}
//Destructor. Deletes each subnode.
Node::~Node()
{
// if (nw != NULL) delete nw;
// if (ne != NULL) delete ne;
// if (se != NULL) delete se;
// if (sw != NULL) delete sw;
// if (NodeBody != NULL) delete &NodeBody;
this->NodeBodyCount = 0;
NodeBody = NULL;
NodeCounter--;
// NodeCounter--;
// cout << center << endl;
delete nw;
delete ne;
delete se;
delete sw;
// delete NodeBody;
}
//Inserts a body in to a Node, as described above each case a body may encounter.
void Node::InsertToNode(Body *NewBody)
{
//If node has more bodies, check if desired node exists.
//If it does, InsertToNode there, if not, create 4 subnodes,
//then insert to desired node.
if (this->NodeBodyCount > 1)
{
// cout << size;
int asd;
int quad = GetQuad(NewBody);
switch (quad)
{
case 1 :
{
if (ne == NULL)
{
Vect neCent;
neCent.x = center.x + (size / 2.0);
neCent.y = center.y + (size / 2.0);
ne = new Node(neCent, (size / 2.0));
}
ne->InsertToNode(NewBody);
} break;
case 2 :
{
if (se == NULL)
{
Vect seCent;
seCent.x = center.x + (size / 2.0);
seCent.y = center.y - (size / 2.0);
se = new Node(seCent, (size / 2.0));
}
se->InsertToNode(NewBody);
} break;
case 3 :
{
if (nw == NULL)
{
Vect nwCent;
nwCent.x = center.x - (size / 2.0);
nwCent.y = center.y + (size / 2.0);
nw = new Node(nwCent, (size / 2.0));
}
nw->InsertToNode(NewBody);
} break;
case 4 :
{
if (sw == NULL)
{
Vect swCent;
swCent.x = center.x - (size / 2.0);
swCent.y = center.y - (size / 2.0);
sw = new Node(swCent, (size / 2.0));
}
sw->InsertToNode(NewBody);
} break;
}
}
else
{
// cout << size;
//If node has one body, find it's new quadrant. If the new quadrant exists, place it there. If it doesn't,
//create it and place it there.
//Then take the new body and find it's own quadrant and check if it exists. If it does, place it there,
//if not, create it and place it there.
if (this->NodeBodyCount == 1)
{
int quad = GetQuad(NodeBody);
switch (quad)
{
case 1 :
{
if (ne == NULL)
{
Vect neCent;
neCent.x = center.x + (size / 2.0);
neCent.y = center.y + (size / 2.0);
ne = new Node(neCent, (size / 2.0));
ne->InsertToNode(NodeBody);
}
else
{
ne->InsertToNode (NodeBody);
}
} break;
case 2 :
{
if (se == NULL)
{
Vect seCent;
seCent.x = center.x + (size / 2.0);
seCent.y = center.y - (size / 2.0);
se = new Node(seCent, (size / 2.0));
se->InsertToNode (NodeBody);
}
else
{
se->InsertToNode (NodeBody);
}
} break;
case 3 :
{
if (nw == NULL)
{
Vect nwCent;
nwCent.x = center.x - (size / 2.0);
nwCent.y = center.y + (size / 2.0);
nw = new Node(nwCent, (size / 2.0));
nw->InsertToNode(NodeBody);
}
else
{
nw->InsertToNode(NodeBody);
}
} break;
case 4 :
{
if (sw == NULL)
{
Vect swCent;
swCent.x = center.x - (size / 2.0);
swCent.y = center.y - (size / 2.0);
sw = new Node(swCent, (size / 2.0));
sw->InsertToNode(NodeBody);
}
else
{
sw->InsertToNode(NodeBody);
}
} break;
}
quad = GetQuad(NewBody);
switch (quad)
{
case 1 :
{
if (ne == NULL)
{
Vect neCent;
neCent.x = center.x + (size / 2.0);
neCent.y = center.y + (size / 2.0);
ne = new Node(neCent, (size / 2.0));
}
ne->InsertToNode(NewBody);
} break;
case 2 :
{
if (se == NULL)
{
Vect seCent;
seCent.x = center.x + (size / 2.0);
seCent.y = center.y - (size / 2.0);
se = new Node(seCent, (size / 2.0));
}
se->InsertToNode(NewBody);
} break;
case 3 :
{
if (nw == NULL)
{
Vect nwCent;
nwCent.x = center.x - (size / 2.0);
nwCent.y = center.y + (size / 2.0);
nw = new Node(nwCent, (size / 2.0));
}
nw->InsertToNode(NewBody);
} break;
case 4 :
{
if (sw == NULL)
{
Vect swCent;
swCent.x = center.x - (size / 2.0);
swCent.y = center.y - (size / 2.0);
sw = new Node(swCent, (size / 2.0));
}
sw->InsertToNode(NewBody);
} break;
}
}
//If the node is emtpy, store the new body as the Node Body
else
{
// cout << size;
NodeBody = NewBody;
}
}
//Increase the number of particles in the node;
this->NodeBodyCount++;
}
int Node::BHcount()
{
int num = 0;
if (ne != NULL)
{
num += ne->BHcount();
}
if (nw != NULL)
{
num += nw->BHcount();
}
if (se != NULL)
{
num += se->BHcount();
}
if (sw != NULL)
{
num += sw->BHcount();
}
return num;
}
//Returns the quadrant in which the forwarded body belongs.
//Returns number from 1 to 4.
int Node::GetQuad(Body *body)
{
if (body->r.x > this->center.x)
{
if (body->r.y > this->center.y)
{
return 1; //NE
}
else
{
return 2; //SE
}
}
else
{
if (body->r.y > this->center.y)
{
return 3; //NW
}
else
{
return 4; //SW
}
}
}
void Node::ComputeMassDistribution()
{
if (this->NodeBodyCount == 1)
{
centerOfMass = NodeBody->r;
mass = NodeBody->m;
}
else
{
/* code */
}
}
//Overloaded operator that allows the user to recursively output the center and size of each node and with subnodes.
ostream &operator<<(ostream &O, Node &B)
{
O << B.center << "\t" << B.size << endl;
if (B.ne != NULL)
O << *B.ne;
if (B.nw != NULL)
O << *B.nw;
if (B.sw != NULL)
O << *B.sw;
if (B.se != NULL)
O << *B.se;
return O;
}