// Copyright 2009-2021 Intel Corporation // SPDX-License-Identifier: Apache-2.0 #pragma once #include "../common/ray.h" #include "curve_intersector_precalculations.h" namespace embree { namespace isa { template struct LineIntersectorHitM { __forceinline LineIntersectorHitM() {} __forceinline LineIntersectorHitM(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]); } __forceinline Vec2vf uv() const { return Vec2vf(vu,vv); } __forceinline vfloat t () const { return vt; } __forceinline Vec3vf Ng() const { return vNg; } public: vfloat vu; vfloat vv; vfloat vt; Vec3vf vNg; }; template struct FlatLinearCurveIntersector1 { typedef CurvePrecalculations1 Precalculations; 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 Epilog& epilog) { /* transform end points into ray space */ vbool valid = valid_i; vfloat depth_scale = pre.depth_scale; LinearSpace3> ray_space = pre.ray_space; const Vec3vf ray_org ((Vec3fa)ray.org); const Vec4vf v0 = enlargeRadiusToMinWidth(context,geom,ray_org,v0i); const Vec4vf v1 = enlargeRadiusToMinWidth(context,geom,ray_org,v1i); Vec4vf p0(xfmVector(ray_space,v0.xyz()-ray_org), v0.w); Vec4vf p1(xfmVector(ray_space,v1.xyz()-ray_org), v1.w); /* approximative intersection with cone */ const Vec4vf v = p1-p0; const Vec4vf w = -p0; const vfloat d0 = madd(w.x,v.x,w.y*v.y); const vfloat d1 = madd(v.x,v.x,v.y*v.y); const vfloat u = clamp(d0*rcp(d1),vfloat(zero),vfloat(one)); const Vec4vf p = madd(u,v,p0); const vfloat t = p.z; const vfloat d2 = madd(p.x,p.x,p.y*p.y); const vfloat r = p.w; const vfloat r2 = r*r; valid &= (d2 <= r2) & (vfloat(ray.tnear()) <= t) & (t <= vfloat(ray.tfar)); if (EMBREE_CURVE_SELF_INTERSECTION_AVOIDANCE_FACTOR != 0.0f) valid &= t > float(EMBREE_CURVE_SELF_INTERSECTION_AVOIDANCE_FACTOR)*r*depth_scale; // ignore self intersections if (unlikely(none(valid))) return false; /* ignore denormalized segments */ const Vec3vf T = v1.xyz()-v0.xyz(); valid &= (T.x != vfloat(zero)) | (T.y != vfloat(zero)) | (T.z != vfloat(zero)); if (unlikely(none(valid))) return false; /* update hit information */ LineIntersectorHitM hit(u,zero,t,T); return epilog(valid,hit); } }; template struct FlatLinearCurveIntersectorK { typedef CurvePrecalculationsK Precalculations; 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 Epilog& epilog) { /* transform end points into ray space */ vbool valid = valid_i; vfloat depth_scale = pre.depth_scale[k]; LinearSpace3> ray_space = pre.ray_space[k]; 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 Vec4vf v0 = enlargeRadiusToMinWidth(context,geom,ray_org,v0i); const Vec4vf v1 = enlargeRadiusToMinWidth(context,geom,ray_org,v1i); Vec4vf p0(xfmVector(ray_space,v0.xyz()-ray_org), v0.w); Vec4vf p1(xfmVector(ray_space,v1.xyz()-ray_org), v1.w); /* approximative intersection with cone */ const Vec4vf v = p1-p0; const Vec4vf w = -p0; const vfloat d0 = madd(w.x,v.x,w.y*v.y); const vfloat d1 = madd(v.x,v.x,v.y*v.y); const vfloat u = clamp(d0*rcp(d1),vfloat(zero),vfloat(one)); const Vec4vf p = madd(u,v,p0); const vfloat t = p.z; const vfloat d2 = madd(p.x,p.x,p.y*p.y); const vfloat r = p.w; const vfloat r2 = r*r; valid &= (d2 <= r2) & (vfloat(ray.tnear()[k]) <= t) & (t <= vfloat(ray.tfar[k])); if (EMBREE_CURVE_SELF_INTERSECTION_AVOIDANCE_FACTOR != 0.0f) valid &= t > float(EMBREE_CURVE_SELF_INTERSECTION_AVOIDANCE_FACTOR)*r*depth_scale; // ignore self intersections if (unlikely(none(valid))) return false; /* ignore denormalized segments */ const Vec3vf T = v1.xyz()-v0.xyz(); valid &= (T.x != vfloat(zero)) | (T.y != vfloat(zero)) | (T.z != vfloat(zero)); if (unlikely(none(valid))) return false; /* update hit information */ LineIntersectorHitM hit(u,zero,t,T); return epilog(valid,hit); } }; } }