disc_intersector.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. return Vec2f(vu[i], vv[i]);
  22. }
  23. __forceinline Vec2vf<M> uv() const {
  24. return Vec2vf<M>(vu, vv);
  25. }
  26. __forceinline float t(const size_t i) const {
  27. return vt[i];
  28. }
  29. __forceinline vfloat<M> t() const {
  30. return vt;
  31. }
  32. __forceinline Vec3fa Ng(const size_t i) const {
  33. return Vec3fa(vNg.x[i], vNg.y[i], vNg.z[i]);
  34. }
  35. __forceinline Vec3vf<M> Ng() const {
  36. return vNg;
  37. }
  38. public:
  39. vfloat<M> vu;
  40. vfloat<M> vv;
  41. vfloat<M> vt;
  42. Vec3vf<M> vNg;
  43. };
  44. template<>
  45. struct DiscIntersectorHitM<1>
  46. {
  47. __forceinline DiscIntersectorHitM() {}
  48. __forceinline DiscIntersectorHitM(const float& u, const float& v, const float& t, const Vec3fa& Ng)
  49. : vu(u), vv(v), vt(t), vNg(Ng) {}
  50. __forceinline void finalize() {}
  51. __forceinline Vec2f uv() const {
  52. return Vec2f(vu, vv);
  53. }
  54. __forceinline float t() const {
  55. return vt;
  56. }
  57. __forceinline Vec3fa Ng() const {
  58. return vNg;
  59. }
  60. public:
  61. float vu;
  62. float vv;
  63. float vt;
  64. Vec3fa vNg;
  65. };
  66. template<int M>
  67. struct DiscIntersector1
  68. {
  69. typedef CurvePrecalculations1 Precalculations;
  70. template<typename Ray, typename Epilog>
  71. static __forceinline bool intersect(
  72. const vbool<M>& valid_i,
  73. Ray& ray,
  74. RayQueryContext* context,
  75. const Points* geom,
  76. const Precalculations& pre,
  77. const Vec4vf<M>& v0i,
  78. const Epilog& epilog)
  79. {
  80. vbool<M> valid = valid_i;
  81. const Vec3vf<M> ray_org(ray.org.x, ray.org.y, ray.org.z);
  82. const Vec3vf<M> ray_dir(ray.dir.x, ray.dir.y, ray.dir.z);
  83. const vfloat<M> rd2 = rcp(dot(ray_dir, ray_dir));
  84. const Vec4vf<M> v0 = enlargeRadiusToMinWidth<M>(context,geom,ray_org,v0i);
  85. const Vec3vf<M> center = v0.xyz();
  86. const vfloat<M> radius = v0.w;
  87. /* compute ray distance projC0 to hit point with ray oriented plane */
  88. const Vec3vf<M> c0 = center - ray_org;
  89. const vfloat<M> projC0 = dot(c0, ray_dir) * rd2;
  90. valid &= (vfloat<M>(ray.tnear()) <= projC0) & (projC0 <= vfloat<M>(ray.tfar));
  91. if (unlikely(none(valid)))
  92. return false;
  93. /* check if hit point lies inside disc */
  94. const Vec3vf<M> perp = c0 - projC0 * ray_dir;
  95. const vfloat<M> l2 = dot(perp, perp);
  96. const vfloat<M> r2 = radius * radius;
  97. valid &= (l2 <= r2);
  98. if (unlikely(none(valid)))
  99. return false;
  100. /* We reject hits where the ray origin lies inside the ray
  101. * oriented disc to avoid self intersections. */
  102. #if defined(EMBREE_DISC_POINT_SELF_INTERSECTION_AVOIDANCE)
  103. const vfloat<M> m2 = dot(c0, c0);
  104. valid &= (m2 > r2);
  105. if (unlikely(none(valid)))
  106. return false;
  107. #endif
  108. DiscIntersectorHitM<M> hit(zero, zero, projC0, -ray_dir);
  109. return epilog(valid, hit);
  110. }
  111. template<typename Ray, typename Epilog>
  112. static __forceinline bool intersect(const vbool<M>& valid_i,
  113. Ray& ray,
  114. RayQueryContext* context,
  115. const Points* geom,
  116. const Precalculations& pre,
  117. const Vec4vf<M>& v0i,
  118. const Vec3vf<M>& normal,
  119. const Epilog& epilog)
  120. {
  121. vbool<M> valid = valid_i;
  122. const Vec3vf<M> ray_org(ray.org.x, ray.org.y, ray.org.z);
  123. const Vec4vf<M> v0 = enlargeRadiusToMinWidth<M>(context,geom,ray_org,v0i);
  124. const Vec3vf<M> center = v0.xyz();
  125. const vfloat<M> radius = v0.w;
  126. vfloat<M> divisor = dot(Vec3vf<M>((Vec3fa)ray.dir), normal);
  127. const vbool<M> parallel = divisor == vfloat<M>(0.f);
  128. valid &= !parallel;
  129. divisor = select(parallel, 1.f, divisor); // prevent divide by zero
  130. vfloat<M> t = dot(center - Vec3vf<M>((Vec3fa)ray.org), Vec3vf<M>(normal)) / divisor;
  131. valid &= (vfloat<M>(ray.tnear()) <= t) & (t <= vfloat<M>(ray.tfar));
  132. if (unlikely(none(valid)))
  133. return false;
  134. Vec3vf<M> intersection = Vec3vf<M>((Vec3fa)ray.org) + Vec3vf<M>((Vec3fa)ray.dir) * t;
  135. vfloat<M> dist2 = dot(intersection - center, intersection - center);
  136. valid &= dist2 < radius * radius;
  137. if (unlikely(none(valid)))
  138. return false;
  139. DiscIntersectorHitM<M> hit(zero, zero, t, normal);
  140. return epilog(valid, hit);
  141. }
  142. };
  143. template<int M, int K>
  144. struct DiscIntersectorK
  145. {
  146. typedef CurvePrecalculationsK<K> Precalculations;
  147. template<typename Epilog>
  148. static __forceinline bool intersect(const vbool<M>& valid_i,
  149. RayK<K>& ray,
  150. size_t k,
  151. RayQueryContext* context,
  152. const Points* geom,
  153. const Precalculations& pre,
  154. const Vec4vf<M>& v0i,
  155. const Epilog& epilog)
  156. {
  157. vbool<M> valid = valid_i;
  158. const Vec3vf<M> ray_org(ray.org.x[k], ray.org.y[k], ray.org.z[k]);
  159. const Vec3vf<M> ray_dir(ray.dir.x[k], ray.dir.y[k], ray.dir.z[k]);
  160. const vfloat<M> rd2 = rcp(dot(ray_dir, ray_dir));
  161. const Vec4vf<M> v0 = enlargeRadiusToMinWidth<M>(context,geom,ray_org,v0i);
  162. const Vec3vf<M> center = v0.xyz();
  163. const vfloat<M> radius = v0.w;
  164. /* compute ray distance projC0 to hit point with ray oriented plane */
  165. const Vec3vf<M> c0 = center - ray_org;
  166. const vfloat<M> projC0 = dot(c0, ray_dir) * rd2;
  167. valid &= (vfloat<M>(ray.tnear()[k]) <= projC0) & (projC0 <= vfloat<M>(ray.tfar[k]));
  168. if (unlikely(none(valid)))
  169. return false;
  170. /* check if hit point lies inside disc */
  171. const Vec3vf<M> perp = c0 - projC0 * ray_dir;
  172. const vfloat<M> l2 = dot(perp, perp);
  173. const vfloat<M> r2 = radius * radius;
  174. valid &= (l2 <= r2);
  175. if (unlikely(none(valid)))
  176. return false;
  177. /* We reject hits where the ray origin lies inside the ray
  178. * oriented disc to avoid self intersections. */
  179. #if defined(EMBREE_DISC_POINT_SELF_INTERSECTION_AVOIDANCE)
  180. const vfloat<M> m2 = dot(c0, c0);
  181. valid &= (m2 > r2);
  182. if (unlikely(none(valid)))
  183. return false;
  184. #endif
  185. DiscIntersectorHitM<M> hit(zero, zero, projC0, -ray_dir);
  186. return epilog(valid, hit);
  187. }
  188. template<typename Epilog>
  189. static __forceinline bool intersect(const vbool<M>& valid_i,
  190. RayK<K>& ray,
  191. size_t k,
  192. RayQueryContext* context,
  193. const Points* geom,
  194. const Precalculations& pre,
  195. const Vec4vf<M>& v0i,
  196. const Vec3vf<M>& normal,
  197. const Epilog& epilog)
  198. {
  199. vbool<M> valid = valid_i;
  200. const Vec3vf<M> ray_org(ray.org.x[k], ray.org.y[k], ray.org.z[k]);
  201. const Vec3vf<M> ray_dir(ray.dir.x[k], ray.dir.y[k], ray.dir.z[k]);
  202. const Vec4vf<M> v0 = enlargeRadiusToMinWidth<M>(context,geom,ray_org,v0i);
  203. const Vec3vf<M> center = v0.xyz();
  204. const vfloat<M> radius = v0.w;
  205. vfloat<M> divisor = dot(Vec3vf<M>(ray_dir), normal);
  206. const vbool<M> parallel = divisor == vfloat<M>(0.f);
  207. valid &= !parallel;
  208. divisor = select(parallel, 1.f, divisor); // prevent divide by zero
  209. vfloat<M> t = dot(center - Vec3vf<M>(ray_org), Vec3vf<M>(normal)) / divisor;
  210. valid &= (vfloat<M>(ray.tnear()[k]) <= t) & (t <= vfloat<M>(ray.tfar[k]));
  211. if (unlikely(none(valid)))
  212. return false;
  213. Vec3vf<M> intersection = Vec3vf<M>(ray_org) + Vec3vf<M>(ray_dir) * t;
  214. vfloat<M> dist2 = dot(intersection - center, intersection - center);
  215. valid &= dist2 < radius * radius;
  216. if (unlikely(none(valid)))
  217. return false;
  218. DiscIntersectorHitM<M> hit(zero, zero, t, normal);
  219. return epilog(valid, hit);
  220. }
  221. };
  222. } // namespace isa
  223. } // namespace embree