coneline_intersector.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. namespace __coneline_internal
  11. {
  12. template<int M, typename Epilog, typename ray_tfar_func>
  13. static __forceinline bool intersectCone(const vbool<M>& valid_i,
  14. const Vec3vf<M>& ray_org_in, const Vec3vf<M>& ray_dir,
  15. const vfloat<M>& ray_tnear, const ray_tfar_func& ray_tfar,
  16. const Vec4vf<M>& v0, const Vec4vf<M>& v1,
  17. const vbool<M>& cL, const vbool<M>& cR,
  18. const Epilog& epilog)
  19. {
  20. vbool<M> valid = valid_i;
  21. /* move ray origin closer to make calculations numerically stable */
  22. const vfloat<M> dOdO = sqr(ray_dir);
  23. const vfloat<M> rcp_dOdO = rcp(dOdO);
  24. const Vec3vf<M> center = vfloat<M>(0.5f)*(v0.xyz()+v1.xyz());
  25. const vfloat<M> dt = dot(center-ray_org_in,ray_dir)*rcp_dOdO;
  26. const Vec3vf<M> ray_org = ray_org_in + dt*ray_dir;
  27. const Vec3vf<M> dP = v1.xyz() - v0.xyz();
  28. const Vec3vf<M> p0 = ray_org - v0.xyz();
  29. const Vec3vf<M> p1 = ray_org - v1.xyz();
  30. const vfloat<M> dPdP = sqr(dP);
  31. const vfloat<M> dP0 = dot(p0,dP);
  32. const vfloat<M> dP1 = dot(p1,dP);
  33. const vfloat<M> dOdP = dot(ray_dir,dP);
  34. // intersect cone body
  35. const vfloat<M> dr = v0.w - v1.w;
  36. const vfloat<M> hy = dPdP + sqr(dr);
  37. const vfloat<M> dO0 = dot(ray_dir,p0);
  38. const vfloat<M> OO = sqr(p0);
  39. const vfloat<M> dPdP2 = sqr(dPdP);
  40. const vfloat<M> dPdPr0 = dPdP*v0.w;
  41. const vfloat<M> A = dPdP2 - sqr(dOdP)*hy;
  42. const vfloat<M> B = dPdP2*dO0 - dP0*dOdP*hy + dPdPr0*(dr*dOdP);
  43. const vfloat<M> C = dPdP2*OO - sqr(dP0)*hy + dPdPr0*(2.0f*dr*dP0 - dPdPr0);
  44. const vfloat<M> D = B*B - A*C;
  45. valid &= D >= 0.0f;
  46. if (unlikely(none(valid))) {
  47. return false;
  48. }
  49. /* standard case for "non-parallel" rays */
  50. const vfloat<M> Q = sqrt(D);
  51. const vfloat<M> rcp_A = rcp(A);
  52. /* special case for rays that are "parallel" to the cone - assume miss */
  53. const vbool<M> isParallel = abs(A) <= min_rcp_input;
  54. vfloat<M> t_cone_lower = select (isParallel, neg_inf, (-B-Q)*rcp_A);
  55. vfloat<M> t_cone_upper = select (isParallel, pos_inf, (-B+Q)*rcp_A);
  56. const vfloat<M> y_lower = dP0 + t_cone_lower*dOdP;
  57. const vfloat<M> y_upper = dP0 + t_cone_upper*dOdP;
  58. t_cone_lower = select(valid & y_lower > 0.0f & y_lower < dPdP, t_cone_lower, pos_inf);
  59. t_cone_upper = select(valid & y_upper > 0.0f & y_upper < dPdP, t_cone_upper, neg_inf);
  60. const vbool<M> hitDisk0 = valid & cL;
  61. const vbool<M> hitDisk1 = valid & cR;
  62. const vfloat<M> rcp_dOdP = rcp(dOdP);
  63. const vfloat<M> t_disk0 = select (hitDisk0, select (sqr(p0*dOdP-ray_dir*dP0)<(sqr(v0.w)*sqr(dOdP)), -dP0*rcp_dOdP, pos_inf), pos_inf);
  64. const vfloat<M> t_disk1 = select (hitDisk1, select (sqr(p1*dOdP-ray_dir*dP1)<(sqr(v1.w)*sqr(dOdP)), -dP1*rcp_dOdP, pos_inf), pos_inf);
  65. const vfloat<M> t_disk_lower = min(t_disk0, t_disk1);
  66. const vfloat<M> t_disk_upper = max(t_disk0, t_disk1);
  67. const vfloat<M> t_lower = min(t_cone_lower, t_disk_lower);
  68. const vfloat<M> t_upper = max(t_cone_upper, select(t_lower==t_disk_lower,
  69. select(t_disk_upper==vfloat<M>(pos_inf),neg_inf,t_disk_upper),
  70. select(t_disk_lower==vfloat<M>(pos_inf),neg_inf,t_disk_lower)));
  71. const vbool<M> valid_lower = valid & ray_tnear <= dt+t_lower & dt+t_lower <= ray_tfar() & t_lower != vfloat<M>(pos_inf);
  72. const vbool<M> valid_upper = valid & ray_tnear <= dt+t_upper & dt+t_upper <= ray_tfar() & t_upper != vfloat<M>(neg_inf);
  73. const vbool<M> valid_first = valid_lower | valid_upper;
  74. if (unlikely(none(valid_first)))
  75. return false;
  76. const vfloat<M> t_first = select(valid_lower, t_lower, t_upper);
  77. const vfloat<M> y_first = select(valid_lower, y_lower, y_upper);
  78. const vfloat<M> rcp_dPdP = rcp(dPdP);
  79. const Vec3vf<M> dP2drr0dP = dPdP*dr*v0.w*dP;
  80. const Vec3vf<M> dPhy = dP*hy;
  81. const vbool<M> cone_hit_first = valid & (t_first == t_cone_lower | t_first == t_cone_upper);
  82. const vbool<M> disk0_hit_first = valid & (t_first == t_disk0);
  83. const Vec3vf<M> Ng_first = select(cone_hit_first, dPdP2*(p0+t_first*ray_dir)+dP2drr0dP-dPhy*y_first, select(disk0_hit_first, -dP, dP));
  84. const vfloat<M> u_first = select(cone_hit_first, y_first*rcp_dPdP, select(disk0_hit_first, vfloat<M>(zero), vfloat<M>(one)));
  85. /* invoke intersection filter for first hit */
  86. RoundLineIntersectorHitM<M> hit(u_first,zero,dt+t_first,Ng_first);
  87. const bool is_hit_first = epilog(valid_first, hit);
  88. /* check for possible second hits before potentially accepted hit */
  89. const vfloat<M> t_second = t_upper;
  90. const vfloat<M> y_second = y_upper;
  91. const vbool<M> valid_second = valid_lower & valid_upper & (dt+t_upper <= ray_tfar());
  92. if (unlikely(none(valid_second)))
  93. return is_hit_first;
  94. /* invoke intersection filter for second hit */
  95. const vbool<M> cone_hit_second = t_second == t_cone_lower | t_second == t_cone_upper;
  96. const vbool<M> disk0_hit_second = t_second == t_disk0;
  97. const Vec3vf<M> Ng_second = select(cone_hit_second, dPdP2*(p0+t_second*ray_dir)+dP2drr0dP-dPhy*y_second, select(disk0_hit_second, -dP, dP));
  98. const vfloat<M> u_second = select(cone_hit_second, y_second*rcp_dPdP, select(disk0_hit_first, vfloat<M>(zero), vfloat<M>(one)));
  99. hit = RoundLineIntersectorHitM<M>(u_second,zero,dt+t_second,Ng_second);
  100. const bool is_hit_second = epilog(valid_second, hit);
  101. return is_hit_first | is_hit_second;
  102. }
  103. }
  104. template<int M>
  105. struct ConeLineIntersectorHitM
  106. {
  107. __forceinline ConeLineIntersectorHitM() {}
  108. __forceinline ConeLineIntersectorHitM(const vfloat<M>& u, const vfloat<M>& v, const vfloat<M>& t, const Vec3vf<M>& Ng)
  109. : vu(u), vv(v), vt(t), vNg(Ng) {}
  110. __forceinline void finalize() {}
  111. __forceinline Vec2f uv (const size_t i) const { return Vec2f(vu[i],vv[i]); }
  112. __forceinline float t (const size_t i) const { return vt[i]; }
  113. __forceinline Vec3fa Ng(const size_t i) const { return Vec3fa(vNg.x[i],vNg.y[i],vNg.z[i]); }
  114. public:
  115. vfloat<M> vu;
  116. vfloat<M> vv;
  117. vfloat<M> vt;
  118. Vec3vf<M> vNg;
  119. };
  120. template<int M>
  121. struct ConeCurveIntersector1
  122. {
  123. typedef CurvePrecalculations1 Precalculations;
  124. struct ray_tfar {
  125. Ray& ray;
  126. __forceinline ray_tfar(Ray& ray) : ray(ray) {}
  127. __forceinline vfloat<M> operator() () const { return ray.tfar; };
  128. };
  129. template<typename Epilog>
  130. static __forceinline bool intersect(const vbool<M>& valid_i,
  131. Ray& ray,
  132. RayQueryContext* context,
  133. const LineSegments* geom,
  134. const Precalculations& pre,
  135. const Vec4vf<M>& v0i, const Vec4vf<M>& v1i,
  136. const vbool<M>& cL, const vbool<M>& cR,
  137. const Epilog& epilog)
  138. {
  139. const Vec3vf<M> ray_org(ray.org.x, ray.org.y, ray.org.z);
  140. const Vec3vf<M> ray_dir(ray.dir.x, ray.dir.y, ray.dir.z);
  141. const vfloat<M> ray_tnear(ray.tnear());
  142. const Vec4vf<M> v0 = enlargeRadiusToMinWidth<M>(context,geom,ray_org,v0i);
  143. const Vec4vf<M> v1 = enlargeRadiusToMinWidth<M>(context,geom,ray_org,v1i);
  144. return __coneline_internal::intersectCone<M>(valid_i,ray_org,ray_dir,ray_tnear,ray_tfar(ray),v0,v1,cL,cR,epilog);
  145. }
  146. };
  147. template<int M, int K>
  148. struct ConeCurveIntersectorK
  149. {
  150. typedef CurvePrecalculationsK<K> Precalculations;
  151. struct ray_tfar {
  152. RayK<K>& ray;
  153. size_t k;
  154. __forceinline ray_tfar(RayK<K>& ray, size_t k) : ray(ray), k(k) {}
  155. __forceinline vfloat<M> operator() () const { return ray.tfar[k]; };
  156. };
  157. template<typename Epilog>
  158. static __forceinline bool intersect(const vbool<M>& valid_i,
  159. RayK<K>& ray, size_t k,
  160. RayQueryContext* context,
  161. const LineSegments* geom,
  162. const Precalculations& pre,
  163. const Vec4vf<M>& v0i, const Vec4vf<M>& v1i,
  164. const vbool<M>& cL, const vbool<M>& cR,
  165. const Epilog& epilog)
  166. {
  167. const Vec3vf<M> ray_org(ray.org.x[k], ray.org.y[k], ray.org.z[k]);
  168. const Vec3vf<M> ray_dir(ray.dir.x[k], ray.dir.y[k], ray.dir.z[k]);
  169. const vfloat<M> ray_tnear = ray.tnear()[k];
  170. const Vec4vf<M> v0 = enlargeRadiusToMinWidth<M>(context,geom,ray_org,v0i);
  171. const Vec4vf<M> v1 = enlargeRadiusToMinWidth<M>(context,geom,ray_org,v1i);
  172. return __coneline_internal::intersectCone<M>(valid_i,ray_org,ray_dir,ray_tnear,ray_tfar(ray,k),v0,v1,cL,cR,epilog);
  173. }
  174. };
  175. }
  176. }