载入中...
搜索中...
未找到
Bounds.h
浏览该文件的文档.
1#pragma once
2
3#include <algorithm>
4#include <cmath>
5#include <cstdint>
6
7namespace eve::spatial {
8
10struct AABB2 {
11 float minX = 0.f;
12 float minY = 0.f;
13 float maxX = 0.f;
14 float maxY = 0.f;
15
17 float width() const { return maxX - minX; }
19 float height() const { return maxY - minY; }
21 float centerX() const { return (minX + maxX) * 0.5f; }
23 float centerY() const { return (minY + maxY) * 0.5f; }
24
26 bool valid() const { return minX <= maxX && minY <= maxY; }
27
29 bool containsPoint(float x, float y) const {
30 return x >= minX && x <= maxX && y >= minY && y <= maxY;
31 }
32
34 bool containsAABB(const AABB2 &o) const {
35 return o.minX >= minX && o.maxX <= maxX && o.minY >= minY && o.maxY <= maxY;
36 }
37
39 bool intersectsAABB(const AABB2 &o) const {
40 return minX <= o.maxX && maxX >= o.minX && minY <= o.maxY && maxY >= o.minY;
41 }
42
44 bool intersectsCircle(float cx, float cy, float radius) const {
45 const float nearestX = std::clamp(cx, minX, maxX);
46 const float nearestY = std::clamp(cy, minY, maxY);
47 const float dx = cx - nearestX;
48 const float dy = cy - nearestY;
49 return dx * dx + dy * dy <= radius * radius;
50 }
51};
52
54struct AABB3 {
55 float minX = 0.f;
56 float minY = 0.f;
57 float minZ = 0.f;
58 float maxX = 0.f;
59 float maxY = 0.f;
60 float maxZ = 0.f;
61
63 float width() const { return maxX - minX; }
65 float height() const { return maxY - minY; }
67 float depth() const { return maxZ - minZ; }
69 float centerX() const { return (minX + maxX) * 0.5f; }
71 float centerY() const { return (minY + maxY) * 0.5f; }
73 float centerZ() const { return (minZ + maxZ) * 0.5f; }
74
76 bool valid() const { return minX <= maxX && minY <= maxY && minZ <= maxZ; }
77
79 bool containsPoint(float x, float y, float z) const {
80 return x >= minX && x <= maxX && y >= minY && y <= maxY && z >= minZ && z <= maxZ;
81 }
82
84 bool containsAABB(const AABB3 &o) const {
85 return o.minX >= minX && o.maxX <= maxX && o.minY >= minY && o.maxY <= maxY &&
86 o.minZ >= minZ && o.maxZ <= maxZ;
87 }
88
90 bool intersectsAABB(const AABB3 &o) const {
91 return minX <= o.maxX && maxX >= o.minX && minY <= o.maxY && maxY >= o.minY &&
92 minZ <= o.maxZ && maxZ >= o.minZ;
93 }
94
96 bool intersectsSphere(float cx, float cy, float cz, float radius) const {
97 const float nearestX = std::clamp(cx, minX, maxX);
98 const float nearestY = std::clamp(cy, minY, maxY);
99 const float nearestZ = std::clamp(cz, minZ, maxZ);
100 const float dx = cx - nearestX;
101 const float dy = cy - nearestY;
102 const float dz = cz - nearestZ;
103 return dx * dx + dy * dy + dz * dz <= radius * radius;
104 }
105};
106
108inline AABB2 makeAABB2(float minX, float minY, float maxX, float maxY) {
109 if (minX > maxX) std::swap(minX, maxX);
110 if (minY > maxY) std::swap(minY, maxY);
111 return AABB2{minX, minY, maxX, maxY};
112}
113
115inline AABB3 makeAABB3(float minX, float minY, float minZ, float maxX, float maxY, float maxZ) {
116 if (minX > maxX) std::swap(minX, maxX);
117 if (minY > maxY) std::swap(minY, maxY);
118 if (minZ > maxZ) std::swap(minZ, maxZ);
119 return AABB3{minX, minY, minZ, maxX, maxY, maxZ};
120}
121
123inline uint64_t cellKey2(int cx, int cy) {
124 return (uint64_t(uint32_t(cx)) << 32) | uint64_t(uint32_t(cy));
125}
126
128inline uint64_t cellKey3(int cx, int cy, int cz) {
129 // 21 bits per axis packed into 63 bits (signed via uint cast).
130 const uint64_t x = uint64_t(uint32_t(cx) & 0x1fffffu);
131 const uint64_t y = uint64_t(uint32_t(cy) & 0x1fffffu);
132 const uint64_t z = uint64_t(uint32_t(cz) & 0x1fffffu);
133 return (x << 42) | (y << 21) | z;
134}
135
136} // namespace eve::spatial
float y
Definition AnimClip.cpp:738
float x
Definition AnimClip.cpp:738
float z
Definition AnimClip.cpp:738
float cx
Definition CardTypes.cpp:33
float cy
Definition CardTypes.cpp:34
float radius
float dz
float dy
float dx
AABB2 makeAABB2(float minX, float minY, float maxX, float maxY)
Builds a normalized 2D AABB (swaps inverted mins/maxes).
Definition Bounds.h:108
AABB3 makeAABB3(float minX, float minY, float minZ, float maxX, float maxY, float maxZ)
Builds a normalized 3D AABB (swaps inverted mins/maxes).
Definition Bounds.h:115
uint64_t cellKey3(int cx, int cy, int cz)
Packs 3D cell indices into a 63-bit hash key (21 bits/axis).
Definition Bounds.h:128
uint64_t cellKey2(int cx, int cy)
Integer cell key for spatial hashing (stable across platforms for reasonable ranges).
Definition Bounds.h:123
Axis-aligned 2D bounding box (min/max inclusive).
Definition Bounds.h:10
bool containsPoint(float x, float y) const
True if the point lies inside or on the boundary.
Definition Bounds.h:29
bool valid() const
True when min <= max on every axis.
Definition Bounds.h:26
float centerX() const
Center X of the box.
Definition Bounds.h:21
bool containsAABB(const AABB2 &o) const
True if the other box is fully inside this one.
Definition Bounds.h:34
bool intersectsAABB(const AABB2 &o) const
True if the boxes overlap (inclusive edges).
Definition Bounds.h:39
bool intersectsCircle(float cx, float cy, float radius) const
True if the circle overlaps this 2D box.
Definition Bounds.h:44
float centerY() const
Center Y of the box.
Definition Bounds.h:23
float width() const
Extent along X (max - min).
Definition Bounds.h:17
float height() const
Extent along Y (max - min).
Definition Bounds.h:19
Axis-aligned 3D bounding box (min/max inclusive).
Definition Bounds.h:54
float height() const
Extent along Y (max - min).
Definition Bounds.h:65
float centerZ() const
Center Z of the box.
Definition Bounds.h:73
float centerX() const
Center X of the box.
Definition Bounds.h:69
bool containsPoint(float x, float y, float z) const
True if the point lies inside or on the boundary.
Definition Bounds.h:79
bool intersectsAABB(const AABB3 &o) const
True if the boxes overlap (inclusive edges).
Definition Bounds.h:90
bool valid() const
True when min <= max on every axis.
Definition Bounds.h:76
bool containsAABB(const AABB3 &o) const
True if the other box is fully inside this one.
Definition Bounds.h:84
float width() const
Extent along X (max - min).
Definition Bounds.h:63
float depth() const
Extent along Z (max - min).
Definition Bounds.h:67
bool intersectsSphere(float cx, float cy, float cz, float radius) const
True if the sphere overlaps this 3D box.
Definition Bounds.h:96
float centerY() const
Center Y of the box.
Definition Bounds.h:71