catmullrom_curve.h 11 KB

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