--- id: wiki-2026-0508-spatial-partitioning title: Spatial Partitioning category: 10_Wiki/Topics status: verified canonical_id: self aliases: [bvh, octree, kd-tree, spatial-hashing, broadphase] duplicate_of: none source_trust_level: A confidence_score: 0.9 verification_status: applied tags: [graphics, game-engine, data-structures, collision] raw_sources: [] last_reinforced: 2026-05-10 github_commit: pending tech_stack: language: cpp framework: unity-unreal --- # Spatial Partitioning ## 매 한 줄 > **"매 N 의 brute force → log N 의 spatial query"**. Spatial partitioning 은 매 3D/2D space 를 hierarchical 의 chunk 로 분할하여 collision / raycast / nearest-neighbor query 를 가속 — 매 game engine, ray tracing, physics, particle 의 핵심. 매 2026 의 RTX hardware BVH 가 game-changer. ## 매 핵심 ### 매 주요 구조 - **Uniform Grid**: 매 fixed-size cell 의 array — 매 simple, 균일 분포에 best. - **Spatial Hash**: 매 grid 의 hash 변형 — 매 unbounded space 처리. - **Quadtree (2D) / Octree (3D)**: 매 recursive 4/8 분할 — 매 sparse 분포 efficient. - **KD-Tree**: 매 axis-aligned plane 의 split — 매 nearest-neighbor 의 best. - **BVH (Bounding Volume Hierarchy)**: 매 AABB 의 tree — 매 ray tracing 의 standard, RTX 의 hardware accel. - **BSP Tree**: 매 arbitrary plane 의 split — 매 Quake-era, 현재 의 obsolete. ### 매 Trade-off - **Build cost**: BVH > Octree > Grid (매 dynamic scene 의 Grid 가 cheap). - **Query cost**: BVH ≈ KD-tree (log N) > Octree > Grid. - **Memory**: Grid 가 dense 분포에 최악, BVH 가 balanced. - **Update cost**: Grid 의 O(1) per object, BVH 의 refit / rebuild 필요. ### 매 응용 1. **Broadphase collision** — 매 physics 의 candidate pair 추출. 2. **Ray tracing** — 매 BVH traversal (RTX, Embree, OptiX). 3. **Frustum culling** — 매 octree 의 view 외 chunk skip. 4. **Nearest neighbor** — 매 KD-tree (kNN, point cloud). 5. **Particle system** — 매 spatial hash 의 SPH fluid. ## 💻 패턴 ### Uniform Grid (2D, fast broadphase) ```cpp struct Grid { float cellSize; std::unordered_map> cells; int64_t key(int x, int y) { return (int64_t)x << 32 | (uint32_t)y; } void insert(Entity* e) { int x = (int)(e->pos.x / cellSize); int y = (int)(e->pos.y / cellSize); cells[key(x, y)].push_back(e); } std::vector queryRadius(Vec2 c, float r) { std::vector out; int minX = (int)((c.x - r) / cellSize), maxX = (int)((c.x + r) / cellSize); int minY = (int)((c.y - r) / cellSize), maxY = (int)((c.y + r) / cellSize); for (int x = minX; x <= maxX; ++x) for (int y = minY; y <= maxY; ++y) for (auto* e : cells[key(x, y)]) if ((e->pos - c).lengthSq() <= r * r) out.push_back(e); return out; } }; ``` ### Octree (recursive) ```cpp struct OctreeNode { AABB bounds; std::vector objects; std::unique_ptr children[8]; static constexpr int MAX = 8, MAX_DEPTH = 6; void insert(Entity* e, int depth = 0) { if (children[0]) { int idx = childIndex(e->pos); if (idx >= 0) { children[idx]->insert(e, depth + 1); return; } } objects.push_back(e); if (objects.size() > MAX && depth < MAX_DEPTH) subdivide(); } void subdivide() { /* split bounds, redistribute objects */ } int childIndex(Vec3 p) { /* return 0..7 or -1 if straddles */ } }; ``` ### BVH (top-down SAH build) ```cpp struct BVHNode { AABB box; int left, right; std::vector tris; }; int buildBVH(std::vector& tris, int begin, int end) { BVHNode node; node.box = computeAABB(tris, begin, end); if (end - begin <= 4) { node.tris.assign(tris.begin()+begin, tris.begin()+end); return addNode(node); } int axis = node.box.longestAxis(); int mid = surfaceAreaHeuristicSplit(tris, begin, end, axis); node.left = buildBVH(tris, begin, mid); node.right = buildBVH(tris, mid, end); return addNode(node); } ``` ### KD-Tree (kNN) ```cpp struct KDNode { Point p; int axis; KDNode *left, *right; }; void kNN(KDNode* n, Point q, int k, std::priority_queue<...>& heap) { if (!n) return; float d = dist(n->p, q); if (heap.size() < k || d < heap.top().d) heap.push({n->p, d}); float diff = q[n->axis] - n->p[n->axis]; KDNode *near = diff < 0 ? n->left : n->right; KDNode *far = diff < 0 ? n->right : n->left; kNN(near, q, k, heap); if (heap.size() < k || diff*diff < heap.top().d) kNN(far, q, k, heap); } ``` ### RTX Hardware BVH (DXR / Vulkan RT) ```hlsl RaytracingAccelerationStructure scene : register(t0); RayDesc ray = { origin, 0.0, dir, 1000.0 }; RayQuery q; q.TraceRayInline(scene, 0, 0xFF, ray); q.Proceed(); if (q.CommittedStatus() == COMMITTED_TRIANGLE_HIT) { /* hit */ } ``` ### Unity Job + Burst spatial hash ```csharp [BurstCompile] struct HashJob : IJobParallelFor { [ReadOnly] public NativeArray positions; public NativeParallelMultiHashMap.ParallelWriter hash; public float cellSize; public void Execute(int i) { int3 c = (int3)math.floor(positions[i] / cellSize); hash.Add((c.x*73856093) ^ (c.y*19349663) ^ (c.z*83492791), i); } } ``` ## 매 결정 기준 | 상황 | Approach | |---|---| | Dynamic 균일 entity (RTS) | Uniform Grid / Spatial Hash | | Static scene + ray tracing | BVH (SAH build) | | Sparse 큰 world | Octree | | Point cloud kNN | KD-Tree | | GPU ray tracing | Hardware BVH (DXR/Vulkan RT) | | Particle SPH | Spatial Hash (Burst) | **기본값**: dynamic 의 Spatial Hash, static 의 BVH. 매 GPU 가능 하면 hardware BVH 우선. ## 🔗 Graph - 부모: [[Computational Geometry (Frontend)]] - 변형: [[BVH]] · [[Octree]] · [[KD-Tree]] - 응용: [[Collision-Detection]] · [[Frustum Culling]] · [[K-Nearest-Neighbors-K-NN|Nearest-Neighbor-Search]] - Adjacent: [[Physics-Engine]] · [[GPU-Compute]] · [[ECS]] ## 🤖 LLM 활용 **언제**: structure choice 추천, Burst/SIMD 의 boilerplate 생성, debug visualization code. **언제 X**: production engine 의 from-scratch BVH — 매 Embree / OptiX 의 battle-tested 사용. ## ❌ 안티패턴 - **Brute force O(N²)**: 100+ entities 에서 frame drop. 매 Grid 만으로도 충분히 해결. - **Octree 의 over-depth**: 작은 scene 의 8-level 의 cache miss. Depth cap. - **BVH 의 매 frame full rebuild**: dynamic 의 refit 사용. 큰 변화 시 만 rebuild. - **Grid cell size mismatch**: object size 와 mismatch 시 either cell 의 폭주 or query 의 over-scan. cellSize ≈ avg object diameter. - **Cache-unfriendly traversal**: pointer chase. SoA / linearized BVH (flat array) 사용. ## 🧪 검증 / 검증 - Verified (Real-Time Collision Detection by Ericson, PBRT v4, NVIDIA OptiX docs, Unity DOTS Physics). - 신뢰도 A. ## 🕓 Changelog | 날짜 | 변경 | |---|---| | 2026-05-08 | Phase 1 | | 2026-05-10 | Manual cleanup — spatial structures + RTX hardware BVH |