roundline_intersector.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  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. /*
  7. This file implements the intersection of a ray with a round linear
  8. curve segment. We define the geometry of such a round linear curve
  9. segment from point p0 with radius r0 to point p1 with radius r1
  10. using the cone that touches spheres p0/r0 and p1/r1 tangentially
  11. plus the sphere p1/r1. We denote the tangentially touching cone from
  12. p0/r0 to p1/r1 with cone(p0,r0,p1,r1) and the cone plus the ending
  13. sphere with cone_sphere(p0,r0,p1,r1).
  14. For multiple connected round linear curve segments this construction
  15. yield a proper shape when viewed from the outside. Using the
  16. following CSG we can also handle the interior in most common cases:
  17. round_linear_curve(pl,rl,p0,r0,p1,r1,pr,rr) =
  18. cone_sphere(p0,r0,p1,r1) - cone(pl,rl,p0,r0) - cone(p1,r1,pr,rr)
  19. Thus by subtracting the neighboring cone geometries, we cut away
  20. parts of the center cone_sphere surface which lie inside the
  21. combined curve. This approach works as long as geometry of the
  22. current cone_sphere penetrates into direct neighbor segments only,
  23. and not into segments further away.
  24. To construct a cone that touches two spheres at p0 and p1 with r0
  25. and r1, one has to increase the cone radius at r0 and r1 to obtain
  26. larger radii w0 and w1, such that the infinite cone properly touches
  27. the spheres. From the paper "Ray Tracing Generalized Tube
  28. Primitives: Method and Applications"
  29. (https://www.researchgate.net/publication/334378683_Ray_Tracing_Generalized_Tube_Primitives_Method_and_Applications)
  30. one can derive the following equations for these increased
  31. radii:
  32. sr = 1.0f / sqrt(1-sqr(dr)/sqr(p1-p0))
  33. w0 = sr*r0
  34. w1 = sr*r1
  35. Further, we want the cone to start where it touches the sphere at p0
  36. and to end where it touches sphere at p1. Therefore, we need to
  37. construct clipping locations y0 and y1 for the start and end of the
  38. cone. These start and end clipping location of the cone can get
  39. calculated as:
  40. Y0 = - r0 * (r1-r0) / length(p1-p0)
  41. Y1 = length(p1-p0) - r1 * (r1-r0) / length(p1-p0)
  42. Where the cone starts a distance Y0 and ends a distance Y1 away of
  43. point p0 along the cone center. The distance between Y1-Y0 can get
  44. calculated as:
  45. dY = length(p1-p0) - (r1-r0)^2 / length(p1-p0)
  46. In the code below, Y will always be scaled by length(p1-p0) to
  47. obtain y and you will find the terms r0*(r1-r0) and
  48. (p1-p0)^2-(r1-r0)^2.
  49. */
  50. namespace embree
  51. {
  52. namespace isa
  53. {
  54. template<int M>
  55. struct RoundLineIntersectorHitM
  56. {
  57. __forceinline RoundLineIntersectorHitM() {}
  58. __forceinline RoundLineIntersectorHitM(const vfloat<M>& u, const vfloat<M>& v, const vfloat<M>& t, const Vec3vf<M>& Ng)
  59. : vu(u), vv(v), vt(t), vNg(Ng) {}
  60. __forceinline void finalize() {}
  61. __forceinline Vec2f uv (const size_t i) const { return Vec2f(vu[i],vv[i]); }
  62. __forceinline float t (const size_t i) const { return vt[i]; }
  63. __forceinline Vec3fa Ng(const size_t i) const { return Vec3fa(vNg.x[i],vNg.y[i],vNg.z[i]); }
  64. __forceinline Vec2vf<M> uv() const { return Vec2vf<M>(vu,vv); }
  65. __forceinline vfloat<M> t () const { return vt; }
  66. __forceinline Vec3vf<M> Ng() const { return vNg; }
  67. public:
  68. vfloat<M> vu;
  69. vfloat<M> vv;
  70. vfloat<M> vt;
  71. Vec3vf<M> vNg;
  72. };
  73. namespace __roundline_internal
  74. {
  75. template<int M>
  76. struct ConeGeometry
  77. {
  78. ConeGeometry (const Vec4vf<M>& a, const Vec4vf<M>& b)
  79. : p0(a.xyz()), p1(b.xyz()), dP(p1-p0), dPdP(dot(dP,dP)), r0(a.w), sqr_r0(sqr(r0)), r1(b.w), dr(r1-r0), drdr(dr*dr), r0dr (r0*dr), g(dPdP - drdr) {}
  80. /*
  81. This function tests if a point is accepted by first cone
  82. clipping plane.
  83. First, we need to project the point onto the line p0->p1:
  84. Y = (p-p0)*(p1-p0)/length(p1-p0)
  85. This value y is the distance to the projection point from
  86. p0. The clip distances are calculated as:
  87. Y0 = - r0 * (r1-r0) / length(p1-p0)
  88. Y1 = length(p1-p0) - r1 * (r1-r0) / length(p1-p0)
  89. Thus to test if the point p is accepted by the first
  90. clipping plane we need to test Y > Y0 and to test if it
  91. is accepted by the second clipping plane we need to test
  92. Y < Y1.
  93. By multiplying the calculations with length(p1-p0) these
  94. calculation can get simplied to:
  95. y = (p-p0)*(p1-p0)
  96. y0 = - r0 * (r1-r0)
  97. y1 = (p1-p0)^2 - r1 * (r1-r0)
  98. and the test y > y0 and y < y1.
  99. */
  100. __forceinline vbool<M> isClippedByPlane (const vbool<M>& valid_i, const Vec3vf<M>& p) const
  101. {
  102. const Vec3vf<M> p0p = p - p0;
  103. const vfloat<M> y = dot(p0p,dP);
  104. const vfloat<M> cap0 = -r0dr;
  105. const vbool<M> inside_cone = y > cap0;
  106. return valid_i & (p0.x != vfloat<M>(inf)) & (p1.x != vfloat<M>(inf)) & inside_cone;
  107. }
  108. /*
  109. This function tests whether a point lies inside the capped cone
  110. tangential to its ending spheres.
  111. Therefore one has to check if the point is inside the
  112. region defined by the cone clipping planes, which is
  113. performed similar as in the previous function.
  114. To perform the inside cone test we need to project the
  115. point onto the line p0->p1:
  116. dP = p1-p0
  117. Y = (p-p0)*dP/length(dP)
  118. This value Y is the distance to the projection point from
  119. p0. To obtain a parameter value u going from 0 to 1 along
  120. the line p0->p1 we calculate:
  121. U = Y/length(dP)
  122. The radii to use at points p0 and p1 are:
  123. w0 = sr * r0
  124. w1 = sr * r1
  125. dw = w1-w0
  126. Using these radii and u one can directly test if the point
  127. lies inside the cone using the formula dP*dP < wy*wy with:
  128. wy = w0 + u*dw
  129. py = p0 + u*dP - p
  130. By multiplying the calculations with length(p1-p0) and
  131. inserting the definition of w can obtain simpler equations:
  132. y = (p-p0)*dP
  133. ry = r0 + y/dP^2 * dr
  134. wy = sr*ry
  135. py = p0 + y/dP^2*dP - p
  136. y0 = - r0 * dr
  137. y1 = dP^2 - r1 * dr
  138. Thus for the in-cone test we get:
  139. py^2 < wy^2
  140. <=> py^2 < sr^2 * ry^2
  141. <=> py^2 * ( dP^2 - dr^2 ) < dP^2 * ry^2
  142. This can further get simplified to:
  143. (p0-p)^2 * (dP^2 - dr^2) - y^2 < dP^2 * r0^2 + 2.0f*r0*dr*y;
  144. */
  145. __forceinline vbool<M> isInsideCappedCone (const vbool<M>& valid_i, const Vec3vf<M>& p) const
  146. {
  147. const Vec3vf<M> p0p = p - p0;
  148. const vfloat<M> y = dot(p0p,dP);
  149. const vfloat<M> cap0 = -r0dr+vfloat<M>(ulp);
  150. const vfloat<M> cap1 = -r1*dr + dPdP;
  151. vbool<M> inside_cone = valid_i & (p0.x != vfloat<M>(inf)) & (p1.x != vfloat<M>(inf));
  152. inside_cone &= y > cap0; // start clipping plane
  153. inside_cone &= y < cap1; // end clipping plane
  154. inside_cone &= sqr(p0p)*g - sqr(y) < dPdP * sqr_r0 + 2.0f*r0dr*y; // in cone test
  155. return inside_cone;
  156. }
  157. protected:
  158. Vec3vf<M> p0;
  159. Vec3vf<M> p1;
  160. Vec3vf<M> dP;
  161. vfloat<M> dPdP;
  162. vfloat<M> r0;
  163. vfloat<M> sqr_r0;
  164. vfloat<M> r1;
  165. vfloat<M> dr;
  166. vfloat<M> drdr;
  167. vfloat<M> r0dr;
  168. vfloat<M> g;
  169. };
  170. template<int M>
  171. struct ConeGeometryIntersector : public ConeGeometry<M>
  172. {
  173. using ConeGeometry<M>::p0;
  174. using ConeGeometry<M>::p1;
  175. using ConeGeometry<M>::dP;
  176. using ConeGeometry<M>::dPdP;
  177. using ConeGeometry<M>::r0;
  178. using ConeGeometry<M>::sqr_r0;
  179. using ConeGeometry<M>::r1;
  180. using ConeGeometry<M>::dr;
  181. using ConeGeometry<M>::r0dr;
  182. using ConeGeometry<M>::g;
  183. ConeGeometryIntersector (const Vec3vf<M>& ray_org, const Vec3vf<M>& ray_dir, const vfloat<M>& dOdO, const vfloat<M>& rcp_dOdO, const Vec4vf<M>& a, const Vec4vf<M>& b)
  184. : ConeGeometry<M>(a,b), org(ray_org), O(ray_org-p0), dO(ray_dir), dOdO(dOdO), rcp_dOdO(rcp_dOdO), OdP(dot(dP,O)), dOdP(dot(dP,dO)), yp(OdP + r0dr) {}
  185. /*
  186. This function intersects a ray with a cone that touches a
  187. start sphere p0/r0 and end sphere p1/r1.
  188. To find this ray/cone intersections one could just
  189. calculate radii w0 and w1 as described above and use a
  190. standard ray/cone intersection routine with these
  191. radii. However, it turns out that calculations can get
  192. simplified when deriving a specialized ray/cone
  193. intersection for this special case. We perform
  194. calculations relative to the cone origin p0 and define:
  195. O = ray_org - p0
  196. dO = ray_dir
  197. dP = p1-p0
  198. dr = r1-r0
  199. dw = w1-w0
  200. For some t we can compute the potential hit point h = O + t*dO and
  201. project it onto the cone vector dP to obtain u = (h*dP)/(dP*dP). In
  202. case of an intersection, the squared distance from the hit point
  203. projected onto the cone center line to the hit point should be equal
  204. to the squared cone radius at u:
  205. (u*dP - h)^2 = (w0 + u*dw)^2
  206. Inserting the definition of h, u, w0, and dw into this formula, then
  207. factoring out all terms, and sorting by t^2, t^1, and t^0 terms
  208. yields a quadratic equation to solve.
  209. Inserting u:
  210. ( (h*dP)*dP/dP^2 - h )^2 = ( w0 + (h*dP)*dw/dP^2 )^2
  211. Multiplying by dP^4:
  212. ( (h*dP)*dP - h*dP^2 )^2 = ( w0*dP^2 + (h*dP)*dw )^2
  213. Inserting w0 and dw:
  214. ( (h*dP)*dP - h*dP^2 )^2 = ( r0*dP^2 + (h*dP)*dr )^2 / (1-dr^2/dP^2)
  215. ( (h*dP)*dP - h*dP^2 )^2 *(dP^2 - dr^2) = dP^2 * ( r0*dP^2 + (h*dP)*dr )^2
  216. Now one can insert the definition of h, factor out, and presort by t:
  217. ( ((O + t*dO)*dP)*dP - (O + t*dO)*dP^2 )^2 *(dP^2 - dr^2) = dP^2 * ( r0*dP^2 + ((O + t*dO)*dP)*dr )^2
  218. ( (O*dP)*dP-O*dP^2 + t*( (dO*dP)*dP - dO*dP^2 ) )^2 *(dP^2 - dr^2) = dP^2 * ( r0*dP^2 + (O*dP)*dr + t*(dO*dP)*dr )^2
  219. Factoring out further and sorting by t^2, t^1 and t^0 yields:
  220. 0 = t^2 * [ ((dO*dP)*dP - dO-dP^2)^2 * (dP^2 - dr^2) - dP^2*(dO*dP)^2*dr^2 ]
  221. + 2*t^1 * [ ((O*dP)*dP - O*dP^2) * ((dO*dP)*dP - dO*dP^2) * (dP^2 - dr^2) - dP^2*(r0*dP^2 + (O*dP)*dr)*(dO*dP)*dr ]
  222. + t^0 * [ ( (O*dP)*dP - O*dP^2)^2 * (dP^2-dr^2) - dP^2*(r0*dP^2 + (O*dP)*dr)^2 ]
  223. This can be simplified to:
  224. 0 = t^2 * [ (dP^2 - dr^2)*dO^2 - (dO*dP)^2 ]
  225. + 2*t^1 * [ (dP^2 - dr^2)*(O*dO) - (dO*dP)*(O*dP + r0*dr) ]
  226. + t^0 * [ (dP^2 - dr^2)*O^2 - (O*dP)^2 - r0^2*dP^2 - 2.0f*r0*dr*(O*dP) ]
  227. Solving this quadratic equation yields the values for t at which the
  228. ray intersects the cone.
  229. */
  230. __forceinline bool intersectCone(vbool<M>& valid, vfloat<M>& lower, vfloat<M>& upper)
  231. {
  232. /* return no hit by default */
  233. lower = pos_inf;
  234. upper = neg_inf;
  235. /* compute quadratic equation A*t^2 + B*t + C = 0 */
  236. const vfloat<M> OO = dot(O,O);
  237. const vfloat<M> OdO = dot(dO,O);
  238. const vfloat<M> A = g * dOdO - sqr(dOdP);
  239. const vfloat<M> B = 2.0f * (g*OdO - dOdP*yp);
  240. const vfloat<M> C = g*OO - sqr(OdP) - sqr_r0*dPdP - 2.0f*r0dr*OdP;
  241. /* we miss the cone if determinant is smaller than zero */
  242. const vfloat<M> D = B*B - 4.0f*A*C;
  243. valid &= (D >= 0.0f & g > 0.0f); // if g <= 0 then the cone is inside a sphere end
  244. /* When rays are parallel to the cone surface, then the
  245. * ray may be inside or outside the cone. We just assume a
  246. * miss in that case, which is fine as rays inside the
  247. * cone would anyway hit the ending spheres in that
  248. * case. */
  249. valid &= abs(A) > min_rcp_input;
  250. if (unlikely(none(valid))) {
  251. return false;
  252. }
  253. /* compute distance to front and back hit */
  254. const vfloat<M> Q = sqrt(D);
  255. const vfloat<M> rcp_2A = rcp(2.0f*A);
  256. t_cone_front = (-B-Q)*rcp_2A;
  257. y_cone_front = yp + t_cone_front*dOdP;
  258. lower = select( (y_cone_front > -(float)ulp) & (y_cone_front <= g) & (g > 0.0f), t_cone_front, vfloat<M>(pos_inf));
  259. #if !defined (EMBREE_BACKFACE_CULLING_CURVES)
  260. t_cone_back = (-B+Q)*rcp_2A;
  261. y_cone_back = yp + t_cone_back *dOdP;
  262. upper = select( (y_cone_back > -(float)ulp) & (y_cone_back <= g) & (g > 0.0f), t_cone_back , vfloat<M>(neg_inf));
  263. #endif
  264. return true;
  265. }
  266. /*
  267. This function intersects the ray with the end sphere at
  268. p1. We already clip away hits that are inside the
  269. neighboring cone segment.
  270. */
  271. __forceinline void intersectEndSphere(vbool<M>& valid,
  272. const ConeGeometry<M>& coneR,
  273. vfloat<M>& lower, vfloat<M>& upper)
  274. {
  275. /* calculate front and back hit with end sphere */
  276. const Vec3vf<M> O1 = org - p1;
  277. const vfloat<M> O1dO = dot(O1,dO);
  278. const vfloat<M> h2 = sqr(O1dO) - dOdO*(sqr(O1) - sqr(r1));
  279. const vfloat<M> rhs1 = select( h2 >= 0.0f, sqrt(h2), vfloat<M>(neg_inf) );
  280. /* clip away front hit if it is inside next cone segment */
  281. t_sph1_front = (-O1dO - rhs1)*rcp_dOdO;
  282. const Vec3vf<M> hit_front = org + t_sph1_front*dO;
  283. vbool<M> valid_sph1_front = h2 >= 0.0f & yp + t_sph1_front*dOdP > g & !coneR.isClippedByPlane (valid, hit_front);
  284. lower = select(valid_sph1_front, t_sph1_front, vfloat<M>(pos_inf));
  285. #if !defined(EMBREE_BACKFACE_CULLING_CURVES)
  286. /* clip away back hit if it is inside next cone segment */
  287. t_sph1_back = (-O1dO + rhs1)*rcp_dOdO;
  288. const Vec3vf<M> hit_back = org + t_sph1_back*dO;
  289. vbool<M> valid_sph1_back = h2 >= 0.0f & yp + t_sph1_back*dOdP > g & !coneR.isClippedByPlane (valid, hit_back);
  290. upper = select(valid_sph1_back, t_sph1_back, vfloat<M>(neg_inf));
  291. #else
  292. upper = vfloat<M>(neg_inf);
  293. #endif
  294. }
  295. __forceinline void intersectBeginSphere(const vbool<M>& valid,
  296. vfloat<M>& lower, vfloat<M>& upper)
  297. {
  298. /* calculate front and back hit with end sphere */
  299. const Vec3vf<M> O1 = org - p0;
  300. const vfloat<M> O1dO = dot(O1,dO);
  301. const vfloat<M> h2 = sqr(O1dO) - dOdO*(sqr(O1) - sqr(r0));
  302. const vfloat<M> rhs1 = select( h2 >= 0.0f, sqrt(h2), vfloat<M>(neg_inf) );
  303. /* clip away front hit if it is inside next cone segment */
  304. t_sph0_front = (-O1dO - rhs1)*rcp_dOdO;
  305. vbool<M> valid_sph1_front = valid & h2 >= 0.0f & yp + t_sph0_front*dOdP < 0;
  306. lower = select(valid_sph1_front, t_sph0_front, vfloat<M>(pos_inf));
  307. #if !defined(EMBREE_BACKFACE_CULLING_CURVES)
  308. /* clip away back hit if it is inside next cone segment */
  309. t_sph0_back = (-O1dO + rhs1)*rcp_dOdO;
  310. vbool<M> valid_sph1_back = valid & h2 >= 0.0f & yp + t_sph0_back*dOdP < 0;
  311. upper = select(valid_sph1_back, t_sph0_back, vfloat<M>(neg_inf));
  312. #else
  313. upper = vfloat<M>(neg_inf);
  314. #endif
  315. }
  316. /*
  317. This function calculates the geometry normal of some cone hit.
  318. For a given hit point h (relative to p0) with a cone
  319. starting at p0 with radius w0 and ending at p1 with
  320. radius w1 one normally calculates the geometry normal by
  321. first calculating the parmetric u hit location along the
  322. cone:
  323. u = dot(h,dP)/dP^2
  324. Using this value one can now directly calculate the
  325. geometry normal by bending the connection vector (h-u*dP)
  326. from hit to projected hit with some cone dependent value
  327. dw/sqrt(dP^2) * normalize(dP):
  328. Ng = normalize(h-u*dP) - dw/length(dP) * normalize(dP)
  329. The length of the vector (h-u*dP) can also get calculated
  330. by interpolating the radii as w0+u*dw which yields:
  331. Ng = (h-u*dP)/(w0+u*dw) - dw/dP^2 * dP
  332. Multiplying with (w0+u*dw) yield a scaled Ng':
  333. Ng' = (h-u*dP) - (w0+u*dw)*dw/dP^2*dP
  334. Inserting the definition of w0 and dw and refactoring
  335. yield a further scaled Ng'':
  336. Ng'' = (dP^2 - dr^2) (h-q) - (r0+u*dr)*dr*dP
  337. Now inserting the definition of u gives and multiplying
  338. with the denominator yields:
  339. Ng''' = (dP^2-dr^2)*(dP^2*h-dot(h,dP)*dP) - (dP^2*r0+dot(h,dP)*dr)*dr*dP
  340. Factoring out, cancelling terms, dividing by dP^2, and
  341. factoring again yields finally:
  342. Ng'''' = (dP^2-dr^2)*h - dP*(dot(h,dP) + r0*dr)
  343. */
  344. __forceinline Vec3vf<M> Ng_cone(const vbool<M>& front_hit) const
  345. {
  346. #if !defined(EMBREE_BACKFACE_CULLING_CURVES)
  347. const vfloat<M> y = select(front_hit, y_cone_front, y_cone_back);
  348. const vfloat<M> t = select(front_hit, t_cone_front, t_cone_back);
  349. const Vec3vf<M> h = O + t*dO;
  350. return g*h-dP*y;
  351. #else
  352. const Vec3vf<M> h = O + t_cone_front*dO;
  353. return g*h-dP*y_cone_front;
  354. #endif
  355. }
  356. /* compute geometry normal of sphere hit as the difference
  357. * vector from hit point to sphere center */
  358. __forceinline Vec3vf<M> Ng_sphere1(const vbool<M>& front_hit) const
  359. {
  360. #if !defined(EMBREE_BACKFACE_CULLING_CURVES)
  361. const vfloat<M> t_sph1 = select(front_hit, t_sph1_front, t_sph1_back);
  362. return org+t_sph1*dO-p1;
  363. #else
  364. return org+t_sph1_front*dO-p1;
  365. #endif
  366. }
  367. __forceinline Vec3vf<M> Ng_sphere0(const vbool<M>& front_hit) const
  368. {
  369. #if !defined(EMBREE_BACKFACE_CULLING_CURVES)
  370. const vfloat<M> t_sph0 = select(front_hit, t_sph0_front, t_sph0_back);
  371. return org+t_sph0*dO-p0;
  372. #else
  373. return org+t_sph0_front*dO-p0;
  374. #endif
  375. }
  376. /*
  377. This function calculates the u coordinate of a
  378. hit. Therefore we use the hit distance y (which is zero
  379. at the first cone clipping plane) and divide by distance
  380. g between the clipping planes.
  381. */
  382. __forceinline vfloat<M> u_cone(const vbool<M>& front_hit) const
  383. {
  384. #if !defined(EMBREE_BACKFACE_CULLING_CURVES)
  385. const vfloat<M> y = select(front_hit, y_cone_front, y_cone_back);
  386. return clamp(y*rcp(g));
  387. #else
  388. return clamp(y_cone_front*rcp(g));
  389. #endif
  390. }
  391. private:
  392. Vec3vf<M> org;
  393. Vec3vf<M> O;
  394. Vec3vf<M> dO;
  395. vfloat<M> dOdO;
  396. vfloat<M> rcp_dOdO;
  397. vfloat<M> OdP;
  398. vfloat<M> dOdP;
  399. /* for ray/cone intersection */
  400. private:
  401. vfloat<M> yp;
  402. vfloat<M> y_cone_front;
  403. vfloat<M> t_cone_front;
  404. #if !defined (EMBREE_BACKFACE_CULLING_CURVES)
  405. vfloat<M> y_cone_back;
  406. vfloat<M> t_cone_back;
  407. #endif
  408. /* for ray/sphere intersection */
  409. private:
  410. vfloat<M> t_sph1_front;
  411. vfloat<M> t_sph0_front;
  412. #if !defined (EMBREE_BACKFACE_CULLING_CURVES)
  413. vfloat<M> t_sph1_back;
  414. vfloat<M> t_sph0_back;
  415. #endif
  416. };
  417. template<int M, typename Epilog, typename ray_tfar_func>
  418. static __forceinline bool intersectConeSphere(const vbool<M>& valid_i,
  419. const Vec3vf<M>& ray_org_in, const Vec3vf<M>& ray_dir,
  420. const vfloat<M>& ray_tnear, const ray_tfar_func& ray_tfar,
  421. const Vec4vf<M>& v0, const Vec4vf<M>& v1,
  422. const Vec4vf<M>& vL, const Vec4vf<M>& vR,
  423. const Epilog& epilog)
  424. {
  425. vbool<M> valid = valid_i;
  426. /* move ray origin closer to make calculations numerically stable */
  427. const vfloat<M> dOdO = sqr(ray_dir);
  428. const vfloat<M> rcp_dOdO = rcp(dOdO);
  429. const Vec3vf<M> center = vfloat<M>(0.5f)*(v0.xyz()+v1.xyz());
  430. const vfloat<M> dt = dot(center-ray_org_in,ray_dir)*rcp_dOdO;
  431. const Vec3vf<M> ray_org = ray_org_in + dt*ray_dir;
  432. /* intersect with cone from v0 to v1 */
  433. vfloat<M> t_cone_lower, t_cone_upper;
  434. ConeGeometryIntersector<M> cone (ray_org, ray_dir, dOdO, rcp_dOdO, v0, v1);
  435. vbool<M> validCone = valid;
  436. cone.intersectCone(validCone, t_cone_lower, t_cone_upper);
  437. valid &= (validCone | (cone.g <= 0.0f)); // if cone is entirely in sphere end - check sphere
  438. if (unlikely(none(valid)))
  439. return false;
  440. /* cone hits inside the neighboring capped cones are inside the geometry and thus ignored */
  441. const ConeGeometry<M> coneL (v0, vL);
  442. const ConeGeometry<M> coneR (v1, vR);
  443. #if !defined(EMBREE_BACKFACE_CULLING_CURVES)
  444. const Vec3vf<M> hit_lower = ray_org + t_cone_lower*ray_dir;
  445. const Vec3vf<M> hit_upper = ray_org + t_cone_upper*ray_dir;
  446. t_cone_lower = select (!coneL.isInsideCappedCone (validCone, hit_lower) & !coneR.isInsideCappedCone (validCone, hit_lower), t_cone_lower, vfloat<M>(pos_inf));
  447. t_cone_upper = select (!coneL.isInsideCappedCone (validCone, hit_upper) & !coneR.isInsideCappedCone (validCone, hit_upper), t_cone_upper, vfloat<M>(neg_inf));
  448. #endif
  449. /* intersect ending sphere */
  450. vfloat<M> t_sph1_lower, t_sph1_upper;
  451. vfloat<M> t_sph0_lower = vfloat<M>(pos_inf);
  452. vfloat<M> t_sph0_upper = vfloat<M>(neg_inf);
  453. cone.intersectEndSphere(valid, coneR, t_sph1_lower, t_sph1_upper);
  454. const vbool<M> isBeginPoint = valid & (vL[0] == vfloat<M>(pos_inf));
  455. if (unlikely(any(isBeginPoint))) {
  456. cone.intersectBeginSphere (isBeginPoint, t_sph0_lower, t_sph0_upper);
  457. }
  458. /* CSG union of cone and end sphere */
  459. vfloat<M> t_sph_lower = min(t_sph0_lower, t_sph1_lower);
  460. vfloat<M> t_cone_sphere_lower = min(t_cone_lower, t_sph_lower);
  461. #if !defined (EMBREE_BACKFACE_CULLING_CURVES)
  462. vfloat<M> t_sph_upper = max(t_sph0_upper, t_sph1_upper);
  463. vfloat<M> t_cone_sphere_upper = max(t_cone_upper, t_sph_upper);
  464. /* filter out hits that are not in tnear/tfar range */
  465. const vbool<M> valid_lower = valid & ray_tnear <= dt+t_cone_sphere_lower & dt+t_cone_sphere_lower <= ray_tfar() & t_cone_sphere_lower != vfloat<M>(pos_inf);
  466. const vbool<M> valid_upper = valid & ray_tnear <= dt+t_cone_sphere_upper & dt+t_cone_sphere_upper <= ray_tfar() & t_cone_sphere_upper != vfloat<M>(neg_inf);
  467. /* check if there is a first hit */
  468. const vbool<M> valid_first = valid_lower | valid_upper;
  469. if (unlikely(none(valid_first)))
  470. return false;
  471. /* construct first hit */
  472. const vfloat<M> t_first = select(valid_lower, t_cone_sphere_lower, t_cone_sphere_upper);
  473. const vbool<M> cone_hit_first = t_first == t_cone_lower | t_first == t_cone_upper;
  474. const vbool<M> sph0_hit_first = t_first == t_sph0_lower | t_first == t_sph0_upper;
  475. const Vec3vf<M> Ng_first = select(cone_hit_first, cone.Ng_cone(valid_lower), select (sph0_hit_first, cone.Ng_sphere0(valid_lower), cone.Ng_sphere1(valid_lower)));
  476. const vfloat<M> u_first = select(cone_hit_first, cone.u_cone(valid_lower), select (sph0_hit_first, vfloat<M>(zero), vfloat<M>(one)));
  477. /* invoke intersection filter for first hit */
  478. RoundLineIntersectorHitM<M> hit(u_first,zero,dt+t_first,Ng_first);
  479. const bool is_hit_first = epilog(valid_first, hit);
  480. /* check for possible second hits before potentially accepted hit */
  481. const vfloat<M> t_second = t_cone_sphere_upper;
  482. const vbool<M> valid_second = valid_lower & valid_upper & (dt+t_cone_sphere_upper <= ray_tfar());
  483. if (unlikely(none(valid_second)))
  484. return is_hit_first;
  485. /* invoke intersection filter for second hit */
  486. const vbool<M> cone_hit_second = t_second == t_cone_lower | t_second == t_cone_upper;
  487. const vbool<M> sph0_hit_second = t_second == t_sph0_lower | t_second == t_sph0_upper;
  488. const Vec3vf<M> Ng_second = select(cone_hit_second, cone.Ng_cone(false), select (sph0_hit_second, cone.Ng_sphere0(false), cone.Ng_sphere1(false)));
  489. const vfloat<M> u_second = select(cone_hit_second, cone.u_cone(false), select (sph0_hit_second, vfloat<M>(zero), vfloat<M>(one)));
  490. hit = RoundLineIntersectorHitM<M>(u_second,zero,dt+t_second,Ng_second);
  491. const bool is_hit_second = epilog(valid_second, hit);
  492. return is_hit_first | is_hit_second;
  493. #else
  494. /* filter out hits that are not in tnear/tfar range */
  495. const vbool<M> valid_lower = valid & ray_tnear <= dt+t_cone_sphere_lower & dt+t_cone_sphere_lower <= ray_tfar() & t_cone_sphere_lower != vfloat<M>(pos_inf);
  496. /* check if there is a valid hit */
  497. if (unlikely(none(valid_lower)))
  498. return false;
  499. /* construct first hit */
  500. const vbool<M> cone_hit_first = t_cone_sphere_lower == t_cone_lower | t_cone_sphere_lower == t_cone_upper;
  501. const vbool<M> sph0_hit_first = t_cone_sphere_lower == t_sph0_lower | t_cone_sphere_lower == t_sph0_upper;
  502. const Vec3vf<M> Ng_first = select(cone_hit_first, cone.Ng_cone(valid_lower), select (sph0_hit_first, cone.Ng_sphere0(valid_lower), cone.Ng_sphere1(valid_lower)));
  503. const vfloat<M> u_first = select(cone_hit_first, cone.u_cone(valid_lower), select (sph0_hit_first, vfloat<M>(zero), vfloat<M>(one)));
  504. /* invoke intersection filter for first hit */
  505. RoundLineIntersectorHitM<M> hit(u_first,zero,dt+t_cone_sphere_lower,Ng_first);
  506. const bool is_hit_first = epilog(valid_lower, hit);
  507. return is_hit_first;
  508. #endif
  509. }
  510. } // end namespace __roundline_internal
  511. template<int M>
  512. struct RoundLinearCurveIntersector1
  513. {
  514. typedef CurvePrecalculations1 Precalculations;
  515. template<typename Ray>
  516. struct ray_tfar {
  517. Ray& ray;
  518. __forceinline ray_tfar(Ray& ray) : ray(ray) {}
  519. __forceinline vfloat<M> operator() () const { return ray.tfar; };
  520. };
  521. template<typename Ray, typename Epilog>
  522. static __forceinline bool intersect(const vbool<M>& valid_i,
  523. Ray& ray,
  524. RayQueryContext* context,
  525. const LineSegments* geom,
  526. const Precalculations& pre,
  527. const Vec4vf<M>& v0i, const Vec4vf<M>& v1i,
  528. const Vec4vf<M>& vLi, const Vec4vf<M>& vRi,
  529. const Epilog& epilog)
  530. {
  531. const Vec3vf<M> ray_org(ray.org.x, ray.org.y, ray.org.z);
  532. const Vec3vf<M> ray_dir(ray.dir.x, ray.dir.y, ray.dir.z);
  533. const vfloat<M> ray_tnear(ray.tnear());
  534. const Vec4vf<M> v0 = enlargeRadiusToMinWidth<M>(context,geom,ray_org,v0i);
  535. const Vec4vf<M> v1 = enlargeRadiusToMinWidth<M>(context,geom,ray_org,v1i);
  536. const Vec4vf<M> vL = enlargeRadiusToMinWidth<M>(context,geom,ray_org,vLi);
  537. const Vec4vf<M> vR = enlargeRadiusToMinWidth<M>(context,geom,ray_org,vRi);
  538. return __roundline_internal::intersectConeSphere<M>(valid_i,ray_org,ray_dir,ray_tnear,ray_tfar<Ray>(ray),v0,v1,vL,vR,epilog);
  539. }
  540. };
  541. template<int M, int K>
  542. struct RoundLinearCurveIntersectorK
  543. {
  544. typedef CurvePrecalculationsK<K> Precalculations;
  545. struct ray_tfar {
  546. RayK<K>& ray;
  547. size_t k;
  548. __forceinline ray_tfar(RayK<K>& ray, size_t k) : ray(ray), k(k) {}
  549. __forceinline vfloat<M> operator() () const { return ray.tfar[k]; };
  550. };
  551. template<typename Epilog>
  552. static __forceinline bool intersect(const vbool<M>& valid_i,
  553. RayK<K>& ray, size_t k,
  554. RayQueryContext* context,
  555. const LineSegments* geom,
  556. const Precalculations& pre,
  557. const Vec4vf<M>& v0i, const Vec4vf<M>& v1i,
  558. const Vec4vf<M>& vLi, const Vec4vf<M>& vRi,
  559. const Epilog& epilog)
  560. {
  561. const Vec3vf<M> ray_org(ray.org.x[k], ray.org.y[k], ray.org.z[k]);
  562. const Vec3vf<M> ray_dir(ray.dir.x[k], ray.dir.y[k], ray.dir.z[k]);
  563. const vfloat<M> ray_tnear = ray.tnear()[k];
  564. const Vec4vf<M> v0 = enlargeRadiusToMinWidth<M>(context,geom,ray_org,v0i);
  565. const Vec4vf<M> v1 = enlargeRadiusToMinWidth<M>(context,geom,ray_org,v1i);
  566. const Vec4vf<M> vL = enlargeRadiusToMinWidth<M>(context,geom,ray_org,vLi);
  567. const Vec4vf<M> vR = enlargeRadiusToMinWidth<M>(context,geom,ray_org,vRi);
  568. return __roundline_internal::intersectConeSphere<M>(valid_i,ray_org,ray_dir,ray_tnear,ray_tfar(ray,k),v0,v1,vL,vR,epilog);
  569. }
  570. };
  571. }
  572. }