This repository was archived by the owner on Jul 18, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathSortAndSweep.cpp
248 lines (201 loc) · 8.15 KB
/
SortAndSweep.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
/*
* SortAndSweep.cpp
* SpatialTest Project
*
* Sort and Sweep approach.
*
* Created by radix on 07/04/08.
* Copyright Mykola Konyk, <[email protected]>, 2008.
*
* This code is under Microsoft Reciprocal License (Ms-RL)
* Please see http://www.opensource.org/licenses/ms-rl.html
*
* Important points about the license (from Ms-RL):
*
* [A] For any file you distribute that contains code from the software (in source code or binary format), you must provide
* recipients the source code to that file along with a copy of this license, which license will govern that file.
* You may license other files that are entirely your own work and do not contain code from the software under any terms
* you choose.
*
* [B] No Trademark License- This license does not grant you rights to use any contributors' name, logo, or trademarks.
*
* [C] If you bring a patent claim against any contributor over patents that you claim are infringed by the software, your
* patent license from such contributor to the software ends automatically.
*
* [D] If you distribute any portion of the software, you must retain all copyright, patent, trademark, and attribution notices
* that are present in the software.
*
* [E] If you distribute any portion of the software in source code form, you may do so only under this license by including a
* complete copy of this license with your distribution. If you distribute any portion of the software in compiled or object
* code form, you may only do so under a license that complies with this license.
*
* [F] The software is licensed "as-is." You bear the risk of using it. The contributors give no express warranties, guarantees
* or conditions. You may have additional consumer rights under your local laws which this license cannot change. To the extent
* permitted under your local laws, the contributors exclude the implied warranties of merchantability, fitness for a particular
* purpose and non-infringement.
*
*/
#include "SortAndSweep.h"
#include <algorithm>
#include <cstdlib>
namespace SpatialTest
{
//--
struct CompareAxisX
{
bool operator()(const ISpatialObject* pLeft, const ISpatialObject* pRight)
{
return(pLeft->VGetPosition().x - pLeft->VGetRadius() < pRight->VGetPosition().x - pRight->VGetRadius());
}
};
//--
struct CompareAxisY
{
bool operator()(const ISpatialObject* pLeft, const ISpatialObject* pRight)
{
return(pLeft->VGetPosition().y - pLeft->VGetRadius() < pRight->VGetPosition().y - pRight->VGetRadius());
}
};
//--
struct CompareAxisZ
{
bool operator()(const ISpatialObject* pLeft, const ISpatialObject* pRight)
{
return(pLeft->VGetPosition().z - pLeft->VGetRadius() < pRight->VGetPosition().z - pRight->VGetRadius());
}
};
//--
SortAndSweep::SortAndSweep() :
ISpatialStructure(),
m_i32SortAxis(0)
{
}
//--
void
SortAndSweep::VAddObjects(const std::vector<ISpatialObject*>& refObjects)
{
// [rad] Copy elements into the local vector
std::vector<ISpatialObject*>::const_iterator iter_object;
for(iter_object = refObjects.begin(); iter_object != refObjects.end(); iter_object++)
{
m_vecObjects.push_back(*iter_object);
}
}
//--
void
SortAndSweep::VUpdate()
{
int i32Index;
int i32Break;
float f32Sum1[3] = {0.0f, 0.0f, 0.0f};
float f32Sum2[3] = {0.0f, 0.0f, 0.0f};
float f32Var[3];
Vector3 vec3Center1;
Vector3 vec3Center2;
float f32Radius1;
float f32Radius2;
std::vector<ISpatialObject*>::iterator iter_object1;
std::vector<ISpatialObject*>::iterator iter_object2;
// [rad] Sort Objects on the given axis
switch(m_i32SortAxis)
{
case 0:
{
// [rad] Sort on X axis
std::sort(m_vecObjects.begin(), m_vecObjects.end(), CompareAxisX());
}
break;
case 1:
{
// [rad] Sort on Y axis
std::sort(m_vecObjects.begin(), m_vecObjects.end(), CompareAxisY());
}
break;
case 2:
{
// [rad] Sort on Z axis
std::sort(m_vecObjects.begin(), m_vecObjects.end(), CompareAxisZ());
}
break;
}
// [rad] Now iterate through objects
for(iter_object1 = m_vecObjects.begin(); iter_object1 != m_vecObjects.end(); iter_object1++)
{
// [rad] Retrieve this object's center and radius
vec3Center1 = (*iter_object1)->VGetPosition();
f32Radius1 = (*iter_object1)->VGetRadius();
// [rad] Update sums: we'll need this to compute variance of sphere centers
for(i32Index = 0; i32Index < 3; i32Index++)
{
f32Sum1[i32Index] += vec3Center1[i32Index];
f32Sum2[i32Index] += vec3Center1[i32Index] * vec3Center1[i32Index];
}
// [rad] Set break flag
i32Break = 0;
// [rad] Test collisions vs all possible objects, starting from
// the current position
for(iter_object2 = iter_object1; iter_object2 != m_vecObjects.end(); iter_object2++)
{
vec3Center2 = (*iter_object2)->VGetPosition();
f32Radius2 = (*iter_object2)->VGetRadius();
// [rad] Self check
if(*iter_object2 == *iter_object1) continue;
// [rad] Can stop if we went beyond the current AABB
switch(m_i32SortAxis)
{
case 0:
{
if(vec3Center2.x - f32Radius2 > vec3Center1.x + f32Radius1)
{
i32Break = 1;
}
}
break;
case 1:
{
if(vec3Center2.y - f32Radius2 > vec3Center1.y + f32Radius1)
{
i32Break = 1;
}
}
break;
case 2:
{
if(vec3Center2.z - f32Radius2 > vec3Center1.z + f32Radius1)
{
i32Break = 1;
}
}
break;
}
// [rad] Stop if we are beyond the current AABB
if(i32Break)
{
break;
}
// [rad] Perform actual collision test
if((*iter_object1)->VCheckCollision((*iter_object2)))
{
// [rad] Mark both as in collision
(*iter_object1)->VCollisionOn();
(*iter_object2)->VCollisionOn();
}
}
}
// [rad] Compute variance
for(i32Index = 0; i32Index < 3; i32Index++)
{
f32Var[i32Index] = f32Sum2[i32Index] - f32Sum1[i32Index] * f32Sum1[i32Index] / static_cast<float>(m_vecObjects.size());
}
// [rad] Update axis - select one with highest AABB variance
m_i32SortAxis = 0;
if(f32Var[1] > f32Var[0])
{
m_i32SortAxis = 1;
}
if(f32Var[2] > f32Var[m_i32SortAxis])
{
m_i32SortAxis = 2;
}
}
}