bezier_ribbon_intersector.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // ======================================================================== //
  2. // Copyright 2009-2017 Intel Corporation //
  3. // //
  4. // Licensed under the Apache License, Version 2.0 (the "License"); //
  5. // you may not use this file except in compliance with the License. //
  6. // You may obtain a copy of the License at //
  7. // //
  8. // http://www.apache.org/licenses/LICENSE-2.0 //
  9. // //
  10. // Unless required by applicable law or agreed to in writing, software //
  11. // distributed under the License is distributed on an "AS IS" BASIS, //
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //
  13. // See the License for the specific language governing permissions and //
  14. // limitations under the License. //
  15. // ======================================================================== //
  16. #pragma once
  17. #include "../common/ray.h"
  18. #include "filter.h"
  19. #include "quad_intersector.h"
  20. #define Bezier1Intersector1 Ribbon1Intersector1
  21. #define Bezier1IntersectorK Ribbon1IntersectorK
  22. namespace embree
  23. {
  24. namespace isa
  25. {
  26. template<typename NativeCurve3fa, int M>
  27. struct RibbonHit
  28. {
  29. __forceinline RibbonHit() {}
  30. __forceinline RibbonHit(const vbool<M>& valid, const vfloat<M>& U, const vfloat<M>& V, const vfloat<M>& T, const int i, const int N,
  31. const Vec3fa& p0, const Vec3fa& p1, const Vec3fa& p2, const Vec3fa& p3)
  32. : U(U), V(V), T(T), i(i), N(N), p0(p0), p1(p1), p2(p2), p3(p3), valid(valid) {}
  33. __forceinline void finalize()
  34. {
  35. vu = (vfloat<M>(step)+U+vfloat<M>(float(i)))*(1.0f/float(N));
  36. vv = V;
  37. vt = T;
  38. }
  39. __forceinline Vec2f uv (const size_t i) const { return Vec2f(vu[i],vv[i]); }
  40. __forceinline float t (const size_t i) const { return vt[i]; }
  41. __forceinline Vec3fa Ng(const size_t i) const
  42. {
  43. Vec3fa T = NativeCurve3fa(p0,p1,p2,p3).eval_du(vu[i]);
  44. return T == Vec3fa(zero) ? Vec3fa(one) : T;
  45. }
  46. public:
  47. vfloat<M> U;
  48. vfloat<M> V;
  49. vfloat<M> T;
  50. int i, N;
  51. Vec3fa p0,p1,p2,p3;
  52. public:
  53. vbool<M> valid;
  54. vfloat<M> vu;
  55. vfloat<M> vv;
  56. vfloat<M> vt;
  57. };
  58. /* calculate squared distance of point p0 to line p1->p2 */
  59. __forceinline std::pair<vfloatx,vfloatx> sqr_point_line_distance(const Vec2vfx& p0, const Vec2vfx& p1, const Vec2vfx& p2)
  60. {
  61. const vfloatx num = det(p2-p1,p1-p0);
  62. const vfloatx den2 = dot(p2-p1,p2-p1);
  63. return std::make_pair(num*num,den2);
  64. }
  65. /* performs culling against a cylinder */
  66. __forceinline vboolx cylinder_culling_test(const Vec2vfx& p0, const Vec2vfx& p1, const Vec2vfx& p2, const vfloatx& r)
  67. {
  68. const std::pair<vfloatx,vfloatx> d = sqr_point_line_distance(p0,p1,p2);
  69. return d.first <= r*r*d.second;
  70. }
  71. template<typename NativeCurve3fa, typename Epilog>
  72. __forceinline bool intersect_ribbon(const Vec3fa& ray_org, const Vec3fa& ray_dir, const float ray_tnear, const float ray_tfar,
  73. const float depth_scale, const LinearSpace3fa& ray_space,
  74. const Vec3fa& v0, const Vec3fa& v1, const Vec3fa& v2, const Vec3fa& v3, const int N,
  75. const Epilog& epilog)
  76. {
  77. /* transform control points into ray space */
  78. Vec3fa w0 = xfmVector(ray_space,v0-ray_org); w0.w = v0.w;
  79. Vec3fa w1 = xfmVector(ray_space,v1-ray_org); w1.w = v1.w;
  80. Vec3fa w2 = xfmVector(ray_space,v2-ray_org); w2.w = v2.w;
  81. Vec3fa w3 = xfmVector(ray_space,v3-ray_org); w3.w = v3.w;
  82. NativeCurve3fa curve2D(w0,w1,w2,w3);
  83. /* evaluate the bezier curve */
  84. bool ishit = false;
  85. vboolx valid = vfloatx(step) < vfloatx(float(N));
  86. const Vec4vfx p0 = curve2D.template eval0<VSIZEX>(0,N);
  87. const Vec4vfx p1 = curve2D.template eval1<VSIZEX>(0,N);
  88. valid &= cylinder_culling_test(zero,Vec2vfx(p0.x,p0.y),Vec2vfx(p1.x,p1.y),max(p0.w,p1.w));
  89. if (any(valid))
  90. {
  91. const Vec3vfx dp0dt = curve2D.template derivative0<VSIZEX>(0,N);
  92. const Vec3vfx dp1dt = curve2D.template derivative1<VSIZEX>(0,N);
  93. const Vec3vfx n0(dp0dt.y,-dp0dt.x,0.0f);
  94. const Vec3vfx n1(dp1dt.y,-dp1dt.x,0.0f);
  95. const Vec3vfx nn0 = normalize(n0);
  96. const Vec3vfx nn1 = normalize(n1);
  97. const Vec3vfx lp0 = madd(p0.w,nn0,Vec3vfx(p0));
  98. const Vec3vfx lp1 = madd(p1.w,nn1,Vec3vfx(p1));
  99. const Vec3vfx up0 = nmadd(p0.w,nn0,Vec3vfx(p0));
  100. const Vec3vfx up1 = nmadd(p1.w,nn1,Vec3vfx(p1));
  101. vfloatx vu,vv,vt;
  102. vboolx valid0 = intersect_quad_backface_culling(valid,zero,Vec3fa(0,0,1),ray_tnear*depth_scale,ray_tfar*depth_scale,lp0,lp1,up1,up0,vu,vv,vt);
  103. if (any(valid0))
  104. {
  105. vv = madd(2.0f,vv,vfloatx(-1.0f));
  106. RibbonHit<NativeCurve3fa,VSIZEX> bhit(valid0,vu,vv,depth_scale*vt,0,N,v0,v1,v2,v3);
  107. ishit |= epilog(bhit.valid,bhit);
  108. }
  109. }
  110. if (unlikely(VSIZEX < N))
  111. {
  112. /* process SIMD-size many segments per iteration */
  113. for (int i=VSIZEX; i<N; i+=VSIZEX)
  114. {
  115. /* evaluate the bezier curve */
  116. vboolx valid = vintx(i)+vintx(step) < vintx(N);
  117. const Vec4vfx p0 = curve2D.template eval0<VSIZEX>(i,N);
  118. const Vec4vfx p1 = curve2D.template eval1<VSIZEX>(i,N);
  119. valid &= cylinder_culling_test(zero,Vec2vfx(p0.x,p0.y),Vec2vfx(p1.x,p1.y),max(p0.w,p1.w));
  120. if (none(valid)) continue;
  121. const Vec3vfx dp0dt = curve2D.template derivative0<VSIZEX>(i,N);
  122. const Vec3vfx dp1dt = curve2D.template derivative1<VSIZEX>(i,N);
  123. const Vec3vfx n0(dp0dt.y,-dp0dt.x,0.0f);
  124. const Vec3vfx n1(dp1dt.y,-dp1dt.x,0.0f);
  125. const Vec3vfx nn0 = normalize(n0);
  126. const Vec3vfx nn1 = normalize(n1);
  127. const Vec3vfx lp0 = madd(p0.w,nn0,Vec3vfx(p0));
  128. const Vec3vfx lp1 = madd(p1.w,nn1,Vec3vfx(p1));
  129. const Vec3vfx up0 = nmadd(p0.w,nn0,Vec3vfx(p0));
  130. const Vec3vfx up1 = nmadd(p1.w,nn1,Vec3vfx(p1));
  131. vfloatx vu,vv,vt;
  132. vboolx valid0 = intersect_quad_backface_culling(valid,zero,Vec3fa(0,0,1),ray_tnear*depth_scale,ray_tfar*depth_scale,lp0,lp1,up1,up0,vu,vv,vt);
  133. if (any(valid0))
  134. {
  135. vv = madd(2.0f,vv,vfloatx(-1.0f));
  136. RibbonHit<NativeCurve3fa,VSIZEX> bhit(valid0,vu,vv,depth_scale*vt,i,N,v0,v1,v2,v3);
  137. ishit |= epilog(bhit.valid,bhit);
  138. }
  139. }
  140. }
  141. return ishit;
  142. }
  143. template<typename NativeCurve3fa>
  144. struct Ribbon1Intersector1
  145. {
  146. float depth_scale;
  147. LinearSpace3fa ray_space;
  148. __forceinline Ribbon1Intersector1() {}
  149. __forceinline Ribbon1Intersector1(const Ray& ray, const void* ptr)
  150. : depth_scale(rsqrt(dot(ray.dir,ray.dir))), ray_space(frame(depth_scale*ray.dir).transposed()) {}
  151. template<typename Epilog>
  152. __forceinline bool intersect(Ray& ray,
  153. const Vec3fa& v0, const Vec3fa& v1, const Vec3fa& v2, const Vec3fa& v3, const int N,
  154. const Epilog& epilog) const
  155. {
  156. return intersect_ribbon<NativeCurve3fa>(ray.org,ray.dir,ray.tnear,ray.tfar,
  157. depth_scale,ray_space,
  158. v0,v1,v2,v3,N,
  159. epilog);
  160. }
  161. };
  162. template<typename NativeCurve3fa, int K>
  163. struct Ribbon1IntersectorK
  164. {
  165. vfloat<K> depth_scale;
  166. LinearSpace3fa ray_space[K];
  167. __forceinline Ribbon1IntersectorK(const vbool<K>& valid, const RayK<K>& ray)
  168. {
  169. size_t mask = movemask(valid);
  170. depth_scale = rsqrt(dot(ray.dir,ray.dir));
  171. while (mask) {
  172. size_t k = __bscf(mask);
  173. ray_space[k] = frame(depth_scale[k]*Vec3fa(ray.dir.x[k],ray.dir.y[k],ray.dir.z[k])).transposed();
  174. }
  175. }
  176. template<typename Epilog>
  177. __forceinline bool intersect(RayK<K>& ray, size_t k,
  178. const Vec3fa& v0, const Vec3fa& v1, const Vec3fa& v2, const Vec3fa& v3, const int N,
  179. const Epilog& epilog) const
  180. {
  181. const Vec3fa ray_org(ray.org.x[k],ray.org.y[k],ray.org.z[k]);
  182. const Vec3fa ray_dir(ray.dir.x[k],ray.dir.y[k],ray.dir.z[k]);
  183. const float ray_tnear = ray.tnear[k];
  184. const float ray_tfar = ray.tfar [k];
  185. return intersect_ribbon<NativeCurve3fa>(ray_org,ray_dir,ray_tnear,ray_tfar,
  186. depth_scale[k],ray_space[k],
  187. v0,v1,v2,v3,N,
  188. epilog);
  189. }
  190. };
  191. }
  192. }