// Copyright 2009-2020 Intel Corporation // SPDX-License-Identifier: Apache-2.0 #pragma once #include "triangle.h" #include "trianglev.h" #include "trianglev_mb.h" #include "intersector_epilog.h" /*! Modified Pluecker ray/triangle intersector. The test first shifts * the ray origin into the origin of the coordinate system and then * uses Pluecker coordinates for the intersection. Due to the shift, * the Pluecker coordinate calculation simplifies and the tests get * numerically stable. The edge equations are watertight along the * edge for neighboring triangles. */ namespace embree { namespace isa { template struct PlueckerHitM { __forceinline PlueckerHitM(const vfloat& U, const vfloat& V, const vfloat& UVW, const vfloat& t, const Vec3vf& Ng, const UVMapper& mapUV) : U(U), V(V), UVW(UVW), mapUV(mapUV), vt(t), vNg(Ng) {} __forceinline void finalize() { const vbool invalid = abs(UVW) < min_rcp_input; const vfloat rcpUVW = select(invalid,vfloat(0.0f),rcp(UVW)); vu = U * rcpUVW; vv = V * rcpUVW; mapUV(vu,vv); } __forceinline Vec2f uv (const size_t i) const { return Vec2f(vu[i],vv[i]); } __forceinline float t (const size_t i) const { return vt[i]; } __forceinline Vec3fa Ng(const size_t i) const { return Vec3fa(vNg.x[i],vNg.y[i],vNg.z[i]); } private: const vfloat U; const vfloat V; const vfloat UVW; const UVMapper& mapUV; public: vfloat vu; vfloat vv; vfloat vt; Vec3vf vNg; }; template struct PlueckerIntersector1 { __forceinline PlueckerIntersector1() {} __forceinline PlueckerIntersector1(const Ray& ray, const void* ptr) {} template __forceinline bool intersect(Ray& ray, const Vec3vf& tri_v0, const Vec3vf& tri_v1, const Vec3vf& tri_v2, const UVMapper& mapUV, const Epilog& epilog) const { /* calculate vertices relative to ray origin */ const Vec3vf O = Vec3vf((Vec3fa)ray.org); const Vec3vf D = Vec3vf((Vec3fa)ray.dir); const Vec3vf v0 = tri_v0-O; const Vec3vf v1 = tri_v1-O; const Vec3vf v2 = tri_v2-O; /* calculate triangle edges */ const Vec3vf e0 = v2-v0; const Vec3vf e1 = v0-v1; const Vec3vf e2 = v1-v2; /* perform edge tests */ const vfloat U = dot(cross(e0,v2+v0),D); const vfloat V = dot(cross(e1,v0+v1),D); const vfloat W = dot(cross(e2,v1+v2),D); const vfloat UVW = U+V+W; const vfloat eps = float(ulp)*abs(UVW); #if defined(EMBREE_BACKFACE_CULLING) vbool valid = max(U,V,W) <= eps; #else vbool valid = (min(U,V,W) >= -eps) | (max(U,V,W) <= eps); #endif if (unlikely(none(valid))) return false; /* calculate geometry normal and denominator */ const Vec3vf Ng = stable_triangle_normal(e0,e1,e2); const vfloat den = twice(dot(Ng,D)); /* perform depth test */ const vfloat T = twice(dot(v0,Ng)); const vfloat t = rcp(den)*T; valid &= vfloat(ray.tnear()) <= t & t <= vfloat(ray.tfar); valid &= den != vfloat(zero); if (unlikely(none(valid))) return false; /* update hit information */ PlueckerHitM hit(U,V,UVW,t,Ng,mapUV); return epilog(valid,hit); } }; template struct PlueckerHitK { __forceinline PlueckerHitK(const vfloat& U, const vfloat& V, const vfloat& UVW, const vfloat& t, const Vec3vf& Ng, const UVMapper& mapUV) : U(U), V(V), UVW(UVW), t(t), Ng(Ng), mapUV(mapUV) {} __forceinline std::tuple,vfloat,vfloat,Vec3vf> operator() () const { const vbool invalid = abs(UVW) < min_rcp_input; const vfloat rcpUVW = select(invalid,vfloat(0.0f),rcp(UVW)); vfloat u = U * rcpUVW; vfloat v = V * rcpUVW; mapUV(u,v); return std::make_tuple(u,v,t,Ng); } private: const vfloat U; const vfloat V; const vfloat UVW; const vfloat t; const Vec3vf Ng; const UVMapper& mapUV; }; template struct PlueckerIntersectorK { __forceinline PlueckerIntersectorK(const vbool& valid, const RayK& ray) {} /*! Intersects K rays with one of M triangles. */ template __forceinline vbool intersectK(const vbool& valid0, RayK& ray, const Vec3vf& tri_v0, const Vec3vf& tri_v1, const Vec3vf& tri_v2, const UVMapper& mapUV, const Epilog& epilog) const { /* calculate vertices relative to ray origin */ vbool valid = valid0; const Vec3vf O = ray.org; const Vec3vf D = ray.dir; const Vec3vf v0 = tri_v0-O; const Vec3vf v1 = tri_v1-O; const Vec3vf v2 = tri_v2-O; /* calculate triangle edges */ const Vec3vf e0 = v2-v0; const Vec3vf e1 = v0-v1; const Vec3vf e2 = v1-v2; /* perform edge tests */ const vfloat U = dot(Vec3vf(cross(e0,v2+v0)),D); const vfloat V = dot(Vec3vf(cross(e1,v0+v1)),D); const vfloat W = dot(Vec3vf(cross(e2,v1+v2)),D); const vfloat UVW = U+V+W; const vfloat eps = float(ulp)*abs(UVW); #if defined(EMBREE_BACKFACE_CULLING) valid &= max(U,V,W) <= eps; #else valid &= (min(U,V,W) >= -eps) | (max(U,V,W) <= eps); #endif if (unlikely(none(valid))) return false; /* calculate geometry normal and denominator */ const Vec3vf Ng = stable_triangle_normal(e0,e1,e2); const vfloat den = twice(dot(Vec3vf(Ng),D)); /* perform depth test */ const vfloat T = twice(dot(v0,Vec3vf(Ng))); const vfloat t = rcp(den)*T; valid &= ray.tnear() <= t & t <= ray.tfar; valid &= den != vfloat(zero); if (unlikely(none(valid))) return false; /* calculate hit information */ PlueckerHitK hit(U,V,UVW,t,Ng,mapUV); return epilog(valid,hit); } /*! Intersect k'th ray from ray packet of size K with M triangles. */ template __forceinline bool intersect(RayK& ray, size_t k, const Vec3vf& tri_v0, const Vec3vf& tri_v1, const Vec3vf& tri_v2, const UVMapper& mapUV, const Epilog& epilog) const { /* calculate vertices relative to ray origin */ const Vec3vf O = broadcast>(ray.org,k); const Vec3vf D = broadcast>(ray.dir,k); const Vec3vf v0 = tri_v0-O; const Vec3vf v1 = tri_v1-O; const Vec3vf v2 = tri_v2-O; /* calculate triangle edges */ const Vec3vf e0 = v2-v0; const Vec3vf e1 = v0-v1; const Vec3vf e2 = v1-v2; /* perform edge tests */ const vfloat U = dot(cross(e0,v2+v0),D); const vfloat V = dot(cross(e1,v0+v1),D); const vfloat W = dot(cross(e2,v1+v2),D); const vfloat UVW = U+V+W; const vfloat eps = float(ulp)*abs(UVW); #if defined(EMBREE_BACKFACE_CULLING) vbool valid = max(U,V,W) <= eps; #else vbool valid = (min(U,V,W) >= -eps) | (max(U,V,W) <= eps); #endif if (unlikely(none(valid))) return false; /* calculate geometry normal and denominator */ const Vec3vf Ng = stable_triangle_normal(e0,e1,e2); const vfloat den = twice(dot(Ng,D)); /* perform depth test */ const vfloat T = twice(dot(v0,Ng)); const vfloat t = rcp(den)*T; valid &= vfloat(ray.tnear()[k]) <= t & t <= vfloat(ray.tfar[k]); if (unlikely(none(valid))) return false; /* avoid division by 0 */ valid &= den != vfloat(zero); if (unlikely(none(valid))) return false; /* update hit information */ PlueckerHitM hit(U,V,UVW,t,Ng,mapUV); return epilog(valid,hit); } }; } }