-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuadTree.cpp
346 lines (296 loc) · 6.83 KB
/
QuadTree.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#include "QuadTree.h"
#define BIG_G 6.673e-11
QuadTreeNode::QuadTreeNode(const XMFLOAT2& min, const XMFLOAT2& max, QuadTreeNode* parent)
:_assignedEntity()
, _mass(0)
, _cm()
, _min(min)
, _max(max)
, _center(min.x + (max.x - min.x) / 2.0, min.y + (max.y - min.y) / 2.0)
, _parent(parent)
, _num(0)
, _bSubdivided(false)
{
_quadNode[0] = _quadNode[1] = _quadNode[2] = _quadNode[3] = nullptr;
}
bool QuadTreeNode::IsRoot() const
{
return _parent == nullptr;
}
bool QuadTreeNode::IsExternal() const
{
return _quadNode[0] == nullptr &&
_quadNode[1] == nullptr &&
_quadNode[2] == nullptr &&
_quadNode[3] == nullptr;
}
bool QuadTreeNode::WasTooClose() const
{
return _bSubdivided;
}
// Returns the number of particles not assigned to any node
int QuadTreeNode::GetNumRenegades() const
{
return s_renegades.size();
}
// Returns the number of particles inside this node
int QuadTreeNode::GetNum() const
{
return _num;
}
const XMFLOAT2& QuadTreeNode::GetMin() const
{
return _min;
}
const XMFLOAT2& QuadTreeNode::GetMax() const
{
return _max;
}
const XMFLOAT2& QuadTreeNode::GetCenterOfMass() const
{
return _cm;
}
double QuadTreeNode::GetTheta() const
{
return s_theta;
}
void QuadTreeNode::SetTheta(double theta)
{
s_theta = theta;
}
QuadTreeNode::EQuadrant QuadTreeNode::GetQuadrant(float x, float y) const
{
if (x <= _center.x && y <= _center.y)
{
return SW;
}
else if (x <= _center.x && y >= _center.y)
{
return NW;
}
else if (x >= _center.x && y >= _center.y)
{
return NE;
}
else if (x >= _center.x && y <= _center.y)
{
return SE;
}
else if (x > _max.x || y > _max.y || x < _min.x || y < _min.y)
{
return NONE;
}
else
{
return NONE;
}
}
QuadTreeNode* QuadTreeNode::CreateQuadNode(EQuadrant eQuad)
{
if (eQuad == SE)
return new QuadTreeNode(_center, _max, this);
else if (eQuad == SW)
return new QuadTreeNode(XMFLOAT2(_min.x, _center.y),
XMFLOAT2(_center.x, _max.y),
this);
else if (eQuad == NW)
return new QuadTreeNode(_min, _center, this);
else if (eQuad == NE)
return new QuadTreeNode(XMFLOAT2(_center.x, _min.y),
XMFLOAT2(_max.x, _center.y),
this);
}
void QuadTreeNode::Reset(const XMFLOAT2& min, const XMFLOAT2& max)
{
if (IsRoot())
{
if (_assignedEntity)
_assignedEntity = nullptr;
}
for (int i = 0; i < 4; i++)
{
delete _quadNode[i];
_quadNode[i] = nullptr;
}
_min = min;
_max = max;
_center = XMFLOAT2(min.x + (max.x - min.x) / 2.0f, min.y + (max.y - min.y) / 2.0f);
_num = 0;
_mass = 0;
_cm = XMFLOAT2(0.0f, 0.0f);
s_renegades.clear();
}
void QuadTreeNode::Insert(Entity* newParticle, int level)
{
// Check if Entity is inside bounding box
const XMFLOAT3 pos = newParticle->GetPositionFloat3();
if ((pos.x < _min.x || pos.x > _max.x) || (pos.y > _min.y || pos.y < _max.y))
return;
if (_num > 1)
{
EQuadrant eQuad = GetQuadrant(pos.x, pos.y);
if (!_quadNode[eQuad])
_quadNode[eQuad] = CreateQuadNode(eQuad);
_quadNode[eQuad]->Insert(newParticle, level + 1);
}
else if (_num == 1)
{
const XMFLOAT3 pos2 = _assignedEntity->GetPositionFloat3();
// There are two bodies at the exact same coordinates, put it in the renegade vector.
if (pos.x == pos2.x && pos.y == pos2.y)
{
s_renegades.push_back(newParticle);
}
else
{
// There is already a particle, subdivide the node and relocate that particle
EQuadrant eQuad = GetQuadrant(pos2.x, pos2.y);
if (_quadNode[eQuad] == nullptr)
_quadNode[eQuad] = CreateQuadNode(eQuad);
_quadNode[eQuad]->Insert(_assignedEntity, level + 1);
eQuad = GetQuadrant(pos.x, pos.y);
if (!_quadNode[eQuad])
_quadNode[eQuad] = CreateQuadNode(eQuad);
_quadNode[eQuad]->Insert(newParticle, level + 1);
}
}
else if (_num == 0)
{
_assignedEntity = newParticle;
}
_num++;
}
void QuadTreeNode::ComputeMassDistribution()
{
if (_num == 1)
{
XMFLOAT3 pos = _assignedEntity->GetPositionFloat3();
_cm = XMFLOAT2(pos.x, pos.y);
_mass = _assignedEntity->GetMass();
}
else
{
// Compute the center of mass based on the masses of all child quadrants and the center of
// mass as the center of mass of the child quadrants weightes with their mass
_mass = 0.0f;
_cm = XMFLOAT2(0.0f, 0.0f);
for (int i = 0; i < 4; i++)
{
if (_quadNode[i])
{
_quadNode[i]->ComputeMassDistribution();
_mass += _quadNode[i]->_mass;
_cm.x += _quadNode[i]->_cm.x * _quadNode[i]->_mass;
_cm.y += _quadNode[i]->_cm.y * _quadNode[i]->_mass;
}
}
_cm.x /= _mass;
_cm.y /= _mass;
}
}
XMFLOAT2 QuadTreeNode::CalcAcc(Entity* p1, Entity* p2) const
{
XMFLOAT2 acc(0.0f, 0.0f);
if (p1 == p2)
{
return acc;
}
const XMFLOAT3& p1Pos(p1->GetPositionFloat3());
const XMFLOAT3& p2Pos(p2->GetPositionFloat3());
float xDistance = p1Pos.x - p2Pos.x;
float yDistance = p1Pos.y - p2Pos.y;
float r = sqrt((xDistance * xDistance) + (yDistance * yDistance) + s_soft);
if (r > s_attractionThreshold)
{
float k = gamma_1 * p2->GetMass() / (r * r * r);
acc.x = k * (p2Pos.x - p1Pos.x);
acc.y = k * (p2Pos.y - p1Pos.y);
}
else
{
acc.x = acc.y = 0.0f;
}
return acc;
}
XMFLOAT2 QuadTreeNode::CalcForce(Entity* particle) const
{
XMFLOAT2 acc = CalcTreeForce(particle);
if (s_renegades.size())
{
for (std::size_t i = 0; i < s_renegades.size(); i++)
{
XMFLOAT2 buf = CalcAcc(particle, s_renegades[i]);
acc.x += buf.x;
acc.y += buf.y;
}
}
return acc;
}
XMFLOAT2 QuadTreeNode::CalcTreeForce(Entity* particle) const
{
XMFLOAT2 acc(0.0f, 0.0f);
float r(0), d(0), k(0);
if (_num == 1)
{
acc = this->CalcAcc(particle, _assignedEntity);
}
else
{
XMFLOAT3 pPos = particle->GetPositionFloat3();
float xDistance = (pPos.x - _cm.x);
float yDistance = (pPos.y - _cm.y);
r = sqrt(xDistance * xDistance + yDistance * yDistance);
d = _max.x - _min.x;
if (d / r <= s_theta)
{
_bSubdivided = false;
k = gamma_1 * _mass / (r * r * r);
acc.x = k * (_cm.x - pPos.x);
acc.y = k * (_cm.y - pPos.y);
}
else
{
_bSubdivided = true;
XMFLOAT2 buf(0.0f, 0.0f);
for (int q = 0; q < 4; q++)
{
if (_quadNode[q])
{
buf = _quadNode[q]->CalcTreeForce(particle);
acc.x += buf.x;
acc.y += buf.y;
}
}
}
}
return acc;
}
void QuadTreeNode::DrawEntities(const XMMATRIX& viewProjectionMatrix)
{
if (_assignedEntity)
_assignedEntity->Draw(viewProjectionMatrix);
for (int i = 0; i < 4; i++)
{
if (_quadNode[i])
_quadNode[i]->DrawEntities(viewProjectionMatrix);
}
}
void QuadTreeNode::ReleaseEntities()
{
if (_parent == nullptr)
_assignedEntity->Release();
if (_assignedEntity != nullptr)
_assignedEntity->Release();
for (int i = 0; i < 4; i++)
{
if (_quadNode[i] != nullptr)
_quadNode[i]->ReleaseEntities();
}
for (Entity* r : s_renegades)
if (r) r->Release();
}
QuadTreeNode::~QuadTreeNode()
{
for (int i = 0; i < 4; ++i)
delete _quadNode[i];
}