18#ifndef INCLUDED_SPATIAL
19#define INCLUDED_SPATIAL
43 items.push_back(value);
49 if ((
int)
items.size() > 1)
83 for (
size_t i = 0; i < n; ++i)
97 for (
size_t i = 0; i < n; ++i)
130 std::sort(a.begin(), a.end());
131 std::sort(b.begin(), b.end());
140 return !(*
this == rhs);
159 ENSURE(toMin.
X <= toMax.
X && toMin.
Y <= toMax.
Y);
165 for (
u32 j = j0; j <= j1; ++j)
167 for (
u32 i = i0; i <= i1; ++i)
181 ENSURE(fromMin.
X <= fromMax.
X && fromMin.
Y <= fromMax.
Y);
187 for (
u32 j = j0; j <= j1; ++j)
189 for (
u32 i = i0; i <= i1; ++i)
192 int size = div.
items.size();
193 for (
int n = 0; n < size; ++n)
195 if (div.
items[n] == item)
214 Remove(item, fromMin, fromMax);
215 Add(item, toMin, toMax);
240 Move(item, from, from, to, to);
250 ENSURE(posMin.
X <= posMax.
X && posMin.
Y <= posMax.
Y);
256 for (
u32 j = j0; j <= j1; ++j)
258 for (
u32 i = i0; i <= i1; ++i)
264 std::sort(
out.begin(),
out.end());
265 out.erase(std::unique(
out.begin(),
out.end()),
out.end());
329 for (
size_t i = 0; i < count; ++i)
341 for (
size_t i = 0; i < count; ++i)
391 std::vector<entity_id_t>::iterator it = std::find(vector.begin(), vector.end(), item);
392 if (it == vector.end())
458 return !(*
this == rhs);
474 if (std::find(subdivision.begin(), subdivision.end(), item) == subdivision.end())
475 subdivision.push_back(item);
509 newSubdivision.push_back(item);
519 size_t minX =
Index(posMin.
X);
520 size_t minY =
Index(posMin.
Y);
521 size_t maxX =
Index(posMax.
X) + 1;
522 size_t maxY =
Index(posMax.
Y) + 1;
526 minX = minX > 0 ? minX-1 : 0;
527 minY = minY > 0 ? minY-1 : 0;
531 ENSURE(
out.empty() &&
"GetInRange: out is not clean");
536 for (
size_t Y = minY;
Y < maxY; ++
Y)
538 for (
size_t X = minX;
X < maxX; ++
X)
541 if (!subdivision.empty())
542 out.insert(
out.end(), subdivision.begin(), subdivision.end());
#define X(id)
Definition: CStrIntern.cpp:110
@ Y
Definition: Decompose.h:22
T Clamp(T value, T min, T max)
Definition: MathUtil.h:32
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:120
static CFixed Zero()
Definition: Fixed.h:131
A basic square subdivision scheme for finding entities in range More efficient than SpatialSubdivisio...
Definition: Spatial.h:367
size_t GetWidth() const
Definition: Spatial.h:564
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:517
FastSpatialSubdivision()
Definition: Spatial.h:401
size_t GetDivisionSize() const
Definition: Spatial.h:559
size_t Index(fixed position) const
Definition: Spatial.h:375
std::vector< entity_id_t > * m_SpatialDivisionsData
Definition: Spatial.h:372
void Remove(entity_id_t item, CFixedVector2D position, u32 size)
Remove an item.
Definition: Spatial.h:483
~FastSpatialSubdivision()
Definition: Spatial.h:413
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:551
FastSpatialSubdivision(const FastSpatialSubdivision &other)
Definition: Spatial.h:406
size_t m_ArrayWidth
Definition: Spatial.h:373
void Move(entity_id_t item, CFixedVector2D oldPosition, CFixedVector2D newPosition, u32 size)
Equivalent to Remove() then Add(), but slightly faster.
Definition: Spatial.h:498
size_t SubdivisionIdx(CFixedVector2D position) const
Definition: Spatial.h:380
bool operator!=(const FastSpatialSubdivision &rhs) const
Definition: Spatial.h:456
static const int SUBDIVISION_SIZE
Definition: Spatial.h:369
std::vector< entity_id_t > m_OverSizedData
Definition: Spatial.h:371
void Add(entity_id_t item, CFixedVector2D position, u32 size)
Add an item.
Definition: Spatial.h:464
FastSpatialSubdivision & operator=(const FastSpatialSubdivision &other)
Definition: Spatial.h:434
void Reset(size_t arrayWidth)
Definition: Spatial.h:418
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:389
bool operator==(const FastSpatialSubdivision &other) const
Definition: Spatial.h:444
void Reset(fixed w, fixed h)
Definition: Spatial.h:427
Deserialization interface; see serialization overview.
Definition: IDeserializer.h:35
virtual void NumberFixed_Unbounded(const char *name, fixed &out)
Definition: IDeserializer.cpp:148
virtual void NumberU32_Unbounded(const char *name, uint32_t &out)
Definition: IDeserializer.cpp:124
Serialization interface; see serialization overview.
Definition: ISerializer.h:121
void NumberU32_Unbounded(const char *name, uint32_t value)
Serialize a number.
Definition: ISerializer.h:171
void NumberFixed_Unbounded(const char *name, fixed value)
Serialize a number.
Definition: ISerializer.h:191
A very basic subdivision scheme for finding items in ranges.
Definition: Spatial.h:36
SubDivisionGrid * m_Divisions
Definition: Spatial.h:62
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:247
uint32_t GetHeight() const
Definition: Spatial.h:105
uint32_t GetI1(entity_pos_t x) const
Definition: Spatial.h:295
entity_pos_t GetDivisionSize() const
Definition: Spatial.h:103
uint32_t GetIndex0(CFixedVector2D pos) const
Definition: Spatial.h:305
void Move(uint32_t item, CFixedVector2D from, CFixedVector2D to)
Convenience function for Move() of individual points.
Definition: Spatial.h:238
uint32_t GetIndex1(CFixedVector2D pos) const
Definition: Spatial.h:310
SpatialSubdivision(const SpatialSubdivision &rhs)
Definition: Spatial.h:76
void Add(uint32_t item, CFixedVector2D to)
Convenience function for Add() of individual points.
Definition: Spatial.h:222
void Remove(uint32_t item, CFixedVector2D fromMin, CFixedVector2D fromMax)
Remove an item with the given 'from' size.
Definition: Spatial.h:179
SpatialSubdivision & operator=(const SpatialSubdivision &rhs)
Definition: Spatial.h:86
uint32_t m_DivisionsH
Definition: Spatial.h:64
uint32_t GetJ1(entity_pos_t z) const
Definition: Spatial.h:300
void Move(uint32_t item, CFixedVector2D fromMin, CFixedVector2D fromMax, CFixedVector2D toMin, CFixedVector2D toMax)
Equivalent to Remove() then Add(), but potentially faster.
Definition: Spatial.h:208
uint32_t GetJ0(entity_pos_t z) const
Definition: Spatial.h:290
~SpatialSubdivision()
Definition: Spatial.h:72
void Create(size_t count)
Definition: Spatial.h:107
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:272
entity_pos_t m_DivisionSize
Definition: Spatial.h:61
bool operator!=(const SpatialSubdivision &rhs) const
Definition: Spatial.h:138
uint32_t GetI0(entity_pos_t x) const
Definition: Spatial.h:285
uint32_t GetWidth() const
Definition: Spatial.h:104
void Add(uint32_t item, CFixedVector2D toMin, CFixedVector2D toMax)
Add an item with the given 'to' size.
Definition: Spatial.h:157
void Remove(uint32_t item, CFixedVector2D from)
Convenience function for Remove() of individual points.
Definition: Spatial.h:230
uint32_t m_DivisionsW
Definition: Spatial.h:63
bool operator==(const SpatialSubdivision &rhs) const
Equivalence test (ignoring order of items within each subdivision)
Definition: Spatial.h:116
void Reset(entity_pos_t maxX, entity_pos_t maxZ, entity_pos_t divisionSize)
Definition: Spatial.h:143
SpatialSubdivision()
Definition: Spatial.h:69
#define UNUSED(param)
mark a function parameter as unused and avoid the corresponding compiler warning.
Definition: code_annotation.h:40
#define ENSURE(expr)
ensure the expression <expr> evaluates to non-zero.
Definition: debug.h:277
u32 entity_id_t
Entity ID type.
Definition: Entity.h:29
void operator()(IDeserializer &serialize, const char *name, SpatialSubdivision &value)
Definition: Spatial.h:333
void operator()(ISerializer &serialize, const char *name, SpatialSubdivision &value)
Definition: Spatial.h:322
Definition: SerializeTemplates.h:42
void push_back(uint32_t value)
Definition: Spatial.h:41
std::vector< uint32_t > items
Definition: Spatial.h:39
void copy_items_at_end(std::vector< uint32_t > &out) const
Definition: Spatial.h:54
void erase(int index)
Definition: Spatial.h:46
uint32_t u32
Definition: types.h:39
static void out(const wchar_t *fmt,...)
Definition: wdbg_sym.cpp:407
unsigned int uint32_t
Definition: wposix_types.h:53