curve_intersector_ribbon.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // Copyright 2009-2021 Intel Corporation
  2. // SPDX-License-Identifier: Apache-2.0
  3. #pragma once
  4. #include "../common/ray.h"
  5. #include "quad_intersector.h"
  6. #include "curve_intersector_precalculations.h"
  7. #define Bezier1Intersector1 RibbonCurve1Intersector1
  8. #define Bezier1IntersectorK RibbonCurve1IntersectorK
  9. namespace embree
  10. {
  11. namespace isa
  12. {
  13. template<typename NativeCurve3ff, int M>
  14. struct RibbonHit
  15. {
  16. __forceinline RibbonHit() {}
  17. __forceinline RibbonHit(const vbool<M>& valid, const vfloat<M>& U, const vfloat<M>& V, const vfloat<M>& T, const int i, const int N,
  18. const NativeCurve3ff& curve3D)
  19. : U(U), V(V), T(T), i(i), N(N), curve3D(curve3D), valid(valid) {}
  20. __forceinline void finalize()
  21. {
  22. vu = (vfloat<M>(step)+U+vfloat<M>(float(i)))*(1.0f/float(N));
  23. vv = V;
  24. vt = T;
  25. }
  26. __forceinline Vec2f uv (const size_t i) const { return Vec2f(vu[i],vv[i]); }
  27. __forceinline float t (const size_t i) const { return vt[i]; }
  28. __forceinline Vec3fa Ng(const size_t i) const { return curve3D.eval_du(vu[i]); }
  29. __forceinline Vec2vf<M> uv() const { return Vec2vf<M>(vu,vv); }
  30. __forceinline vfloat<M> t () const { return vt; }
  31. __forceinline Vec3vf<M> Ng() const { return (Vec3vf<M>) curve3D.template veval_du<M>(vu); }
  32. public:
  33. vfloat<M> U;
  34. vfloat<M> V;
  35. vfloat<M> T;
  36. int i, N;
  37. NativeCurve3ff curve3D;
  38. public:
  39. vbool<M> valid;
  40. vfloat<M> vu;
  41. vfloat<M> vv;
  42. vfloat<M> vt;
  43. };
  44. /* calculate squared distance of point p0 to line p1->p2 */
  45. __forceinline std::pair<vfloatx,vfloatx> sqr_point_line_distance(const Vec2vfx& p0, const Vec2vfx& p1, const Vec2vfx& p2)
  46. {
  47. const vfloatx num = det(p2-p1,p1-p0);
  48. const vfloatx den2 = dot(p2-p1,p2-p1);
  49. return std::make_pair(num*num,den2);
  50. }
  51. /* performs culling against a cylinder */
  52. __forceinline vboolx cylinder_culling_test(const Vec2vfx& p0, const Vec2vfx& p1, const Vec2vfx& p2, const vfloatx& r)
  53. {
  54. const std::pair<vfloatx,vfloatx> d = sqr_point_line_distance(p0,p1,p2);
  55. return d.first <= r*r*d.second;
  56. }
  57. template<typename NativeCurve3ff, typename Epilog>
  58. __forceinline bool intersect_ribbon(const Vec3fa& ray_org, const Vec3fa& ray_dir, const float ray_tnear, const float& ray_tfar,
  59. const LinearSpace3fa& ray_space, const float& depth_scale,
  60. const NativeCurve3ff& curve3D, const int N,
  61. const Epilog& epilog)
  62. {
  63. /* transform control points into ray space */
  64. const NativeCurve3ff curve2D = curve3D.xfm_pr(ray_space,ray_org);
  65. float eps = 4.0f*float(ulp)*reduce_max(max(abs(curve2D.v0),abs(curve2D.v1),abs(curve2D.v2),abs(curve2D.v3)));
  66. /* evaluate the bezier curve */
  67. bool ishit = false;
  68. vboolx valid = vfloatx(step) < vfloatx(float(N));
  69. const Vec4vfx p0 = curve2D.template eval0<VSIZEX>(0,N);
  70. const Vec4vfx p1 = curve2D.template eval1<VSIZEX>(0,N);
  71. valid &= cylinder_culling_test(zero,Vec2vfx(p0.x,p0.y),Vec2vfx(p1.x,p1.y),max(p0.w,p1.w));
  72. if (any(valid))
  73. {
  74. Vec3vfx dp0dt = curve2D.template derivative0<VSIZEX>(0,N);
  75. Vec3vfx dp1dt = curve2D.template derivative1<VSIZEX>(0,N);
  76. dp0dt = select(reduce_max(abs(dp0dt)) < vfloatx(eps),Vec3vfx(p1-p0),dp0dt);
  77. dp1dt = select(reduce_max(abs(dp1dt)) < vfloatx(eps),Vec3vfx(p1-p0),dp1dt);
  78. const Vec3vfx n0(dp0dt.y,-dp0dt.x,0.0f);
  79. const Vec3vfx n1(dp1dt.y,-dp1dt.x,0.0f);
  80. const Vec3vfx nn0 = normalize(n0);
  81. const Vec3vfx nn1 = normalize(n1);
  82. const Vec3vfx lp0 = madd(p0.w,nn0,Vec3vfx(p0));
  83. const Vec3vfx lp1 = madd(p1.w,nn1,Vec3vfx(p1));
  84. const Vec3vfx up0 = nmadd(p0.w,nn0,Vec3vfx(p0));
  85. const Vec3vfx up1 = nmadd(p1.w,nn1,Vec3vfx(p1));
  86. vfloatx vu,vv,vt;
  87. vboolx valid0 = intersect_quad_backface_culling<VSIZEX>(valid,zero,Vec3fa(0,0,1),ray_tnear,ray_tfar,lp0,lp1,up1,up0,vu,vv,vt);
  88. if (any(valid0))
  89. {
  90. /* ignore self intersections */
  91. if (EMBREE_CURVE_SELF_INTERSECTION_AVOIDANCE_FACTOR != 0.0f) {
  92. vfloatx r = lerp(p0.w, p1.w, vu);
  93. valid0 &= vt > float(EMBREE_CURVE_SELF_INTERSECTION_AVOIDANCE_FACTOR)*r*depth_scale;
  94. }
  95. if (any(valid0))
  96. {
  97. vv = madd(2.0f,vv,vfloatx(-1.0f));
  98. RibbonHit<NativeCurve3ff,VSIZEX> bhit(valid0,vu,vv,vt,0,N,curve3D);
  99. ishit |= epilog(bhit.valid,bhit);
  100. }
  101. }
  102. }
  103. if (unlikely(VSIZEX < N))
  104. {
  105. /* process SIMD-size many segments per iteration */
  106. for (int i=VSIZEX; i<N; i+=VSIZEX)
  107. {
  108. /* evaluate the bezier curve */
  109. vboolx valid = vintx(i)+vintx(step) < vintx(N);
  110. const Vec4vfx p0 = curve2D.template eval0<VSIZEX>(i,N);
  111. const Vec4vfx p1 = curve2D.template eval1<VSIZEX>(i,N);
  112. valid &= cylinder_culling_test(zero,Vec2vfx(p0.x,p0.y),Vec2vfx(p1.x,p1.y),max(p0.w,p1.w));
  113. if (none(valid)) continue;
  114. Vec3vfx dp0dt = curve2D.template derivative0<VSIZEX>(i,N);
  115. Vec3vfx dp1dt = curve2D.template derivative1<VSIZEX>(i,N);
  116. dp0dt = select(reduce_max(abs(dp0dt)) < vfloatx(eps),Vec3vfx(p1-p0),dp0dt);
  117. dp1dt = select(reduce_max(abs(dp1dt)) < vfloatx(eps),Vec3vfx(p1-p0),dp1dt);
  118. const Vec3vfx n0(dp0dt.y,-dp0dt.x,0.0f);
  119. const Vec3vfx n1(dp1dt.y,-dp1dt.x,0.0f);
  120. const Vec3vfx nn0 = normalize(n0);
  121. const Vec3vfx nn1 = normalize(n1);
  122. const Vec3vfx lp0 = madd(p0.w,nn0,Vec3vfx(p0));
  123. const Vec3vfx lp1 = madd(p1.w,nn1,Vec3vfx(p1));
  124. const Vec3vfx up0 = nmadd(p0.w,nn0,Vec3vfx(p0));
  125. const Vec3vfx up1 = nmadd(p1.w,nn1,Vec3vfx(p1));
  126. vfloatx vu,vv,vt;
  127. vboolx valid0 = intersect_quad_backface_culling<VSIZEX>(valid,zero,Vec3fa(0,0,1),ray_tnear,ray_tfar,lp0,lp1,up1,up0,vu,vv,vt);
  128. if (any(valid0))
  129. {
  130. /* ignore self intersections */
  131. if (EMBREE_CURVE_SELF_INTERSECTION_AVOIDANCE_FACTOR != 0.0f) {
  132. vfloatx r = lerp(p0.w, p1.w, vu);
  133. valid0 &= vt > float(EMBREE_CURVE_SELF_INTERSECTION_AVOIDANCE_FACTOR)*r*depth_scale;
  134. }
  135. if (any(valid0))
  136. {
  137. vv = madd(2.0f,vv,vfloatx(-1.0f));
  138. RibbonHit<NativeCurve3ff,VSIZEX> bhit(valid0,vu,vv,vt,i,N,curve3D);
  139. ishit |= epilog(bhit.valid,bhit);
  140. }
  141. }
  142. }
  143. }
  144. return ishit;
  145. }
  146. template<template<typename Ty> class NativeCurve>
  147. struct RibbonCurve1Intersector1
  148. {
  149. typedef NativeCurve<Vec3ff> NativeCurve3ff;
  150. template<typename Epilog>
  151. __forceinline bool intersect(const CurvePrecalculations1& pre, Ray& ray,
  152. IntersectContext* context,
  153. const CurveGeometry* geom, const unsigned int primID,
  154. const Vec3ff& v0, const Vec3ff& v1, const Vec3ff& v2, const Vec3ff& v3,
  155. const Epilog& epilog)
  156. {
  157. const int N = geom->tessellationRate;
  158. NativeCurve3ff curve(v0,v1,v2,v3);
  159. curve = enlargeRadiusToMinWidth(context,geom,ray.org,curve);
  160. return intersect_ribbon<NativeCurve3ff>(ray.org,ray.dir,ray.tnear(),ray.tfar,
  161. pre.ray_space,pre.depth_scale,
  162. curve,N,
  163. epilog);
  164. }
  165. };
  166. template<template<typename Ty> class NativeCurve, int K>
  167. struct RibbonCurve1IntersectorK
  168. {
  169. typedef NativeCurve<Vec3ff> NativeCurve3ff;
  170. template<typename Epilog>
  171. __forceinline bool intersect(const CurvePrecalculationsK<K>& pre, RayK<K>& ray, size_t k,
  172. IntersectContext* context,
  173. const CurveGeometry* geom, const unsigned int primID,
  174. const Vec3ff& v0, const Vec3ff& v1, const Vec3ff& v2, const Vec3ff& v3,
  175. const Epilog& epilog)
  176. {
  177. const int N = geom->tessellationRate;
  178. const Vec3fa ray_org(ray.org.x[k],ray.org.y[k],ray.org.z[k]);
  179. const Vec3fa ray_dir(ray.dir.x[k],ray.dir.y[k],ray.dir.z[k]);
  180. NativeCurve3ff curve(v0,v1,v2,v3);
  181. curve = enlargeRadiusToMinWidth(context,geom,ray_org,curve);
  182. return intersect_ribbon<NativeCurve3ff>(ray_org,ray_dir,ray.tnear()[k],ray.tfar[k],
  183. pre.ray_space[k],pre.depth_scale[k],
  184. curve,N,
  185. epilog);
  186. }
  187. };
  188. }
  189. }