Skip to content

Commit

Permalink
Cleanup intersections
Browse files Browse the repository at this point in the history
  • Loading branch information
xelatihy committed Jan 30, 2024
1 parent 940fdbc commit c5ff247
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 74 deletions.
64 changes: 32 additions & 32 deletions libs/yocto/yocto_raycasting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ static void refit_elements_bvh(bvh_tree& bvh, BBox&& bbox) {

// Intersect ray with a bvh.
template <typename Intersect>
static shape_intersection intersect_elements_bvh(const bvh_tree& bvh,
static intersection3f intersect_elements_bvh(const bvh_tree& bvh,
const ray3f& ray_, bool find_any, Intersect&& intersect_element) {
// check empty
if (bvh.nodes.empty()) return {};
Expand All @@ -331,7 +331,7 @@ static shape_intersection intersect_elements_bvh(const bvh_tree& bvh,
node_stack[node_cur++] = 0;

// shared variables
auto intersection = shape_intersection{};
auto intersection = intersection3f{};

// copy ray to modify it
auto ray = ray_;
Expand Down Expand Up @@ -367,9 +367,9 @@ static shape_intersection intersect_elements_bvh(const bvh_tree& bvh,
auto primitive = bvh.primitives[node.start + idx];
auto eintersection = intersect_element(primitive, ray);
if (!eintersection.hit) continue;
intersection = {
primitive, eintersection.uv, eintersection.distance, true};
ray.tmax = eintersection.distance;
intersection = {invalidid, primitive, eintersection.uv,
eintersection.distance, true};
ray.tmax = eintersection.distance;
}
}

Expand All @@ -382,7 +382,7 @@ static shape_intersection intersect_elements_bvh(const bvh_tree& bvh,

// Intersect ray with a bvh.
template <typename Overlap>
static shape_intersection overlap_elements_bvh(const bvh_tree& bvh, vec3f pos,
static intersection3f overlap_elements_bvh(const bvh_tree& bvh, vec3f pos,
float max_distance, bool find_any, Overlap&& overlap_element) {
// check if empty
if (bvh.nodes.empty()) return {};
Expand All @@ -393,7 +393,7 @@ static shape_intersection overlap_elements_bvh(const bvh_tree& bvh, vec3f pos,
node_stack[node_cur++] = 0;

// hit
auto intersection = shape_intersection{};
auto intersection = intersection3f{};

// walking stack
while (node_cur) {
Expand All @@ -414,8 +414,8 @@ static shape_intersection overlap_elements_bvh(const bvh_tree& bvh, vec3f pos,
auto primitive = bvh.primitives[node.start + idx];
auto eintersection = overlap_element(primitive, pos, max_distance);
if (!eintersection.hit) continue;
intersection = {
primitive, eintersection.uv, eintersection.distance, true};
intersection = {invalidid, primitive, eintersection.uv,
eintersection.distance, true};
max_distance = eintersection.distance;
}
}
Expand Down Expand Up @@ -508,7 +508,7 @@ void refit_quads_bvh(
}

// Intersect ray with a bvh.
shape_intersection intersect_points_bvh(const bvh_tree& bvh,
intersection3f intersect_points_bvh(const bvh_tree& bvh,
const vector<int>& points, const vector<vec3f>& positions,
const vector<float>& radius, const ray3f& ray, bool find_any) {
return intersect_elements_bvh(
Expand All @@ -517,7 +517,7 @@ shape_intersection intersect_points_bvh(const bvh_tree& bvh,
return intersect_point(ray, positions[p], radius[p]);
});
}
shape_intersection intersect_lines_bvh(const bvh_tree& bvh,
intersection3f intersect_lines_bvh(const bvh_tree& bvh,
const vector<vec2i>& lines, const vector<vec3f>& positions,
const vector<float>& radius, const ray3f& ray, bool find_any) {
return intersect_elements_bvh(
Expand All @@ -527,7 +527,7 @@ shape_intersection intersect_lines_bvh(const bvh_tree& bvh,
ray, positions[l.x], positions[l.y], radius[l.x], radius[l.y]);
});
}
shape_intersection intersect_triangles_bvh(const bvh_tree& bvh,
intersection3f intersect_triangles_bvh(const bvh_tree& bvh,
const vector<vec3i>& triangles, const vector<vec3f>& positions,
const ray3f& ray, bool find_any) {
return intersect_elements_bvh(
Expand All @@ -537,7 +537,7 @@ shape_intersection intersect_triangles_bvh(const bvh_tree& bvh,
ray, positions[t.x], positions[t.y], positions[t.z]);
});
}
shape_intersection intersect_quads_bvh(const bvh_tree& bvh,
intersection3f intersect_quads_bvh(const bvh_tree& bvh,
const vector<vec4i>& quads, const vector<vec3f>& positions,
const ray3f& ray, bool find_any) {
return intersect_elements_bvh(
Expand All @@ -552,7 +552,7 @@ shape_intersection intersect_quads_bvh(const bvh_tree& bvh,
// max distance, returning either the closest or any overlap depending on
// `find_any`. Returns the point distance, the instance id, the shape element
// index and the element barycentric coordinates.
shape_intersection overlap_points_bvh(const bvh_tree& bvh,
intersection3f overlap_points_bvh(const bvh_tree& bvh,
const vector<int>& points, const vector<vec3f>& positions,
const vector<float>& radius, vec3f pos, float max_distance, bool find_any) {
return overlap_elements_bvh(bvh, pos, max_distance, find_any,
Expand All @@ -561,7 +561,7 @@ shape_intersection overlap_points_bvh(const bvh_tree& bvh,
return overlap_point(pos, max_distance, positions[p], radius[p]);
});
}
shape_intersection overlap_lines_bvh(const bvh_tree& bvh,
intersection3f overlap_lines_bvh(const bvh_tree& bvh,
const vector<vec2i>& lines, const vector<vec3f>& positions,
const vector<float>& radius, vec3f pos, float max_distance, bool find_any) {
return overlap_elements_bvh(bvh, pos, max_distance, find_any,
Expand All @@ -571,7 +571,7 @@ shape_intersection overlap_lines_bvh(const bvh_tree& bvh,
radius[l.x], radius[l.y]);
});
}
shape_intersection overlap_triangles_bvh(const bvh_tree& bvh,
intersection3f overlap_triangles_bvh(const bvh_tree& bvh,
const vector<vec3i>& triangles, const vector<vec3f>& positions,
const vector<float>& radius, vec3f pos, float max_distance, bool find_any) {
return overlap_elements_bvh(bvh, pos, max_distance, find_any,
Expand All @@ -583,7 +583,7 @@ shape_intersection overlap_triangles_bvh(const bvh_tree& bvh,
radius[t.z]);
});
}
shape_intersection overlap_quads_bvh(const bvh_tree& bvh,
intersection3f overlap_quads_bvh(const bvh_tree& bvh,
const vector<vec4i>& quads, const vector<vec3f>& positions,
const vector<float>& radius, vec3f pos, float max_distance, bool find_any) {
return overlap_elements_bvh(bvh, pos, max_distance, find_any,
Expand Down Expand Up @@ -712,7 +712,7 @@ bbox3f shape_bounds(const shape_data& shape) {
// -----------------------------------------------------------------------------
namespace yocto {

shape_intersection intersect_shape_bvh(const shape_bvh& sbvh,
intersection3f intersect_shape_bvh(const shape_bvh& sbvh,
const shape_data& shape, const ray3f& ray_, bool find_any) {
if (!shape.points.empty()) {
return intersect_points_bvh(
Expand Down Expand Up @@ -815,8 +815,8 @@ intersection3f intersect_instance_bvh(const scene_bvh& sbvh,
namespace yocto {

// Intersect ray with a bvh.
shape_intersection overlap_shape_bvh(const shape_bvh& sbvh,
const shape_data& shape, vec3f pos, float max_distance, bool find_any) {
intersection3f overlap_shape_bvh(const shape_bvh& sbvh, const shape_data& shape,
vec3f pos, float max_distance, bool find_any) {
// get bvh tree
auto& bvh = sbvh.bvh;

Expand All @@ -829,7 +829,7 @@ shape_intersection overlap_shape_bvh(const shape_bvh& sbvh,
node_stack[node_cur++] = 0;

// intersection
auto intersection = shape_intersection{};
auto intersection = intersection3f{};

// walking stack
while (node_cur != 0) {
Expand All @@ -852,8 +852,8 @@ shape_intersection overlap_shape_bvh(const shape_bvh& sbvh,
auto eintersection = overlap_point(
pos, max_distance, shape.positions[p], shape.radius[p]);
if (!eintersection.hit) continue;
intersection = {
primitive, eintersection.uv, eintersection.distance, true};
intersection = {invalidid, primitive, eintersection.uv,
eintersection.distance, true};
max_distance = eintersection.distance;
}
} else if (!shape.lines.empty()) {
Expand All @@ -864,8 +864,8 @@ shape_intersection overlap_shape_bvh(const shape_bvh& sbvh,
shape.positions[l.x], shape.positions[l.y], shape.radius[l.x],
shape.radius[l.y]);
if (!eintersection.hit) continue;
intersection = {
primitive, eintersection.uv, eintersection.distance, true};
intersection = {invalidid, primitive, eintersection.uv,
eintersection.distance, true};
max_distance = eintersection.distance;
}
} else if (!shape.triangles.empty()) {
Expand All @@ -876,8 +876,8 @@ shape_intersection overlap_shape_bvh(const shape_bvh& sbvh,
shape.positions[t.x], shape.positions[t.y], shape.positions[t.z],
shape.radius[t.x], shape.radius[t.y], shape.radius[t.z]);
if (!eintersection.hit) continue;
intersection = {
primitive, eintersection.uv, eintersection.distance, true};
intersection = {invalidid, primitive, eintersection.uv,
eintersection.distance, true};
max_distance = eintersection.distance;
}
} else if (!shape.quads.empty()) {
Expand All @@ -889,8 +889,8 @@ shape_intersection overlap_shape_bvh(const shape_bvh& sbvh,
shape.positions[q.w], shape.radius[q.x], shape.radius[q.y],
shape.radius[q.z], shape.radius[q.w]);
if (!eintersection.hit) continue;
intersection = {
primitive, eintersection.uv, eintersection.distance, true};
intersection = {invalidid, primitive, eintersection.uv,
eintersection.distance, true};
max_distance = eintersection.distance;
}
}
Expand Down Expand Up @@ -1225,7 +1225,7 @@ void refit_scene_ebvh(scene_ebvh& sbvh, const scene_data& scene,
// Intersect ray with a bvh returning either the first or any intersection
// depending on `find_any`. Returns the ray distance , the instance id,
// the shape element index and the element barycentric coordinates.
shape_intersection intersect_shape_ebvh(const shape_ebvh& sbvh,
intersection3f intersect_shape_ebvh(const shape_ebvh& sbvh,
const shape_data& shape, const ray3f& ray, bool find_any) {
RTCRayHit embree_ray;
embree_ray.ray.org_x = ray.o.x;
Expand All @@ -1246,7 +1246,7 @@ shape_intersection intersect_shape_ebvh(const shape_ebvh& sbvh,
auto element = (int)embree_ray.hit.primID;
auto uv = vec2f{embree_ray.hit.u, embree_ray.hit.v};
auto distance = embree_ray.ray.tfar;
return {element, uv, distance, true};
return {invalidid, element, uv, distance, true};
}

intersection3f intersect_scene_ebvh(const scene_ebvh& sbvh,
Expand Down Expand Up @@ -1309,7 +1309,7 @@ void update_scene_ebvh(scene_ebvh& sbvh, const scene_data& scene,
}

// Not implemented
shape_intersection intersect_shape_ebvh(const shape_ebvh& sbvh,
intersection3f intersect_shape_ebvh(const shape_ebvh& sbvh,
const shape_data& shape, const ray3f& ray, bool find_any) {
throw embree_error{"Embree not available"};
}
Expand Down
Loading

0 comments on commit c5ff247

Please sign in to comment.