---
id: wiki-2026-0508-bvh
title: BVH
category: 10_Wiki/Topics
status: verified
canonical_id: self
aliases: [P-Reinforce-AUTO-D211FC, Bounding Volume Hierarchy]
duplicate_of: none
source_trust_level: A
confidence_score: 0.9
verification_status: applied
tags: [graphics, raytracing, datastructure, performance]
raw_sources: []
last_reinforced: 2026-05-10
github_commit: pending
tech_stack:
  language: rust
  framework: none
---

# BVH

## 매 한 줄
> **"매 ray-object intersection 의 O(N) → O(log N) 변환의 표준 자료구조."**. Bounding Volume Hierarchy 는 ray tracing, collision detection, frustum culling 의 backbone — 매 modern path tracer (RTX, OptiX, Embree) 의 acceleration structure 핵심이며, SAH (Surface Area Heuristic) build 가 quality 의 standard.

## 매 핵심

### 매 Build Strategy
- **Median split**: simple, fast build, mediocre traversal.
- **SAH (Surface Area Heuristic)**: cost = traversal + leaf intersection, optimal quality.
- **HLBVH / LBVH**: GPU-friendly Morton-code build.
- **PLOC**: parallel locally-ordered clustering, modern GPU SOTA.

### 매 Traversal
- Stack-based DFS (CPU).
- Stackless / restart trail (GPU register-friendly).
- Wide BVH (BVH4, BVH8) — SIMD-friendly child arrays.

### 매 응용
1. Path tracing (Embree, OptiX, RTX hardware BVH).
2. Physics broadphase (Bullet, PhysX).
3. Three.js raycast acceleration (three-mesh-bvh).
4. WebGPU ray queries.

## 💻 패턴

### AABB Struct
```rust
#[derive(Copy, Clone)]
struct Aabb { min: [f32; 3], max: [f32; 3] }

impl Aabb {
    fn surface_area(&self) -> f32 {
        let d = [self.max[0]-self.min[0], self.max[1]-self.min[1], self.max[2]-self.min[2]];
        2.0 * (d[0]*d[1] + d[1]*d[2] + d[2]*d[0])
    }
    fn union(a: Aabb, b: Aabb) -> Aabb {
        Aabb {
            min: [a.min[0].min(b.min[0]), a.min[1].min(b.min[1]), a.min[2].min(b.min[2])],
            max: [a.max[0].max(b.max[0]), a.max[1].max(b.max[1]), a.max[2].max(b.max[2])],
        }
    }
}
```

### Slab Ray-AABB Test
```rust
fn ray_aabb(o: [f32;3], inv_d: [f32;3], box_: &Aabb) -> Option<f32> {
    let mut tmin = 0.0_f32;
    let mut tmax = f32::INFINITY;
    for i in 0..3 {
        let t1 = (box_.min[i] - o[i]) * inv_d[i];
        let t2 = (box_.max[i] - o[i]) * inv_d[i];
        tmin = tmin.max(t1.min(t2));
        tmax = tmax.min(t1.max(t2));
    }
    if tmax >= tmin.max(0.0) { Some(tmin) } else { None }
}
```

### SAH Cost
```rust
fn sah_cost(left: &Aabb, n_left: usize, right: &Aabb, n_right: usize, parent: &Aabb) -> f32 {
    const C_TRAV: f32 = 1.0;
    const C_ISECT: f32 = 1.5;
    let inv_pa = 1.0 / parent.surface_area();
    C_TRAV + C_ISECT * (left.surface_area() * n_left as f32 + right.surface_area() * n_right as f32) * inv_pa
}
```

### Top-Down SAH Build (sketch)
```rust
fn build(prims: &mut [Prim]) -> Box<Node> {
    if prims.len() <= 4 { return Box::new(Node::Leaf(prims.to_vec())); }
    let (axis, split, _cost) = best_sah_split(prims);
    prims.select_nth_unstable_by(split, |a, b| a.centroid[axis].partial_cmp(&b.centroid[axis]).unwrap());
    let (l, r) = prims.split_at_mut(split);
    Box::new(Node::Internal(build(l), build(r)))
}
```

### Stack Traversal
```rust
fn traverse(root: &Node, ray: &Ray) -> Option<Hit> {
    let mut stack = vec![root];
    let mut closest: Option<Hit> = None;
    while let Some(n) = stack.pop() {
        match n {
            Node::Leaf(prims) => for p in prims { if let Some(h) = p.intersect(ray) { closest = Some(h.min_or(closest)); } },
            Node::Internal(l, r) => { stack.push(r); stack.push(l); }
        }
    }
    closest
}
```

### LBVH Morton Build
```rust
fn morton3d(x: u32, y: u32, z: u32) -> u32 {
    fn spread(mut v: u32) -> u32 {
        v = (v | v << 16) & 0x030000FF;
        v = (v | v << 8) & 0x0300F00F;
        v = (v | v << 4) & 0x030C30C3;
        v = (v | v << 2) & 0x09249249;
        v
    }
    spread(x) | (spread(y) << 1) | (spread(z) << 2)
}
```

## 매 결정 기준
| 상황 | BVH 변종 |
|---|---|
| Static scene, CPU PT | SAH BVH2 |
| Dynamic scene | Refit + occasional rebuild |
| GPU PT | Wide BVH (BVH4/8) + LBVH/PLOC |
| Animated chars | Two-level BVH (TLAS+BLAS) |
| Web (three.js) | three-mesh-bvh (SAH) |

**기본값**: SAH BVH2 for CPU; BVH8 + PLOC for GPU.

## 🔗 Graph
- 부모: [[Acceleration Structure]] · [[Spatial Data Structure]]
- 변형: [[KD-Tree]] · [[Octree]] · [[Grid]]
- 응용: [[Ray Tracing]] · [[Collision Detection]] · [[Frustum Culling]]
- Adjacent: [[SAH]] · [[Morton Code]] · [[OptiX]]

## 🤖 LLM 활용
**언제**: explain SAH math, generate boilerplate AABB/traversal code.
**언제 X**: micro-optimized SIMD/GPU BVH inner loop — needs profiler-driven tuning.

## ❌ 안티패턴
- **Median split for production PT**: 10-30% slower traversal vs SAH.
- **Recursive traversal on GPU**: stack overflow in registers — use iterative.
- **Refit-only forever**: quality degrades; periodic rebuild.
- **Per-triangle leaf**: cache-unfriendly; pack 4-8 prims/leaf.

## 🧪 검증 / 중복
- Verified (PBRT 4th ed, Embree paper, Wald 2007 SAH).
- 신뢰도 A.

## 🕓 Changelog
| 날짜 | 변경 |
|---|---|
| 2026-05-08 | Phase 1 |
| 2026-05-10 | Manual cleanup — full content with SAH/LBVH patterns |
