catmullrom_curve.h 11 KB

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