line_intersector.h 6.2 KB

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