linear_bezier_patch.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. // Copyright 2009-2021 Intel Corporation
  2. // SPDX-License-Identifier: Apache-2.0
  3. #pragma once
  4. #include "bezier_curve.h"
  5. namespace embree
  6. {
  7. namespace isa
  8. {
  9. template<typename V>
  10. struct TensorLinearQuadraticBezierSurface
  11. {
  12. QuadraticBezierCurve<V> L;
  13. QuadraticBezierCurve<V> R;
  14. __forceinline TensorLinearQuadraticBezierSurface() {}
  15. __forceinline TensorLinearQuadraticBezierSurface(const TensorLinearQuadraticBezierSurface<V>& curve)
  16. : L(curve.L), R(curve.R) {}
  17. __forceinline TensorLinearQuadraticBezierSurface& operator= (const TensorLinearQuadraticBezierSurface& other) {
  18. L = other.L; R = other.R; return *this;
  19. }
  20. __forceinline TensorLinearQuadraticBezierSurface(const QuadraticBezierCurve<V>& L, const QuadraticBezierCurve<V>& R)
  21. : L(L), R(R) {}
  22. __forceinline BBox<V> bounds() const {
  23. return merge(L.bounds(),R.bounds());
  24. }
  25. };
  26. template<>
  27. struct TensorLinearQuadraticBezierSurface<Vec2fa>
  28. {
  29. QuadraticBezierCurve<vfloat4> LR;
  30. __forceinline TensorLinearQuadraticBezierSurface() {}
  31. __forceinline TensorLinearQuadraticBezierSurface(const TensorLinearQuadraticBezierSurface<Vec2fa>& curve)
  32. : LR(curve.LR) {}
  33. __forceinline TensorLinearQuadraticBezierSurface& operator= (const TensorLinearQuadraticBezierSurface& other) {
  34. LR = other.LR; return *this;
  35. }
  36. __forceinline TensorLinearQuadraticBezierSurface(const QuadraticBezierCurve<vfloat4>& LR)
  37. : LR(LR) {}
  38. __forceinline BBox<Vec2fa> bounds() const
  39. {
  40. const BBox<vfloat4> b = LR.bounds();
  41. const BBox<Vec2fa> bl(Vec2fa(b.lower),Vec2fa(b.upper));
  42. const BBox<Vec2fa> br(Vec2fa(shuffle<2,3,2,3>(b.lower)),Vec2fa(shuffle<2,3,2,3>(b.upper)));
  43. return merge(bl,br);
  44. }
  45. };
  46. template<typename V>
  47. struct TensorLinearCubicBezierSurface
  48. {
  49. CubicBezierCurve<V> L;
  50. CubicBezierCurve<V> R;
  51. __forceinline TensorLinearCubicBezierSurface() {}
  52. __forceinline TensorLinearCubicBezierSurface(const TensorLinearCubicBezierSurface& curve)
  53. : L(curve.L), R(curve.R) {}
  54. __forceinline TensorLinearCubicBezierSurface& operator= (const TensorLinearCubicBezierSurface& other) {
  55. L = other.L; R = other.R; return *this;
  56. }
  57. __forceinline TensorLinearCubicBezierSurface(const CubicBezierCurve<V>& L, const CubicBezierCurve<V>& R)
  58. : L(L), R(R) {}
  59. template<template<typename T> class SourceCurve>
  60. __forceinline static TensorLinearCubicBezierSurface fromCenterAndNormalCurve(const SourceCurve<Vec3ff>& center, const SourceCurve<Vec3fa>& normal)
  61. {
  62. SourceCurve<Vec3ff> vcurve = center;
  63. SourceCurve<Vec3fa> ncurve = normal;
  64. /* here we construct a patch which follows the curve l(t) =
  65. * p(t) +/- r(t)*normalize(cross(n(t),dp(t))) */
  66. const Vec3ff p0 = vcurve.eval(0.0f);
  67. const Vec3ff dp0 = vcurve.eval_du(0.0f);
  68. //const Vec3ff ddp0 = vcurve.eval_dudu(0.0f); // ddp0 is assumed to be 0
  69. const Vec3fa n0 = ncurve.eval(0.0f);
  70. const Vec3fa dn0 = ncurve.eval_du(0.0f);
  71. const Vec3ff p1 = vcurve.eval(1.0f);
  72. const Vec3ff dp1 = vcurve.eval_du(1.0f);
  73. //const Vec3ff ddp1 = vcurve.eval_dudu(1.0f); // ddp1 is assumed to be 0
  74. const Vec3fa n1 = ncurve.eval(1.0f);
  75. const Vec3fa dn1 = ncurve.eval_du(1.0f);
  76. const Vec3fa bt0 = cross(n0,dp0);
  77. const Vec3fa dbt0 = cross(dn0,dp0);// + cross(n0,ddp0);
  78. const Vec3fa bt1 = cross(n1,dp1);
  79. const Vec3fa dbt1 = cross(dn1,dp1);// + cross(n1,ddp1);
  80. const Vec3fa k0 = normalize(bt0);
  81. const Vec3fa dk0 = dnormalize(bt0,dbt0);
  82. const Vec3fa k1 = normalize(bt1);
  83. const Vec3fa dk1 = dnormalize(bt1,dbt1);
  84. const Vec3fa l0 = p0 - p0.w*k0;
  85. const Vec3fa dl0 = dp0 - (dp0.w*k0 + p0.w*dk0);
  86. const Vec3fa r0 = p0 + p0.w*k0;
  87. const Vec3fa dr0 = dp0 + (dp0.w*k0 + p0.w*dk0);
  88. const Vec3fa l1 = p1 - p1.w*k1;
  89. const Vec3fa dl1 = dp1 - (dp1.w*k1 + p1.w*dk1);
  90. const Vec3fa r1 = p1 + p1.w*k1;
  91. const Vec3fa dr1 = dp1 + (dp1.w*k1 + p1.w*dk1);
  92. const float scale = 1.0f/3.0f;
  93. CubicBezierCurve<V> L(l0,l0+scale*dl0,l1-scale*dl1,l1);
  94. CubicBezierCurve<V> R(r0,r0+scale*dr0,r1-scale*dr1,r1);
  95. return TensorLinearCubicBezierSurface(L,R);
  96. }
  97. __forceinline BBox<V> bounds() const {
  98. return merge(L.bounds(),R.bounds());
  99. }
  100. __forceinline BBox3fa accurateBounds() const {
  101. return merge(L.accurateBounds(),R.accurateBounds());
  102. }
  103. __forceinline CubicBezierCurve<Interval1f> reduce_v() const {
  104. return merge(CubicBezierCurve<Interval<V>>(L),CubicBezierCurve<Interval<V>>(R));
  105. }
  106. __forceinline LinearBezierCurve<Interval1f> reduce_u() const {
  107. return LinearBezierCurve<Interval1f>(L.bounds(),R.bounds());
  108. }
  109. __forceinline TensorLinearCubicBezierSurface<float> xfm(const V& dx) const {
  110. return TensorLinearCubicBezierSurface<float>(L.xfm(dx),R.xfm(dx));
  111. }
  112. __forceinline TensorLinearCubicBezierSurface<vfloatx> vxfm(const V& dx) const {
  113. return TensorLinearCubicBezierSurface<vfloatx>(L.vxfm(dx),R.vxfm(dx));
  114. }
  115. __forceinline TensorLinearCubicBezierSurface<float> xfm(const V& dx, const V& p) const {
  116. return TensorLinearCubicBezierSurface<float>(L.xfm(dx,p),R.xfm(dx,p));
  117. }
  118. __forceinline TensorLinearCubicBezierSurface<Vec3fa> xfm(const LinearSpace3fa& space) const {
  119. return TensorLinearCubicBezierSurface(L.xfm(space),R.xfm(space));
  120. }
  121. __forceinline TensorLinearCubicBezierSurface<Vec3fa> xfm(const LinearSpace3fa& space, const Vec3fa& p) const {
  122. return TensorLinearCubicBezierSurface(L.xfm(space,p),R.xfm(space,p));
  123. }
  124. __forceinline TensorLinearCubicBezierSurface<Vec3fa> xfm(const LinearSpace3fa& space, const Vec3fa& p, const float s) const {
  125. return TensorLinearCubicBezierSurface(L.xfm(space,p,s),R.xfm(space,p,s));
  126. }
  127. __forceinline TensorLinearCubicBezierSurface clip_u(const Interval1f& u) const {
  128. return TensorLinearCubicBezierSurface(L.clip(u),R.clip(u));
  129. }
  130. __forceinline TensorLinearCubicBezierSurface clip_v(const Interval1f& v) const {
  131. return TensorLinearCubicBezierSurface(clerp(L,R,V(v.lower)),clerp(L,R,V(v.upper)));
  132. }
  133. __forceinline TensorLinearCubicBezierSurface clip(const Interval1f& u, const Interval1f& v) const {
  134. return clip_v(v).clip_u(u);
  135. }
  136. __forceinline void split_u(TensorLinearCubicBezierSurface& left, TensorLinearCubicBezierSurface& right, const float u = 0.5f) const
  137. {
  138. CubicBezierCurve<V> L0,L1; L.split(L0,L1,u);
  139. CubicBezierCurve<V> R0,R1; R.split(R0,R1,u);
  140. new (&left ) TensorLinearCubicBezierSurface(L0,R0);
  141. new (&right) TensorLinearCubicBezierSurface(L1,R1);
  142. }
  143. __forceinline TensorLinearCubicBezierSurface<Vec2vfx> vsplit_u(vboolx& valid, const BBox1f& u) const {
  144. valid = true; clear(valid,VSIZEX-1);
  145. return TensorLinearCubicBezierSurface<Vec2vfx>(L.split(u),R.split(u));
  146. }
  147. __forceinline V eval(const float u, const float v) const {
  148. return clerp(L,R,V(v)).eval(u);
  149. }
  150. __forceinline V eval_du(const float u, const float v) const {
  151. return clerp(L,R,V(v)).eval_dt(u);
  152. }
  153. __forceinline V eval_dv(const float u, const float v) const {
  154. return (R-L).eval(u);
  155. }
  156. __forceinline void eval(const float u, const float v, V& p, V& dpdu, V& dpdv) const
  157. {
  158. V p0, dp0du; L.eval(u,p0,dp0du);
  159. V p1, dp1du; R.eval(u,p1,dp1du);
  160. p = lerp(p0,p1,v);
  161. dpdu = lerp(dp0du,dp1du,v);
  162. dpdv = p1-p0;
  163. }
  164. __forceinline TensorLinearQuadraticBezierSurface<V> derivative_u() const {
  165. return TensorLinearQuadraticBezierSurface<V>(L.derivative(),R.derivative());
  166. }
  167. __forceinline CubicBezierCurve<V> derivative_v() const {
  168. return R-L;
  169. }
  170. __forceinline V axis_u() const {
  171. return (L.end()-L.begin())+(R.end()-R.begin());
  172. }
  173. __forceinline V axis_v() const {
  174. return (R.begin()-L.begin())+(R.end()-L.end());
  175. }
  176. friend embree_ostream operator<<(embree_ostream cout, const TensorLinearCubicBezierSurface& a)
  177. {
  178. return cout << "TensorLinearCubicBezierSurface" << embree_endl
  179. << "{" << embree_endl
  180. << " L = " << a.L << ", " << embree_endl
  181. << " R = " << a.R << embree_endl
  182. << "}";
  183. }
  184. friend __forceinline TensorLinearCubicBezierSurface clerp(const TensorLinearCubicBezierSurface& a, const TensorLinearCubicBezierSurface& b, const float t) {
  185. return TensorLinearCubicBezierSurface(clerp(a.L,b.L,V(t)), clerp(a.R,b.R,V(t)));
  186. }
  187. };
  188. template<>
  189. struct TensorLinearCubicBezierSurface<Vec2fa>
  190. {
  191. CubicBezierCurve<vfloat4> LR;
  192. __forceinline TensorLinearCubicBezierSurface() {}
  193. __forceinline TensorLinearCubicBezierSurface(const TensorLinearCubicBezierSurface& curve)
  194. : LR(curve.LR) {}
  195. __forceinline TensorLinearCubicBezierSurface& operator= (const TensorLinearCubicBezierSurface& other) {
  196. LR = other.LR; return *this;
  197. }
  198. __forceinline TensorLinearCubicBezierSurface(const CubicBezierCurve<vfloat4>& LR)
  199. : LR(LR) {}
  200. __forceinline TensorLinearCubicBezierSurface(const CubicBezierCurve<Vec2fa>& L, const CubicBezierCurve<Vec2fa>& R)
  201. : LR(shuffle<0,1,0,1>(vfloat4(L.v0),vfloat4(R.v0)),shuffle<0,1,0,1>(vfloat4(L.v1),vfloat4(R.v1)),shuffle<0,1,0,1>(vfloat4(L.v2),vfloat4(R.v2)),shuffle<0,1,0,1>(vfloat4(L.v3),vfloat4(R.v3))) {}
  202. __forceinline CubicBezierCurve<Vec2fa> getL() const {
  203. return CubicBezierCurve<Vec2fa>(Vec2fa(LR.v0),Vec2fa(LR.v1),Vec2fa(LR.v2),Vec2fa(LR.v3));
  204. }
  205. __forceinline CubicBezierCurve<Vec2fa> getR() const {
  206. return CubicBezierCurve<Vec2fa>(Vec2fa(shuffle<2,3,2,3>(LR.v0)),Vec2fa(shuffle<2,3,2,3>(LR.v1)),Vec2fa(shuffle<2,3,2,3>(LR.v2)),Vec2fa(shuffle<2,3,2,3>(LR.v3)));
  207. }
  208. __forceinline BBox<Vec2fa> bounds() const
  209. {
  210. const BBox<vfloat4> b = LR.bounds();
  211. const BBox<Vec2fa> bl(Vec2fa(b.lower),Vec2fa(b.upper));
  212. const BBox<Vec2fa> br(Vec2fa(shuffle<2,3,2,3>(b.lower)),Vec2fa(shuffle<2,3,2,3>(b.upper)));
  213. return merge(bl,br);
  214. }
  215. __forceinline BBox1f bounds(const Vec2fa& axis) const
  216. {
  217. const CubicBezierCurve<vfloat4> LRx = LR;
  218. const CubicBezierCurve<vfloat4> LRy(shuffle<1,0,3,2>(LR.v0),shuffle<1,0,3,2>(LR.v1),shuffle<1,0,3,2>(LR.v2),shuffle<1,0,3,2>(LR.v3));
  219. const CubicBezierCurve<vfloat4> LRa = cmadd(shuffle<0>(vfloat4(axis)),LRx,shuffle<1>(vfloat4(axis))*LRy);
  220. const BBox<vfloat4> Lb = LRa.bounds();
  221. const BBox<vfloat4> Rb(shuffle<3>(Lb.lower),shuffle<3>(Lb.upper));
  222. const BBox<vfloat4> b = merge(Lb,Rb);
  223. return BBox1f(b.lower[0],b.upper[0]);
  224. }
  225. __forceinline TensorLinearCubicBezierSurface<float> xfm(const Vec2fa& dx) const
  226. {
  227. const CubicBezierCurve<vfloat4> LRx = LR;
  228. const CubicBezierCurve<vfloat4> LRy(shuffle<1,0,3,2>(LR.v0),shuffle<1,0,3,2>(LR.v1),shuffle<1,0,3,2>(LR.v2),shuffle<1,0,3,2>(LR.v3));
  229. const CubicBezierCurve<vfloat4> LRa = cmadd(shuffle<0>(vfloat4(dx)),LRx,shuffle<1>(vfloat4(dx))*LRy);
  230. return TensorLinearCubicBezierSurface<float>(CubicBezierCurve<float>(LRa.v0[0],LRa.v1[0],LRa.v2[0],LRa.v3[0]),
  231. CubicBezierCurve<float>(LRa.v0[2],LRa.v1[2],LRa.v2[2],LRa.v3[2]));
  232. }
  233. __forceinline TensorLinearCubicBezierSurface<float> xfm(const Vec2fa& dx, const Vec2fa& p) const
  234. {
  235. const vfloat4 pxyxy = shuffle<0,1,0,1>(vfloat4(p));
  236. const CubicBezierCurve<vfloat4> LRx = LR-pxyxy;
  237. const CubicBezierCurve<vfloat4> LRy(shuffle<1,0,3,2>(LR.v0),shuffle<1,0,3,2>(LR.v1),shuffle<1,0,3,2>(LR.v2),shuffle<1,0,3,2>(LR.v3));
  238. const CubicBezierCurve<vfloat4> LRa = cmadd(shuffle<0>(vfloat4(dx)),LRx,shuffle<1>(vfloat4(dx))*LRy);
  239. return TensorLinearCubicBezierSurface<float>(CubicBezierCurve<float>(LRa.v0[0],LRa.v1[0],LRa.v2[0],LRa.v3[0]),
  240. CubicBezierCurve<float>(LRa.v0[2],LRa.v1[2],LRa.v2[2],LRa.v3[2]));
  241. }
  242. __forceinline TensorLinearCubicBezierSurface clip_u(const Interval1f& u) const {
  243. return TensorLinearCubicBezierSurface(LR.clip(u));
  244. }
  245. __forceinline TensorLinearCubicBezierSurface clip_v(const Interval1f& v) const
  246. {
  247. const CubicBezierCurve<vfloat4> LL(shuffle<0,1,0,1>(LR.v0),shuffle<0,1,0,1>(LR.v1),shuffle<0,1,0,1>(LR.v2),shuffle<0,1,0,1>(LR.v3));
  248. const CubicBezierCurve<vfloat4> RR(shuffle<2,3,2,3>(LR.v0),shuffle<2,3,2,3>(LR.v1),shuffle<2,3,2,3>(LR.v2),shuffle<2,3,2,3>(LR.v3));
  249. return TensorLinearCubicBezierSurface(clerp(LL,RR,vfloat4(v.lower,v.lower,v.upper,v.upper)));
  250. }
  251. __forceinline TensorLinearCubicBezierSurface clip(const Interval1f& u, const Interval1f& v) const {
  252. return clip_v(v).clip_u(u);
  253. }
  254. __forceinline void split_u(TensorLinearCubicBezierSurface& left, TensorLinearCubicBezierSurface& right, const float u = 0.5f) const
  255. {
  256. CubicBezierCurve<vfloat4> LR0,LR1; LR.split(LR0,LR1,u);
  257. new (&left ) TensorLinearCubicBezierSurface(LR0);
  258. new (&right) TensorLinearCubicBezierSurface(LR1);
  259. }
  260. __forceinline TensorLinearCubicBezierSurface<Vec2vfx> vsplit_u(vboolx& valid, const BBox1f& u) const {
  261. valid = true; clear(valid,VSIZEX-1);
  262. return TensorLinearCubicBezierSurface<Vec2vfx>(getL().split(u),getR().split(u));
  263. }
  264. __forceinline Vec2fa eval(const float u, const float v) const
  265. {
  266. const vfloat4 p = LR.eval(u);
  267. return Vec2fa(lerp(shuffle<0,1,0,1>(p),shuffle<2,3,2,3>(p),v));
  268. }
  269. __forceinline Vec2fa eval_du(const float u, const float v) const
  270. {
  271. const vfloat4 dpdu = LR.eval_dt(u);
  272. return Vec2fa(lerp(shuffle<0,1,0,1>(dpdu),shuffle<2,3,2,3>(dpdu),v));
  273. }
  274. __forceinline Vec2fa eval_dv(const float u, const float v) const
  275. {
  276. const vfloat4 p = LR.eval(u);
  277. return Vec2fa(shuffle<2,3,2,3>(p)-shuffle<0,1,0,1>(p));
  278. }
  279. __forceinline void eval(const float u, const float v, Vec2fa& p, Vec2fa& dpdu, Vec2fa& dpdv) const
  280. {
  281. vfloat4 p0, dp0du; LR.eval(u,p0,dp0du);
  282. p = Vec2fa(lerp(shuffle<0,1,0,1>(p0),shuffle<2,3,2,3>(p0),v));
  283. dpdu = Vec2fa(lerp(shuffle<0,1,0,1>(dp0du),shuffle<2,3,2,3>(dp0du),v));
  284. dpdv = Vec2fa(shuffle<2,3,2,3>(p0)-shuffle<0,1,0,1>(p0));
  285. }
  286. __forceinline TensorLinearQuadraticBezierSurface<Vec2fa> derivative_u() const {
  287. return TensorLinearQuadraticBezierSurface<Vec2fa>(LR.derivative());
  288. }
  289. __forceinline CubicBezierCurve<Vec2fa> derivative_v() const {
  290. return getR()-getL();
  291. }
  292. __forceinline Vec2fa axis_u() const
  293. {
  294. const CubicBezierCurve<Vec2fa> L = getL();
  295. const CubicBezierCurve<Vec2fa> R = getR();
  296. return (L.end()-L.begin())+(R.end()-R.begin());
  297. }
  298. __forceinline Vec2fa axis_v() const
  299. {
  300. const CubicBezierCurve<Vec2fa> L = getL();
  301. const CubicBezierCurve<Vec2fa> R = getR();
  302. return (R.begin()-L.begin())+(R.end()-L.end());
  303. }
  304. friend embree_ostream operator<<(embree_ostream cout, const TensorLinearCubicBezierSurface& a)
  305. {
  306. return cout << "TensorLinearCubicBezierSurface" << embree_endl
  307. << "{" << embree_endl
  308. << " L = " << a.getL() << ", " << embree_endl
  309. << " R = " << a.getR() << embree_endl
  310. << "}";
  311. }
  312. };
  313. typedef TensorLinearCubicBezierSurface<float> TensorLinearCubicBezierSurface1f;
  314. typedef TensorLinearCubicBezierSurface<Vec2fa> TensorLinearCubicBezierSurface2fa;
  315. typedef TensorLinearCubicBezierSurface<Vec3fa> TensorLinearCubicBezierSurface3fa;
  316. }
  317. }