Path: blob/master/thirdparty/embree/kernels/geometry/curve_intersector_oriented.h
9905 views
// Copyright 2009-2021 Intel Corporation1// SPDX-License-Identifier: Apache-2.023#pragma once45#include "../common/ray.h"6#include "curve_intersector_precalculations.h"7#include "curve_intersector_sweep.h"8#include "../subdiv/linear_bezier_patch.h"910#define DBG(x)1112namespace embree13{14namespace isa15{16template<typename Ray, typename Epilog, int N = VSIZEX-1, int V = VSIZEX>17struct TensorLinearCubicBezierSurfaceIntersector18{19const LinearSpace3fa& ray_space;20Ray& ray;21TensorLinearCubicBezierSurface3fa curve3d;22TensorLinearCubicBezierSurface2fa curve2d;23float eps;24const Epilog& epilog;25bool isHit;2627__forceinline TensorLinearCubicBezierSurfaceIntersector (const LinearSpace3fa& ray_space, Ray& ray, const TensorLinearCubicBezierSurface3fa& curve3d, const Epilog& epilog)28: ray_space(ray_space), ray(ray), curve3d(curve3d), epilog(epilog), isHit(false)29{30const TensorLinearCubicBezierSurface3fa curve3dray = curve3d.xfm(ray_space,ray.org);31curve2d = TensorLinearCubicBezierSurface2fa(CubicBezierCurve2fa(curve3dray.L),CubicBezierCurve2fa(curve3dray.R));32const BBox2fa b2 = curve2d.bounds();33eps = 8.0f*float(ulp)*reduce_max(max(abs(b2.lower),abs(b2.upper)));34}3536__forceinline Interval1f solve_linear(const float u0, const float u1, const float& p0, const float& p1)37{38if (p1 == p0) {39if (p0 == 0.0f) return Interval1f(u0,u1);40else return Interval1f(empty);41}42const float t = -p0/(p1-p0);43const float tt = lerp(u0,u1,t);44return Interval1f(tt);45}4647__forceinline void solve_linear(const float u0, const float u1, const Interval1f& p0, const Interval1f& p1, Interval1f& u)48{49if (sign(p0.lower) != sign(p0.upper)) u.extend(u0);50if (sign(p0.lower) != sign(p1.lower)) u.extend(solve_linear(u0,u1,p0.lower,p1.lower));51if (sign(p0.upper) != sign(p1.upper)) u.extend(solve_linear(u0,u1,p0.upper,p1.upper));52if (sign(p1.lower) != sign(p1.upper)) u.extend(u1);53}5455__forceinline Interval1f bezier_clipping(const CubicBezierCurve<Interval1f>& curve)56{57Interval1f u = empty;58solve_linear(0.0f/3.0f,1.0f/3.0f,curve.v0,curve.v1,u);59solve_linear(0.0f/3.0f,2.0f/3.0f,curve.v0,curve.v2,u);60solve_linear(0.0f/3.0f,3.0f/3.0f,curve.v0,curve.v3,u);61solve_linear(1.0f/3.0f,2.0f/3.0f,curve.v1,curve.v2,u);62solve_linear(1.0f/3.0f,3.0f/3.0f,curve.v1,curve.v3,u);63solve_linear(2.0f/3.0f,3.0f/3.0f,curve.v2,curve.v3,u);64return intersect(u,Interval1f(0.0f,1.0f));65}6667__forceinline Interval1f bezier_clipping(const LinearBezierCurve<Interval1f>& curve)68{69Interval1f v = empty;70solve_linear(0.0f,1.0f,curve.v0,curve.v1,v);71return intersect(v,Interval1f(0.0f,1.0f));72}7374__forceinline void solve_bezier_clipping(BBox1f cu, BBox1f cv, const TensorLinearCubicBezierSurface2fa& curve2)75{76BBox2fa bounds = curve2.bounds();77if (bounds.upper.x < 0.0f) return;78if (bounds.upper.y < 0.0f) return;79if (bounds.lower.x > 0.0f) return;80if (bounds.lower.y > 0.0f) return;8182if (max(cu.size(),cv.size()) < 1E-4f)83{84const float u = cu.center();85const float v = cv.center();86TensorLinearCubicBezierSurface1f curve_z = curve3d.xfm(ray_space.row2(),ray.org);87const float t = curve_z.eval(u,v);88if (ray.tnear() <= t && t <= ray.tfar) {89const Vec3fa Ng = cross(curve3d.eval_du(u,v),curve3d.eval_dv(u,v));90BezierCurveHit hit(t,u,v,Ng);91isHit |= epilog(hit);92}93return;94}9596const Vec2fa dv = curve2.axis_v();97const TensorLinearCubicBezierSurface1f curve1v = curve2.xfm(dv);98LinearBezierCurve<Interval1f> curve0v = curve1v.reduce_u();99if (!curve0v.hasRoot()) return;100101const Interval1f v = bezier_clipping(curve0v);102if (isEmpty(v)) return;103TensorLinearCubicBezierSurface2fa curve2a = curve2.clip_v(v);104cv = BBox1f(lerp(cv.lower,cv.upper,v.lower),lerp(cv.lower,cv.upper,v.upper));105106const Vec2fa du = curve2.axis_u();107const TensorLinearCubicBezierSurface1f curve1u = curve2a.xfm(du);108CubicBezierCurve<Interval1f> curve0u = curve1u.reduce_v();109int roots = curve0u.maxRoots();110if (roots == 0) return;111112if (roots == 1)113{114const Interval1f u = bezier_clipping(curve0u);115if (isEmpty(u)) return;116TensorLinearCubicBezierSurface2fa curve2b = curve2a.clip_u(u);117cu = BBox1f(lerp(cu.lower,cu.upper,u.lower),lerp(cu.lower,cu.upper,u.upper));118solve_bezier_clipping(cu,cv,curve2b);119return;120}121122TensorLinearCubicBezierSurface2fa curve2l, curve2r;123curve2a.split_u(curve2l,curve2r);124solve_bezier_clipping(BBox1f(cu.lower,cu.center()),cv,curve2l);125solve_bezier_clipping(BBox1f(cu.center(),cu.upper),cv,curve2r);126}127128__forceinline bool solve_bezier_clipping()129{130solve_bezier_clipping(BBox1f(0.0f,1.0f),BBox1f(0.0f,1.0f),curve2d);131return isHit;132}133134__forceinline void solve_newton_raphson(BBox1f cu, BBox1f cv)135{136Vec2fa uv(cu.center(),cv.center());137const Vec2fa dfdu = curve2d.eval_du(uv.x,uv.y);138const Vec2fa dfdv = curve2d.eval_dv(uv.x,uv.y);139const LinearSpace2fa rcp_J = rcp(LinearSpace2fa(dfdu,dfdv));140solve_newton_raphson_loop(cu,cv,uv,dfdu,dfdv,rcp_J);141}142143__forceinline void solve_newton_raphson_loop(BBox1f cu, BBox1f cv, const Vec2fa& uv_in, const Vec2fa& dfdu, const Vec2fa& dfdv, const LinearSpace2fa& rcp_J)144{145Vec2fa uv = uv_in;146147for (size_t i=0; i<200; i++)148{149const Vec2fa f = curve2d.eval(uv.x,uv.y);150const Vec2fa duv = rcp_J*f;151uv -= duv;152153if (max(abs(f.x),abs(f.y)) < eps)154{155const float u = uv.x;156const float v = uv.y;157if (!(u >= 0.0f && u <= 1.0f)) return; // rejects NaNs158if (!(v >= 0.0f && v <= 1.0f)) return; // rejects NaNs159const TensorLinearCubicBezierSurface1f curve_z = curve3d.xfm(ray_space.row2(),ray.org);160const float t = curve_z.eval(u,v);161if (!(ray.tnear() <= t && t <= ray.tfar)) return; // rejects NaNs162const Vec3fa Ng = cross(curve3d.eval_du(u,v),curve3d.eval_dv(u,v));163BezierCurveHit hit(t,u,v,Ng);164isHit |= epilog(hit);165return;166}167}168}169170__forceinline bool clip_v(BBox1f& cu, BBox1f& cv)171{172const Vec2fa dv = curve2d.eval_dv(cu.lower,cv.lower);173const TensorLinearCubicBezierSurface1f curve1v = curve2d.xfm(dv).clip(cu,cv);174LinearBezierCurve<Interval1f> curve0v = curve1v.reduce_u();175if (!curve0v.hasRoot()) return false;176Interval1f v = bezier_clipping(curve0v);177if (isEmpty(v)) return false;178v = intersect(v + Interval1f(-0.1f,+0.1f),Interval1f(0.0f,1.0f));179cv = BBox1f(lerp(cv.lower,cv.upper,v.lower),lerp(cv.lower,cv.upper,v.upper));180return true;181}182183__forceinline bool solve_krawczyk(bool very_small, BBox1f& cu, BBox1f& cv)184{185/* perform bezier clipping in v-direction to get tight v-bounds */186TensorLinearCubicBezierSurface2fa curve2 = curve2d.clip(cu,cv);187const Vec2fa dv = curve2.axis_v();188const TensorLinearCubicBezierSurface1f curve1v = curve2.xfm(dv);189LinearBezierCurve<Interval1f> curve0v = curve1v.reduce_u();190if (unlikely(!curve0v.hasRoot())) return true;191Interval1f v = bezier_clipping(curve0v);192if (unlikely(isEmpty(v))) return true;193v = intersect(v + Interval1f(-0.1f,+0.1f),Interval1f(0.0f,1.0f));194curve2 = curve2.clip_v(v);195cv = BBox1f(lerp(cv.lower,cv.upper,v.lower),lerp(cv.lower,cv.upper,v.upper));196197/* perform one newton raphson iteration */198Vec2fa c(cu.center(),cv.center());199Vec2fa f,dfdu,dfdv; curve2d.eval(c.x,c.y,f,dfdu,dfdv);200const LinearSpace2fa rcp_J = rcp(LinearSpace2fa(dfdu,dfdv));201const Vec2fa c1 = c - rcp_J*f;202203/* calculate bounds of derivatives */204const BBox2fa bounds_du = (1.0f/cu.size())*curve2.derivative_u().bounds();205const BBox2fa bounds_dv = (1.0f/cv.size())*curve2.derivative_v().bounds();206207/* calculate krawczyk test */208LinearSpace2<Vec2<Interval1f>> I(Interval1f(1.0f), Interval1f(0.0f),209Interval1f(0.0f), Interval1f(1.0f));210211LinearSpace2<Vec2<Interval1f>> G(Interval1f(bounds_du.lower.x,bounds_du.upper.x), Interval1f(bounds_dv.lower.x,bounds_dv.upper.x),212Interval1f(bounds_du.lower.y,bounds_du.upper.y), Interval1f(bounds_dv.lower.y,bounds_dv.upper.y));213214const LinearSpace2<Vec2f> rcp_J2(rcp_J);215const LinearSpace2<Vec2<Interval1f>> rcp_Ji(rcp_J2);216217const Vec2<Interval1f> x(cu,cv);218const Vec2<Interval1f> K = Vec2<Interval1f>(Vec2f(c1)) + (I - rcp_Ji*G)*(x-Vec2<Interval1f>(Vec2f(c)));219220/* test if there is no solution */221const Vec2<Interval1f> KK = intersect(K,x);222if (unlikely(isEmpty(KK.x) || isEmpty(KK.y))) return true;223224/* exit if convergence cannot get proven, but terminate if we are very small */225if (unlikely(!subset(K,x) && !very_small)) return false;226227/* solve using newton raphson iteration of convergence is guaranteed */228solve_newton_raphson_loop(cu,cv,c1,dfdu,dfdv,rcp_J);229return true;230}231232__forceinline void solve_newton_raphson_no_recursion(BBox1f cu, BBox1f cv)233{234if (!clip_v(cu,cv)) return;235return solve_newton_raphson(cu,cv);236}237238__forceinline void solve_newton_raphson_recursion(BBox1f cu, BBox1f cv)239{240unsigned int sptr = 0;241const unsigned int stack_size = 4;242unsigned int mask_stack[stack_size];243BBox1f cu_stack[stack_size];244BBox1f cv_stack[stack_size];245goto entry;246247/* terminate if stack is empty */248while (sptr)249{250/* pop from stack */251{252sptr--;253size_t mask = mask_stack[sptr];254cu = cu_stack[sptr];255cv = cv_stack[sptr];256const size_t i = bscf(mask);257mask_stack[sptr] = mask;258if (mask) sptr++; // there are still items on the stack259260/* process next element recurse into each hit curve segment */261const float u0 = float(i+0)*(1.0f/(N));262const float u1 = float(i+1)*(1.0f/(N));263const BBox1f cui(lerp(cu.lower,cu.upper,u0),lerp(cu.lower,cu.upper,u1));264cu = cui;265}266267#if 0268solve_newton_raphson_no_recursion(cu,cv);269continue;270271#else272/* we assume convergence for small u ranges and verify using krawczyk */273if (cu.size() < 1.0f/6.0f) {274const bool very_small = cu.size() < 0.001f || sptr >= stack_size;275if (solve_krawczyk(very_small,cu,cv)) {276continue;277}278}279#endif280281entry:282283/* split the curve into N segments in u-direction */284unsigned int mask = 0;285for (int i=0; i<N;)286{287int i0 = i;288vbool<V> valid = true;289TensorLinearCubicBezierSurface<Vec2vf<V>> subcurves = curve2d.clip_v(cv).template vsplit_u<V>(valid,cu,i,N);290291/* slabs test in u-direction */292Vec2vf<V> ndv = cross(subcurves.axis_v());293BBox<vfloat<V>> boundsv = subcurves.template vxfm<V>(ndv).bounds();294valid &= boundsv.lower <= eps;295valid &= boundsv.upper >= -eps;296if (none(valid)) continue;297298/* slabs test in v-direction */299Vec2vf<V> ndu = cross(subcurves.axis_u());300BBox<vfloat<V>> boundsu = subcurves.template vxfm<V>(ndu).bounds();301valid &= boundsu.lower <= eps;302valid &= boundsu.upper >= -eps;303if (none(valid)) continue;304305mask |= movemask(valid) << i0;306}307308if (!mask) continue;309310/* push valid segments to stack */311assert(sptr < stack_size);312mask_stack [sptr] = mask;313cu_stack [sptr] = cu;314cv_stack [sptr] = cv;315sptr++;316}317}318319__forceinline bool solve_newton_raphson_main()320{321BBox1f vu(0.0f,1.0f);322BBox1f vv(0.0f,1.0f);323solve_newton_raphson_recursion(vu,vv);324return isHit;325}326};327328329template<template<typename Ty> class SourceCurve, int N = VSIZEX-1, int V = VSIZEX>330struct OrientedCurve1Intersector1331{332//template<typename Ty> using Curve = SourceCurve<Ty>;333typedef SourceCurve<Vec3ff> SourceCurve3ff;334typedef SourceCurve<Vec3fa> SourceCurve3fa;335336__forceinline OrientedCurve1Intersector1() {}337338__forceinline OrientedCurve1Intersector1(const Ray& ray, const void* ptr) {}339340template<typename Ray, typename Epilog>341__forceinline bool intersect(const CurvePrecalculations1& pre, Ray& ray,342RayQueryContext* context,343const CurveGeometry* geom, const unsigned int primID,344const Vec3ff& v0i, const Vec3ff& v1i, const Vec3ff& v2i, const Vec3ff& v3i,345const Vec3fa& n0i, const Vec3fa& n1i, const Vec3fa& n2i, const Vec3fa& n3i,346const Epilog& epilog) const347{348STAT3(normal.trav_prims,1,1,1);349SourceCurve3ff ccurve(v0i,v1i,v2i,v3i);350SourceCurve3fa ncurve(n0i,n1i,n2i,n3i);351ccurve = enlargeRadiusToMinWidth(context,geom,ray.org,ccurve);352TensorLinearCubicBezierSurface3fa curve = TensorLinearCubicBezierSurface3fa::fromCenterAndNormalCurve(ccurve,ncurve);353//return TensorLinearCubicBezierSurfaceIntersector<Ray,Epilog>(pre.ray_space,ray,curve,epilog).solve_bezier_clipping();354return TensorLinearCubicBezierSurfaceIntersector<Ray,Epilog,N,V>(pre.ray_space,ray,curve,epilog).solve_newton_raphson_main();355}356357template<typename Ray, typename Epilog>358__forceinline bool intersect(const CurvePrecalculations1& pre, Ray& ray,359RayQueryContext* context,360const CurveGeometry* geom, const unsigned int primID,361const TensorLinearCubicBezierSurface3fa& curve, const Epilog& epilog) const362{363STAT3(normal.trav_prims,1,1,1);364//return TensorLinearCubicBezierSurfaceIntersector<Ray,Epilog>(pre.ray_space,ray,curve,epilog).solve_bezier_clipping();365return TensorLinearCubicBezierSurfaceIntersector<Ray,Epilog,N,V>(pre.ray_space,ray,curve,epilog).solve_newton_raphson_main();366}367};368369template<template<typename Ty> class SourceCurve, int K>370struct OrientedCurve1IntersectorK371{372//template<typename Ty> using Curve = SourceCurve<Ty>;373typedef SourceCurve<Vec3ff> SourceCurve3ff;374typedef SourceCurve<Vec3fa> SourceCurve3fa;375376struct Ray1377{378__forceinline Ray1(RayK<K>& ray, size_t k)379: org(ray.org.x[k],ray.org.y[k],ray.org.z[k]), dir(ray.dir.x[k],ray.dir.y[k],ray.dir.z[k]), _tnear(ray.tnear()[k]), tfar(ray.tfar[k]) {}380381Vec3fa org;382Vec3fa dir;383float _tnear;384float& tfar;385386__forceinline float& tnear() { return _tnear; }387//__forceinline float& tfar() { return _tfar; }388__forceinline const float& tnear() const { return _tnear; }389//__forceinline const float& tfar() const { return _tfar; }390};391392template<typename Epilog>393__forceinline bool intersect(const CurvePrecalculationsK<K>& pre, RayK<K>& vray, size_t k,394RayQueryContext* context,395const CurveGeometry* geom, const unsigned int primID,396const Vec3ff& v0i, const Vec3ff& v1i, const Vec3ff& v2i, const Vec3ff& v3i,397const Vec3fa& n0i, const Vec3fa& n1i, const Vec3fa& n2i, const Vec3fa& n3i,398const Epilog& epilog)399{400STAT3(normal.trav_prims,1,1,1);401Ray1 ray(vray,k);402SourceCurve3ff ccurve(v0i,v1i,v2i,v3i);403SourceCurve3fa ncurve(n0i,n1i,n2i,n3i);404ccurve = enlargeRadiusToMinWidth(context,geom,ray.org,ccurve);405TensorLinearCubicBezierSurface3fa curve = TensorLinearCubicBezierSurface3fa::fromCenterAndNormalCurve(ccurve,ncurve);406//return TensorLinearCubicBezierSurfaceIntersector<Ray1,Epilog>(pre.ray_space[k],ray,curve,epilog).solve_bezier_clipping();407return TensorLinearCubicBezierSurfaceIntersector<Ray1,Epilog>(pre.ray_space[k],ray,curve,epilog).solve_newton_raphson_main();408}409410template<typename Epilog>411__forceinline bool intersect(const CurvePrecalculationsK<K>& pre, RayK<K>& vray, size_t k,412RayQueryContext* context,413const CurveGeometry* geom, const unsigned int primID,414const TensorLinearCubicBezierSurface3fa& curve,415const Epilog& epilog)416{417STAT3(normal.trav_prims,1,1,1);418Ray1 ray(vray,k);419//return TensorLinearCubicBezierSurfaceIntersector<Ray1,Epilog>(pre.ray_space[k],ray,curve,epilog).solve_bezier_clipping();420return TensorLinearCubicBezierSurfaceIntersector<Ray1,Epilog>(pre.ray_space[k],ray,curve,epilog).solve_newton_raphson_main();421}422};423}424}425426427