disc_intersector.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. /* compute ray distance projC0 to hit point with ray oriented plane */
  60. const Vec3vf<M> c0 = center - ray_org;
  61. const vfloat<M> projC0 = dot(c0, ray_dir) * rd2;
  62. valid &= (vfloat<M>(ray.tnear()) <= projC0) & (projC0 <= vfloat<M>(ray.tfar));
  63. if (unlikely(none(valid)))
  64. return false;
  65. /* check if hit point lies inside disc */
  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. /* We reject hits where the ray origin lies inside the ray
  73. * oriented disc to avoid self intersections. */
  74. #if defined(EMBREE_DISC_POINT_SELF_INTERSECTION_AVOIDANCE)
  75. const vfloat<M> m2 = dot(c0, c0);
  76. valid &= (m2 > r2);
  77. if (unlikely(none(valid)))
  78. return false;
  79. #endif
  80. DiscIntersectorHitM<M> hit(zero, zero, projC0, -ray_dir);
  81. return epilog(valid, hit);
  82. }
  83. template<typename Epilog>
  84. static __forceinline bool intersect(const vbool<M>& valid_i,
  85. Ray& ray,
  86. IntersectContext* context,
  87. const Points* geom,
  88. const Precalculations& pre,
  89. const Vec4vf<M>& v0i,
  90. const Vec3vf<M>& normal,
  91. const Epilog& epilog)
  92. {
  93. vbool<M> valid = valid_i;
  94. const Vec3vf<M> ray_org(ray.org.x, ray.org.y, ray.org.z);
  95. const Vec4vf<M> v0 = enlargeRadiusToMinWidth<M>(context,geom,ray_org,v0i);
  96. const Vec3vf<M> center = v0.xyz();
  97. const vfloat<M> radius = v0.w;
  98. vfloat<M> divisor = dot(Vec3vf<M>((Vec3fa)ray.dir), normal);
  99. const vbool<M> parallel = divisor == vfloat<M>(0.f);
  100. valid &= !parallel;
  101. divisor = select(parallel, 1.f, divisor); // prevent divide by zero
  102. vfloat<M> t = dot(center - Vec3vf<M>((Vec3fa)ray.org), Vec3vf<M>(normal)) / divisor;
  103. valid &= (vfloat<M>(ray.tnear()) <= t) & (t <= vfloat<M>(ray.tfar));
  104. if (unlikely(none(valid)))
  105. return false;
  106. Vec3vf<M> intersection = Vec3vf<M>((Vec3fa)ray.org) + Vec3vf<M>((Vec3fa)ray.dir) * t;
  107. vfloat<M> dist2 = dot(intersection - center, intersection - center);
  108. valid &= dist2 < radius * radius;
  109. if (unlikely(none(valid)))
  110. return false;
  111. DiscIntersectorHitM<M> hit(zero, zero, t, normal);
  112. return epilog(valid, hit);
  113. }
  114. };
  115. template<int M, int K>
  116. struct DiscIntersectorK
  117. {
  118. typedef CurvePrecalculationsK<K> Precalculations;
  119. template<typename Epilog>
  120. static __forceinline bool intersect(const vbool<M>& valid_i,
  121. RayK<K>& ray,
  122. size_t k,
  123. IntersectContext* context,
  124. const Points* geom,
  125. const Precalculations& pre,
  126. const Vec4vf<M>& v0i,
  127. const Epilog& epilog)
  128. {
  129. vbool<M> valid = valid_i;
  130. const Vec3vf<M> ray_org(ray.org.x[k], ray.org.y[k], ray.org.z[k]);
  131. const Vec3vf<M> ray_dir(ray.dir.x[k], ray.dir.y[k], ray.dir.z[k]);
  132. const vfloat<M> rd2 = rcp(dot(ray_dir, ray_dir));
  133. const Vec4vf<M> v0 = enlargeRadiusToMinWidth<M>(context,geom,ray_org,v0i);
  134. const Vec3vf<M> center = v0.xyz();
  135. const vfloat<M> radius = v0.w;
  136. /* compute ray distance projC0 to hit point with ray oriented plane */
  137. const Vec3vf<M> c0 = center - ray_org;
  138. const vfloat<M> projC0 = dot(c0, ray_dir) * rd2;
  139. valid &= (vfloat<M>(ray.tnear()[k]) <= projC0) & (projC0 <= vfloat<M>(ray.tfar[k]));
  140. if (unlikely(none(valid)))
  141. return false;
  142. /* check if hit point lies inside disc */
  143. const Vec3vf<M> perp = c0 - projC0 * ray_dir;
  144. const vfloat<M> l2 = dot(perp, perp);
  145. const vfloat<M> r2 = radius * radius;
  146. valid &= (l2 <= r2);
  147. if (unlikely(none(valid)))
  148. return false;
  149. /* We reject hits where the ray origin lies inside the ray
  150. * oriented disc to avoid self intersections. */
  151. #if defined(EMBREE_DISC_POINT_SELF_INTERSECTION_AVOIDANCE)
  152. const vfloat<M> m2 = dot(c0, c0);
  153. valid &= (m2 > r2);
  154. if (unlikely(none(valid)))
  155. return false;
  156. #endif
  157. DiscIntersectorHitM<M> hit(zero, zero, projC0, -ray_dir);
  158. return epilog(valid, hit);
  159. }
  160. template<typename Epilog>
  161. static __forceinline bool intersect(const vbool<M>& valid_i,
  162. RayK<K>& ray,
  163. size_t k,
  164. IntersectContext* context,
  165. const Points* geom,
  166. const Precalculations& pre,
  167. const Vec4vf<M>& v0i,
  168. const Vec3vf<M>& normal,
  169. const Epilog& epilog)
  170. {
  171. vbool<M> valid = valid_i;
  172. const Vec3vf<M> ray_org(ray.org.x[k], ray.org.y[k], ray.org.z[k]);
  173. const Vec3vf<M> ray_dir(ray.dir.x[k], ray.dir.y[k], ray.dir.z[k]);
  174. const Vec4vf<M> v0 = enlargeRadiusToMinWidth<M>(context,geom,ray_org,v0i);
  175. const Vec3vf<M> center = v0.xyz();
  176. const vfloat<M> radius = v0.w;
  177. vfloat<M> divisor = dot(Vec3vf<M>(ray_dir), normal);
  178. const vbool<M> parallel = divisor == vfloat<M>(0.f);
  179. valid &= !parallel;
  180. divisor = select(parallel, 1.f, divisor); // prevent divide by zero
  181. vfloat<M> t = dot(center - Vec3vf<M>(ray_org), Vec3vf<M>(normal)) / divisor;
  182. valid &= (vfloat<M>(ray.tnear()[k]) <= t) & (t <= vfloat<M>(ray.tfar[k]));
  183. if (unlikely(none(valid)))
  184. return false;
  185. Vec3vf<M> intersection = Vec3vf<M>(ray_org) + Vec3vf<M>(ray_dir) * t;
  186. vfloat<M> dist2 = dot(intersection - center, intersection - center);
  187. valid &= dist2 < radius * radius;
  188. if (unlikely(none(valid)))
  189. return false;
  190. DiscIntersectorHitM<M> hit(zero, zero, t, normal);
  191. return epilog(valid, hit);
  192. }
  193. };
  194. } // namespace isa
  195. } // namespace embree