Skip to content

Commit

Permalink
updated
Browse files Browse the repository at this point in the history
  • Loading branch information
xelatihy committed Jan 30, 2024
1 parent 07dc2dd commit d523a61
Showing 1 changed file with 30 additions and 33 deletions.
63 changes: 30 additions & 33 deletions libs/yocto/yocto_raycasting.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ struct intersection3f {
int instance = invalidid;
int element = invalidid;
vec2f uv = {0, 0};
float distance = 0;
float distance = flt_max;
bool hit = false;

// Default constructor for no intersection.
Expand Down Expand Up @@ -265,30 +265,23 @@ inline ray3f camera_ray(
// -----------------------------------------------------------------------------
namespace yocto {

// Primitive intersection
struct prim_intersection {
vec2f uv = {0, 0};
float distance = flt_max;
bool hit = false;
};

// Intersect a ray with a point (approximate)
inline prim_intersection intersect_point(const ray3f& ray, vec3f p, float r);
inline intersection3f intersect_point(const ray3f& ray, vec3f p, float r);

// Intersect a ray with a line
inline prim_intersection intersect_line(
inline intersection3f intersect_line(
const ray3f& ray, vec3f p0, vec3f p1, float r0, float r1);

// Intersect a ray with a triangle
inline prim_intersection intersect_triangle(
inline intersection3f intersect_triangle(
const ray3f& ray, vec3f p0, vec3f p1, vec3f p2);

// Intersect a ray with a quad.
inline prim_intersection intersect_quad(
inline intersection3f intersect_quad(
const ray3f& ray, vec3f p0, vec3f p1, vec3f p2, vec3f p3);

// Intersect a ray with a sphere
inline prim_intersection intersect_sphere(const ray3f& ray, vec3f p, float r);
inline intersection3f intersect_sphere(const ray3f& ray, vec3f p, float r);

// Intersect a ray with a axis-aligned bounding box
inline bool intersect_bbox(const ray3f& ray, const bbox3f& bbox);
Expand All @@ -305,26 +298,26 @@ inline bool intersect_bbox(
namespace yocto {

// Check if a point overlaps a position pos withint a maximum distance dist_max.
inline prim_intersection overlap_point(
inline intersection3f overlap_point(
vec3f pos, float dist_max, vec3f p, float r);

// Compute the closest line uv to a give position pos.
inline float closestuv_line(vec3f pos, vec3f p0, vec3f p1);

// Check if a line overlaps a position pos withint a maximum distance dist_max.
inline prim_intersection overlap_line(
inline intersection3f overlap_line(
vec3f pos, float dist_max, vec3f p0, vec3f p1, float r0, float r1);

// Compute the closest triangle uv to a give position pos.
inline vec2f closestuv_triangle(vec3f pos, vec3f p0, vec3f p1, vec3f p2);

// Check if a triangle overlaps a position pos withint a maximum distance
// dist_max.
inline prim_intersection overlap_triangle(vec3f pos, float dist_max, vec3f p0,
inline intersection3f overlap_triangle(vec3f pos, float dist_max, vec3f p0,
vec3f p1, vec3f p2, float r0, float r1, float r2);

// Check if a quad overlaps a position pos withint a maximum distance dist_max.
inline prim_intersection overlap_quad(vec3f pos, float dist_max, vec3f p0,
inline intersection3f overlap_quad(vec3f pos, float dist_max, vec3f p0,
vec3f p1, vec3f p2, vec3f p3, float r0, float r1, float r2, float r3);

// Check if a bbox overlaps a position pos withint a maximum distance dist_max.
Expand Down Expand Up @@ -798,7 +791,7 @@ inline ray3f camera_ray(const frame3f& frame, float lens, float aspect,
namespace yocto {

// Intersect a ray with a point (approximate)
inline prim_intersection intersect_point(const ray3f& ray, vec3f p, float r) {
inline intersection3f intersect_point(const ray3f& ray, vec3f p, float r) {
// find parameter for line-point minimum distance
auto w = p - ray.o;
auto t = dot(w, ray.d) / dot(ray.d, ray.d);
Expand All @@ -812,11 +805,11 @@ inline prim_intersection intersect_point(const ray3f& ray, vec3f p, float r) {
if (dot(prp, prp) > r * r) return {};

// intersection occurred: set params and exit
return {{0, 0}, t, true};
return intersection3f{{0, 0}, t};
}

// Intersect a ray with a line
inline prim_intersection intersect_line(
inline intersection3f intersect_line(
const ray3f& ray, vec3f p0, vec3f p1, float r0, float r1) {
// setup intersection params
auto u = ray.d;
Expand Down Expand Up @@ -856,11 +849,11 @@ inline prim_intersection intersect_line(
if (d2 > r * r) return {};

// intersection occurred: set params and exit
return {{s, sqrt(d2) / r}, t, true};
return intersection3f{{s, sqrt(d2) / r}, t};
}

// Intersect a ray with a triangle
inline prim_intersection intersect_triangle(
inline intersection3f intersect_triangle(
const ray3f& ray, vec3f p0, vec3f p1, vec3f p2) {
// compute triangle edges
auto edge1 = p1 - p0;
Expand Down Expand Up @@ -890,21 +883,23 @@ inline prim_intersection intersect_triangle(
if (t < ray.tmin || t > ray.tmax) return {};

// intersection occurred: set params and exit
return {{u, v}, t, true};
return intersection3f{{u, v}, t};
}

// Intersect a ray with a quad.
inline prim_intersection intersect_quad(
inline intersection3f intersect_quad(
const ray3f& ray, vec3f p0, vec3f p1, vec3f p2, vec3f p3) {
if (p2 == p3) return intersect_triangle(ray, p0, p1, p3);
auto isec1 = intersect_triangle(ray, p0, p1, p3);
auto isec2 = intersect_triangle(ray, p2, p3, p1);
if (isec2.hit) isec2.uv = 1 - isec2.uv;
if (isec1.hit && !isec2.hit) return isec1;
if (!isec1.hit && isec2.hit) return isec2;
return isec1.distance < isec2.distance ? isec1 : isec2;
}

// Intersect a ray with a sphere
inline prim_intersection intersect_sphere(const ray3f& ray, vec3f p, float r) {
inline intersection3f intersect_sphere(const ray3f& ray, vec3f p, float r) {
// compute parameters
auto a = dot(ray.d, ray.d);
auto b = 2 * dot(ray.o - p, ray.d);
Expand All @@ -930,7 +925,7 @@ inline prim_intersection intersect_sphere(const ray3f& ray, vec3f p, float r) {
auto v = acos(clamp(plocal.z, -1.0f, 1.0f)) / pif;

// intersection occurred: set params and exit
return {{u, v}, t, true};
return intersection3f{{u, v}, t};
}

// Intersect a ray with a axis-aligned bounding box
Expand Down Expand Up @@ -970,11 +965,11 @@ inline bool intersect_bbox(
namespace yocto {

// Check if a point overlaps a position pos withint a maximum distance dist_max.
inline prim_intersection overlap_point(
inline intersection3f overlap_point(
vec3f pos, float dist_max, vec3f p, float r) {
auto d2 = dot(pos - p, pos - p);
if (d2 > (dist_max + r) * (dist_max + r)) return {};
return {{0, 0}, sqrt(d2), true};
return intersection3f{{0, 0}, sqrt(d2)};
}

// Compute the closest line uv to a give position pos.
Expand All @@ -989,7 +984,7 @@ inline float closestuv_line(vec3f pos, vec3f p0, vec3f p1) {
}

// Check if a line overlaps a position pos withint a maximum distance dist_max.
inline prim_intersection overlap_line(
inline intersection3f overlap_line(
vec3f pos, float dist_max, vec3f p0, vec3f p1, float r0, float r1) {
auto u = closestuv_line(pos, p0, p1);
// Compute projected position from the clamped t d = a + t * ab;
Expand All @@ -999,7 +994,7 @@ inline prim_intersection overlap_line(
// check distance
if (d2 > (dist_max + r) * (dist_max + r)) return {};
// done
return {{u, 0}, sqrt(d2), true};
return intersection3f{{u, 0}, sqrt(d2)};
}

// Compute the closest triangle uv to a give position pos.
Expand Down Expand Up @@ -1047,23 +1042,25 @@ inline vec2f closestuv_triangle(vec3f pos, vec3f p0, vec3f p1, vec3f p2) {

// Check if a triangle overlaps a position pos withint a maximum distance
// dist_max.
inline prim_intersection overlap_triangle(vec3f pos, float dist_max, vec3f p0,
inline intersection3f overlap_triangle(vec3f pos, float dist_max, vec3f p0,
vec3f p1, vec3f p2, float r0, float r1, float r2) {
auto cuv = closestuv_triangle(pos, p0, p1, p2);
auto p = p0 * (1 - cuv.x - cuv.y) + p1 * cuv.x + p2 * cuv.y;
auto r = r0 * (1 - cuv.x - cuv.y) + r1 * cuv.x + r2 * cuv.y;
auto dd = dot(p - pos, p - pos);
if (dd > (dist_max + r) * (dist_max + r)) return {};
return {cuv, sqrt(dd), true};
return intersection3f{cuv, sqrt(dd)};
}

// Check if a quad overlaps a position pos withint a maximum distance dist_max.
inline prim_intersection overlap_quad(vec3f pos, float dist_max, vec3f p0,
inline intersection3f overlap_quad(vec3f pos, float dist_max, vec3f p0,
vec3f p1, vec3f p2, vec3f p3, float r0, float r1, float r2, float r3) {
if (p2 == p3) return overlap_triangle(pos, dist_max, p0, p1, p3, r0, r1, r2);
auto isec1 = overlap_triangle(pos, dist_max, p0, p1, p3, r0, r1, r2);
auto isec2 = overlap_triangle(pos, dist_max, p2, p3, p1, r2, r3, r1);
if (isec2.hit) isec2.uv = 1 - isec2.uv;
if (isec1.hit && !isec2.hit) return isec1;
if (!isec1.hit && isec2.hit) return isec2;
return isec1.distance < isec2.distance ? isec1 : isec2;
}

Expand Down

0 comments on commit d523a61

Please sign in to comment.