bspline_curve.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. // Copyright 2009-2021 Intel Corporation
  2. // SPDX-License-Identifier: Apache-2.0
  3. #pragma once
  4. #include "../common/default.h"
  5. #include "bezier_curve.h"
  6. namespace embree
  7. {
  8. class BSplineBasis
  9. {
  10. public:
  11. template<typename T>
  12. static __forceinline Vec4<T> eval(const T& u)
  13. {
  14. const T t = u;
  15. const T s = T(1.0f) - u;
  16. const T n0 = s*s*s;
  17. const T n1 = (4.0f*(s*s*s)+(t*t*t)) + (12.0f*((s*t)*s) + 6.0f*((t*s)*t));
  18. const T n2 = (4.0f*(t*t*t)+(s*s*s)) + (12.0f*((t*s)*t) + 6.0f*((s*t)*s));
  19. const T n3 = t*t*t;
  20. return T(1.0f/6.0f)*Vec4<T>(n0,n1,n2,n3);
  21. }
  22. template<typename T>
  23. static __forceinline Vec4<T> derivative(const T& u)
  24. {
  25. const T t = u;
  26. const T s = 1.0f - u;
  27. const T n0 = -s*s;
  28. const T n1 = -t*t - 4.0f*(t*s);
  29. const T n2 = s*s + 4.0f*(s*t);
  30. const T n3 = t*t;
  31. return T(0.5f)*Vec4<T>(n0,n1,n2,n3);
  32. }
  33. template<typename T>
  34. static __forceinline Vec4<T> derivative2(const T& u)
  35. {
  36. const T t = u;
  37. const T s = 1.0f - u;
  38. const T n0 = s;
  39. const T n1 = t - 2.0f*s;
  40. const T n2 = s - 2.0f*t;
  41. const T n3 = t;
  42. return Vec4<T>(n0,n1,n2,n3);
  43. }
  44. };
  45. struct PrecomputedBSplineBasis
  46. {
  47. enum { N = 16 };
  48. public:
  49. PrecomputedBSplineBasis() {}
  50. PrecomputedBSplineBasis(int shift);
  51. /* basis for bspline evaluation */
  52. public:
  53. float c0[N+1][N+1];
  54. float c1[N+1][N+1];
  55. float c2[N+1][N+1];
  56. float c3[N+1][N+1];
  57. /* basis for bspline derivative evaluation */
  58. public:
  59. float d0[N+1][N+1];
  60. float d1[N+1][N+1];
  61. float d2[N+1][N+1];
  62. float d3[N+1][N+1];
  63. };
  64. extern PrecomputedBSplineBasis bspline_basis0;
  65. extern PrecomputedBSplineBasis bspline_basis1;
  66. template<typename Vertex>
  67. struct BSplineCurveT
  68. {
  69. Vertex v0,v1,v2,v3;
  70. __forceinline BSplineCurveT() {}
  71. __forceinline BSplineCurveT(const Vertex& v0, const Vertex& v1, const Vertex& v2, const Vertex& v3)
  72. : v0(v0), v1(v1), v2(v2), v3(v3) {}
  73. __forceinline Vertex begin() const {
  74. return madd(1.0f/6.0f,v0,madd(2.0f/3.0f,v1,1.0f/6.0f*v2));
  75. }
  76. __forceinline Vertex end() const {
  77. return madd(1.0f/6.0f,v1,madd(2.0f/3.0f,v2,1.0f/6.0f*v3));
  78. }
  79. __forceinline Vertex center() const {
  80. return 0.25f*(v0+v1+v2+v3);
  81. }
  82. __forceinline BBox<Vertex> bounds() const {
  83. return merge(BBox<Vertex>(v0),BBox<Vertex>(v1),BBox<Vertex>(v2),BBox<Vertex>(v3));
  84. }
  85. __forceinline friend BSplineCurveT operator -( const BSplineCurveT& a, const Vertex& b ) {
  86. return BSplineCurveT(a.v0-b,a.v1-b,a.v2-b,a.v3-b);
  87. }
  88. __forceinline BSplineCurveT<Vec3ff> xfm_pr(const LinearSpace3fa& space, const Vec3fa& p) const
  89. {
  90. const Vec3ff q0(xfmVector(space,(Vec3fa)v0-p), v0.w);
  91. const Vec3ff q1(xfmVector(space,(Vec3fa)v1-p), v1.w);
  92. const Vec3ff q2(xfmVector(space,(Vec3fa)v2-p), v2.w);
  93. const Vec3ff q3(xfmVector(space,(Vec3fa)v3-p), v3.w);
  94. return BSplineCurveT<Vec3ff>(q0,q1,q2,q3);
  95. }
  96. __forceinline Vertex eval(const float t) const
  97. {
  98. const Vec4<float> b = BSplineBasis::eval(t);
  99. return madd(b.x,v0,madd(b.y,v1,madd(b.z,v2,b.w*v3)));
  100. }
  101. __forceinline Vertex eval_du(const float t) const
  102. {
  103. const Vec4<float> b = BSplineBasis::derivative(t);
  104. return madd(b.x,v0,madd(b.y,v1,madd(b.z,v2,b.w*v3)));
  105. }
  106. __forceinline Vertex eval_dudu(const float t) const
  107. {
  108. const Vec4<float> b = BSplineBasis::derivative2(t);
  109. return madd(b.x,v0,madd(b.y,v1,madd(b.z,v2,b.w*v3)));
  110. }
  111. __forceinline void eval(const float t, Vertex& p, Vertex& dp, Vertex& ddp) const
  112. {
  113. p = eval(t);
  114. dp = eval_du(t);
  115. ddp = eval_dudu(t);
  116. }
  117. template<int M>
  118. __forceinline Vec4vf<M> veval(const vfloat<M>& t) const
  119. {
  120. const Vec4vf<M> b = BSplineBasis::eval(t);
  121. return madd(b.x, Vec4vf<M>(v0), madd(b.y, Vec4vf<M>(v1), madd(b.z, Vec4vf<M>(v2), b.w * Vec4vf<M>(v3))));
  122. }
  123. template<int M>
  124. __forceinline Vec4vf<M> veval_du(const vfloat<M>& t) const
  125. {
  126. const Vec4vf<M> b = BSplineBasis::derivative(t);
  127. return madd(b.x, Vec4vf<M>(v0), madd(b.y, Vec4vf<M>(v1), madd(b.z, Vec4vf<M>(v2), b.w * Vec4vf<M>(v3))));
  128. }
  129. template<int M>
  130. __forceinline Vec4vf<M> veval_dudu(const vfloat<M>& t) const
  131. {
  132. const Vec4vf<M> b = BSplineBasis::derivative2(t);
  133. return madd(b.x, Vec4vf<M>(v0), madd(b.y, Vec4vf<M>(v1), madd(b.z, Vec4vf<M>(v2), b.w * Vec4vf<M>(v3))));
  134. }
  135. template<int M>
  136. __forceinline void veval(const vfloat<M>& t, Vec4vf<M>& p, Vec4vf<M>& dp) const
  137. {
  138. p = veval<M>(t);
  139. dp = veval_du<M>(t);
  140. }
  141. template<int M>
  142. __forceinline Vec4vf<M> eval0(const int ofs, const int size) const
  143. {
  144. assert(size <= PrecomputedBSplineBasis::N);
  145. assert(ofs <= size);
  146. return madd(vfloat<M>::loadu(&bspline_basis0.c0[size][ofs]), Vec4vf<M>(v0),
  147. madd(vfloat<M>::loadu(&bspline_basis0.c1[size][ofs]), Vec4vf<M>(v1),
  148. madd(vfloat<M>::loadu(&bspline_basis0.c2[size][ofs]), Vec4vf<M>(v2),
  149. vfloat<M>::loadu(&bspline_basis0.c3[size][ofs]) * Vec4vf<M>(v3))));
  150. }
  151. template<int M>
  152. __forceinline Vec4vf<M> eval1(const int ofs, const int size) const
  153. {
  154. assert(size <= PrecomputedBSplineBasis::N);
  155. assert(ofs <= size);
  156. return madd(vfloat<M>::loadu(&bspline_basis1.c0[size][ofs]), Vec4vf<M>(v0),
  157. madd(vfloat<M>::loadu(&bspline_basis1.c1[size][ofs]), Vec4vf<M>(v1),
  158. madd(vfloat<M>::loadu(&bspline_basis1.c2[size][ofs]), Vec4vf<M>(v2),
  159. vfloat<M>::loadu(&bspline_basis1.c3[size][ofs]) * Vec4vf<M>(v3))));
  160. }
  161. template<int M>
  162. __forceinline Vec4vf<M> derivative0(const int ofs, const int size) const
  163. {
  164. assert(size <= PrecomputedBSplineBasis::N);
  165. assert(ofs <= size);
  166. return madd(vfloat<M>::loadu(&bspline_basis0.d0[size][ofs]), Vec4vf<M>(v0),
  167. madd(vfloat<M>::loadu(&bspline_basis0.d1[size][ofs]), Vec4vf<M>(v1),
  168. madd(vfloat<M>::loadu(&bspline_basis0.d2[size][ofs]), Vec4vf<M>(v2),
  169. vfloat<M>::loadu(&bspline_basis0.d3[size][ofs]) * Vec4vf<M>(v3))));
  170. }
  171. template<int M>
  172. __forceinline Vec4vf<M> derivative1(const int ofs, const int size) const
  173. {
  174. assert(size <= PrecomputedBSplineBasis::N);
  175. assert(ofs <= size);
  176. return madd(vfloat<M>::loadu(&bspline_basis1.d0[size][ofs]), Vec4vf<M>(v0),
  177. madd(vfloat<M>::loadu(&bspline_basis1.d1[size][ofs]), Vec4vf<M>(v1),
  178. madd(vfloat<M>::loadu(&bspline_basis1.d2[size][ofs]), Vec4vf<M>(v2),
  179. vfloat<M>::loadu(&bspline_basis1.d3[size][ofs]) * Vec4vf<M>(v3))));
  180. }
  181. /* calculates bounds of bspline curve geometry */
  182. __forceinline BBox3fa accurateRoundBounds() const
  183. {
  184. const int N = 7;
  185. const float scale = 1.0f/(3.0f*(N-1));
  186. Vec4vfx pl(pos_inf), pu(neg_inf);
  187. for (int i=0; i<=N; i+=VSIZEX)
  188. {
  189. vintx vi = vintx(i)+vintx(step);
  190. vboolx valid = vi <= vintx(N);
  191. const Vec4vfx p = eval0<VSIZEX>(i,N);
  192. const Vec4vfx dp = derivative0<VSIZEX>(i,N);
  193. const Vec4vfx pm = p-Vec4vfx(scale)*select(vi!=vintx(0),dp,Vec4vfx(zero));
  194. const Vec4vfx pp = p+Vec4vfx(scale)*select(vi!=vintx(N),dp,Vec4vfx(zero));
  195. pl = select(valid,min(pl,p,pm,pp),pl); // FIXME: use masked min
  196. pu = select(valid,max(pu,p,pm,pp),pu); // FIXME: use masked min
  197. }
  198. const Vec3fa lower(reduce_min(pl.x),reduce_min(pl.y),reduce_min(pl.z));
  199. const Vec3fa upper(reduce_max(pu.x),reduce_max(pu.y),reduce_max(pu.z));
  200. const float r_min = reduce_min(pl.w);
  201. const float r_max = reduce_max(pu.w);
  202. const Vec3fa upper_r = Vec3fa(max(abs(r_min),abs(r_max)));
  203. return enlarge(BBox3fa(lower,upper),upper_r);
  204. }
  205. /* calculates bounds when tessellated into N line segments */
  206. __forceinline BBox3fa accurateFlatBounds(int N) const
  207. {
  208. if (likely(N == 4))
  209. {
  210. const Vec4vf4 pi = eval0<4>(0,4);
  211. const Vec3fa lower(reduce_min(pi.x),reduce_min(pi.y),reduce_min(pi.z));
  212. const Vec3fa upper(reduce_max(pi.x),reduce_max(pi.y),reduce_max(pi.z));
  213. const Vec3fa upper_r = Vec3fa(reduce_max(abs(pi.w)));
  214. const Vec3ff pe = end();
  215. return enlarge(BBox3fa(min(lower,pe),max(upper,pe)),max(upper_r,Vec3fa(abs(pe.w))));
  216. }
  217. else
  218. {
  219. Vec3vfx pl(pos_inf), pu(neg_inf); vfloatx ru(0.0f);
  220. for (int i=0; i<=N; i+=VSIZEX)
  221. {
  222. vboolx valid = vintx(i)+vintx(step) <= vintx(N);
  223. const Vec4vfx pi = eval0<VSIZEX>(i,N);
  224. pl.x = select(valid,min(pl.x,pi.x),pl.x); // FIXME: use masked min
  225. pl.y = select(valid,min(pl.y,pi.y),pl.y);
  226. pl.z = select(valid,min(pl.z,pi.z),pl.z);
  227. pu.x = select(valid,max(pu.x,pi.x),pu.x); // FIXME: use masked min
  228. pu.y = select(valid,max(pu.y,pi.y),pu.y);
  229. pu.z = select(valid,max(pu.z,pi.z),pu.z);
  230. ru = select(valid,max(ru,abs(pi.w)),ru);
  231. }
  232. const Vec3fa lower(reduce_min(pl.x),reduce_min(pl.y),reduce_min(pl.z));
  233. const Vec3fa upper(reduce_max(pu.x),reduce_max(pu.y),reduce_max(pu.z));
  234. const Vec3fa upper_r(reduce_max(ru));
  235. return enlarge(BBox3fa(lower,upper),upper_r);
  236. }
  237. }
  238. friend __forceinline embree_ostream operator<<(embree_ostream cout, const BSplineCurveT& curve) {
  239. return cout << "BSplineCurve { v0 = " << curve.v0 << ", v1 = " << curve.v1 << ", v2 = " << curve.v2 << ", v3 = " << curve.v3 << " }";
  240. }
  241. };
  242. template<typename Vertex>
  243. __forceinline void convert(const BezierCurveT<Vertex>& icurve, BezierCurveT<Vertex>& ocurve) {
  244. ocurve = icurve;
  245. }
  246. template<typename Vertex>
  247. __forceinline void convert(const BSplineCurveT<Vertex>& icurve, BSplineCurveT<Vertex>& ocurve) {
  248. ocurve = icurve;
  249. }
  250. template<typename Vertex>
  251. __forceinline void convert(const BezierCurveT<Vertex>& icurve, BSplineCurveT<Vertex>& ocurve)
  252. {
  253. const Vertex v0 = madd(6.0f,icurve.v0,madd(-7.0f,icurve.v1,2.0f*icurve.v2));
  254. const Vertex v1 = msub(2.0f,icurve.v1,icurve.v2);
  255. const Vertex v2 = msub(2.0f,icurve.v2,icurve.v1);
  256. const Vertex v3 = madd(2.0f,icurve.v1,madd(-7.0f,icurve.v2,6.0f*icurve.v3));
  257. ocurve = BSplineCurveT<Vertex>(v0,v1,v2,v3);
  258. }
  259. template<typename Vertex>
  260. __forceinline void convert(const BSplineCurveT<Vertex>& icurve, BezierCurveT<Vertex>& ocurve)
  261. {
  262. const Vertex v0 = madd(1.0f/6.0f,icurve.v0,madd(2.0f/3.0f,icurve.v1,1.0f/6.0f*icurve.v2));
  263. const Vertex v1 = madd(2.0f/3.0f,icurve.v1,1.0f/3.0f*icurve.v2);
  264. const Vertex v2 = madd(1.0f/3.0f,icurve.v1,2.0f/3.0f*icurve.v2);
  265. const Vertex v3 = madd(1.0f/6.0f,icurve.v1,madd(2.0f/3.0f,icurve.v2,1.0f/6.0f*icurve.v3));
  266. ocurve = BezierCurveT<Vertex>(v0,v1,v2,v3);
  267. }
  268. template<typename CurveGeometry>
  269. __forceinline BSplineCurveT<Vec3ff> enlargeRadiusToMinWidth(const IntersectContext* context, const CurveGeometry* geom, const Vec3fa& ray_org, const BSplineCurveT<Vec3ff>& curve)
  270. {
  271. return BSplineCurveT<Vec3ff>(enlargeRadiusToMinWidth(context,geom,ray_org,curve.v0),
  272. enlargeRadiusToMinWidth(context,geom,ray_org,curve.v1),
  273. enlargeRadiusToMinWidth(context,geom,ray_org,curve.v2),
  274. enlargeRadiusToMinWidth(context,geom,ray_org,curve.v3));
  275. }
  276. typedef BSplineCurveT<Vec3fa> BSplineCurve3fa;
  277. }