载入中...
搜索中...
未找到
Bounds.h
浏览该文件的文档.
1#pragma once
2
3#include <algorithm>
4#include <cmath>
5#include <cstdint>
6
7namespace eve::spatial {
8
9struct AABB2 {
10 float minX = 0.f;
11 float minY = 0.f;
12 float maxX = 0.f;
13 float maxY = 0.f;
14
15 float width() const { return maxX - minX; }
16 float height() const { return maxY - minY; }
17 float centerX() const { return (minX + maxX) * 0.5f; }
18 float centerY() const { return (minY + maxY) * 0.5f; }
19
20 bool valid() const { return minX <= maxX && minY <= maxY; }
21
22 bool containsPoint(float x, float y) const {
23 return x >= minX && x <= maxX && y >= minY && y <= maxY;
24 }
25
26 bool containsAABB(const AABB2 &o) const {
27 return o.minX >= minX && o.maxX <= maxX && o.minY >= minY && o.maxY <= maxY;
28 }
29
30 bool intersectsAABB(const AABB2 &o) const {
31 return minX <= o.maxX && maxX >= o.minX && minY <= o.maxY && maxY >= o.minY;
32 }
33
34 bool intersectsCircle(float cx, float cy, float radius) const {
35 const float nearestX = std::clamp(cx, minX, maxX);
36 const float nearestY = std::clamp(cy, minY, maxY);
37 const float dx = cx - nearestX;
38 const float dy = cy - nearestY;
39 return dx * dx + dy * dy <= radius * radius;
40 }
41};
42
43struct AABB3 {
44 float minX = 0.f;
45 float minY = 0.f;
46 float minZ = 0.f;
47 float maxX = 0.f;
48 float maxY = 0.f;
49 float maxZ = 0.f;
50
51 float width() const { return maxX - minX; }
52 float height() const { return maxY - minY; }
53 float depth() const { return maxZ - minZ; }
54 float centerX() const { return (minX + maxX) * 0.5f; }
55 float centerY() const { return (minY + maxY) * 0.5f; }
56 float centerZ() const { return (minZ + maxZ) * 0.5f; }
57
58 bool valid() const { return minX <= maxX && minY <= maxY && minZ <= maxZ; }
59
60 bool containsPoint(float x, float y, float z) const {
61 return x >= minX && x <= maxX && y >= minY && y <= maxY && z >= minZ && z <= maxZ;
62 }
63
64 bool containsAABB(const AABB3 &o) const {
65 return o.minX >= minX && o.maxX <= maxX && o.minY >= minY && o.maxY <= maxY &&
66 o.minZ >= minZ && o.maxZ <= maxZ;
67 }
68
69 bool intersectsAABB(const AABB3 &o) const {
70 return minX <= o.maxX && maxX >= o.minX && minY <= o.maxY && maxY >= o.minY &&
71 minZ <= o.maxZ && maxZ >= o.minZ;
72 }
73
74 bool intersectsSphere(float cx, float cy, float cz, float radius) const {
75 const float nearestX = std::clamp(cx, minX, maxX);
76 const float nearestY = std::clamp(cy, minY, maxY);
77 const float nearestZ = std::clamp(cz, minZ, maxZ);
78 const float dx = cx - nearestX;
79 const float dy = cy - nearestY;
80 const float dz = cz - nearestZ;
81 return dx * dx + dy * dy + dz * dz <= radius * radius;
82 }
83};
84
85inline AABB2 makeAABB2(float minX, float minY, float maxX, float maxY) {
86 if (minX > maxX) std::swap(minX, maxX);
87 if (minY > maxY) std::swap(minY, maxY);
88 return AABB2{minX, minY, maxX, maxY};
89}
90
91inline AABB3 makeAABB3(float minX, float minY, float minZ, float maxX, float maxY, float maxZ) {
92 if (minX > maxX) std::swap(minX, maxX);
93 if (minY > maxY) std::swap(minY, maxY);
94 if (minZ > maxZ) std::swap(minZ, maxZ);
95 return AABB3{minX, minY, minZ, maxX, maxY, maxZ};
96}
97
99inline uint64_t cellKey2(int cx, int cy) {
100 return (uint64_t(uint32_t(cx)) << 32) | uint64_t(uint32_t(cy));
101}
102
103inline uint64_t cellKey3(int cx, int cy, int cz) {
104 // 21 bits per axis packed into 63 bits (signed via uint cast).
105 const uint64_t x = uint64_t(uint32_t(cx) & 0x1fffffu);
106 const uint64_t y = uint64_t(uint32_t(cy) & 0x1fffffu);
107 const uint64_t z = uint64_t(uint32_t(cz) & 0x1fffffu);
108 return (x << 42) | (y << 21) | z;
109}
110
111} // namespace eve::spatial
float cx
Definition CardTypes.cpp:31
float cy
Definition CardTypes.cpp:32
int y
Definition Grass.cpp:135
int z
Definition Grass.cpp:135
int x
Definition Grass.cpp:135
AABB2 makeAABB2(float minX, float minY, float maxX, float maxY)
Definition Bounds.h:85
AABB3 makeAABB3(float minX, float minY, float minZ, float maxX, float maxY, float maxZ)
Definition Bounds.h:91
uint64_t cellKey3(int cx, int cy, int cz)
Definition Bounds.h:103
uint64_t cellKey2(int cx, int cy)
Integer cell key for spatial hashing (stable across platforms for reasonable ranges).
Definition Bounds.h:99
bool containsPoint(float x, float y) const
Definition Bounds.h:22
bool valid() const
Definition Bounds.h:20
float centerX() const
Definition Bounds.h:17
bool containsAABB(const AABB2 &o) const
Definition Bounds.h:26
bool intersectsAABB(const AABB2 &o) const
Definition Bounds.h:30
bool intersectsCircle(float cx, float cy, float radius) const
Definition Bounds.h:34
float centerY() const
Definition Bounds.h:18
float width() const
Definition Bounds.h:15
float height() const
Definition Bounds.h:16
float height() const
Definition Bounds.h:52
float centerZ() const
Definition Bounds.h:56
float centerX() const
Definition Bounds.h:54
bool containsPoint(float x, float y, float z) const
Definition Bounds.h:60
bool intersectsAABB(const AABB3 &o) const
Definition Bounds.h:69
bool valid() const
Definition Bounds.h:58
bool containsAABB(const AABB3 &o) const
Definition Bounds.h:64
float width() const
Definition Bounds.h:51
float depth() const
Definition Bounds.h:53
bool intersectsSphere(float cx, float cy, float cz, float radius) const
Definition Bounds.h:74
float centerY() const
Definition Bounds.h:55