sphere_intersector.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 SphereIntersectorHitM
  13. {
  14. __forceinline SphereIntersectorHitM() {}
  15. __forceinline SphereIntersectorHitM(const vfloat<M>& t, const Vec3vf<M>& Ng)
  16. : vt(t), vNg(Ng) {}
  17. __forceinline void finalize() {}
  18. __forceinline Vec2f uv(const size_t i) const {
  19. return Vec2f(0.0f, 0.0f);
  20. }
  21. __forceinline float t(const size_t i) const {
  22. return vt[i];
  23. }
  24. __forceinline Vec3fa Ng(const size_t i) const {
  25. return Vec3fa(vNg.x[i], vNg.y[i], vNg.z[i]);
  26. }
  27. public:
  28. vfloat<M> vt;
  29. Vec3vf<M> vNg;
  30. };
  31. template<int M>
  32. struct SphereIntersector1
  33. {
  34. typedef CurvePrecalculations1 Precalculations;
  35. template<typename Epilog>
  36. static __forceinline bool intersect(
  37. const vbool<M>& valid_i, Ray& ray,
  38. const Precalculations& pre, const Vec4vf<M>& v0, const Epilog& epilog)
  39. {
  40. vbool<M> valid = valid_i;
  41. const vfloat<M> rd2 = rcp(dot(ray.dir, ray.dir));
  42. const Vec3vf<M> ray_org(ray.org.x, ray.org.y, ray.org.z);
  43. const Vec3vf<M> ray_dir(ray.dir.x, ray.dir.y, ray.dir.z);
  44. const Vec3vf<M> center = v0.xyz();
  45. const vfloat<M> radius = v0.w;
  46. const Vec3vf<M> c0 = center - ray_org;
  47. const vfloat<M> projC0 = dot(c0, ray_dir) * rd2;
  48. const Vec3vf<M> perp = c0 - projC0 * ray_dir;
  49. const vfloat<M> l2 = dot(perp, perp);
  50. const vfloat<M> r2 = radius * radius;
  51. valid &= (l2 <= r2);
  52. if (unlikely(none(valid)))
  53. return false;
  54. const vfloat<M> td = sqrt((r2 - l2) * rd2);
  55. const vfloat<M> t_front = projC0 - td;
  56. const vfloat<M> t_back = projC0 + td;
  57. const vbool<M> valid_front = valid & (ray.tnear() <= t_front) & (t_front <= ray.tfar);
  58. const vbool<M> valid_back = valid & (ray.tnear() <= t_back ) & (t_back <= ray.tfar);
  59. /* check if there is a first hit */
  60. const vbool<M> valid_first = valid_front | valid_back;
  61. if (unlikely(none(valid_first)))
  62. return false;
  63. /* construct first hit */
  64. const vfloat<M> td_front = -td;
  65. const vfloat<M> td_back = +td;
  66. const vfloat<M> t_first = select(valid_front, t_front, t_back);
  67. const Vec3vf<M> Ng_first = select(valid_front, td_front, td_back) * ray_dir - perp;
  68. SphereIntersectorHitM<M> hit(t_first, Ng_first);
  69. /* invoke intersection filter for first hit */
  70. const bool is_hit_first = epilog(valid_first, hit);
  71. /* check for possible second hits before potentially accepted hit */
  72. const vfloat<M> t_second = t_back;
  73. const vbool<M> valid_second = valid_front & valid_back & (t_second <= ray.tfar);
  74. if (unlikely(none(valid_second)))
  75. return is_hit_first;
  76. /* invoke intersection filter for second hit */
  77. const Vec3vf<M> Ng_second = td_back * ray_dir - perp;
  78. hit = SphereIntersectorHitM<M> (t_second, Ng_second);
  79. const bool is_hit_second = epilog(valid_second, hit);
  80. return is_hit_first | is_hit_second;
  81. }
  82. template<typename Epilog>
  83. static __forceinline bool intersect(
  84. const vbool<M>& valid_i, Ray& ray, IntersectContext* context, const Points* geom,
  85. const Precalculations& pre, const Vec4vf<M>& v0i, const Epilog& epilog)
  86. {
  87. const Vec3vf<M> ray_org(ray.org.x, ray.org.y, ray.org.z);
  88. const Vec4vf<M> v0 = enlargeRadiusToMinWidth<M>(context,geom,ray_org,v0i);
  89. return intersect(valid_i,ray,pre,v0,epilog);
  90. }
  91. };
  92. template<int M, int K>
  93. struct SphereIntersectorK
  94. {
  95. typedef CurvePrecalculationsK<K> Precalculations;
  96. template<typename Epilog>
  97. static __forceinline bool intersect(const vbool<M>& valid_i,
  98. RayK<K>& ray, size_t k,
  99. IntersectContext* context,
  100. const Points* geom,
  101. const Precalculations& pre,
  102. const Vec4vf<M>& v0i,
  103. const Epilog& epilog)
  104. {
  105. vbool<M> valid = valid_i;
  106. const Vec3vf<M> ray_org(ray.org.x[k], ray.org.y[k], ray.org.z[k]);
  107. const Vec3vf<M> ray_dir(ray.dir.x[k], ray.dir.y[k], ray.dir.z[k]);
  108. const vfloat<M> rd2 = rcp(dot(ray_dir, ray_dir));
  109. const Vec4vf<M> v0 = enlargeRadiusToMinWidth<M>(context,geom,ray_org,v0i);
  110. const Vec3vf<M> center = v0.xyz();
  111. const vfloat<M> radius = v0.w;
  112. const Vec3vf<M> c0 = center - ray_org;
  113. const vfloat<M> projC0 = dot(c0, ray_dir) * rd2;
  114. const Vec3vf<M> perp = c0 - projC0 * ray_dir;
  115. const vfloat<M> l2 = dot(perp, perp);
  116. const vfloat<M> r2 = radius * radius;
  117. valid &= (l2 <= r2);
  118. if (unlikely(none(valid)))
  119. return false;
  120. const vfloat<M> td = sqrt((r2 - l2) * rd2);
  121. const vfloat<M> t_front = projC0 - td;
  122. const vfloat<M> t_back = projC0 + td;
  123. const vbool<M> valid_front = valid & (ray.tnear()[k] <= t_front) & (t_front <= ray.tfar[k]);
  124. const vbool<M> valid_back = valid & (ray.tnear()[k] <= t_back ) & (t_back <= ray.tfar[k]);
  125. /* check if there is a first hit */
  126. const vbool<M> valid_first = valid_front | valid_back;
  127. if (unlikely(none(valid_first)))
  128. return false;
  129. /* construct first hit */
  130. const vfloat<M> td_front = -td;
  131. const vfloat<M> td_back = +td;
  132. const vfloat<M> t_first = select(valid_front, t_front, t_back);
  133. const Vec3vf<M> Ng_first = select(valid_front, td_front, td_back) * ray_dir - perp;
  134. SphereIntersectorHitM<M> hit(t_first, Ng_first);
  135. /* invoke intersection filter for first hit */
  136. const bool is_hit_first = epilog(valid_first, hit);
  137. /* check for possible second hits before potentially accepted hit */
  138. const vfloat<M> t_second = t_back;
  139. const vbool<M> valid_second = valid_front & valid_back & (t_second <= ray.tfar[k]);
  140. if (unlikely(none(valid_second)))
  141. return is_hit_first;
  142. /* invoke intersection filter for second hit */
  143. const Vec3vf<M> Ng_second = td_back * ray_dir - perp;
  144. hit = SphereIntersectorHitM<M> (t_second, Ng_second);
  145. const bool is_hit_second = epilog(valid_second, hit);
  146. return is_hit_first | is_hit_second;
  147. }
  148. };
  149. } // namespace isa
  150. } // namespace embree