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<typename NativeCurve3fa, int M>
14 struct DistanceCurveHit
15 {
16 __forceinline DistanceCurveHit() {}
17
18 __forceinline DistanceCurveHit(const vbool<M>& valid, const vfloat<M>& U, const vfloat<M>& V, const vfloat<M>& T, const int i, const int N,
19 const NativeCurve3fa& curve3D)
20 : U(U), V(V), T(T), i(i), N(N), curve3D(curve3D), valid(valid) {}
21
22 __forceinline void finalize()
23 {
24 vu = (vfloat<M>(step)+U+vfloat<M>(float(i)))*(1.0f/float(N));
25 vv = V;
26 vt = T;
27 }
28
29 __forceinline Vec2f uv (const size_t i) const { return Vec2f(vu[i],vv[i]); }
30 __forceinline float t (const size_t i) const { return vt[i]; }
31 __forceinline Vec3fa Ng(const size_t i) const {
32 return curve3D.eval_du(vu[i]);
33 }
34
35 public:
36 vfloat<M> U;
37 vfloat<M> V;
38 vfloat<M> T;
39 int i, N;
40 NativeCurve3fa curve3D;
41
42 public:
43 vbool<M> valid;
44 vfloat<M> vu;
45 vfloat<M> vv;
46 vfloat<M> vt;
47 };
48
49 template<typename NativeCurve3fa>
50 struct DistanceCurve1Intersector1
51 {
52 template<typename Epilog>
53 __forceinline bool intersect(const CurvePrecalculations1& pre,Ray& ray,
54 IntersectContext* context,
55 const CurveGeometry* geom, const unsigned int primID,
56 const Vec3fa& v0, const Vec3fa& v1, const Vec3fa& v2, const Vec3fa& v3,
57 const Epilog& epilog)
58 {
59 const int N = geom->tessellationRate;
60
61 /* transform control points into ray space */
62 const NativeCurve3fa curve3Di(v0,v1,v2,v3);
63 const NativeCurve3fa curve3D = enlargeRadiusToMinWidth(context,geom,ray.org,curve3Di);
64 const NativeCurve3fa curve2D = curve3D.xfm_pr(pre.ray_space,ray.org);
65
66 /* evaluate the bezier curve */
67 vboolx valid = vfloatx(step) < vfloatx(float(N));
68 const Vec4vfx p0 = curve2D.template eval0<VSIZEX>(0,N);
69 const Vec4vfx p1 = curve2D.template eval1<VSIZEX>(0,N);
70
71 /* approximative intersection with cone */
72 const Vec4vfx v = p1-p0;
73 const Vec4vfx w = -p0;
74 const vfloatx d0 = madd(w.x,v.x,w.y*v.y);
75 const vfloatx d1 = madd(v.x,v.x,v.y*v.y);
76 const vfloatx u = clamp(d0*rcp(d1),vfloatx(zero),vfloatx(one));
77 const Vec4vfx p = madd(u,v,p0);
78 const vfloatx t = p.z*pre.depth_scale;
79 const vfloatx d2 = madd(p.x,p.x,p.y*p.y);
80 const vfloatx r = p.w;
81 const vfloatx r2 = r*r;
82 valid &= (d2 <= r2) & (vfloatx(ray.tnear()) <= t) & (t <= vfloatx(ray.tfar));
83 if (EMBREE_CURVE_SELF_INTERSECTION_AVOIDANCE_FACTOR != 0.0f)
84 valid &= t > float(EMBREE_CURVE_SELF_INTERSECTION_AVOIDANCE_FACTOR)*r*pre.depth_scale; // ignore self intersections
85
86 /* update hit information */
87 bool ishit = false;
88 if (unlikely(any(valid))) {
89 DistanceCurveHit<NativeCurve3fa,VSIZEX> hit(valid,u,0.0f,t,0,N,curve3D);
90 ishit = ishit | epilog(valid,hit);
91 }
92
93 if (unlikely(VSIZEX < N))
94 {
95 /* process SIMD-size many segments per iteration */
96 for (int i=VSIZEX; i<N; i+=VSIZEX)
97 {
98 /* evaluate the bezier curve */
99 vboolx valid = vintx(i)+vintx(step) < vintx(N);
100 const Vec4vfx p0 = curve2D.template eval0<VSIZEX>(i,N);
101 const Vec4vfx p1 = curve2D.template eval1<VSIZEX>(i,N);
102
103 /* approximative intersection with cone */
104 const Vec4vfx v = p1-p0;
105 const Vec4vfx w = -p0;
106 const vfloatx d0 = madd(w.x,v.x,w.y*v.y);
107 const vfloatx d1 = madd(v.x,v.x,v.y*v.y);
108 const vfloatx u = clamp(d0*rcp(d1),vfloatx(zero),vfloatx(one));
109 const Vec4vfx p = madd(u,v,p0);
110 const vfloatx t = p.z*pre.depth_scale;
111 const vfloatx d2 = madd(p.x,p.x,p.y*p.y);
112 const vfloatx r = p.w;
113 const vfloatx r2 = r*r;
114 valid &= (d2 <= r2) & (vfloatx(ray.tnear()) <= t) & (t <= vfloatx(ray.tfar));
115 if (EMBREE_CURVE_SELF_INTERSECTION_AVOIDANCE_FACTOR != 0.0f)
116 valid &= t > float(EMBREE_CURVE_SELF_INTERSECTION_AVOIDANCE_FACTOR)*r*pre.depth_scale; // ignore self intersections
117
118 /* update hit information */
119 if (unlikely(any(valid))) {
120 DistanceCurveHit<NativeCurve3fa,VSIZEX> hit(valid,u,0.0f,t,i,N,curve3D);
121 ishit = ishit | epilog(valid,hit);
122 }
123 }
124 }
125 return ishit;
126 }
127 };
128 }
129}
130