Path: blob/master/thirdparty/embree/kernels/geometry/curve_intersector_ribbon.h
9905 views
// Copyright 2009-2021 Intel Corporation1// SPDX-License-Identifier: Apache-2.023#pragma once45#include "../common/ray.h"6#include "quad_intersector.h"7#include "curve_intersector_precalculations.h"89#define Bezier1Intersector1 RibbonCurve1Intersector110#define Bezier1IntersectorK RibbonCurve1IntersectorK1112namespace embree13{14namespace isa15{16template<typename NativeCurve3ff, int M>17struct RibbonHit18{19__forceinline RibbonHit() {}2021__forceinline RibbonHit(const vbool<M>& valid, const vfloat<M>& U, const vfloat<M>& V, const vfloat<M>& T, const int i, const int N,22const NativeCurve3ff& curve3D)23: U(U), V(V), T(T), i(i), N(N), curve3D(curve3D), valid(valid) {}2425__forceinline void finalize()26{27vu = (vfloat<M>(step)+U+vfloat<M>(float(i)))*(1.0f/float(N));28vv = V;29vt = T;30}3132__forceinline Vec2f uv (const size_t i) const { return Vec2f(vu[i],vv[i]); }33__forceinline float t (const size_t i) const { return vt[i]; }34__forceinline Vec3fa Ng(const size_t i) const { return curve3D.eval_du(vu[i]); }3536__forceinline Vec2vf<M> uv() const { return Vec2vf<M>(vu,vv); }37__forceinline vfloat<M> t () const { return vt; }38__forceinline Vec3vf<M> Ng() const { return (Vec3vf<M>) curve3D.template veval_du<M>(vu); }3940public:41vfloat<M> U;42vfloat<M> V;43vfloat<M> T;44int i, N;45NativeCurve3ff curve3D;4647public:48vbool<M> valid;49vfloat<M> vu;50vfloat<M> vv;51vfloat<M> vt;52};5354/* calculate squared distance of point p0 to line p1->p2 */55template<int M>56__forceinline std::pair<vfloat<M>,vfloat<M>> sqr_point_line_distance(const Vec2vf<M>& p0, const Vec2vf<M>& p1, const Vec2vf<M>& p2)57{58const vfloat<M> num = det(p2-p1,p1-p0);59const vfloat<M> den2 = dot(p2-p1,p2-p1);60return std::make_pair(num*num,den2);61}6263/* performs culling against a cylinder */64template<int M>65__forceinline vbool<M> cylinder_culling_test(const Vec2vf<M>& p0, const Vec2vf<M>& p1, const Vec2vf<M>& p2, const vfloat<M>& r)66{67const std::pair<vfloat<M>,vfloat<M>> d = sqr_point_line_distance<M>(p0,p1,p2);68return d.first <= r*r*d.second;69}7071template<int M = VSIZEX, typename NativeCurve3ff, typename Epilog>72__forceinline bool intersect_ribbon(const Vec3fa& ray_org, const Vec3fa& ray_dir, const float ray_tnear, const float& ray_tfar,73const LinearSpace3fa& ray_space, const float& depth_scale,74const NativeCurve3ff& curve3D, const int N,75const Epilog& epilog)76{77/* transform control points into ray space */78const NativeCurve3ff curve2D = curve3D.xfm_pr(ray_space,ray_org);79float eps = 4.0f*float(ulp)*reduce_max(max(abs(curve2D.v0),abs(curve2D.v1),abs(curve2D.v2),abs(curve2D.v3)));8081int i=0;82bool ishit = false;8384#if !defined(__SYCL_DEVICE_ONLY__)85{86/* evaluate the bezier curve */87vbool<M> valid = vfloat<M>(step) < vfloat<M>(float(N));88const Vec4vf<M> p0 = curve2D.template eval0<M>(0,N);89const Vec4vf<M> p1 = curve2D.template eval1<M>(0,N);90valid &= cylinder_culling_test<M>(zero,Vec2vf<M>(p0.x,p0.y),Vec2vf<M>(p1.x,p1.y),max(p0.w,p1.w));9192if (any(valid))93{94Vec3vf<M> dp0dt = curve2D.template derivative0<M>(0,N);95Vec3vf<M> dp1dt = curve2D.template derivative1<M>(0,N);96dp0dt = select(reduce_max(abs(dp0dt)) < vfloat<M>(eps),Vec3vf<M>(p1-p0),dp0dt);97dp1dt = select(reduce_max(abs(dp1dt)) < vfloat<M>(eps),Vec3vf<M>(p1-p0),dp1dt);98const Vec3vf<M> n0(dp0dt.y,-dp0dt.x,0.0f);99const Vec3vf<M> n1(dp1dt.y,-dp1dt.x,0.0f);100const Vec3vf<M> nn0 = normalize(n0);101const Vec3vf<M> nn1 = normalize(n1);102const Vec3vf<M> lp0 = madd(p0.w,nn0,Vec3vf<M>(p0));103const Vec3vf<M> lp1 = madd(p1.w,nn1,Vec3vf<M>(p1));104const Vec3vf<M> up0 = nmadd(p0.w,nn0,Vec3vf<M>(p0));105const Vec3vf<M> up1 = nmadd(p1.w,nn1,Vec3vf<M>(p1));106107vfloat<M> vu,vv,vt;108vbool<M> valid0 = intersect_quad_backface_culling<M>(valid,zero,Vec3fa(0,0,1),ray_tnear,ray_tfar,lp0,lp1,up1,up0,vu,vv,vt);109110if (any(valid0))111{112/* ignore self intersections */113if (EMBREE_CURVE_SELF_INTERSECTION_AVOIDANCE_FACTOR != 0.0f) {114vfloat<M> r = lerp(p0.w, p1.w, vu);115valid0 &= vt > float(EMBREE_CURVE_SELF_INTERSECTION_AVOIDANCE_FACTOR)*r*depth_scale;116}117118if (any(valid0))119{120vv = madd(2.0f,vv,vfloat<M>(-1.0f));121RibbonHit<NativeCurve3ff,M> bhit(valid0,vu,vv,vt,0,N,curve3D);122ishit |= epilog(bhit.valid,bhit);123}124}125}126i += M;127}128129if (unlikely(i < N))130#endif131{132/* process SIMD-size many segments per iteration */133for (; i<N; i+=M)134{135/* evaluate the bezier curve */136vbool<M> valid = vint<M>(i)+vint<M>(step) < vint<M>(N);137const Vec4vf<M> p0 = curve2D.template eval0<M>(i,N);138const Vec4vf<M> p1 = curve2D.template eval1<M>(i,N);139valid &= cylinder_culling_test<M>(zero,Vec2vf<M>(p0.x,p0.y),Vec2vf<M>(p1.x,p1.y),max(p0.w,p1.w));140if (none(valid)) continue;141142Vec3vf<M> dp0dt = curve2D.template derivative0<M>(i,N);143Vec3vf<M> dp1dt = curve2D.template derivative1<M>(i,N);144dp0dt = select(reduce_max(abs(dp0dt)) < vfloat<M>(eps),Vec3vf<M>(p1-p0),dp0dt);145dp1dt = select(reduce_max(abs(dp1dt)) < vfloat<M>(eps),Vec3vf<M>(p1-p0),dp1dt);146const Vec3vf<M> n0(dp0dt.y,-dp0dt.x,0.0f);147const Vec3vf<M> n1(dp1dt.y,-dp1dt.x,0.0f);148const Vec3vf<M> nn0 = normalize(n0);149const Vec3vf<M> nn1 = normalize(n1);150const Vec3vf<M> lp0 = madd(p0.w,nn0,Vec3vf<M>(p0));151const Vec3vf<M> lp1 = madd(p1.w,nn1,Vec3vf<M>(p1));152const Vec3vf<M> up0 = nmadd(p0.w,nn0,Vec3vf<M>(p0));153const Vec3vf<M> up1 = nmadd(p1.w,nn1,Vec3vf<M>(p1));154155vfloat<M> vu,vv,vt;156vbool<M> valid0 = intersect_quad_backface_culling<M>(valid,zero,Vec3fa(0,0,1),ray_tnear,ray_tfar,lp0,lp1,up1,up0,vu,vv,vt);157158if (any(valid0))159{160/* ignore self intersections */161if (EMBREE_CURVE_SELF_INTERSECTION_AVOIDANCE_FACTOR != 0.0f) {162vfloat<M> r = lerp(p0.w, p1.w, vu);163valid0 &= vt > float(EMBREE_CURVE_SELF_INTERSECTION_AVOIDANCE_FACTOR)*r*depth_scale;164}165166if (any(valid0))167{168vv = madd(2.0f,vv,vfloat<M>(-1.0f));169RibbonHit<NativeCurve3ff,M> bhit(valid0,vu,vv,vt,i,N,curve3D);170ishit |= epilog(bhit.valid,bhit);171}172}173}174}175return ishit;176}177178template<template<typename Ty> class NativeCurve, int M = VSIZEX>179struct RibbonCurve1Intersector1180{181typedef NativeCurve<Vec3ff> NativeCurve3ff;182183template<typename Ray, typename Epilog>184__forceinline bool intersect(const CurvePrecalculations1& pre, Ray& ray,185RayQueryContext* context,186const CurveGeometry* geom, const unsigned int primID,187const Vec3ff& v0, const Vec3ff& v1, const Vec3ff& v2, const Vec3ff& v3,188const Epilog& epilog)189{190const int N = geom->tessellationRate;191NativeCurve3ff curve(v0,v1,v2,v3);192curve = enlargeRadiusToMinWidth(context,geom,ray.org,curve);193return intersect_ribbon<M,NativeCurve3ff>(ray.org,ray.dir,ray.tnear(),ray.tfar,194pre.ray_space,pre.depth_scale,195curve,N,196epilog);197}198};199200template<template<typename Ty> class NativeCurve, int K, int M = VSIZEX>201struct RibbonCurve1IntersectorK202{203typedef NativeCurve<Vec3ff> NativeCurve3ff;204205template<typename Epilog>206__forceinline bool intersect(const CurvePrecalculationsK<K>& pre, RayK<K>& ray, size_t k,207RayQueryContext* context,208const CurveGeometry* geom, const unsigned int primID,209const Vec3ff& v0, const Vec3ff& v1, const Vec3ff& v2, const Vec3ff& v3,210const Epilog& epilog)211{212const int N = geom->tessellationRate;213const Vec3fa ray_org(ray.org.x[k],ray.org.y[k],ray.org.z[k]);214const Vec3fa ray_dir(ray.dir.x[k],ray.dir.y[k],ray.dir.z[k]);215NativeCurve3ff curve(v0,v1,v2,v3);216curve = enlargeRadiusToMinWidth(context,geom,ray_org,curve);217return intersect_ribbon<M,NativeCurve3ff>(ray_org,ray_dir,ray.tnear()[k],ray.tfar[k],218pre.ray_space[k],pre.depth_scale[k],219curve,N,220epilog);221}222};223}224}225226227