Skip to content

Commit

Permalink
Cleanup overlap
Browse files Browse the repository at this point in the history
  • Loading branch information
xelatihy committed Jan 30, 2024
1 parent 07c009a commit ac75fc9
Showing 1 changed file with 55 additions and 48 deletions.
103 changes: 55 additions & 48 deletions libs/yocto/yocto_raycasting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ static intersection3f intersect_instances_bvh(const bvh_tree& bvh,
return intersection;
}

// Intersect ray with a bvh.
// Overlap a point with a bvh.
template <typename Overlap>
static intersection3f overlap_elements_bvh(const bvh_tree& bvh, vec3f pos,
float max_distance, bool find_any, Overlap&& overlap_element) {
Expand Down Expand Up @@ -486,6 +486,53 @@ static intersection3f overlap_elements_bvh(const bvh_tree& bvh, vec3f pos,
return intersection;
}

// Overlap a point with a bvh.
template <typename Overlap>
static intersection3f overlap_instances_bvh(const bvh_tree& bvh, vec3f pos,
float max_distance, bool find_any, Overlap&& overlap_element) {
// check if empty
if (bvh.nodes.empty()) return {};

// node stack
auto node_stack = array<int, 64>{};
auto node_cur = 0;
node_stack[node_cur++] = 0;

// intersection
auto intersection = intersection3f{};

// walking stack
while (node_cur != 0) {
// grab node
auto& node = bvh.nodes[node_stack[--node_cur]];

// intersect bbox
if (!overlap_bbox(pos, max_distance, node.bbox)) continue;

// intersect node, switching based on node type
// for each type, iterate over the the primitive list
if (node.internal) {
// internal node
node_stack[node_cur++] = node.start + 0;
node_stack[node_cur++] = node.start + 1;
} else {
for (auto idx : range(node.num)) {
auto primitive = bvh.primitives[node.start + idx];
auto sintersection = overlap_element(primitive, pos, max_distance);
if (!sintersection.hit) continue;
intersection = {primitive, sintersection.element, sintersection.uv,
sintersection.distance};
max_distance = sintersection.distance;
}
}

// check for early exit
if (find_any && intersection.hit) return intersection;
}

return intersection;
}

} // namespace yocto

// -----------------------------------------------------------------------------
Expand Down Expand Up @@ -842,53 +889,13 @@ intersection3f overlap_shape_bvh(const shape_bvh& sbvh, const shape_data& shape,
// Intersect ray with a bvh.
intersection3f overlap_scene_bvh(const scene_bvh& sbvh, const scene_data& scene,
vec3f pos, float max_distance, bool find_any) {
// get instances bvh
auto& bvh = sbvh.bvh;

// check if empty
if (bvh.nodes.empty()) return {};

// node stack
auto node_stack = array<int, 64>{};
auto node_cur = 0;
node_stack[node_cur++] = 0;

// intersection
auto intersection = intersection3f{};

// walking stack
while (node_cur != 0) {
// grab node
auto& node = bvh.nodes[node_stack[--node_cur]];

// intersect bbox
if (!overlap_bbox(pos, max_distance, node.bbox)) continue;

// intersect node, switching based on node type
// for each type, iterate over the the primitive list
if (node.internal) {
// internal node
node_stack[node_cur++] = node.start + 0;
node_stack[node_cur++] = node.start + 1;
} else {
for (auto idx : range(node.num)) {
auto primitive = bvh.primitives[node.start + idx];
auto& instance_ = scene.instances[primitive];
auto inv_pos = transform_point(inverse(instance_.frame, true), pos);
auto sintersection = overlap_shape_bvh(sbvh.shapes[instance_.shape],
scene.shapes[instance_.shape], inv_pos, max_distance, find_any);
if (!sintersection.hit) continue;
intersection = {primitive, sintersection.element, sintersection.uv,
sintersection.distance};
max_distance = sintersection.distance;
}
}

// check for early exit
if (find_any && intersection.hit) return intersection;
}

return intersection;
return overlap_instances_bvh(sbvh.bvh, pos, max_distance, find_any,
[&](int idx, const vec3f& pos, float max_distance) {
auto& instance = scene.instances[idx];
auto inv_pos = transform_point(inverse(instance.frame, true), pos);
return overlap_shape_bvh(sbvh.shapes[instance.shape],
scene.shapes[instance.shape], inv_pos, max_distance, find_any);
});
}

#if 0
Expand Down

0 comments on commit ac75fc9

Please sign in to comment.