Skip to content

Commit

Permalink
Simpler bvh (#1301)
Browse files Browse the repository at this point in the history
  • Loading branch information
xelatihy authored Oct 19, 2021
1 parent a8bd3df commit 2db5f16
Show file tree
Hide file tree
Showing 8 changed files with 256 additions and 222 deletions.
4 changes: 2 additions & 2 deletions apps/ymesh/ymesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ struct sculpt_params {

struct sculpt_state {
// data structures
shape_bvh bvh = {};
bvh_tree bvh = {};
hash_grid grid = {};
vector<vector<int>> adjacencies = {};
geodesic_solver solver = {};
Expand Down Expand Up @@ -1048,7 +1048,7 @@ static scene_data make_sculptscene(const shape_data& ioshape_) {
}

// To make the stroke sampling (position, normal) following the mouse
static pair<vector<shape_point>, vec2f> sample_stroke(const shape_bvh& bvh,
static pair<vector<shape_point>, vec2f> sample_stroke(const bvh_tree& bvh,
const shape_data& shape, const vec2f& last_uv, const vec2f& mouse_uv,
const camera_data& camera, const sculpt_params& params) {
// helper
Expand Down
43 changes: 22 additions & 21 deletions docs/yocto/yocto_bvh.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,62 +44,63 @@ by setting the `embree` flag to true.

```cpp
auto scene = scene_data{...}; // make a complete scene
auto bvh = build_bvh(scene); // build a BVH
auto embree = build_bvh(scene,true,true); // use Embree
auto bvh = make_bvh(scene); // build a BVH
auto embree = make_bvh(scene,true,true); // use Embree
```
Use `update_bvh(bvh,shape)` to update a shape BVH, and
`update_bvh(bvh,scene,updated_instances,updated_shapes)` to update a scene BVH,
where we indicate the indices of the instances and shapes that have beed modified.
where we indicate the indices of the instances and shapes that have been modified.
Updating works ony for change to instance frames and shapes positions.
For changes like adding or removing elements, the BVH has to be built again.
```cpp
auto scene = scene_data{...}; // make a complete scene
auto bvh = build_bvh(scene); // build a BVH
auto bvh = make_bvh(scene); // build a BVH
auto shapes = update_shapes(scene); // updates some shapes
auto instances = update_instances(scene); // updates some instances
update_bvh(bvh, scene, shapes, instances);// update bvh
```

## Ray intersection

Use `intersect_bvh(bvh,scene,ray)` and `intersect_bvh(bvh,shape,ray)` to
Use `intersect_scene(bvh,scene,ray)` and `intersect_shape(bvh,shape,ray)` to
compute the ray-scene and ray-shape intersection respectively, and
`intersect_bvh(bvh,scene,instance,ray)` to intersect a single scene instance.
These functions return a `bvh_intersection` that includes a `hit` flag,
the `instance` index, the shape `element` index and shape element `uv`s and
the intersection `distance`. Results values are set only if `hit` is true.
`intersect_instance(bvh,scene,instance,ray)` to intersect a single scene instance.
These functions return a `scene_intersection` or a `shape_intersection` that
includes a `hit` flag, the `instance` index, the shape `element` index and
shape element `uv`s and the intersection `distance`.
Results values are set only if `hit` is true.
By default Yocto/Bvh computes the closet intersection, but this can be
relaxed to accept any intersection, for shadow rays, by passing an optional flag.

```cpp
auto isec = intersect_bvh(bvh,scene,ray); // ray-scene intersection
if (isec.hit) { // check intersection
handle_intersection(isec.instance, // work on intersection data
auto isec = intersect_scene(bvh,scene,ray); // ray-scene intersection
if (isec.hit) { // check intersection
handle_intersection(isec.instance, // work on intersection data
isec.element, isec.uv, isec.distance);
}
auto isecv = intersect_bvh(bvh,scene,ray,true); // check any intersection
auto isecv = intersect_scene(bvh,scene,ray,true); // check any intersection
if (!isecv.hit) { // check for not intersection
handle_unoccluded(...); // handle unoccluded case
}
```

## Point overlap

Use `overlap_bvh(bvh,scene,position,max_distance)` and
`overlap_bvh(bvh,shape,position,max_distance)` to compute the scene or
Use `overlap_scene(bvh,scene,position,max_distance)` and
`overlap_shape(bvh,shape,position,max_distance)` to compute the scene or
shape element closest to a given point and within a maximum distance.
Use `overlap_bvh(bvh,scene,instance,position,max_distance)` to test
a single scene instance. These functions return a `bvh_intersection`
as the intersection ones.
Use `overlap_instance(bvh,scene,instance,position,max_distance)` to test
a single scene instance. These functions return a `scene_intersection` or a
`shape_intersection`, as the intersection ones.
By default Yocto/Bvh computes the closet element, but this can be
relaxed to accept any element, by passing an optional flag.

```cpp
auto isec = overlap_bvh(bvh,scene,point,dist); // closest element
if (isec.hit) { // check intersection
handle_intersection(isec.instance, // work on intersection data
auto isec = overlap_scene(bvh,scene,point,dist); // closest element
if (isec.hit) { // check intersection
handle_intersection(isec.instance, // work on intersection data
isec.element, isec.uv, isec.distance);
}
```
Loading

0 comments on commit 2db5f16

Please sign in to comment.