Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
Spatial.h
Go to the documentation of this file.
1/* Copyright (C) 2026 Wildfire Games.
2 * This file is part of 0 A.D.
3 *
4 * 0 A.D. is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * 0 A.D. is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef INCLUDED_SPATIAL
19#define INCLUDED_SPATIAL
20
21#include "lib/debug.h"
22#include "maths/Fixed.h"
23#include "maths/FixedVector2D.h"
24#include "maths/MathUtil.h"
29
30#include <algorithm>
31#include <cstddef>
32#include <cstdint>
33#include <utility>
34#include <vector>
35
36/**
37 * A very basic subdivision scheme for finding items in ranges.
38 * Items are stored in lists in dynamic-sized divisions.
39 * Items have a size (min/max values of their axis-aligned bounding box)
40 * and are stored in all divisions overlapping that area.
41 *
42 * It is the caller's responsibility to ensure items are only added
43 * once, aren't removed unless they've been added, etc, and that
44 * Move/Remove are called with the same coordinates originally passed
45 * to Add (since this class doesn't remember which divisions an item
46 * occupies).
47 */
49{
51 {
52 std::vector<uint32_t> items;
53
54 inline void push_back(uint32_t value)
55 {
56 items.push_back(value);
57 }
58
59 inline void erase(int index)
60 {
61 // Delete by swapping with the last element then popping
62 if ((int)items.size() > 1) // but only if we have more than 1 elements
63 items[index] = items.back();
64 items.pop_back();
65 }
66
67 void copy_items_at_end(std::vector<uint32_t>& out) const
68 {
69 out.insert(out.end(), items.begin(), items.end());
70 }
71 };
72
73
76 uint32_t m_DivisionsW;
77 uint32_t m_DivisionsH;
78
80
81public:
83 {
84 }
86 {
87 delete[] m_Divisions;
88 }
90 {
94 size_t n = m_DivisionsW * m_DivisionsH;
96 for (size_t i = 0; i < n; ++i)
97 m_Divisions[i] = rhs.m_Divisions[i]; // just fall back to piecemeal copy
98 }
100 {
101 if (this != &rhs)
102 {
104 size_t n = rhs.m_DivisionsW * rhs.m_DivisionsH;
106 Create(n); // size changed, recreate
107
110 for (size_t i = 0; i < n; ++i)
111 m_Divisions[i] = rhs.m_Divisions[i]; // just fall back to piecemeal copy
112 }
113 return *this;
114 }
115
116 inline entity_pos_t GetDivisionSize() const { return m_DivisionSize; }
117 inline uint32_t GetWidth() const { return m_DivisionsW; }
118 inline uint32_t GetHeight() const { return m_DivisionsH; }
119
120 void Create(size_t count)
121 {
122 delete[] m_Divisions;
123 m_Divisions = new SubDivisionGrid[count];
124 }
125
126 /**
127 * Equivalence test (ignoring order of items within each subdivision)
128 */
129 bool operator==(const SpatialSubdivision& rhs) const
130 {
132 return false;
133
134 uint32_t n = m_DivisionsH * m_DivisionsW;
135 for (uint32_t i = 0; i < n; ++i)
136 {
137 if (m_Divisions[i].items.size() != rhs.m_Divisions[i].items.size())
138 return false;
139
140 // don't bother optimizing this, this is only used in the TESTING SUITE
141 std::vector<uint32_t> a = m_Divisions[i].items;
142 std::vector<uint32_t> b = rhs.m_Divisions[i].items;
143 std::sort(a.begin(), a.end());
144 std::sort(b.begin(), b.end());
145 if (a != b)
146 return false;
147 }
148 return true;
149 }
150
151 inline bool operator!=(const SpatialSubdivision& rhs) const
152 {
153 return !(*this == rhs);
154 }
155
156 void Reset(entity_pos_t maxX, entity_pos_t maxZ, entity_pos_t divisionSize)
157 {
158 m_DivisionSize = divisionSize;
159 m_DivisionsW = (maxX / m_DivisionSize).ToInt_RoundToInfinity();
160 m_DivisionsH = (maxZ / m_DivisionSize).ToInt_RoundToInfinity();
161
163 }
164
165
166 /**
167 * Add an item with the given 'to' size.
168 * The item must not already be present.
169 */
170 void Add(uint32_t item, CFixedVector2D toMin, CFixedVector2D toMax)
171 {
172 ENSURE(toMin.X <= toMax.X && toMin.Y <= toMax.Y);
173
174 std::uint32_t i0 = GetI0(toMin.X);
175 std::uint32_t j0 = GetJ0(toMin.Y);
176 std::uint32_t i1 = GetI1(toMax.X);
177 std::uint32_t j1 = GetJ1(toMax.Y);
178 for (std::uint32_t j = j0; j <= j1; ++j)
179 {
180 for (std::uint32_t i = i0; i <= i1; ++i)
181 {
183 }
184 }
185 }
186
187 /**
188 * Remove an item with the given 'from' size.
189 * The item should already be present.
190 * The size must match the size that was last used when adding the item.
191 */
192 void Remove(uint32_t item, CFixedVector2D fromMin, CFixedVector2D fromMax)
193 {
194 ENSURE(fromMin.X <= fromMax.X && fromMin.Y <= fromMax.Y);
195
196 std::uint32_t i0 = GetI0(fromMin.X);
197 std::uint32_t j0 = GetJ0(fromMin.Y);
198 std::uint32_t i1 = GetI1(fromMax.X);
199 std::uint32_t j1 = GetJ1(fromMax.Y);
200 for (std::uint32_t j = j0; j <= j1; ++j)
201 {
202 for (std::uint32_t i = i0; i <= i1; ++i)
203 {
205 int size = div.items.size();
206 for (int n = 0; n < size; ++n)
207 {
208 if (div.items[n] == item)
209 {
210 div.erase(n);
211 break;
212 }
213 }
214 }
215 }
216 }
217
218 /**
219 * Equivalent to Remove() then Add(), but potentially faster.
220 */
221 void Move(uint32_t item, CFixedVector2D fromMin, CFixedVector2D fromMax, CFixedVector2D toMin, CFixedVector2D toMax)
222 {
223 // Skip the work if we're staying in the same divisions
224 if (GetIndex0(fromMin) == GetIndex0(toMin) && GetIndex1(fromMax) == GetIndex1(toMax))
225 return;
226
227 Remove(item, fromMin, fromMax);
228 Add(item, toMin, toMax);
229 }
230
231 /**
232 * Convenience function for Add() of individual points.
233 * (Note that points on a boundary may occupy multiple divisions.)
234 */
235 void Add(uint32_t item, CFixedVector2D to)
236 {
237 Add(item, to, to);
238 }
239
240 /**
241 * Convenience function for Remove() of individual points.
242 */
243 void Remove(uint32_t item, CFixedVector2D from)
244 {
245 Remove(item, from, from);
246 }
247
248 /**
249 * Convenience function for Move() of individual points.
250 */
251 void Move(uint32_t item, CFixedVector2D from, CFixedVector2D to)
252 {
253 Move(item, from, from, to, to);
254 }
255
256 /**
257 * Returns a sorted list of unique items that includes all items
258 * within the given axis-aligned square range.
259 */
260 void GetInRange(std::vector<uint32_t>& out, CFixedVector2D posMin, CFixedVector2D posMax) const
261 {
262 out.clear();
263 ENSURE(posMin.X <= posMax.X && posMin.Y <= posMax.Y);
264
265 std::uint32_t i0 = GetI0(posMin.X);
266 std::uint32_t j0 = GetJ0(posMin.Y);
267 std::uint32_t i1 = GetI1(posMax.X);
268 std::uint32_t j1 = GetJ1(posMax.Y);
269 for (std::uint32_t j = j0; j <= j1; ++j)
270 {
271 for (std::uint32_t i = i0; i <= i1; ++i)
272 {
274 }
275 }
276 // some buildings span several tiles, so we need to make it unique
277 std::sort(out.begin(), out.end());
278 out.erase(std::unique(out.begin(), out.end()), out.end());
279 }
280
281 /**
282 * Returns a sorted list of unique items that includes all items
283 * within the given circular distance of the given point.
284 */
285 void GetNear(std::vector<uint32_t>& out, CFixedVector2D pos, entity_pos_t range) const
286 {
287 // TODO: be cleverer and return a circular pattern of divisions,
288 // not this square over-approximation
289 CFixedVector2D r(range, range);
290 GetInRange(out, pos - r, pos + r);
291 }
292
293private:
294 // Helper functions for translating coordinates into division indexes
295 // (avoiding out-of-bounds accesses, and rounding correctly so that
296 // points precisely between divisions are counted in both):
297
298 uint32_t GetI0(entity_pos_t x) const
299 {
300 return Clamp((x / m_DivisionSize).ToInt_RoundToInfinity()-1, 0, (int)m_DivisionsW-1);
301 }
302
303 uint32_t GetJ0(entity_pos_t z) const
304 {
305 return Clamp((z / m_DivisionSize).ToInt_RoundToInfinity()-1, 0, (int)m_DivisionsH-1);
306 }
307
308 uint32_t GetI1(entity_pos_t x) const
309 {
310 return Clamp((x / m_DivisionSize).ToInt_RoundToNegInfinity(), 0, (int)m_DivisionsW-1);
311 }
312
313 uint32_t GetJ1(entity_pos_t z) const
314 {
315 return Clamp((z / m_DivisionSize).ToInt_RoundToNegInfinity(), 0, (int)m_DivisionsH-1);
316 }
317
318 uint32_t GetIndex0(CFixedVector2D pos) const
319 {
320 return GetI0(pos.X) + GetJ0(pos.Y)*m_DivisionsW;
321 }
322
323 uint32_t GetIndex1(CFixedVector2D pos) const
324 {
325 return GetI1(pos.X) + GetJ1(pos.Y)*m_DivisionsW;
326 }
327};
328
329/**
330 * Serialization helper template for SpatialSubdivision
331 */
332template<>
334{
335 void operator()(ISerializer& serialize, const char* /*name*/, SpatialSubdivision& value)
336 {
337 serialize.NumberFixed_Unbounded("div size", value.m_DivisionSize);
338 serialize.NumberU32_Unbounded("divs w", value.m_DivisionsW);
339 serialize.NumberU32_Unbounded("divs h", value.m_DivisionsH);
340
341 size_t count = value.m_DivisionsH * value.m_DivisionsW;
342 for (size_t i = 0; i < count; ++i)
343 Serializer(serialize, "subdiv items", value.m_Divisions[i].items);
344 }
345
346 void operator()(IDeserializer& serialize, const char* /*name*/, SpatialSubdivision& value)
347 {
348 serialize.NumberFixed_Unbounded("div size", value.m_DivisionSize);
349 serialize.NumberU32_Unbounded("divs w", value.m_DivisionsW);
350 serialize.NumberU32_Unbounded("divs h", value.m_DivisionsH);
351
352 size_t count = value.m_DivisionsW * value.m_DivisionsH;
353 value.Create(count);
354 for (size_t i = 0; i < count; ++i)
355 Serializer(serialize, "subdiv items", value.m_Divisions[i].items);
356 }
357};
358
359
360/**
361 * A basic square subdivision scheme for finding entities in range
362 * More efficient than SpatialSubdivision, but a bit less precise
363 * (so the querier will get more entities to perform tests on).
364 *
365 * Items are stored in vectors in fixed-size divisions.
366 *
367 * Items have a size (min/max values of their axis-aligned bounding box).
368 * If that size is higher than a subdivision's size, they're stored in the "general" vector
369 * This means that if too many objects have a size that's big, it'll end up being slow
370 * We want subdivisions to be as small as possible yet contain as many items as possible.
371 *
372 * It is the caller's responsibility to ensure items are only added once, aren't removed
373 * unless they've been added, etc, and that Move/Remove are called with the same coordinates
374 * originally passed to Add (since this class doesn't remember which divisions an item
375 * occupies).
376 *
377 * TODO: If a unit size were to change, it would need to be updated (that doesn't happen for now)
378 */
380{
381private:
382 static const int SUBDIVISION_SIZE = 20; // bigger than most buildings and entities
383
384 std::vector<entity_id_t> m_OverSizedData;
385 std::vector<entity_id_t>* m_SpatialDivisionsData; // fixed size array of subdivisions
386 size_t m_ArrayWidth; // number of columns in m_SpatialDivisionsData
387
388 inline size_t Index(fixed position) const
389 {
390 return Clamp((position / SUBDIVISION_SIZE).ToInt_RoundToZero(), 0, (int)m_ArrayWidth-1);
391 }
392
393 inline size_t SubdivisionIdx(CFixedVector2D position) const
394 {
395 return Index(position.X) + Index(position.Y)*m_ArrayWidth;
396 }
397
398 /**
399 * Efficiently erase from a vector by swapping with the last element and popping it.
400 * Returns true if the element was found and erased, else returns false.
401 */
402 bool EraseFrom(std::vector<entity_id_t>& vector, entity_id_t item)
403 {
404 std::vector<entity_id_t>::iterator it = std::find(vector.begin(), vector.end(), item);
405 if (it == vector.end())
406 return false;
407
408 *it = vector.back();
409 vector.pop_back();
410 return true;
411 }
412
413public:
416 {
417 }
418
421 {
422 Reset(other.m_ArrayWidth);
424 }
425
427 {
428 delete[] m_SpatialDivisionsData;
429 }
430
431 void Reset(size_t arrayWidth)
432 {
433 delete[] m_SpatialDivisionsData;
434
435 m_ArrayWidth = arrayWidth;
436 m_SpatialDivisionsData = new std::vector<entity_id_t>[m_ArrayWidth*m_ArrayWidth];
437 m_OverSizedData.clear();
438 }
439
440 void Reset(fixed w, fixed h)
441 {
442 ENSURE(w >= fixed::Zero() && h >= fixed::Zero());
443 size_t arrayWidth = std::max((w / SUBDIVISION_SIZE).ToInt_RoundToZero(), (h / SUBDIVISION_SIZE).ToInt_RoundToZero()) + 1;
444 Reset(arrayWidth);
445 }
446
448 {
449 if (this != &other)
450 {
451 Reset(other.m_ArrayWidth);
453 }
454 return *this;
455 }
456
457 bool operator==(const FastSpatialSubdivision& other) const
458 {
459 if (m_ArrayWidth != other.m_ArrayWidth)
460 return false;
461 if (m_OverSizedData != other.m_OverSizedData)
462 return false;
463 for (size_t idx = 0; idx < m_ArrayWidth*m_ArrayWidth; ++idx)
464 if (m_SpatialDivisionsData[idx] != other.m_SpatialDivisionsData[idx])
465 return false;
466 return true;
467 }
468
469 inline bool operator!=(const FastSpatialSubdivision& rhs) const
470 {
471 return !(*this == rhs);
472 }
473
474 /**
475 * Add an item.
476 */
477 void Add(entity_id_t item, CFixedVector2D position, std::uint32_t size)
478 {
479 if (size > SUBDIVISION_SIZE)
480 {
481 if (std::find(m_OverSizedData.begin(), m_OverSizedData.end(), item) == m_OverSizedData.end())
482 m_OverSizedData.push_back(item);
483 }
484 else
485 {
486 std::vector<entity_id_t>& subdivision = m_SpatialDivisionsData[SubdivisionIdx(position)];
487 if (std::find(subdivision.begin(), subdivision.end(), item) == subdivision.end())
488 subdivision.push_back(item);
489 }
490 }
491
492 /**
493 * Remove an item.
494 * Position must be where we expect to find it, or we won't find it.
495 */
496 void Remove(entity_id_t item, CFixedVector2D position, std::uint32_t size)
497 {
498 if (size > SUBDIVISION_SIZE)
500 else
501 {
502 std::vector<entity_id_t>& subdivision = m_SpatialDivisionsData[SubdivisionIdx(position)];
503 EraseFrom(subdivision, item);
504 }
505 }
506
507 /**
508 * Equivalent to Remove() then Add(), but slightly faster.
509 * In particular for big objects nothing needs to be done.
510 */
511 void Move(entity_id_t item, CFixedVector2D oldPosition, CFixedVector2D newPosition, std::uint32_t size)
512 {
513 if (size > SUBDIVISION_SIZE)
514 return;
515 if (SubdivisionIdx(newPosition) == SubdivisionIdx(oldPosition))
516 return;
517
518 std::vector<entity_id_t>& oldSubdivision = m_SpatialDivisionsData[SubdivisionIdx(oldPosition)];
519 if (EraseFrom(oldSubdivision, item))
520 {
521 std::vector<entity_id_t>& newSubdivision = m_SpatialDivisionsData[SubdivisionIdx(newPosition)];
522 newSubdivision.push_back(item);
523 }
524 }
525
526 /**
527 * Returns a (non sorted) list of items that are either in the square or close to it.
528 * It's the responsibility of the querier to do proper distance checking and entity sorting.
529 */
530 void GetInRange(std::vector<entity_id_t>& out, CFixedVector2D posMin, CFixedVector2D posMax) const
531 {
532 size_t minX = Index(posMin.X);
533 size_t minY = Index(posMin.Y);
534 size_t maxX = Index(posMax.X) + 1;
535 size_t maxY = Index(posMax.Y) + 1;
536
537 // Now expand the subdivisions by one so we make sure we've got all elements potentially in range.
538 // Also make sure min >= 0 and max <= width
539 minX = minX > 0 ? minX-1 : 0;
540 minY = minY > 0 ? minY-1 : 0;
541 maxX = maxX < m_ArrayWidth ? maxX+1 : m_ArrayWidth;
542 maxY = maxY < m_ArrayWidth ? maxY+1 : m_ArrayWidth;
543
544 ENSURE(out.empty() && "GetInRange: out is not clean");
545
546 // Add oversized items, they can be anywhere
547 out.insert(out.end(), m_OverSizedData.begin(), m_OverSizedData.end());
548
549 for (size_t Y = minY; Y < maxY; ++Y)
550 {
551 for (size_t X = minX; X < maxX; ++X)
552 {
553 std::vector<entity_id_t>& subdivision = m_SpatialDivisionsData[X + Y*m_ArrayWidth];
554 if (!subdivision.empty())
555 out.insert(out.end(), subdivision.begin(), subdivision.end());
556 }
557 }
558 }
559
560 /**
561 * Returns a (non sorted) list of items that are either in the circle or close to it.
562 * It's the responsibility of the querier to do proper distance checking and entity sorting.
563 */
564 void GetNear(std::vector<entity_id_t>& out, CFixedVector2D pos, entity_pos_t range) const
565 {
566 // Because the subdivision size is rather big wrt typical ranges,
567 // this square over-approximation is hopefully not too bad.
568 CFixedVector2D r(range, range);
569 GetInRange(out, pos - r, pos + r);
570 }
571
572 size_t GetDivisionSize() const
573 {
574 return SUBDIVISION_SIZE;
575 }
576
577 size_t GetWidth() const
578 {
579 return m_ArrayWidth;
580 }
581};
582
583#endif // INCLUDED_SPATIAL
#define X(id)
Definition: CStrIntern.cpp:112
@ Y
Definition: Decompose.h:22
T Clamp(T value, T min, T max)
Definition: MathUtil.h:34
Entity coordinate types.
Helper templates definitions for serializing/deserializing common objects.
void Serializer(S &serialize, const char *name, Args &&... args)
Definition: SerializeTemplates.h:51
Definition: FixedVector2D.h:25
fixed X
Definition: FixedVector2D.h:27
fixed Y
Definition: FixedVector2D.h:27
A simple fixed-point number class.
Definition: Fixed.h:129
static CFixed Zero()
Definition: Fixed.h:140
A basic square subdivision scheme for finding entities in range More efficient than SpatialSubdivisio...
Definition: Spatial.h:380
size_t GetWidth() const
Definition: Spatial.h:577
void GetInRange(std::vector< entity_id_t > &out, CFixedVector2D posMin, CFixedVector2D posMax) const
Returns a (non sorted) list of items that are either in the square or close to it.
Definition: Spatial.h:530
FastSpatialSubdivision()
Definition: Spatial.h:414
size_t GetDivisionSize() const
Definition: Spatial.h:572
size_t Index(fixed position) const
Definition: Spatial.h:388
std::vector< entity_id_t > * m_SpatialDivisionsData
Definition: Spatial.h:385
~FastSpatialSubdivision()
Definition: Spatial.h:426
void Remove(entity_id_t item, CFixedVector2D position, std::uint32_t size)
Remove an item.
Definition: Spatial.h:496
void GetNear(std::vector< entity_id_t > &out, CFixedVector2D pos, entity_pos_t range) const
Returns a (non sorted) list of items that are either in the circle or close to it.
Definition: Spatial.h:564
FastSpatialSubdivision(const FastSpatialSubdivision &other)
Definition: Spatial.h:419
size_t m_ArrayWidth
Definition: Spatial.h:386
size_t SubdivisionIdx(CFixedVector2D position) const
Definition: Spatial.h:393
bool operator!=(const FastSpatialSubdivision &rhs) const
Definition: Spatial.h:469
static const int SUBDIVISION_SIZE
Definition: Spatial.h:382
void Add(entity_id_t item, CFixedVector2D position, std::uint32_t size)
Add an item.
Definition: Spatial.h:477
std::vector< entity_id_t > m_OverSizedData
Definition: Spatial.h:384
void Move(entity_id_t item, CFixedVector2D oldPosition, CFixedVector2D newPosition, std::uint32_t size)
Equivalent to Remove() then Add(), but slightly faster.
Definition: Spatial.h:511
FastSpatialSubdivision & operator=(const FastSpatialSubdivision &other)
Definition: Spatial.h:447
void Reset(size_t arrayWidth)
Definition: Spatial.h:431
bool EraseFrom(std::vector< entity_id_t > &vector, entity_id_t item)
Efficiently erase from a vector by swapping with the last element and popping it.
Definition: Spatial.h:402
bool operator==(const FastSpatialSubdivision &other) const
Definition: Spatial.h:457
void Reset(fixed w, fixed h)
Definition: Spatial.h:440
Deserialization interface; see serialization overview.
Definition: IDeserializer.h:40
virtual void NumberFixed_Unbounded(const char *name, fixed &out)
Definition: IDeserializer.cpp:150
virtual void NumberU32_Unbounded(const char *name, uint32_t &out)
Definition: IDeserializer.cpp:126
Serialization interface; see serialization overview.
Definition: ISerializer.h:126
void NumberU32_Unbounded(const char *name, uint32_t value)
Serialize a number.
Definition: ISerializer.h:176
void NumberFixed_Unbounded(const char *name, fixed value)
Serialize a number.
Definition: ISerializer.h:196
A very basic subdivision scheme for finding items in ranges.
Definition: Spatial.h:49
SubDivisionGrid * m_Divisions
Definition: Spatial.h:75
void GetInRange(std::vector< uint32_t > &out, CFixedVector2D posMin, CFixedVector2D posMax) const
Returns a sorted list of unique items that includes all items within the given axis-aligned square ra...
Definition: Spatial.h:260
uint32_t GetHeight() const
Definition: Spatial.h:118
uint32_t GetI1(entity_pos_t x) const
Definition: Spatial.h:308
entity_pos_t GetDivisionSize() const
Definition: Spatial.h:116
uint32_t GetIndex0(CFixedVector2D pos) const
Definition: Spatial.h:318
void Move(uint32_t item, CFixedVector2D from, CFixedVector2D to)
Convenience function for Move() of individual points.
Definition: Spatial.h:251
uint32_t GetIndex1(CFixedVector2D pos) const
Definition: Spatial.h:323
SpatialSubdivision(const SpatialSubdivision &rhs)
Definition: Spatial.h:89
void Add(uint32_t item, CFixedVector2D to)
Convenience function for Add() of individual points.
Definition: Spatial.h:235
void Remove(uint32_t item, CFixedVector2D fromMin, CFixedVector2D fromMax)
Remove an item with the given 'from' size.
Definition: Spatial.h:192
SpatialSubdivision & operator=(const SpatialSubdivision &rhs)
Definition: Spatial.h:99
uint32_t m_DivisionsH
Definition: Spatial.h:77
uint32_t GetJ1(entity_pos_t z) const
Definition: Spatial.h:313
void Move(uint32_t item, CFixedVector2D fromMin, CFixedVector2D fromMax, CFixedVector2D toMin, CFixedVector2D toMax)
Equivalent to Remove() then Add(), but potentially faster.
Definition: Spatial.h:221
uint32_t GetJ0(entity_pos_t z) const
Definition: Spatial.h:303
~SpatialSubdivision()
Definition: Spatial.h:85
void Create(size_t count)
Definition: Spatial.h:120
void GetNear(std::vector< uint32_t > &out, CFixedVector2D pos, entity_pos_t range) const
Returns a sorted list of unique items that includes all items within the given circular distance of t...
Definition: Spatial.h:285
entity_pos_t m_DivisionSize
Definition: Spatial.h:74
bool operator!=(const SpatialSubdivision &rhs) const
Definition: Spatial.h:151
uint32_t GetI0(entity_pos_t x) const
Definition: Spatial.h:298
uint32_t GetWidth() const
Definition: Spatial.h:117
void Add(uint32_t item, CFixedVector2D toMin, CFixedVector2D toMax)
Add an item with the given 'to' size.
Definition: Spatial.h:170
void Remove(uint32_t item, CFixedVector2D from)
Convenience function for Remove() of individual points.
Definition: Spatial.h:243
uint32_t m_DivisionsW
Definition: Spatial.h:76
bool operator==(const SpatialSubdivision &rhs) const
Equivalence test (ignoring order of items within each subdivision)
Definition: Spatial.h:129
void Reset(entity_pos_t maxX, entity_pos_t maxZ, entity_pos_t divisionSize)
Definition: Spatial.h:156
SpatialSubdivision()
Definition: Spatial.h:82
#define ENSURE(expr)
ensure the expression <expr> evaluates to non-zero.
Definition: debug.h:222
std::uint32_t entity_id_t
Entity ID type.
Definition: Entity.h:29
void operator()(IDeserializer &serialize, const char *, SpatialSubdivision &value)
Definition: Spatial.h:346
void operator()(ISerializer &serialize, const char *, SpatialSubdivision &value)
Definition: Spatial.h:335
Definition: SerializeTemplates.h:42
Definition: Spatial.h:51
void push_back(uint32_t value)
Definition: Spatial.h:54
std::vector< uint32_t > items
Definition: Spatial.h:52
void copy_items_at_end(std::vector< uint32_t > &out) const
Definition: Spatial.h:67
void erase(int index)
Definition: Spatial.h:59
static void out(const wchar_t *fmt,...)
Definition: wdbg_sym.cpp:407