virtualx-engine/thirdparty/embree/kernels/geometry/disc_intersector.h

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

270 lines
9.2 KiB
C++
Raw Normal View History

// Copyright 2009-2021 Intel Corporation
2021-04-20 18:38:09 +02:00
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include "../common/ray.h"
#include "../common/scene_points.h"
#include "curve_intersector_precalculations.h"
namespace embree
{
namespace isa
{
template<int M>
struct DiscIntersectorHitM
{
__forceinline DiscIntersectorHitM() {}
__forceinline DiscIntersectorHitM(const vfloat<M>& u, const vfloat<M>& v, const vfloat<M>& t, const Vec3vf<M>& Ng)
: vu(u), vv(v), vt(t), vNg(Ng)
{
}
__forceinline void finalize() {}
2024-02-24 12:40:55 +01:00
__forceinline Vec2f uv(const size_t i) const {
2021-04-20 18:38:09 +02:00
return Vec2f(vu[i], vv[i]);
}
2024-02-24 12:40:55 +01:00
__forceinline Vec2vf<M> uv() const {
return Vec2vf<M>(vu, vv);
}
__forceinline float t(const size_t i) const {
2021-04-20 18:38:09 +02:00
return vt[i];
}
2024-02-24 12:40:55 +01:00
__forceinline vfloat<M> t() const {
return vt;
}
__forceinline Vec3fa Ng(const size_t i) const {
2021-04-20 18:38:09 +02:00
return Vec3fa(vNg.x[i], vNg.y[i], vNg.z[i]);
}
2024-02-24 12:40:55 +01:00
__forceinline Vec3vf<M> Ng() const {
return vNg;
}
2021-04-20 18:38:09 +02:00
public:
vfloat<M> vu;
vfloat<M> vv;
vfloat<M> vt;
Vec3vf<M> vNg;
};
2024-02-24 12:40:55 +01:00
template<>
struct DiscIntersectorHitM<1>
{
__forceinline DiscIntersectorHitM() {}
__forceinline DiscIntersectorHitM(const float& u, const float& v, const float& t, const Vec3fa& Ng)
: vu(u), vv(v), vt(t), vNg(Ng) {}
__forceinline void finalize() {}
__forceinline Vec2f uv() const {
return Vec2f(vu, vv);
}
__forceinline float t() const {
return vt;
}
__forceinline Vec3fa Ng() const {
return vNg;
}
public:
float vu;
float vv;
float vt;
Vec3fa vNg;
};
2021-04-20 18:38:09 +02:00
template<int M>
struct DiscIntersector1
{
typedef CurvePrecalculations1 Precalculations;
2024-02-24 12:40:55 +01:00
template<typename Ray, typename Epilog>
2021-04-20 18:38:09 +02:00
static __forceinline bool intersect(
const vbool<M>& valid_i,
Ray& ray,
2024-02-24 12:40:55 +01:00
RayQueryContext* context,
2021-04-20 18:38:09 +02:00
const Points* geom,
const Precalculations& pre,
const Vec4vf<M>& v0i,
const Epilog& epilog)
{
vbool<M> valid = valid_i;
const Vec3vf<M> ray_org(ray.org.x, ray.org.y, ray.org.z);
const Vec3vf<M> ray_dir(ray.dir.x, ray.dir.y, ray.dir.z);
const vfloat<M> rd2 = rcp(dot(ray_dir, ray_dir));
const Vec4vf<M> v0 = enlargeRadiusToMinWidth<M>(context,geom,ray_org,v0i);
2021-04-20 18:38:09 +02:00
const Vec3vf<M> center = v0.xyz();
const vfloat<M> radius = v0.w;
2022-11-24 15:45:59 +01:00
/* compute ray distance projC0 to hit point with ray oriented plane */
2021-04-20 18:38:09 +02:00
const Vec3vf<M> c0 = center - ray_org;
const vfloat<M> projC0 = dot(c0, ray_dir) * rd2;
valid &= (vfloat<M>(ray.tnear()) <= projC0) & (projC0 <= vfloat<M>(ray.tfar));
if (unlikely(none(valid)))
return false;
2022-11-24 15:45:59 +01:00
/* check if hit point lies inside disc */
2021-04-20 18:38:09 +02:00
const Vec3vf<M> perp = c0 - projC0 * ray_dir;
const vfloat<M> l2 = dot(perp, perp);
const vfloat<M> r2 = radius * radius;
valid &= (l2 <= r2);
if (unlikely(none(valid)))
return false;
2022-11-24 15:45:59 +01:00
/* We reject hits where the ray origin lies inside the ray
* oriented disc to avoid self intersections. */
#if defined(EMBREE_DISC_POINT_SELF_INTERSECTION_AVOIDANCE)
const vfloat<M> m2 = dot(c0, c0);
valid &= (m2 > r2);
if (unlikely(none(valid)))
return false;
#endif
2021-04-20 18:38:09 +02:00
DiscIntersectorHitM<M> hit(zero, zero, projC0, -ray_dir);
return epilog(valid, hit);
}
2024-02-24 12:40:55 +01:00
template<typename Ray, typename Epilog>
2021-04-20 18:38:09 +02:00
static __forceinline bool intersect(const vbool<M>& valid_i,
Ray& ray,
2024-02-24 12:40:55 +01:00
RayQueryContext* context,
2021-04-20 18:38:09 +02:00
const Points* geom,
const Precalculations& pre,
const Vec4vf<M>& v0i,
const Vec3vf<M>& normal,
const Epilog& epilog)
{
vbool<M> valid = valid_i;
const Vec3vf<M> ray_org(ray.org.x, ray.org.y, ray.org.z);
const Vec4vf<M> v0 = enlargeRadiusToMinWidth<M>(context,geom,ray_org,v0i);
2021-04-20 18:38:09 +02:00
const Vec3vf<M> center = v0.xyz();
const vfloat<M> radius = v0.w;
vfloat<M> divisor = dot(Vec3vf<M>((Vec3fa)ray.dir), normal);
const vbool<M> parallel = divisor == vfloat<M>(0.f);
valid &= !parallel;
divisor = select(parallel, 1.f, divisor); // prevent divide by zero
vfloat<M> t = dot(center - Vec3vf<M>((Vec3fa)ray.org), Vec3vf<M>(normal)) / divisor;
valid &= (vfloat<M>(ray.tnear()) <= t) & (t <= vfloat<M>(ray.tfar));
if (unlikely(none(valid)))
return false;
Vec3vf<M> intersection = Vec3vf<M>((Vec3fa)ray.org) + Vec3vf<M>((Vec3fa)ray.dir) * t;
vfloat<M> dist2 = dot(intersection - center, intersection - center);
valid &= dist2 < radius * radius;
if (unlikely(none(valid)))
return false;
DiscIntersectorHitM<M> hit(zero, zero, t, normal);
return epilog(valid, hit);
}
};
template<int M, int K>
struct DiscIntersectorK
{
typedef CurvePrecalculationsK<K> Precalculations;
template<typename Epilog>
static __forceinline bool intersect(const vbool<M>& valid_i,
RayK<K>& ray,
size_t k,
2024-02-24 12:40:55 +01:00
RayQueryContext* context,
2021-04-20 18:38:09 +02:00
const Points* geom,
const Precalculations& pre,
const Vec4vf<M>& v0i,
const Epilog& epilog)
{
vbool<M> valid = valid_i;
const Vec3vf<M> ray_org(ray.org.x[k], ray.org.y[k], ray.org.z[k]);
const Vec3vf<M> ray_dir(ray.dir.x[k], ray.dir.y[k], ray.dir.z[k]);
const vfloat<M> rd2 = rcp(dot(ray_dir, ray_dir));
const Vec4vf<M> v0 = enlargeRadiusToMinWidth<M>(context,geom,ray_org,v0i);
2021-04-20 18:38:09 +02:00
const Vec3vf<M> center = v0.xyz();
const vfloat<M> radius = v0.w;
2022-11-24 15:45:59 +01:00
/* compute ray distance projC0 to hit point with ray oriented plane */
2021-04-20 18:38:09 +02:00
const Vec3vf<M> c0 = center - ray_org;
const vfloat<M> projC0 = dot(c0, ray_dir) * rd2;
valid &= (vfloat<M>(ray.tnear()[k]) <= projC0) & (projC0 <= vfloat<M>(ray.tfar[k]));
if (unlikely(none(valid)))
return false;
2022-11-24 15:45:59 +01:00
/* check if hit point lies inside disc */
2021-04-20 18:38:09 +02:00
const Vec3vf<M> perp = c0 - projC0 * ray_dir;
const vfloat<M> l2 = dot(perp, perp);
const vfloat<M> r2 = radius * radius;
valid &= (l2 <= r2);
if (unlikely(none(valid)))
return false;
2022-11-24 15:45:59 +01:00
/* We reject hits where the ray origin lies inside the ray
* oriented disc to avoid self intersections. */
#if defined(EMBREE_DISC_POINT_SELF_INTERSECTION_AVOIDANCE)
const vfloat<M> m2 = dot(c0, c0);
valid &= (m2 > r2);
if (unlikely(none(valid)))
return false;
#endif
2021-04-20 18:38:09 +02:00
DiscIntersectorHitM<M> hit(zero, zero, projC0, -ray_dir);
return epilog(valid, hit);
}
template<typename Epilog>
static __forceinline bool intersect(const vbool<M>& valid_i,
RayK<K>& ray,
size_t k,
2024-02-24 12:40:55 +01:00
RayQueryContext* context,
2021-04-20 18:38:09 +02:00
const Points* geom,
const Precalculations& pre,
const Vec4vf<M>& v0i,
const Vec3vf<M>& normal,
const Epilog& epilog)
{
vbool<M> valid = valid_i;
const Vec3vf<M> ray_org(ray.org.x[k], ray.org.y[k], ray.org.z[k]);
const Vec3vf<M> ray_dir(ray.dir.x[k], ray.dir.y[k], ray.dir.z[k]);
const Vec4vf<M> v0 = enlargeRadiusToMinWidth<M>(context,geom,ray_org,v0i);
2021-04-20 18:38:09 +02:00
const Vec3vf<M> center = v0.xyz();
const vfloat<M> radius = v0.w;
vfloat<M> divisor = dot(Vec3vf<M>(ray_dir), normal);
const vbool<M> parallel = divisor == vfloat<M>(0.f);
valid &= !parallel;
divisor = select(parallel, 1.f, divisor); // prevent divide by zero
vfloat<M> t = dot(center - Vec3vf<M>(ray_org), Vec3vf<M>(normal)) / divisor;
valid &= (vfloat<M>(ray.tnear()[k]) <= t) & (t <= vfloat<M>(ray.tfar[k]));
if (unlikely(none(valid)))
return false;
Vec3vf<M> intersection = Vec3vf<M>(ray_org) + Vec3vf<M>(ray_dir) * t;
vfloat<M> dist2 = dot(intersection - center, intersection - center);
valid &= dist2 < radius * radius;
if (unlikely(none(valid)))
return false;
DiscIntersectorHitM<M> hit(zero, zero, t, normal);
return epilog(valid, hit);
}
};
} // namespace isa
} // namespace embree