curve_intersector_distance.h 4.8 KB

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