1// Copyright 2009-2021 Intel Corporation
2// SPDX-License-Identifier: Apache-2.0
3
4#pragma once
5
6#include "../common/ray.h"
7#include "curve_intersector_precalculations.h"
8
9namespace embree
10{
11 namespace isa
12 {
13 template<int M>
14 struct LineIntersectorHitM
15 {
16 __forceinline LineIntersectorHitM() {}
17
18 __forceinline LineIntersectorHitM(const vfloat<M>& u, const vfloat<M>& v, const vfloat<M>& t, const Vec3vf<M>& Ng)
19 : vu(u), vv(v), vt(t), vNg(Ng) {}
20
21 __forceinline void finalize() {}
22
23 __forceinline Vec2f uv (const size_t i) const { return Vec2f(vu[i],vv[i]); }
24 __forceinline float t (const size_t i) const { return vt[i]; }
25 __forceinline Vec3fa Ng(const size_t i) const { return Vec3fa(vNg.x[i],vNg.y[i],vNg.z[i]); }
26
27 __forceinline Vec2vf<M> uv() const { return Vec2vf<M>(vu,vv); }
28 __forceinline vfloat<M> t () const { return vt; }
29 __forceinline Vec3vf<M> Ng() const { return vNg; }
30
31 public:
32 vfloat<M> vu;
33 vfloat<M> vv;
34 vfloat<M> vt;
35 Vec3vf<M> vNg;
36 };
37
38 template<int M>
39 struct FlatLinearCurveIntersector1
40 {
41 typedef CurvePrecalculations1 Precalculations;
42
43 template<typename Ray, typename Epilog>
44 static __forceinline bool intersect(const vbool<M>& valid_i,
45 Ray& ray,
46 IntersectContext* context,
47 const LineSegments* geom,
48 const Precalculations& pre,
49 const Vec4vf<M>& v0i, const Vec4vf<M>& v1i,
50 const Epilog& epilog)
51 {
52 /* transform end points into ray space */
53 vbool<M> valid = valid_i;
54 vfloat<M> depth_scale = pre.depth_scale;
55 LinearSpace3<Vec3vf<M>> ray_space = pre.ray_space;
56
57 const Vec3vf<M> ray_org ((Vec3fa)ray.org);
58 const Vec4vf<M> v0 = enlargeRadiusToMinWidth<M>(context,geom,ray_org,v0i);
59 const Vec4vf<M> v1 = enlargeRadiusToMinWidth<M>(context,geom,ray_org,v1i);
60
61 Vec4vf<M> p0(xfmVector(ray_space,v0.xyz()-ray_org), v0.w);
62 Vec4vf<M> p1(xfmVector(ray_space,v1.xyz()-ray_org), v1.w);
63
64 /* approximative intersection with cone */
65 const Vec4vf<M> v = p1-p0;
66 const Vec4vf<M> w = -p0;
67 const vfloat<M> d0 = madd(w.x,v.x,w.y*v.y);
68 const vfloat<M> d1 = madd(v.x,v.x,v.y*v.y);
69 const vfloat<M> u = clamp(d0*rcp(d1),vfloat<M>(zero),vfloat<M>(one));
70 const Vec4vf<M> p = madd(u,v,p0);
71 const vfloat<M> t = p.z;
72 const vfloat<M> d2 = madd(p.x,p.x,p.y*p.y);
73 const vfloat<M> r = p.w;
74 const vfloat<M> r2 = r*r;
75 valid &= (d2 <= r2) & (vfloat<M>(ray.tnear()) <= t) & (t <= vfloat<M>(ray.tfar));
76 if (EMBREE_CURVE_SELF_INTERSECTION_AVOIDANCE_FACTOR != 0.0f)
77 valid &= t > float(EMBREE_CURVE_SELF_INTERSECTION_AVOIDANCE_FACTOR)*r*depth_scale; // ignore self intersections
78 if (unlikely(none(valid))) return false;
79
80 /* ignore denormalized segments */
81 const Vec3vf<M> T = v1.xyz()-v0.xyz();
82 valid &= (T.x != vfloat<M>(zero)) | (T.y != vfloat<M>(zero)) | (T.z != vfloat<M>(zero));
83 if (unlikely(none(valid))) return false;
84
85 /* update hit information */
86 LineIntersectorHitM<M> hit(u,zero,t,T);
87 return epilog(valid,hit);
88 }
89 };
90
91 template<int M, int K>
92 struct FlatLinearCurveIntersectorK
93 {
94 typedef CurvePrecalculationsK<K> Precalculations;
95
96 template<typename Epilog>
97 static __forceinline bool intersect(const vbool<M>& valid_i,
98 RayK<K>& ray, size_t k,
99 IntersectContext* context,
100 const LineSegments* geom,
101 const Precalculations& pre,
102 const Vec4vf<M>& v0i, const Vec4vf<M>& v1i,
103 const Epilog& epilog)
104 {
105 /* transform end points into ray space */
106 vbool<M> valid = valid_i;
107 vfloat<M> depth_scale = pre.depth_scale[k];
108 LinearSpace3<Vec3vf<M>> ray_space = pre.ray_space[k];
109 const Vec3vf<M> ray_org(ray.org.x[k],ray.org.y[k],ray.org.z[k]);
110 const Vec3vf<M> ray_dir(ray.dir.x[k],ray.dir.y[k],ray.dir.z[k]);
111
112 const Vec4vf<M> v0 = enlargeRadiusToMinWidth<M>(context,geom,ray_org,v0i);
113 const Vec4vf<M> v1 = enlargeRadiusToMinWidth<M>(context,geom,ray_org,v1i);
114
115 Vec4vf<M> p0(xfmVector(ray_space,v0.xyz()-ray_org), v0.w);
116 Vec4vf<M> p1(xfmVector(ray_space,v1.xyz()-ray_org), v1.w);
117
118 /* approximative intersection with cone */
119 const Vec4vf<M> v = p1-p0;
120 const Vec4vf<M> w = -p0;
121 const vfloat<M> d0 = madd(w.x,v.x,w.y*v.y);
122 const vfloat<M> d1 = madd(v.x,v.x,v.y*v.y);
123 const vfloat<M> u = clamp(d0*rcp(d1),vfloat<M>(zero),vfloat<M>(one));
124 const Vec4vf<M> p = madd(u,v,p0);
125 const vfloat<M> t = p.z;
126 const vfloat<M> d2 = madd(p.x,p.x,p.y*p.y);
127 const vfloat<M> r = p.w;
128 const vfloat<M> r2 = r*r;
129 valid &= (d2 <= r2) & (vfloat<M>(ray.tnear()[k]) <= t) & (t <= vfloat<M>(ray.tfar[k]));
130 if (EMBREE_CURVE_SELF_INTERSECTION_AVOIDANCE_FACTOR != 0.0f)
131 valid &= t > float(EMBREE_CURVE_SELF_INTERSECTION_AVOIDANCE_FACTOR)*r*depth_scale; // ignore self intersections
132 if (unlikely(none(valid))) return false;
133
134 /* ignore denormalized segments */
135 const Vec3vf<M> T = v1.xyz()-v0.xyz();
136 valid &= (T.x != vfloat<M>(zero)) | (T.y != vfloat<M>(zero)) | (T.z != vfloat<M>(zero));
137 if (unlikely(none(valid))) return false;
138
139 /* update hit information */
140 LineIntersectorHitM<M> hit(u,zero,t,T);
141 return epilog(valid,hit);
142 }
143 };
144 }
145}
146