disc_intersector.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // Copyright 2009-2021 Intel Corporation
  2. // SPDX-License-Identifier: Apache-2.0
  3. #pragma once
  4. #include "../common/ray.h"
  5. #include "../common/scene_points.h"
  6. #include "curve_intersector_precalculations.h"
  7. namespace embree
  8. {
  9. namespace isa
  10. {
  11. template<int M>
  12. struct DiscIntersectorHitM
  13. {
  14. __forceinline DiscIntersectorHitM() {}
  15. __forceinline DiscIntersectorHitM(const vfloat<M>& u, const vfloat<M>& v, const vfloat<M>& t, const Vec3vf<M>& Ng)
  16. : vu(u), vv(v), vt(t), vNg(Ng)
  17. {
  18. }
  19. __forceinline void finalize() {}
  20. __forceinline Vec2f uv(const size_t i) const
  21. {
  22. return Vec2f(vu[i], vv[i]);
  23. }
  24. __forceinline float t(const size_t i) const
  25. {
  26. return vt[i];
  27. }
  28. __forceinline Vec3fa Ng(const size_t i) const
  29. {
  30. return Vec3fa(vNg.x[i], vNg.y[i], vNg.z[i]);
  31. }
  32. public:
  33. vfloat<M> vu;
  34. vfloat<M> vv;
  35. vfloat<M> vt;
  36. Vec3vf<M> vNg;
  37. };
  38. template<int M>
  39. struct DiscIntersector1
  40. {
  41. typedef CurvePrecalculations1 Precalculations;
  42. template<typename Epilog>
  43. static __forceinline bool intersect(
  44. const vbool<M>& valid_i,
  45. Ray& ray,
  46. IntersectContext* context,
  47. const Points* geom,
  48. const Precalculations& pre,
  49. const Vec4vf<M>& v0i,
  50. const Epilog& epilog)
  51. {
  52. vbool<M> valid = valid_i;
  53. const Vec3vf<M> ray_org(ray.org.x, ray.org.y, ray.org.z);
  54. const Vec3vf<M> ray_dir(ray.dir.x, ray.dir.y, ray.dir.z);
  55. const vfloat<M> rd2 = rcp(dot(ray_dir, ray_dir));
  56. const Vec4vf<M> v0 = enlargeRadiusToMinWidth<M>(context,geom,ray_org,v0i);
  57. const Vec3vf<M> center = v0.xyz();
  58. const vfloat<M> radius = v0.w;
  59. const Vec3vf<M> c0 = center - ray_org;
  60. const vfloat<M> projC0 = dot(c0, ray_dir) * rd2;
  61. valid &= (vfloat<M>(ray.tnear()) <= projC0) & (projC0 <= vfloat<M>(ray.tfar));
  62. if (EMBREE_CURVE_SELF_INTERSECTION_AVOIDANCE_FACTOR != 0.0f)
  63. valid &= projC0 > float(EMBREE_CURVE_SELF_INTERSECTION_AVOIDANCE_FACTOR) * radius * pre.depth_scale; // ignore self intersections
  64. if (unlikely(none(valid)))
  65. return false;
  66. const Vec3vf<M> perp = c0 - projC0 * ray_dir;
  67. const vfloat<M> l2 = dot(perp, perp);
  68. const vfloat<M> r2 = radius * radius;
  69. valid &= (l2 <= r2);
  70. if (unlikely(none(valid)))
  71. return false;
  72. DiscIntersectorHitM<M> hit(zero, zero, projC0, -ray_dir);
  73. return epilog(valid, hit);
  74. }
  75. template<typename Epilog>
  76. static __forceinline bool intersect(const vbool<M>& valid_i,
  77. Ray& ray,
  78. IntersectContext* context,
  79. const Points* geom,
  80. const Precalculations& pre,
  81. const Vec4vf<M>& v0i,
  82. const Vec3vf<M>& normal,
  83. const Epilog& epilog)
  84. {
  85. vbool<M> valid = valid_i;
  86. const Vec3vf<M> ray_org(ray.org.x, ray.org.y, ray.org.z);
  87. const Vec4vf<M> v0 = enlargeRadiusToMinWidth<M>(context,geom,ray_org,v0i);
  88. const Vec3vf<M> center = v0.xyz();
  89. const vfloat<M> radius = v0.w;
  90. vfloat<M> divisor = dot(Vec3vf<M>((Vec3fa)ray.dir), normal);
  91. const vbool<M> parallel = divisor == vfloat<M>(0.f);
  92. valid &= !parallel;
  93. divisor = select(parallel, 1.f, divisor); // prevent divide by zero
  94. vfloat<M> t = dot(center - Vec3vf<M>((Vec3fa)ray.org), Vec3vf<M>(normal)) / divisor;
  95. valid &= (vfloat<M>(ray.tnear()) <= t) & (t <= vfloat<M>(ray.tfar));
  96. if (unlikely(none(valid)))
  97. return false;
  98. Vec3vf<M> intersection = Vec3vf<M>((Vec3fa)ray.org) + Vec3vf<M>((Vec3fa)ray.dir) * t;
  99. vfloat<M> dist2 = dot(intersection - center, intersection - center);
  100. valid &= dist2 < radius * radius;
  101. if (unlikely(none(valid)))
  102. return false;
  103. DiscIntersectorHitM<M> hit(zero, zero, t, normal);
  104. return epilog(valid, hit);
  105. }
  106. };
  107. template<int M, int K>
  108. struct DiscIntersectorK
  109. {
  110. typedef CurvePrecalculationsK<K> Precalculations;
  111. template<typename Epilog>
  112. static __forceinline bool intersect(const vbool<M>& valid_i,
  113. RayK<K>& ray,
  114. size_t k,
  115. IntersectContext* context,
  116. const Points* geom,
  117. const Precalculations& pre,
  118. const Vec4vf<M>& v0i,
  119. const Epilog& epilog)
  120. {
  121. vbool<M> valid = valid_i;
  122. const Vec3vf<M> ray_org(ray.org.x[k], ray.org.y[k], ray.org.z[k]);
  123. const Vec3vf<M> ray_dir(ray.dir.x[k], ray.dir.y[k], ray.dir.z[k]);
  124. const vfloat<M> rd2 = rcp(dot(ray_dir, ray_dir));
  125. const Vec4vf<M> v0 = enlargeRadiusToMinWidth<M>(context,geom,ray_org,v0i);
  126. const Vec3vf<M> center = v0.xyz();
  127. const vfloat<M> radius = v0.w;
  128. const Vec3vf<M> c0 = center - ray_org;
  129. const vfloat<M> projC0 = dot(c0, ray_dir) * rd2;
  130. valid &= (vfloat<M>(ray.tnear()[k]) <= projC0) & (projC0 <= vfloat<M>(ray.tfar[k]));
  131. if (EMBREE_CURVE_SELF_INTERSECTION_AVOIDANCE_FACTOR != 0.0f)
  132. valid &= projC0 > float(EMBREE_CURVE_SELF_INTERSECTION_AVOIDANCE_FACTOR) * radius * pre.depth_scale[k]; // ignore self intersections
  133. if (unlikely(none(valid)))
  134. return false;
  135. const Vec3vf<M> perp = c0 - projC0 * ray_dir;
  136. const vfloat<M> l2 = dot(perp, perp);
  137. const vfloat<M> r2 = radius * radius;
  138. valid &= (l2 <= r2);
  139. if (unlikely(none(valid)))
  140. return false;
  141. DiscIntersectorHitM<M> hit(zero, zero, projC0, -ray_dir);
  142. return epilog(valid, hit);
  143. }
  144. template<typename Epilog>
  145. static __forceinline bool intersect(const vbool<M>& valid_i,
  146. RayK<K>& ray,
  147. size_t k,
  148. IntersectContext* context,
  149. const Points* geom,
  150. const Precalculations& pre,
  151. const Vec4vf<M>& v0i,
  152. const Vec3vf<M>& normal,
  153. const Epilog& epilog)
  154. {
  155. vbool<M> valid = valid_i;
  156. const Vec3vf<M> ray_org(ray.org.x[k], ray.org.y[k], ray.org.z[k]);
  157. const Vec3vf<M> ray_dir(ray.dir.x[k], ray.dir.y[k], ray.dir.z[k]);
  158. const Vec4vf<M> v0 = enlargeRadiusToMinWidth<M>(context,geom,ray_org,v0i);
  159. const Vec3vf<M> center = v0.xyz();
  160. const vfloat<M> radius = v0.w;
  161. vfloat<M> divisor = dot(Vec3vf<M>(ray_dir), normal);
  162. const vbool<M> parallel = divisor == vfloat<M>(0.f);
  163. valid &= !parallel;
  164. divisor = select(parallel, 1.f, divisor); // prevent divide by zero
  165. vfloat<M> t = dot(center - Vec3vf<M>(ray_org), Vec3vf<M>(normal)) / divisor;
  166. valid &= (vfloat<M>(ray.tnear()[k]) <= t) & (t <= vfloat<M>(ray.tfar[k]));
  167. if (unlikely(none(valid)))
  168. return false;
  169. Vec3vf<M> intersection = Vec3vf<M>(ray_org) + Vec3vf<M>(ray_dir) * t;
  170. vfloat<M> dist2 = dot(intersection - center, intersection - center);
  171. valid &= dist2 < radius * radius;
  172. if (unlikely(none(valid)))
  173. return false;
  174. DiscIntersectorHitM<M> hit(zero, zero, t, normal);
  175. return epilog(valid, hit);
  176. }
  177. };
  178. } // namespace isa
  179. } // namespace embree