Path: blob/master/thirdparty/embree/kernels/geometry/coneline_intersector.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"78namespace embree9{10namespace isa11{12namespace __coneline_internal13{14template<int M, typename Epilog, typename ray_tfar_func>15static __forceinline bool intersectCone(const vbool<M>& valid_i,16const Vec3vf<M>& ray_org_in, const Vec3vf<M>& ray_dir,17const vfloat<M>& ray_tnear, const ray_tfar_func& ray_tfar,18const Vec4vf<M>& v0, const Vec4vf<M>& v1,19const vbool<M>& cL, const vbool<M>& cR,20const Epilog& epilog)21{22vbool<M> valid = valid_i;2324/* move ray origin closer to make calculations numerically stable */25const vfloat<M> dOdO = sqr(ray_dir);26const vfloat<M> rcp_dOdO = rcp(dOdO);27const Vec3vf<M> center = vfloat<M>(0.5f)*(v0.xyz()+v1.xyz());28const vfloat<M> dt = dot(center-ray_org_in,ray_dir)*rcp_dOdO;29const Vec3vf<M> ray_org = ray_org_in + dt*ray_dir;3031const Vec3vf<M> dP = v1.xyz() - v0.xyz();32const Vec3vf<M> p0 = ray_org - v0.xyz();33const Vec3vf<M> p1 = ray_org - v1.xyz();3435const vfloat<M> dPdP = sqr(dP);36const vfloat<M> dP0 = dot(p0,dP);37const vfloat<M> dP1 = dot(p1,dP);38const vfloat<M> dOdP = dot(ray_dir,dP);3940// intersect cone body41const vfloat<M> dr = v0.w - v1.w;42const vfloat<M> hy = dPdP + sqr(dr);43const vfloat<M> dO0 = dot(ray_dir,p0);44const vfloat<M> OO = sqr(p0);45const vfloat<M> dPdP2 = sqr(dPdP);46const vfloat<M> dPdPr0 = dPdP*v0.w;4748const vfloat<M> A = dPdP2 - sqr(dOdP)*hy;49const vfloat<M> B = dPdP2*dO0 - dP0*dOdP*hy + dPdPr0*(dr*dOdP);50const vfloat<M> C = dPdP2*OO - sqr(dP0)*hy + dPdPr0*(2.0f*dr*dP0 - dPdPr0);5152const vfloat<M> D = B*B - A*C;53valid &= D >= 0.0f;54if (unlikely(none(valid))) {55return false;56}5758/* standard case for "non-parallel" rays */59const vfloat<M> Q = sqrt(D);60const vfloat<M> rcp_A = rcp(A);61/* special case for rays that are "parallel" to the cone - assume miss */62const vbool<M> isParallel = abs(A) <= min_rcp_input;6364vfloat<M> t_cone_lower = select (isParallel, neg_inf, (-B-Q)*rcp_A);65vfloat<M> t_cone_upper = select (isParallel, pos_inf, (-B+Q)*rcp_A);66const vfloat<M> y_lower = dP0 + t_cone_lower*dOdP;67const vfloat<M> y_upper = dP0 + t_cone_upper*dOdP;68t_cone_lower = select(valid & y_lower > 0.0f & y_lower < dPdP, t_cone_lower, pos_inf);69t_cone_upper = select(valid & y_upper > 0.0f & y_upper < dPdP, t_cone_upper, neg_inf);7071const vbool<M> hitDisk0 = valid & cL;72const vbool<M> hitDisk1 = valid & cR;73const vfloat<M> rcp_dOdP = rcp(dOdP);74const vfloat<M> t_disk0 = select (hitDisk0, select (sqr(p0*dOdP-ray_dir*dP0)<(sqr(v0.w)*sqr(dOdP)), -dP0*rcp_dOdP, pos_inf), pos_inf);75const vfloat<M> t_disk1 = select (hitDisk1, select (sqr(p1*dOdP-ray_dir*dP1)<(sqr(v1.w)*sqr(dOdP)), -dP1*rcp_dOdP, pos_inf), pos_inf);76const vfloat<M> t_disk_lower = min(t_disk0, t_disk1);77const vfloat<M> t_disk_upper = max(t_disk0, t_disk1);7879const vfloat<M> t_lower = min(t_cone_lower, t_disk_lower);80const vfloat<M> t_upper = max(t_cone_upper, select(t_lower==t_disk_lower,81select(t_disk_upper==vfloat<M>(pos_inf),neg_inf,t_disk_upper),82select(t_disk_lower==vfloat<M>(pos_inf),neg_inf,t_disk_lower)));8384const vbool<M> valid_lower = valid & ray_tnear <= dt+t_lower & dt+t_lower <= ray_tfar() & t_lower != vfloat<M>(pos_inf);85const vbool<M> valid_upper = valid & ray_tnear <= dt+t_upper & dt+t_upper <= ray_tfar() & t_upper != vfloat<M>(neg_inf);8687const vbool<M> valid_first = valid_lower | valid_upper;88if (unlikely(none(valid_first)))89return false;9091const vfloat<M> t_first = select(valid_lower, t_lower, t_upper);92const vfloat<M> y_first = select(valid_lower, y_lower, y_upper);9394const vfloat<M> rcp_dPdP = rcp(dPdP);95const Vec3vf<M> dP2drr0dP = dPdP*dr*v0.w*dP;96const Vec3vf<M> dPhy = dP*hy;97const vbool<M> cone_hit_first = valid & (t_first == t_cone_lower | t_first == t_cone_upper);98const vbool<M> disk0_hit_first = valid & (t_first == t_disk0);99const Vec3vf<M> Ng_first = select(cone_hit_first, dPdP2*(p0+t_first*ray_dir)+dP2drr0dP-dPhy*y_first, select(disk0_hit_first, -dP, dP));100const vfloat<M> u_first = select(cone_hit_first, y_first*rcp_dPdP, select(disk0_hit_first, vfloat<M>(zero), vfloat<M>(one)));101102/* invoke intersection filter for first hit */103RoundLineIntersectorHitM<M> hit(u_first,zero,dt+t_first,Ng_first);104const bool is_hit_first = epilog(valid_first, hit);105106/* check for possible second hits before potentially accepted hit */107const vfloat<M> t_second = t_upper;108const vfloat<M> y_second = y_upper;109const vbool<M> valid_second = valid_lower & valid_upper & (dt+t_upper <= ray_tfar());110if (unlikely(none(valid_second)))111return is_hit_first;112113/* invoke intersection filter for second hit */114const vbool<M> cone_hit_second = t_second == t_cone_lower | t_second == t_cone_upper;115const vbool<M> disk0_hit_second = t_second == t_disk0;116const Vec3vf<M> Ng_second = select(cone_hit_second, dPdP2*(p0+t_second*ray_dir)+dP2drr0dP-dPhy*y_second, select(disk0_hit_second, -dP, dP));117const vfloat<M> u_second = select(cone_hit_second, y_second*rcp_dPdP, select(disk0_hit_first, vfloat<M>(zero), vfloat<M>(one)));118119hit = RoundLineIntersectorHitM<M>(u_second,zero,dt+t_second,Ng_second);120const bool is_hit_second = epilog(valid_second, hit);121122return is_hit_first | is_hit_second;123}124}125126template<int M>127struct ConeLineIntersectorHitM128{129__forceinline ConeLineIntersectorHitM() {}130131__forceinline ConeLineIntersectorHitM(const vfloat<M>& u, const vfloat<M>& v, const vfloat<M>& t, const Vec3vf<M>& Ng)132: vu(u), vv(v), vt(t), vNg(Ng) {}133134__forceinline void finalize() {}135136__forceinline Vec2f uv (const size_t i) const { return Vec2f(vu[i],vv[i]); }137__forceinline float t (const size_t i) const { return vt[i]; }138__forceinline Vec3fa Ng(const size_t i) const { return Vec3fa(vNg.x[i],vNg.y[i],vNg.z[i]); }139140public:141vfloat<M> vu;142vfloat<M> vv;143vfloat<M> vt;144Vec3vf<M> vNg;145};146147template<int M>148struct ConeCurveIntersector1149{150typedef CurvePrecalculations1 Precalculations;151152struct ray_tfar {153Ray& ray;154__forceinline ray_tfar(Ray& ray) : ray(ray) {}155__forceinline vfloat<M> operator() () const { return ray.tfar; };156};157158template<typename Epilog>159static __forceinline bool intersect(const vbool<M>& valid_i,160Ray& ray,161RayQueryContext* context,162const LineSegments* geom,163const Precalculations& pre,164const Vec4vf<M>& v0i, const Vec4vf<M>& v1i,165const vbool<M>& cL, const vbool<M>& cR,166const Epilog& epilog)167{168const Vec3vf<M> ray_org(ray.org.x, ray.org.y, ray.org.z);169const Vec3vf<M> ray_dir(ray.dir.x, ray.dir.y, ray.dir.z);170const vfloat<M> ray_tnear(ray.tnear());171const Vec4vf<M> v0 = enlargeRadiusToMinWidth<M>(context,geom,ray_org,v0i);172const Vec4vf<M> v1 = enlargeRadiusToMinWidth<M>(context,geom,ray_org,v1i);173return __coneline_internal::intersectCone<M>(valid_i,ray_org,ray_dir,ray_tnear,ray_tfar(ray),v0,v1,cL,cR,epilog);174}175};176177template<int M, int K>178struct ConeCurveIntersectorK179{180typedef CurvePrecalculationsK<K> Precalculations;181182struct ray_tfar {183RayK<K>& ray;184size_t k;185__forceinline ray_tfar(RayK<K>& ray, size_t k) : ray(ray), k(k) {}186__forceinline vfloat<M> operator() () const { return ray.tfar[k]; };187};188189template<typename Epilog>190static __forceinline bool intersect(const vbool<M>& valid_i,191RayK<K>& ray, size_t k,192RayQueryContext* context,193const LineSegments* geom,194const Precalculations& pre,195const Vec4vf<M>& v0i, const Vec4vf<M>& v1i,196const vbool<M>& cL, const vbool<M>& cR,197const Epilog& epilog)198{199const Vec3vf<M> ray_org(ray.org.x[k], ray.org.y[k], ray.org.z[k]);200const Vec3vf<M> ray_dir(ray.dir.x[k], ray.dir.y[k], ray.dir.z[k]);201const vfloat<M> ray_tnear = ray.tnear()[k];202const Vec4vf<M> v0 = enlargeRadiusToMinWidth<M>(context,geom,ray_org,v0i);203const Vec4vf<M> v1 = enlargeRadiusToMinWidth<M>(context,geom,ray_org,v1i);204return __coneline_internal::intersectCone<M>(valid_i,ray_org,ray_dir,ray_tnear,ray_tfar(ray,k),v0,v1,cL,cR,epilog);205}206};207}208}209210211