Files
2nd/10_Wiki/Topics/AI_and_ML/Spatial Partitioning.md
T
koriweb d8a80f6272 chore(wiki): dangling 링크 canonical 정규화 (768파일/1200건)
이름만 다른(표기 변형) [[위키링크]]를 대상 문서의 canonical 제목으로 치환해
끊겼던 1,200개 링크를 연결. 제목/파일명 정규화 일치만 적용하고 별칭 매칭은
과병합 위험으로 제외(애매성 가드). 원본은 _link_reconcile_backup/ 에 백업.
도구: Datacollect/scripts/link_reconcile_apply.mjs

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-08 12:24:15 +09:00

7.0 KiB

id, title, category, status, canonical_id, aliases, duplicate_of, source_trust_level, confidence_score, verification_status, tags, raw_sources, last_reinforced, github_commit, tech_stack
id title category status canonical_id aliases duplicate_of source_trust_level confidence_score verification_status tags raw_sources last_reinforced github_commit tech_stack
wiki-2026-0508-spatial-partitioning Spatial Partitioning 10_Wiki/Topics verified self
bvh
octree
kd-tree
spatial-hashing
broadphase
none A 0.9 applied
graphics
game-engine
data-structures
collision
2026-05-10 pending
language framework
cpp 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)

struct Grid {
    float cellSize;
    std::unordered_map<int64_t, std::vector<Entity*>> 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<Entity*> queryRadius(Vec2 c, float r) {
        std::vector<Entity*> 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)

struct OctreeNode {
    AABB bounds;
    std::vector<Entity*> objects;
    std::unique_ptr<OctreeNode> 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)

struct BVHNode { AABB box; int left, right; std::vector<int> tris; };

int buildBVH(std::vector<Tri>& 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)

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)

RaytracingAccelerationStructure scene : register(t0);
RayDesc ray = { origin, 0.0, dir, 1000.0 };
RayQuery<RAY_FLAG_CULL_BACK_FACING_TRIANGLES> q;
q.TraceRayInline(scene, 0, 0xFF, ray);
q.Proceed();
if (q.CommittedStatus() == COMMITTED_TRIANGLE_HIT) { /* hit */ }

Unity Job + Burst spatial hash

[BurstCompile]
struct HashJob : IJobParallelFor {
    [ReadOnly] public NativeArray<float3> positions;
    public NativeParallelMultiHashMap<int, int>.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

🤖 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