// Copyright 2009-2020 Intel Corporation // SPDX-License-Identifier: Apache-2.0 #pragma once #include "../common/ray.h" #include "curve_intersector_precalculations.h" namespace embree { namespace isa { namespace __coneline_internal { template static __forceinline bool intersectCone(const vbool& valid_i, const Vec3vf& ray_org_in, const Vec3vf& ray_dir, const vfloat& ray_tnear, const ray_tfar_func& ray_tfar, const Vec4vf& v0, const Vec4vf& v1, const vbool& cL, const vbool& cR, const Epilog& epilog) { vbool valid = valid_i; /* move ray origin closer to make calculations numerically stable */ const vfloat dOdO = sqr(ray_dir); const vfloat rcp_dOdO = rcp(dOdO); const Vec3vf center = vfloat(0.5f)*(v0.xyz()+v1.xyz()); const vfloat dt = dot(center-ray_org_in,ray_dir)*rcp_dOdO; const Vec3vf ray_org = ray_org_in + dt*ray_dir; const Vec3vf dP = v1.xyz() - v0.xyz(); const Vec3vf p0 = ray_org - v0.xyz(); const Vec3vf p1 = ray_org - v1.xyz(); const vfloat dPdP = sqr(dP); const vfloat dP0 = dot(p0,dP); const vfloat dP1 = dot(p1,dP); const vfloat dOdP = dot(ray_dir,dP); // intersect cone body const vfloat dr = v0.w - v1.w; const vfloat hy = dPdP + sqr(dr); const vfloat dO0 = dot(ray_dir,p0); const vfloat OO = sqr(p0); const vfloat dPdP2 = sqr(dPdP); const vfloat dPdPr0 = dPdP*v0.w; const vfloat A = dPdP2 - sqr(dOdP)*hy; const vfloat B = dPdP2*dO0 - dP0*dOdP*hy + dPdPr0*(dr*dOdP); const vfloat C = dPdP2*OO - sqr(dP0)*hy + dPdPr0*(2.0f*dr*dP0 - dPdPr0); const vfloat D = B*B - A*C; valid &= D >= 0.0f; if (unlikely(none(valid))) { return false; } /* standard case for "non-parallel" rays */ const vfloat Q = sqrt(D); const vfloat rcp_A = rcp(A); /* special case for rays that are "parallel" to the cone - assume miss */ const vbool isParallel = abs(A) <= min_rcp_input; vfloat t_cone_lower = select (isParallel, neg_inf, (-B-Q)*rcp_A); vfloat t_cone_upper = select (isParallel, pos_inf, (-B+Q)*rcp_A); const vfloat y_lower = dP0 + t_cone_lower*dOdP; const vfloat y_upper = dP0 + t_cone_upper*dOdP; t_cone_lower = select(valid & y_lower > 0.0f & y_lower < dPdP, t_cone_lower, pos_inf); t_cone_upper = select(valid & y_upper > 0.0f & y_upper < dPdP, t_cone_upper, neg_inf); const vbool hitDisk0 = valid & cL; const vbool hitDisk1 = valid & cR; const vfloat rcp_dOdP = rcp(dOdP); const vfloat t_disk0 = select (hitDisk0, select (sqr(p0*dOdP-ray_dir*dP0)<(sqr(v0.w)*sqr(dOdP)), -dP0*rcp_dOdP, pos_inf), pos_inf); const vfloat t_disk1 = select (hitDisk1, select (sqr(p1*dOdP-ray_dir*dP1)<(sqr(v1.w)*sqr(dOdP)), -dP1*rcp_dOdP, pos_inf), pos_inf); const vfloat t_disk_lower = min(t_disk0, t_disk1); const vfloat t_disk_upper = max(t_disk0, t_disk1); const vfloat t_lower = min(t_cone_lower, t_disk_lower); const vfloat t_upper = max(t_cone_upper, select(t_lower==t_disk_lower, select(t_disk_upper==vfloat(pos_inf),neg_inf,t_disk_upper), select(t_disk_lower==vfloat(pos_inf),neg_inf,t_disk_lower))); const vbool valid_lower = valid & ray_tnear <= dt+t_lower & dt+t_lower <= ray_tfar() & t_lower != vfloat(pos_inf); const vbool valid_upper = valid & ray_tnear <= dt+t_upper & dt+t_upper <= ray_tfar() & t_upper != vfloat(neg_inf); const vbool valid_first = valid_lower | valid_upper; if (unlikely(none(valid_first))) return false; const vfloat t_first = select(valid_lower, t_lower, t_upper); const vfloat y_first = select(valid_lower, y_lower, y_upper); const vfloat rcp_dPdP = rcp(dPdP); const Vec3vf dP2drr0dP = dPdP*dr*v0.w*dP; const Vec3vf dPhy = dP*hy; const vbool cone_hit_first = valid & (t_first == t_cone_lower | t_first == t_cone_upper); const vbool disk0_hit_first = valid & (t_first == t_disk0); const Vec3vf Ng_first = select(cone_hit_first, dPdP2*(p0+t_first*ray_dir)+dP2drr0dP-dPhy*y_first, select(disk0_hit_first, -dP, dP)); const vfloat u_first = select(cone_hit_first, y_first*rcp_dPdP, select(disk0_hit_first, vfloat(zero), vfloat(one))); /* invoke intersection filter for first hit */ RoundLineIntersectorHitM hit(u_first,zero,dt+t_first,Ng_first); const bool is_hit_first = epilog(valid_first, hit); /* check for possible second hits before potentially accepted hit */ const vfloat t_second = t_upper; const vfloat y_second = y_upper; const vbool valid_second = valid_lower & valid_upper & (dt+t_upper <= ray_tfar()); if (unlikely(none(valid_second))) return is_hit_first; /* invoke intersection filter for second hit */ const vbool cone_hit_second = t_second == t_cone_lower | t_second == t_cone_upper; const vbool disk0_hit_second = t_second == t_disk0; const Vec3vf Ng_second = select(cone_hit_second, dPdP2*(p0+t_second*ray_dir)+dP2drr0dP-dPhy*y_second, select(disk0_hit_second, -dP, dP)); const vfloat u_second = select(cone_hit_second, y_second*rcp_dPdP, select(disk0_hit_first, vfloat(zero), vfloat(one))); hit = RoundLineIntersectorHitM(u_second,zero,dt+t_second,Ng_second); const bool is_hit_second = epilog(valid_second, hit); return is_hit_first | is_hit_second; } } template struct ConeLineIntersectorHitM { __forceinline ConeLineIntersectorHitM() {} __forceinline ConeLineIntersectorHitM(const vfloat& u, const vfloat& v, const vfloat& t, const Vec3vf& Ng) : vu(u), vv(v), vt(t), vNg(Ng) {} __forceinline void finalize() {} __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]); } public: vfloat vu; vfloat vv; vfloat vt; Vec3vf vNg; }; template struct ConeCurveIntersector1 { typedef CurvePrecalculations1 Precalculations; struct ray_tfar { Ray& ray; __forceinline ray_tfar(Ray& ray) : ray(ray) {} __forceinline vfloat operator() () const { return ray.tfar; }; }; template static __forceinline bool intersect(const vbool& valid_i, Ray& ray, IntersectContext* context, const LineSegments* geom, const Precalculations& pre, const Vec4vf& v0i, const Vec4vf& v1i, const vbool& cL, const vbool& cR, const Epilog& epilog) { const Vec3vf ray_org(ray.org.x, ray.org.y, ray.org.z); const Vec3vf ray_dir(ray.dir.x, ray.dir.y, ray.dir.z); const vfloat ray_tnear(ray.tnear()); const Vec4vf v0 = enlargeRadiusToMinWidth(context,geom,ray_org,v0i); const Vec4vf v1 = enlargeRadiusToMinWidth(context,geom,ray_org,v1i); return __coneline_internal::intersectCone(valid_i,ray_org,ray_dir,ray_tnear,ray_tfar(ray),v0,v1,cL,cR,epilog); } }; template struct ConeCurveIntersectorK { typedef CurvePrecalculationsK Precalculations; struct ray_tfar { RayK& ray; size_t k; __forceinline ray_tfar(RayK& ray, size_t k) : ray(ray), k(k) {} __forceinline vfloat operator() () const { return ray.tfar[k]; }; }; template static __forceinline bool intersect(const vbool& valid_i, RayK& ray, size_t k, IntersectContext* context, const LineSegments* geom, const Precalculations& pre, const Vec4vf& v0i, const Vec4vf& v1i, const vbool& cL, const vbool& cR, const Epilog& epilog) { const Vec3vf ray_org(ray.org.x[k], ray.org.y[k], ray.org.z[k]); const Vec3vf ray_dir(ray.dir.x[k], ray.dir.y[k], ray.dir.z[k]); const vfloat ray_tnear = ray.tnear()[k]; const Vec4vf v0 = enlargeRadiusToMinWidth(context,geom,ray_org,v0i); const Vec4vf v1 = enlargeRadiusToMinWidth(context,geom,ray_org,v1i); return __coneline_internal::intersectCone(valid_i,ray_org,ray_dir,ray_tnear,ray_tfar(ray,k),v0,v1,cL,cR,epilog); } }; } }