Path: blob/master/thirdparty/embree/kernels/geometry/line_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{12template<int M>13struct LineIntersectorHitM14{15__forceinline LineIntersectorHitM() {}1617__forceinline LineIntersectorHitM(const vfloat<M>& u, const vfloat<M>& v, const vfloat<M>& t, const Vec3vf<M>& Ng)18: vu(u), vv(v), vt(t), vNg(Ng) {}1920__forceinline void finalize() {}2122__forceinline Vec2f uv (const size_t i) const { return Vec2f(vu[i],vv[i]); }23__forceinline float t (const size_t i) const { return vt[i]; }24__forceinline Vec3fa Ng(const size_t i) const { return Vec3fa(vNg.x[i],vNg.y[i],vNg.z[i]); }2526__forceinline Vec2vf<M> uv() const { return Vec2vf<M>(vu,vv); }27__forceinline vfloat<M> t () const { return vt; }28__forceinline Vec3vf<M> Ng() const { return vNg; }2930public:31vfloat<M> vu;32vfloat<M> vv;33vfloat<M> vt;34Vec3vf<M> vNg;35};3637template<int M>38struct FlatLinearCurveIntersector139{40typedef CurvePrecalculations1 Precalculations;4142template<typename Ray, typename Epilog>43static __forceinline bool intersect(const vbool<M>& valid_i,44Ray& ray,45RayQueryContext* context,46const LineSegments* geom,47const Precalculations& pre,48const Vec4vf<M>& v0i, const Vec4vf<M>& v1i,49const Epilog& epilog)50{51/* transform end points into ray space */52vbool<M> valid = valid_i;53vfloat<M> depth_scale = pre.depth_scale;54LinearSpace3<Vec3vf<M>> ray_space = pre.ray_space;5556const Vec3vf<M> ray_org ((Vec3fa)ray.org);57const Vec4vf<M> v0 = enlargeRadiusToMinWidth<M>(context,geom,ray_org,v0i);58const Vec4vf<M> v1 = enlargeRadiusToMinWidth<M>(context,geom,ray_org,v1i);5960Vec4vf<M> p0(xfmVector(ray_space,v0.xyz()-ray_org), v0.w);61Vec4vf<M> p1(xfmVector(ray_space,v1.xyz()-ray_org), v1.w);6263/* approximative intersection with cone */64const Vec4vf<M> v = p1-p0;65const Vec4vf<M> w = -p0;66const vfloat<M> d0 = madd(w.x,v.x,w.y*v.y);67const vfloat<M> d1 = madd(v.x,v.x,v.y*v.y);68const vfloat<M> u = clamp(d0*rcp(d1),vfloat<M>(zero),vfloat<M>(one));69const Vec4vf<M> p = madd(u,v,p0);70const vfloat<M> t = p.z;71const vfloat<M> d2 = madd(p.x,p.x,p.y*p.y);72const vfloat<M> r = p.w;73const vfloat<M> r2 = r*r;74valid &= (d2 <= r2) & (vfloat<M>(ray.tnear()) <= t) & (t <= vfloat<M>(ray.tfar));75if (EMBREE_CURVE_SELF_INTERSECTION_AVOIDANCE_FACTOR != 0.0f)76valid &= t > float(EMBREE_CURVE_SELF_INTERSECTION_AVOIDANCE_FACTOR)*r*depth_scale; // ignore self intersections77if (unlikely(none(valid))) return false;7879/* ignore denormalized segments */80const Vec3vf<M> T = v1.xyz()-v0.xyz();81valid &= (T.x != vfloat<M>(zero)) | (T.y != vfloat<M>(zero)) | (T.z != vfloat<M>(zero));82if (unlikely(none(valid))) return false;8384/* update hit information */85LineIntersectorHitM<M> hit(u,zero,t,T);86return epilog(valid,hit);87}88};8990template<int M, int K>91struct FlatLinearCurveIntersectorK92{93typedef CurvePrecalculationsK<K> Precalculations;9495template<typename Epilog>96static __forceinline bool intersect(const vbool<M>& valid_i,97RayK<K>& ray, size_t k,98RayQueryContext* context,99const LineSegments* geom,100const Precalculations& pre,101const Vec4vf<M>& v0i, const Vec4vf<M>& v1i,102const Epilog& epilog)103{104/* transform end points into ray space */105vbool<M> valid = valid_i;106vfloat<M> depth_scale = pre.depth_scale[k];107LinearSpace3<Vec3vf<M>> ray_space = pre.ray_space[k];108const Vec3vf<M> ray_org(ray.org.x[k],ray.org.y[k],ray.org.z[k]);109const Vec3vf<M> ray_dir(ray.dir.x[k],ray.dir.y[k],ray.dir.z[k]);110111const Vec4vf<M> v0 = enlargeRadiusToMinWidth<M>(context,geom,ray_org,v0i);112const Vec4vf<M> v1 = enlargeRadiusToMinWidth<M>(context,geom,ray_org,v1i);113114Vec4vf<M> p0(xfmVector(ray_space,v0.xyz()-ray_org), v0.w);115Vec4vf<M> p1(xfmVector(ray_space,v1.xyz()-ray_org), v1.w);116117/* approximative intersection with cone */118const Vec4vf<M> v = p1-p0;119const Vec4vf<M> w = -p0;120const vfloat<M> d0 = madd(w.x,v.x,w.y*v.y);121const vfloat<M> d1 = madd(v.x,v.x,v.y*v.y);122const vfloat<M> u = clamp(d0*rcp(d1),vfloat<M>(zero),vfloat<M>(one));123const Vec4vf<M> p = madd(u,v,p0);124const vfloat<M> t = p.z;125const vfloat<M> d2 = madd(p.x,p.x,p.y*p.y);126const vfloat<M> r = p.w;127const vfloat<M> r2 = r*r;128valid &= (d2 <= r2) & (vfloat<M>(ray.tnear()[k]) <= t) & (t <= vfloat<M>(ray.tfar[k]));129if (EMBREE_CURVE_SELF_INTERSECTION_AVOIDANCE_FACTOR != 0.0f)130valid &= t > float(EMBREE_CURVE_SELF_INTERSECTION_AVOIDANCE_FACTOR)*r*depth_scale; // ignore self intersections131if (unlikely(none(valid))) return false;132133/* ignore denormalized segments */134const Vec3vf<M> T = v1.xyz()-v0.xyz();135valid &= (T.x != vfloat<M>(zero)) | (T.y != vfloat<M>(zero)) | (T.z != vfloat<M>(zero));136if (unlikely(none(valid))) return false;137138/* update hit information */139LineIntersectorHitM<M> hit(u,zero,t,T);140return epilog(valid,hit);141}142};143}144}145146147