Path: blob/master/thirdparty/embree/kernels/geometry/quad_intersector_pluecker.h
9905 views
// Copyright 2009-2021 Intel Corporation1// SPDX-License-Identifier: Apache-2.023#pragma once45#include "quad_intersector_moeller.h"67/*! Modified Pluecker ray/triangle intersector. The test first shifts8* the ray origin into the origin of the coordinate system and then9* uses Pluecker coordinates for the intersection. Due to the shift,10* the Pluecker coordinate calculation simplifies and the tests get11* numerically stable. The edge equations are watertight along the12* edge for neighboring triangles. */1314namespace embree15{16namespace isa17{18template<int M>19struct QuadHitPlueckerM20{21__forceinline QuadHitPlueckerM() {}2223__forceinline QuadHitPlueckerM(const vbool<M>& valid,24const vfloat<M>& U,25const vfloat<M>& V,26const vfloat<M>& UVW,27const vfloat<M>& t,28const Vec3vf<M>& Ng,29const vbool<M>& flags)30: U(U), V(V), UVW(UVW), tri_Ng(Ng), valid(valid), vt(t), flags(flags) {}3132__forceinline void finalize()33{34const vbool<M> invalid = abs(UVW) < min_rcp_input;35const vfloat<M> rcpUVW = select(invalid,vfloat<M>(0.0f),rcp(UVW));36const vfloat<M> u = min(U * rcpUVW,1.0f);37const vfloat<M> v = min(V * rcpUVW,1.0f);38const vfloat<M> u1 = vfloat<M>(1.0f) - u;39const vfloat<M> v1 = vfloat<M>(1.0f) - v;40#if !defined(__AVX__) || defined(EMBREE_BACKFACE_CULLING)41vu = select(flags,u1,u);42vv = select(flags,v1,v);43vNg = Vec3vf<M>(tri_Ng.x,tri_Ng.y,tri_Ng.z);44#else45const vfloat<M> flip = select(flags,vfloat<M>(-1.0f),vfloat<M>(1.0f));46vv = select(flags,u1,v);47vu = select(flags,v1,u);48vNg = Vec3vf<M>(flip*tri_Ng.x,flip*tri_Ng.y,flip*tri_Ng.z);49#endif50}5152__forceinline Vec2f uv(const size_t i)53{54const float u = vu[i];55const float v = vv[i];56return Vec2f(u,v);57}5859__forceinline float t(const size_t i) { return vt[i]; }60__forceinline Vec3fa Ng(const size_t i) { return Vec3fa(vNg.x[i],vNg.y[i],vNg.z[i]); }6162private:63vfloat<M> U;64vfloat<M> V;65vfloat<M> UVW;66Vec3vf<M> tri_Ng;6768public:69vbool<M> valid;70vfloat<M> vu;71vfloat<M> vv;72vfloat<M> vt;73Vec3vf<M> vNg;7475public:76const vbool<M> flags;77};7879template<int K>80struct QuadHitPlueckerK81{82__forceinline QuadHitPlueckerK(const vfloat<K>& U,83const vfloat<K>& V,84const vfloat<K>& UVW,85const vfloat<K>& t,86const Vec3vf<K>& Ng,87const vbool<K>& flags)88: U(U), V(V), UVW(UVW), t(t), flags(flags), tri_Ng(Ng) {}8990__forceinline std::tuple<vfloat<K>,vfloat<K>,vfloat<K>,Vec3vf<K>> operator() () const91{92const vbool<K> invalid = abs(UVW) < min_rcp_input;93const vfloat<K> rcpUVW = select(invalid,vfloat<K>(0.0f),rcp(UVW));94const vfloat<K> u0 = min(U * rcpUVW,1.0f);95const vfloat<K> v0 = min(V * rcpUVW,1.0f);96const vfloat<K> u1 = vfloat<K>(1.0f) - u0;97const vfloat<K> v1 = vfloat<K>(1.0f) - v0;98const vfloat<K> u = select(flags,u1,u0);99const vfloat<K> v = select(flags,v1,v0);100const Vec3vf<K> Ng(tri_Ng.x,tri_Ng.y,tri_Ng.z);101return std::make_tuple(u,v,t,Ng);102}103104private:105const vfloat<K> U;106const vfloat<K> V;107const vfloat<K> UVW;108const vfloat<K> t;109const vbool<K> flags;110const Vec3vf<K> tri_Ng;111};112113struct PlueckerIntersectorTriangle1114{115template<int M, typename Epilog>116static __forceinline bool intersect(Ray& ray,117const Vec3vf<M>& tri_v0,118const Vec3vf<M>& tri_v1,119const Vec3vf<M>& tri_v2,120const vbool<M>& flags,121const Epilog& epilog)122{123/* calculate vertices relative to ray origin */124const Vec3vf<M> O = Vec3vf<M>((Vec3fa)ray.org);125const Vec3vf<M> D = Vec3vf<M>((Vec3fa)ray.dir);126const Vec3vf<M> v0 = tri_v0-O;127const Vec3vf<M> v1 = tri_v1-O;128const Vec3vf<M> v2 = tri_v2-O;129130/* calculate triangle edges */131const Vec3vf<M> e0 = v2-v0;132const Vec3vf<M> e1 = v0-v1;133const Vec3vf<M> e2 = v1-v2;134135/* perform edge tests */136const vfloat<M> U = dot(cross(e0,v2+v0),D);137const vfloat<M> V = dot(cross(e1,v0+v1),D);138const vfloat<M> W = dot(cross(e2,v1+v2),D);139const vfloat<M> UVW = U+V+W;140const vfloat<M> eps = float(ulp)*abs(UVW);141#if defined(EMBREE_BACKFACE_CULLING)142vbool<M> valid = max(U,V,W) <= eps;143#else144vbool<M> valid = (min(U,V,W) >= -eps) | (max(U,V,W) <= eps);145#endif146if (unlikely(none(valid))) return false;147148/* calculate geometry normal and denominator */149const Vec3vf<M> Ng = stable_triangle_normal(e0,e1,e2);150const vfloat<M> den = twice(dot(Ng,D));151152/* perform depth test */153const vfloat<M> T = twice(dot(v0,Ng));154const vfloat<M> t = rcp(den)*T;155valid &= vfloat<M>(ray.tnear()) <= t & t <= vfloat<M>(ray.tfar);156valid &= den != vfloat<M>(zero);157if (unlikely(none(valid))) return false;158159/* update hit information */160QuadHitPlueckerM<M> hit(valid,U,V,UVW,t,Ng,flags);161return epilog(valid,hit);162}163};164165/*! Intersects M quads with 1 ray */166template<int M, bool filter>167struct QuadMIntersector1Pluecker168{169__forceinline QuadMIntersector1Pluecker() {}170171__forceinline QuadMIntersector1Pluecker(const Ray& ray, const void* ptr) {}172173__forceinline void intersect(RayHit& ray, RayQueryContext* context,174const Vec3vf<M>& v0, const Vec3vf<M>& v1, const Vec3vf<M>& v2, const Vec3vf<M>& v3,175const vuint<M>& geomID, const vuint<M>& primID) const176{177Intersect1EpilogM<M,filter> epilog(ray,context,geomID,primID);178PlueckerIntersectorTriangle1::intersect<M>(ray,v0,v1,v3,vbool<M>(false),epilog);179PlueckerIntersectorTriangle1::intersect<M>(ray,v2,v3,v1,vbool<M>(true),epilog);180}181182__forceinline bool occluded(Ray& ray, RayQueryContext* context,183const Vec3vf<M>& v0, const Vec3vf<M>& v1, const Vec3vf<M>& v2, const Vec3vf<M>& v3,184const vuint<M>& geomID, const vuint<M>& primID) const185{186Occluded1EpilogM<M,filter> epilog(ray,context,geomID,primID);187if (PlueckerIntersectorTriangle1::intersect<M>(ray,v0,v1,v3,vbool<M>(false),epilog)) return true;188if (PlueckerIntersectorTriangle1::intersect<M>(ray,v2,v3,v1,vbool<M>(true ),epilog)) return true;189return false;190}191};192193#if defined(__AVX__)194195/*! Intersects 4 quads with 1 ray using AVX */196template<bool filter>197struct QuadMIntersector1Pluecker<4,filter>198{199__forceinline QuadMIntersector1Pluecker() {}200201__forceinline QuadMIntersector1Pluecker(const Ray& ray, const void* ptr) {}202203template<typename Epilog>204__forceinline bool intersect(Ray& ray, const Vec3vf4& v0, const Vec3vf4& v1, const Vec3vf4& v2, const Vec3vf4& v3, const Epilog& epilog) const205{206const Vec3vf8 vtx0(vfloat8(v0.x,v2.x),vfloat8(v0.y,v2.y),vfloat8(v0.z,v2.z));207#if !defined(EMBREE_BACKFACE_CULLING)208const Vec3vf8 vtx1(vfloat8(v1.x),vfloat8(v1.y),vfloat8(v1.z));209const Vec3vf8 vtx2(vfloat8(v3.x),vfloat8(v3.y),vfloat8(v3.z));210#else211const Vec3vf8 vtx1(vfloat8(v1.x,v3.x),vfloat8(v1.y,v3.y),vfloat8(v1.z,v3.z));212const Vec3vf8 vtx2(vfloat8(v3.x,v1.x),vfloat8(v3.y,v1.y),vfloat8(v3.z,v1.z));213#endif214const vbool8 flags(0,0,0,0,1,1,1,1);215return PlueckerIntersectorTriangle1::intersect<8>(ray,vtx0,vtx1,vtx2,flags,epilog);216}217218__forceinline bool intersect(RayHit& ray, RayQueryContext* context, const Vec3vf4& v0, const Vec3vf4& v1, const Vec3vf4& v2, const Vec3vf4& v3,219const vuint4& geomID, const vuint4& primID) const220{221return intersect(ray,v0,v1,v2,v3,Intersect1EpilogM<8,filter>(ray,context,vuint8(geomID),vuint8(primID)));222}223224__forceinline bool occluded(Ray& ray, RayQueryContext* context, const Vec3vf4& v0, const Vec3vf4& v1, const Vec3vf4& v2, const Vec3vf4& v3,225const vuint4& geomID, const vuint4& primID) const226{227return intersect(ray,v0,v1,v2,v3,Occluded1EpilogM<8,filter>(ray,context,vuint8(geomID),vuint8(primID)));228}229};230231#endif232233234/* ----------------------------- */235/* -- ray packet intersectors -- */236/* ----------------------------- */237238struct PlueckerIntersector1KTriangleM239{240/*! Intersect k'th ray from ray packet of size K with M triangles. */241template<int M, int K, typename Epilog>242static __forceinline bool intersect1(RayK<K>& ray,243size_t k,244const Vec3vf<M>& tri_v0,245const Vec3vf<M>& tri_v1,246const Vec3vf<M>& tri_v2,247const vbool<M>& flags,248const Epilog& epilog)249{250/* calculate vertices relative to ray origin */251const Vec3vf<M> O = broadcast<vfloat<M>>(ray.org,k);252const Vec3vf<M> D = broadcast<vfloat<M>>(ray.dir,k);253const Vec3vf<M> v0 = tri_v0-O;254const Vec3vf<M> v1 = tri_v1-O;255const Vec3vf<M> v2 = tri_v2-O;256257/* calculate triangle edges */258const Vec3vf<M> e0 = v2-v0;259const Vec3vf<M> e1 = v0-v1;260const Vec3vf<M> e2 = v1-v2;261262/* perform edge tests */263const vfloat<M> U = dot(cross(e0,v2+v0),D);264const vfloat<M> V = dot(cross(e1,v0+v1),D);265const vfloat<M> W = dot(cross(e2,v1+v2),D);266267const vfloat<M> UVW = U+V+W;268const vfloat<M> eps = float(ulp)*abs(UVW);269#if defined(EMBREE_BACKFACE_CULLING)270vbool<M> valid = max(U,V,W) <= eps;271#else272vbool<M> valid = (min(U,V,W) >= -eps) | (max(U,V,W) <= eps);273#endif274if (unlikely(none(valid))) return false;275276/* calculate geometry normal and denominator */277const Vec3vf<M> Ng = stable_triangle_normal(e0,e1,e2);278const vfloat<M> den = twice(dot(Ng,D));279280/* perform depth test */281const vfloat<M> T = twice(dot(v0,Ng));282const vfloat<M> t = rcp(den)*T;283valid &= vfloat<M>(ray.tnear()[k]) <= t & t <= vfloat<M>(ray.tfar[k]);284if (unlikely(none(valid))) return false;285286/* avoid division by 0 */287valid &= den != vfloat<M>(zero);288if (unlikely(none(valid))) return false;289290/* update hit information */291QuadHitPlueckerM<M> hit(valid,U,V,UVW,t,Ng,flags);292return epilog(valid,hit);293}294};295296template<int M, int K, bool filter>297struct QuadMIntersectorKPlueckerBase298{299__forceinline QuadMIntersectorKPlueckerBase(const vbool<K>& valid, const RayK<K>& ray) {}300301/*! Intersects K rays with one of M triangles. */302template<typename Epilog>303__forceinline vbool<K> intersectK(const vbool<K>& valid0,304RayK<K>& ray,305const Vec3vf<K>& tri_v0,306const Vec3vf<K>& tri_v1,307const Vec3vf<K>& tri_v2,308const vbool<K>& flags,309const Epilog& epilog) const310{311/* calculate vertices relative to ray origin */312vbool<K> valid = valid0;313const Vec3vf<K> O = ray.org;314const Vec3vf<K> D = ray.dir;315const Vec3vf<K> v0 = tri_v0-O;316const Vec3vf<K> v1 = tri_v1-O;317const Vec3vf<K> v2 = tri_v2-O;318319/* calculate triangle edges */320const Vec3vf<K> e0 = v2-v0;321const Vec3vf<K> e1 = v0-v1;322const Vec3vf<K> e2 = v1-v2;323324/* perform edge tests */325const vfloat<K> U = dot(Vec3vf<K>(cross(e0,v2+v0)),D);326const vfloat<K> V = dot(Vec3vf<K>(cross(e1,v0+v1)),D);327const vfloat<K> W = dot(Vec3vf<K>(cross(e2,v1+v2)),D);328const vfloat<K> UVW = U+V+W;329const vfloat<K> eps = float(ulp)*abs(UVW);330#if defined(EMBREE_BACKFACE_CULLING)331valid &= max(U,V,W) <= eps;332#else333valid &= (min(U,V,W) >= -eps) | (max(U,V,W) <= eps);334#endif335if (unlikely(none(valid))) return false;336337/* calculate geometry normal and denominator */338const Vec3vf<K> Ng = stable_triangle_normal(e0,e1,e2);339const vfloat<K> den = twice(dot(Vec3vf<K>(Ng),D));340341/* perform depth test */342const vfloat<K> T = twice(dot(v0,Vec3vf<K>(Ng)));343const vfloat<K> t = rcp(den)*T;344valid &= ray.tnear() <= t & t <= ray.tfar;345valid &= den != vfloat<K>(zero);346if (unlikely(none(valid))) return false;347348/* calculate hit information */349QuadHitPlueckerK<K> hit(U,V,UVW,t,Ng,flags);350return epilog(valid,hit);351}352353/*! Intersects K rays with one of M quads. */354template<typename Epilog>355__forceinline bool intersectK(const vbool<K>& valid0,356RayK<K>& ray,357const Vec3vf<K>& v0,358const Vec3vf<K>& v1,359const Vec3vf<K>& v2,360const Vec3vf<K>& v3,361const Epilog& epilog) const362{363intersectK(valid0,ray,v0,v1,v3,vbool<K>(false),epilog);364if (none(valid0)) return true;365intersectK(valid0,ray,v2,v3,v1,vbool<K>(true ),epilog);366return none(valid0);367}368};369370template<int M, int K, bool filter>371struct QuadMIntersectorKPluecker : public QuadMIntersectorKPlueckerBase<M,K,filter>372{373__forceinline QuadMIntersectorKPluecker(const vbool<K>& valid, const RayK<K>& ray)374: QuadMIntersectorKPlueckerBase<M,K,filter>(valid,ray) {}375376__forceinline void intersect1(RayHitK<K>& ray, size_t k, RayQueryContext* context,377const Vec3vf<M>& v0, const Vec3vf<M>& v1, const Vec3vf<M>& v2, const Vec3vf<M>& v3,378const vuint<M>& geomID, const vuint<M>& primID) const379{380Intersect1KEpilogM<M,K,filter> epilog(ray,k,context,geomID,primID);381PlueckerIntersector1KTriangleM::intersect1<M,K>(ray,k,v0,v1,v3,vbool<M>(false),epilog);382PlueckerIntersector1KTriangleM::intersect1<M,K>(ray,k,v2,v3,v1,vbool<M>(true ),epilog);383}384385__forceinline bool occluded1(RayK<K>& ray, size_t k, RayQueryContext* context,386const Vec3vf<M>& v0, const Vec3vf<M>& v1, const Vec3vf<M>& v2, const Vec3vf<M>& v3,387const vuint<M>& geomID, const vuint<M>& primID) const388{389Occluded1KEpilogM<M,K,filter> epilog(ray,k,context,geomID,primID);390if (PlueckerIntersector1KTriangleM::intersect1<M,K>(ray,k,v0,v1,v3,vbool<M>(false),epilog)) return true;391if (PlueckerIntersector1KTriangleM::intersect1<M,K>(ray,k,v2,v3,v1,vbool<M>(true ),epilog)) return true;392return false;393}394};395396#if defined(__AVX__)397398/*! Intersects 4 quads with 1 ray using AVX */399template<int K, bool filter>400struct QuadMIntersectorKPluecker<4,K,filter> : public QuadMIntersectorKPlueckerBase<4,K,filter>401{402__forceinline QuadMIntersectorKPluecker(const vbool<K>& valid, const RayK<K>& ray)403: QuadMIntersectorKPlueckerBase<4,K,filter>(valid,ray) {}404405template<typename Epilog>406__forceinline bool intersect1(RayK<K>& ray, size_t k, const Vec3vf4& v0, const Vec3vf4& v1, const Vec3vf4& v2, const Vec3vf4& v3, const Epilog& epilog) const407{408const Vec3vf8 vtx0(vfloat8(v0.x,v2.x),vfloat8(v0.y,v2.y),vfloat8(v0.z,v2.z));409const vbool8 flags(0,0,0,0,1,1,1,1);410#if !defined(EMBREE_BACKFACE_CULLING)411const Vec3vf8 vtx1(vfloat8(v1.x),vfloat8(v1.y),vfloat8(v1.z));412const Vec3vf8 vtx2(vfloat8(v3.x),vfloat8(v3.y),vfloat8(v3.z));413#else414const Vec3vf8 vtx1(vfloat8(v1.x,v3.x),vfloat8(v1.y,v3.y),vfloat8(v1.z,v3.z));415const Vec3vf8 vtx2(vfloat8(v3.x,v1.x),vfloat8(v3.y,v1.y),vfloat8(v3.z,v1.z));416#endif417return PlueckerIntersector1KTriangleM::intersect1<8,K>(ray,k,vtx0,vtx1,vtx2,flags,epilog);418}419420__forceinline bool intersect1(RayHitK<K>& ray, size_t k, RayQueryContext* context,421const Vec3vf4& v0, const Vec3vf4& v1, const Vec3vf4& v2, const Vec3vf4& v3,422const vuint4& geomID, const vuint4& primID) const423{424return intersect1(ray,k,v0,v1,v2,v3,Intersect1KEpilogM<8,K,filter>(ray,k,context,vuint8(geomID),vuint8(primID)));425}426427__forceinline bool occluded1(RayK<K>& ray, size_t k, RayQueryContext* context,428const Vec3vf4& v0, const Vec3vf4& v1, const Vec3vf4& v2, const Vec3vf4& v3,429const vuint4& geomID, const vuint4& primID) const430{431return intersect1(ray,k,v0,v1,v2,v3,Occluded1KEpilogM<8,K,filter>(ray,k,context,vuint8(geomID),vuint8(primID)));432}433};434435#endif436}437}438439440