curve_intersector_sweep.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. // Copyright 2009-2021 Intel Corporation
  2. // SPDX-License-Identifier: Apache-2.0
  3. #pragma once
  4. #include "../common/ray.h"
  5. #include "cylinder.h"
  6. #include "plane.h"
  7. #include "line_intersector.h"
  8. #include "curve_intersector_precalculations.h"
  9. namespace embree
  10. {
  11. namespace isa
  12. {
  13. static const size_t numJacobianIterations = 5;
  14. #if defined(__AVX__)
  15. static const size_t numBezierSubdivisions = 2;
  16. #else
  17. static const size_t numBezierSubdivisions = 3;
  18. #endif
  19. struct BezierCurveHit
  20. {
  21. __forceinline BezierCurveHit() {}
  22. __forceinline BezierCurveHit(const float t, const float u, const Vec3fa& Ng)
  23. : t(t), u(u), v(0.0f), Ng(Ng) {}
  24. __forceinline BezierCurveHit(const float t, const float u, const float v, const Vec3fa& Ng)
  25. : t(t), u(u), v(v), Ng(Ng) {}
  26. __forceinline void finalize() {}
  27. public:
  28. float t;
  29. float u;
  30. float v;
  31. Vec3fa Ng;
  32. };
  33. template<typename NativeCurve3ff, typename Ray, typename Epilog>
  34. __forceinline bool intersect_bezier_iterative_debug(const Ray& ray, const float dt, const NativeCurve3ff& curve, size_t i,
  35. const vfloatx& u, const BBox<vfloatx>& tp, const BBox<vfloatx>& h0, const BBox<vfloatx>& h1,
  36. const Vec3vfx& Ng, const Vec4vfx& dP0du, const Vec4vfx& dP3du,
  37. const Epilog& epilog)
  38. {
  39. if (tp.lower[i]+dt > ray.tfar) return false;
  40. Vec3fa Ng_o = Vec3fa(Ng.x[i],Ng.y[i],Ng.z[i]);
  41. if (h0.lower[i] == tp.lower[i]) Ng_o = -Vec3fa(dP0du.x[i],dP0du.y[i],dP0du.z[i]);
  42. if (h1.lower[i] == tp.lower[i]) Ng_o = +Vec3fa(dP3du.x[i],dP3du.y[i],dP3du.z[i]);
  43. BezierCurveHit hit(tp.lower[i]+dt,u[i],Ng_o);
  44. return epilog(hit);
  45. }
  46. template<typename NativeCurve3ff, typename Ray, typename Epilog>
  47. __forceinline bool intersect_bezier_iterative_jacobian(const Ray& ray, const float dt, const NativeCurve3ff& curve, float u, float t, const Epilog& epilog)
  48. {
  49. const Vec3fa org = zero;
  50. const Vec3fa dir = ray.dir;
  51. const float length_ray_dir = length(dir);
  52. /* error of curve evaluations is proportional to largest coordinate */
  53. const BBox3ff box = curve.bounds();
  54. const float P_err = 16.0f*float(ulp)*reduce_max(max(abs(box.lower),abs(box.upper)));
  55. for (size_t i=0; i<numJacobianIterations; i++)
  56. {
  57. const Vec3fa Q = madd(Vec3fa(t),dir,org);
  58. //const Vec3fa dQdu = zero;
  59. const Vec3fa dQdt = dir;
  60. const float Q_err = 16.0f*float(ulp)*length_ray_dir*t; // works as org=zero here
  61. Vec3ff P,dPdu,ddPdu; curve.eval(u,P,dPdu,ddPdu);
  62. //const Vec3fa dPdt = zero;
  63. const Vec3fa R = Q-P;
  64. const float len_R = length(R); //reduce_max(abs(R));
  65. const float R_err = max(Q_err,P_err);
  66. const Vec3fa dRdu = /*dQdu*/-dPdu;
  67. const Vec3fa dRdt = dQdt;//-dPdt;
  68. const Vec3fa T = normalize(dPdu);
  69. const Vec3fa dTdu = dnormalize(dPdu,ddPdu);
  70. //const Vec3fa dTdt = zero;
  71. const float cos_err = P_err/length(dPdu);
  72. /* Error estimate for dot(R,T):
  73. dot(R,T) = cos(R,T) |R| |T|
  74. = (cos(R,T) +- cos_error) * (|R| +- |R|_err) * (|T| +- |T|_err)
  75. = cos(R,T)*|R|*|T|
  76. +- cos(R,T)*(|R|*|T|_err + |T|*|R|_err)
  77. +- cos_error*(|R| + |T|)
  78. +- lower order terms
  79. with cos(R,T) being in [0,1] and |T| = 1 we get:
  80. dot(R,T)_err = |R|*|T|_err + |R|_err = cos_error*(|R|+1)
  81. */
  82. const float f = dot(R,T);
  83. const float f_err = len_R*P_err + R_err + cos_err*(1.0f+len_R);
  84. const float dfdu = dot(dRdu,T) + dot(R,dTdu);
  85. const float dfdt = dot(dRdt,T);// + dot(R,dTdt);
  86. const float K = dot(R,R)-sqr(f);
  87. const float dKdu = /*2.0f*/(dot(R,dRdu)-f*dfdu);
  88. const float dKdt = /*2.0f*/(dot(R,dRdt)-f*dfdt);
  89. const float rsqrt_K = rsqrt(K);
  90. const float g = sqrt(K)-P.w;
  91. const float g_err = R_err + f_err + 16.0f*float(ulp)*box.upper.w;
  92. const float dgdu = /*0.5f*/dKdu*rsqrt_K-dPdu.w;
  93. const float dgdt = /*0.5f*/dKdt*rsqrt_K;//-dPdt.w;
  94. const LinearSpace2f J = LinearSpace2f(dfdu,dfdt,dgdu,dgdt);
  95. const Vec2f dut = rcp(J)*Vec2f(f,g);
  96. const Vec2f ut = Vec2f(u,t) - dut;
  97. u = ut.x; t = ut.y;
  98. if (abs(f) < f_err && abs(g) < g_err)
  99. {
  100. t+=dt;
  101. if (!(ray.tnear() <= t && t <= ray.tfar)) return false; // rejects NaNs
  102. if (!(u >= 0.0f && u <= 1.0f)) return false; // rejects NaNs
  103. const Vec3fa R = normalize(Q-P);
  104. const Vec3fa U = madd(Vec3fa(dPdu.w),R,dPdu);
  105. const Vec3fa V = cross(dPdu,R);
  106. BezierCurveHit hit(t,u,cross(V,U));
  107. return epilog(hit);
  108. }
  109. }
  110. return false;
  111. }
  112. template<typename NativeCurve3ff, typename Ray, typename Epilog>
  113. bool intersect_bezier_recursive_jacobian(const Ray& ray, const float dt, const NativeCurve3ff& curve,
  114. float u0, float u1, unsigned int depth, const Epilog& epilog)
  115. {
  116. #if defined(__AVX__)
  117. enum { VSIZEX_ = 8 };
  118. typedef vbool8 vboolx; // maximally 8-wide to work around KNL issues
  119. typedef vint8 vintx;
  120. typedef vfloat8 vfloatx;
  121. #else
  122. enum { VSIZEX_ = 4 };
  123. typedef vbool4 vboolx;
  124. typedef vint4 vintx;
  125. typedef vfloat4 vfloatx;
  126. #endif
  127. typedef Vec3<vfloatx> Vec3vfx;
  128. typedef Vec4<vfloatx> Vec4vfx;
  129. unsigned int maxDepth = numBezierSubdivisions;
  130. bool found = false;
  131. const Vec3fa org = zero;
  132. const Vec3fa dir = ray.dir;
  133. unsigned int sptr = 0;
  134. const unsigned int stack_size = numBezierSubdivisions+1; // +1 because of unstable workaround below
  135. struct StackEntry {
  136. vboolx valid;
  137. vfloatx tlower;
  138. float u0;
  139. float u1;
  140. unsigned int depth;
  141. };
  142. StackEntry stack[stack_size];
  143. goto entry;
  144. /* terminate if stack is empty */
  145. while (sptr)
  146. {
  147. /* pop from stack */
  148. {
  149. sptr--;
  150. vboolx valid = stack[sptr].valid;
  151. const vfloatx tlower = stack[sptr].tlower;
  152. valid &= tlower+dt <= ray.tfar;
  153. if (none(valid)) continue;
  154. u0 = stack[sptr].u0;
  155. u1 = stack[sptr].u1;
  156. depth = stack[sptr].depth;
  157. const size_t i = select_min(valid,tlower); clear(valid,i);
  158. stack[sptr].valid = valid;
  159. if (any(valid)) sptr++; // there are still items on the stack
  160. /* process next segment */
  161. const vfloatx vu0 = lerp(u0,u1,vfloatx(step)*(1.0f/(vfloatx::size-1)));
  162. u0 = vu0[i+0];
  163. u1 = vu0[i+1];
  164. }
  165. entry:
  166. /* subdivide curve */
  167. const float dscale = (u1-u0)*(1.0f/(3.0f*(vfloatx::size-1)));
  168. const vfloatx vu0 = lerp(u0,u1,vfloatx(step)*(1.0f/(vfloatx::size-1)));
  169. Vec4vfx P0, dP0du; curve.template veval<VSIZEX_>(vu0,P0,dP0du); dP0du = dP0du * Vec4vfx(dscale);
  170. const Vec4vfx P3 = shift_right_1(P0);
  171. const Vec4vfx dP3du = shift_right_1(dP0du);
  172. const Vec4vfx P1 = P0 + dP0du;
  173. const Vec4vfx P2 = P3 - dP3du;
  174. /* calculate bounding cylinders */
  175. const vfloatx rr1 = sqr_point_to_line_distance(Vec3vfx(dP0du),Vec3vfx(P3-P0));
  176. const vfloatx rr2 = sqr_point_to_line_distance(Vec3vfx(dP3du),Vec3vfx(P3-P0));
  177. const vfloatx maxr12 = sqrt(max(rr1,rr2));
  178. const vfloatx one_plus_ulp = 1.0f+2.0f*float(ulp);
  179. const vfloatx one_minus_ulp = 1.0f-2.0f*float(ulp);
  180. vfloatx r_outer = max(P0.w,P1.w,P2.w,P3.w)+maxr12;
  181. vfloatx r_inner = min(P0.w,P1.w,P2.w,P3.w)-maxr12;
  182. r_outer = one_plus_ulp*r_outer;
  183. r_inner = max(0.0f,one_minus_ulp*r_inner);
  184. const CylinderN<vfloatx::size> cylinder_outer(Vec3vfx(P0),Vec3vfx(P3),r_outer);
  185. const CylinderN<vfloatx::size> cylinder_inner(Vec3vfx(P0),Vec3vfx(P3),r_inner);
  186. vboolx valid = true; clear(valid,vfloatx::size-1);
  187. /* intersect with outer cylinder */
  188. BBox<vfloatx> tc_outer; vfloatx u_outer0; Vec3vfx Ng_outer0; vfloatx u_outer1; Vec3vfx Ng_outer1;
  189. valid &= cylinder_outer.intersect(org,dir,tc_outer,u_outer0,Ng_outer0,u_outer1,Ng_outer1);
  190. if (none(valid)) continue;
  191. /* intersect with cap-planes */
  192. BBox<vfloatx> tp(ray.tnear()-dt,ray.tfar-dt);
  193. tp = embree::intersect(tp,tc_outer);
  194. BBox<vfloatx> h0 = HalfPlaneN<vfloatx::size>(Vec3vfx(P0),+Vec3vfx(dP0du)).intersect(org,dir);
  195. tp = embree::intersect(tp,h0);
  196. BBox<vfloatx> h1 = HalfPlaneN<vfloatx::size>(Vec3vfx(P3),-Vec3vfx(dP3du)).intersect(org,dir);
  197. tp = embree::intersect(tp,h1);
  198. valid &= tp.lower <= tp.upper;
  199. if (none(valid)) continue;
  200. /* clamp and correct u parameter */
  201. u_outer0 = clamp(u_outer0,vfloatx(0.0f),vfloatx(1.0f));
  202. u_outer1 = clamp(u_outer1,vfloatx(0.0f),vfloatx(1.0f));
  203. u_outer0 = lerp(u0,u1,(vfloatx(step)+u_outer0)*(1.0f/float(vfloatx::size)));
  204. u_outer1 = lerp(u0,u1,(vfloatx(step)+u_outer1)*(1.0f/float(vfloatx::size)));
  205. /* intersect with inner cylinder */
  206. BBox<vfloatx> tc_inner;
  207. vfloatx u_inner0 = zero; Vec3vfx Ng_inner0 = zero; vfloatx u_inner1 = zero; Vec3vfx Ng_inner1 = zero;
  208. const vboolx valid_inner = cylinder_inner.intersect(org,dir,tc_inner,u_inner0,Ng_inner0,u_inner1,Ng_inner1);
  209. /* at the unstable area we subdivide deeper */
  210. const vboolx unstable0 = (!valid_inner) | (abs(dot(Vec3vfx(Vec3fa(ray.dir)),Ng_inner0)) < 0.3f);
  211. const vboolx unstable1 = (!valid_inner) | (abs(dot(Vec3vfx(Vec3fa(ray.dir)),Ng_inner1)) < 0.3f);
  212. /* subtract the inner interval from the current hit interval */
  213. BBox<vfloatx> tp0, tp1;
  214. subtract(tp,tc_inner,tp0,tp1);
  215. vboolx valid0 = valid & (tp0.lower <= tp0.upper);
  216. vboolx valid1 = valid & (tp1.lower <= tp1.upper);
  217. if (none(valid0 | valid1)) continue;
  218. /* iterate over all first hits front to back */
  219. const vintx termDepth0 = select(unstable0,vintx(maxDepth+1),vintx(maxDepth));
  220. vboolx recursion_valid0 = valid0 & (depth < termDepth0);
  221. valid0 &= depth >= termDepth0;
  222. while (any(valid0))
  223. {
  224. const size_t i = select_min(valid0,tp0.lower); clear(valid0,i);
  225. found = found | intersect_bezier_iterative_jacobian(ray,dt,curve,u_outer0[i],tp0.lower[i],epilog);
  226. //found = found | intersect_bezier_iterative_debug (ray,dt,curve,i,u_outer0,tp0,h0,h1,Ng_outer0,dP0du,dP3du,epilog);
  227. valid0 &= tp0.lower+dt <= ray.tfar;
  228. }
  229. valid1 &= tp1.lower+dt <= ray.tfar;
  230. /* iterate over all second hits front to back */
  231. const vintx termDepth1 = select(unstable1,vintx(maxDepth+1),vintx(maxDepth));
  232. vboolx recursion_valid1 = valid1 & (depth < termDepth1);
  233. valid1 &= depth >= termDepth1;
  234. while (any(valid1))
  235. {
  236. const size_t i = select_min(valid1,tp1.lower); clear(valid1,i);
  237. found = found | intersect_bezier_iterative_jacobian(ray,dt,curve,u_outer1[i],tp1.upper[i],epilog);
  238. //found = found | intersect_bezier_iterative_debug (ray,dt,curve,i,u_outer1,tp1,h0,h1,Ng_outer1,dP0du,dP3du,epilog);
  239. valid1 &= tp1.lower+dt <= ray.tfar;
  240. }
  241. /* push valid segments to stack */
  242. recursion_valid0 &= tp0.lower+dt <= ray.tfar;
  243. recursion_valid1 &= tp1.lower+dt <= ray.tfar;
  244. const vboolx recursion_valid = recursion_valid0 | recursion_valid1;
  245. if (any(recursion_valid))
  246. {
  247. assert(sptr < stack_size);
  248. stack[sptr].valid = recursion_valid;
  249. stack[sptr].tlower = select(recursion_valid0,tp0.lower,tp1.lower);
  250. stack[sptr].u0 = u0;
  251. stack[sptr].u1 = u1;
  252. stack[sptr].depth = depth+1;
  253. sptr++;
  254. }
  255. }
  256. return found;
  257. }
  258. template<template<typename Ty> class NativeCurve>
  259. struct SweepCurve1Intersector1
  260. {
  261. typedef NativeCurve<Vec3ff> NativeCurve3ff;
  262. template<typename Epilog>
  263. __noinline bool intersect(const CurvePrecalculations1& pre, Ray& ray,
  264. IntersectContext* context,
  265. const CurveGeometry* geom, const unsigned int primID,
  266. const Vec3ff& v0, const Vec3ff& v1, const Vec3ff& v2, const Vec3ff& v3,
  267. const Epilog& epilog)
  268. {
  269. STAT3(normal.trav_prims,1,1,1);
  270. /* move ray closer to make intersection stable */
  271. NativeCurve3ff curve0(v0,v1,v2,v3);
  272. curve0 = enlargeRadiusToMinWidth(context,geom,ray.org,curve0);
  273. const float dt = dot(curve0.center()-ray.org,ray.dir)*rcp(dot(ray.dir,ray.dir));
  274. const Vec3ff ref(madd(Vec3fa(dt),ray.dir,ray.org),0.0f);
  275. const NativeCurve3ff curve1 = curve0-ref;
  276. return intersect_bezier_recursive_jacobian(ray,dt,curve1,0.0f,1.0f,1,epilog);
  277. }
  278. };
  279. template<template<typename Ty> class NativeCurve, int K>
  280. struct SweepCurve1IntersectorK
  281. {
  282. typedef NativeCurve<Vec3ff> NativeCurve3ff;
  283. struct Ray1
  284. {
  285. __forceinline Ray1(RayK<K>& ray, size_t k)
  286. : org(ray.org.x[k],ray.org.y[k],ray.org.z[k]), dir(ray.dir.x[k],ray.dir.y[k],ray.dir.z[k]), _tnear(ray.tnear()[k]), tfar(ray.tfar[k]) {}
  287. Vec3fa org;
  288. Vec3fa dir;
  289. float _tnear;
  290. float& tfar;
  291. __forceinline float& tnear() { return _tnear; }
  292. //__forceinline float& tfar() { return _tfar; }
  293. __forceinline const float& tnear() const { return _tnear; }
  294. //__forceinline const float& tfar() const { return _tfar; }
  295. };
  296. template<typename Epilog>
  297. __forceinline bool intersect(const CurvePrecalculationsK<K>& pre, RayK<K>& vray, size_t k,
  298. IntersectContext* context,
  299. const CurveGeometry* geom, const unsigned int primID,
  300. const Vec3ff& v0, const Vec3ff& v1, const Vec3ff& v2, const Vec3ff& v3,
  301. const Epilog& epilog)
  302. {
  303. STAT3(normal.trav_prims,1,1,1);
  304. Ray1 ray(vray,k);
  305. /* move ray closer to make intersection stable */
  306. NativeCurve3ff curve0(v0,v1,v2,v3);
  307. curve0 = enlargeRadiusToMinWidth(context,geom,ray.org,curve0);
  308. const float dt = dot(curve0.center()-ray.org,ray.dir)*rcp(dot(ray.dir,ray.dir));
  309. const Vec3ff ref(madd(Vec3fa(dt),ray.dir,ray.org),0.0f);
  310. const NativeCurve3ff curve1 = curve0-ref;
  311. return intersect_bezier_recursive_jacobian(ray,dt,curve1,0.0f,1.0f,1,epilog);
  312. }
  313. };
  314. }
  315. }