bezier_patch.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. // Copyright 2009-2021 Intel Corporation
  2. // SPDX-License-Identifier: Apache-2.0
  3. #pragma once
  4. #include "catmullclark_patch.h"
  5. #include "bezier_curve.h"
  6. namespace embree
  7. {
  8. template<class T, class S>
  9. static __forceinline T deCasteljau(const S& uu, const T& v0, const T& v1, const T& v2, const T& v3)
  10. {
  11. const T v0_1 = lerp(v0,v1,uu);
  12. const T v1_1 = lerp(v1,v2,uu);
  13. const T v2_1 = lerp(v2,v3,uu);
  14. const T v0_2 = lerp(v0_1,v1_1,uu);
  15. const T v1_2 = lerp(v1_1,v2_1,uu);
  16. const T v0_3 = lerp(v0_2,v1_2,uu);
  17. return v0_3;
  18. }
  19. template<class T, class S>
  20. static __forceinline T deCasteljau_tangent(const S& uu, const T& v0, const T& v1, const T& v2, const T& v3)
  21. {
  22. const T v0_1 = lerp(v0,v1,uu);
  23. const T v1_1 = lerp(v1,v2,uu);
  24. const T v2_1 = lerp(v2,v3,uu);
  25. const T v0_2 = lerp(v0_1,v1_1,uu);
  26. const T v1_2 = lerp(v1_1,v2_1,uu);
  27. return S(3.0f)*(v1_2-v0_2);
  28. }
  29. template<typename Vertex>
  30. __forceinline Vertex computeInnerBezierControlPoint(const Vertex v[4][4], const size_t y, const size_t x) {
  31. return 1.0f / 36.0f * (16.0f * v[y][x] + 4.0f * (v[y-1][x] + v[y+1][x] + v[y][x-1] + v[y][x+1]) + (v[y-1][x-1] + v[y+1][x+1] + v[y-1][x+1] + v[y+1][x-1]));
  32. }
  33. template<typename Vertex>
  34. __forceinline Vertex computeTopEdgeBezierControlPoint(const Vertex v[4][4], const size_t y, const size_t x) {
  35. return 1.0f / 18.0f * (8.0f * v[y][x] + 4.0f * v[y-1][x] + 2.0f * (v[y][x-1] + v[y][x+1]) + (v[y-1][x-1] + v[y-1][x+1]));
  36. }
  37. template<typename Vertex>
  38. __forceinline Vertex computeBottomEdgeBezierControlPoint(const Vertex v[4][4], const size_t y, const size_t x) {
  39. return 1.0f / 18.0f * (8.0f * v[y][x] + 4.0f * v[y+1][x] + 2.0f * (v[y][x-1] + v[y][x+1]) + v[y+1][x-1] + v[y+1][x+1]);
  40. }
  41. template<typename Vertex>
  42. __forceinline Vertex computeLeftEdgeBezierControlPoint(const Vertex v[4][4], const size_t y, const size_t x) {
  43. return 1.0f / 18.0f * (8.0f * v[y][x] + 4.0f * v[y][x-1] + 2.0f * (v[y-1][x] + v[y+1][x]) + v[y-1][x-1] + v[y+1][x-1]);
  44. }
  45. template<typename Vertex>
  46. __forceinline Vertex computeRightEdgeBezierControlPoint(const Vertex v[4][4], const size_t y, const size_t x) {
  47. return 1.0f / 18.0f * (8.0f * v[y][x] + 4.0f * v[y][x+1] + 2.0f * (v[y-1][x] + v[y+1][x]) + v[y-1][x+1] + v[y+1][x+1]);
  48. }
  49. template<typename Vertex>
  50. __forceinline Vertex computeCornerBezierControlPoint(const Vertex v[4][4], const size_t y, const size_t x, const ssize_t delta_y, const ssize_t delta_x)
  51. {
  52. return 1.0f / 9.0f * (4.0f * v[y][x] + 2.0f * (v[y+delta_y][x] + v[y][x+delta_x]) + v[y+delta_y][x+delta_x]);
  53. }
  54. template<typename Vertex, typename Vertex_t>
  55. class __aligned(64) BezierPatchT
  56. {
  57. public:
  58. Vertex matrix[4][4];
  59. public:
  60. __forceinline BezierPatchT() {}
  61. __forceinline BezierPatchT (const HalfEdge* edge, const char* vertices, size_t stride);
  62. __forceinline BezierPatchT(const CatmullClarkPatchT<Vertex,Vertex_t>& patch);
  63. __forceinline BezierPatchT(const CatmullClarkPatchT<Vertex,Vertex_t>& patch,
  64. const BezierCurveT<Vertex>* border0,
  65. const BezierCurveT<Vertex>* border1,
  66. const BezierCurveT<Vertex>* border2,
  67. const BezierCurveT<Vertex>* border3);
  68. __forceinline BezierPatchT(const BSplinePatchT<Vertex,Vertex_t>& source)
  69. {
  70. /* compute inner bezier control points */
  71. matrix[0][0] = computeInnerBezierControlPoint(source.v,1,1);
  72. matrix[0][3] = computeInnerBezierControlPoint(source.v,1,2);
  73. matrix[3][3] = computeInnerBezierControlPoint(source.v,2,2);
  74. matrix[3][0] = computeInnerBezierControlPoint(source.v,2,1);
  75. /* compute top edge control points */
  76. matrix[0][1] = computeRightEdgeBezierControlPoint(source.v,1,1);
  77. matrix[0][2] = computeLeftEdgeBezierControlPoint(source.v,1,2);
  78. /* compute bottom edge control points */
  79. matrix[3][1] = computeRightEdgeBezierControlPoint(source.v,2,1);
  80. matrix[3][2] = computeLeftEdgeBezierControlPoint(source.v,2,2);
  81. /* compute left edge control points */
  82. matrix[1][0] = computeBottomEdgeBezierControlPoint(source.v,1,1);
  83. matrix[2][0] = computeTopEdgeBezierControlPoint(source.v,2,1);
  84. /* compute right edge control points */
  85. matrix[1][3] = computeBottomEdgeBezierControlPoint(source.v,1,2);
  86. matrix[2][3] = computeTopEdgeBezierControlPoint(source.v,2,2);
  87. /* compute corner control points */
  88. matrix[1][1] = computeCornerBezierControlPoint(source.v,1,1, 1, 1);
  89. matrix[1][2] = computeCornerBezierControlPoint(source.v,1,2, 1,-1);
  90. matrix[2][2] = computeCornerBezierControlPoint(source.v,2,2,-1,-1);
  91. matrix[2][1] = computeCornerBezierControlPoint(source.v,2,1,-1, 1);
  92. }
  93. static __forceinline Vertex_t bilinear(const Vec4f Bu, const Vertex matrix[4][4], const Vec4f Bv)
  94. {
  95. const Vertex_t M0 = madd(Bu.x,matrix[0][0],madd(Bu.y,matrix[0][1],madd(Bu.z,matrix[0][2],Bu.w * matrix[0][3])));
  96. const Vertex_t M1 = madd(Bu.x,matrix[1][0],madd(Bu.y,matrix[1][1],madd(Bu.z,matrix[1][2],Bu.w * matrix[1][3])));
  97. const Vertex_t M2 = madd(Bu.x,matrix[2][0],madd(Bu.y,matrix[2][1],madd(Bu.z,matrix[2][2],Bu.w * matrix[2][3])));
  98. const Vertex_t M3 = madd(Bu.x,matrix[3][0],madd(Bu.y,matrix[3][1],madd(Bu.z,matrix[3][2],Bu.w * matrix[3][3])));
  99. return madd(Bv.x,M0,madd(Bv.y,M1,madd(Bv.z,M2,Bv.w*M3)));
  100. }
  101. static __forceinline Vertex_t eval(const Vertex matrix[4][4], const float uu, const float vv)
  102. {
  103. const Vec4f Bu = BezierBasis::eval(uu);
  104. const Vec4f Bv = BezierBasis::eval(vv);
  105. return bilinear(Bu,matrix,Bv);
  106. }
  107. static __forceinline Vertex_t eval_du(const Vertex matrix[4][4], const float uu, const float vv)
  108. {
  109. const Vec4f Bu = BezierBasis::derivative(uu);
  110. const Vec4f Bv = BezierBasis::eval(vv);
  111. return bilinear(Bu,matrix,Bv);
  112. }
  113. static __forceinline Vertex_t eval_dv(const Vertex matrix[4][4], const float uu, const float vv)
  114. {
  115. const Vec4f Bu = BezierBasis::eval(uu);
  116. const Vec4f Bv = BezierBasis::derivative(vv);
  117. return bilinear(Bu,matrix,Bv);
  118. }
  119. static __forceinline Vertex_t eval_dudu(const Vertex matrix[4][4], const float uu, const float vv)
  120. {
  121. const Vec4f Bu = BezierBasis::derivative2(uu);
  122. const Vec4f Bv = BezierBasis::eval(vv);
  123. return bilinear(Bu,matrix,Bv);
  124. }
  125. static __forceinline Vertex_t eval_dvdv(const Vertex matrix[4][4], const float uu, const float vv)
  126. {
  127. const Vec4f Bu = BezierBasis::eval(uu);
  128. const Vec4f Bv = BezierBasis::derivative2(vv);
  129. return bilinear(Bu,matrix,Bv);
  130. }
  131. static __forceinline Vertex_t eval_dudv(const Vertex matrix[4][4], const float uu, const float vv)
  132. {
  133. const Vec4f Bu = BezierBasis::derivative(uu);
  134. const Vec4f Bv = BezierBasis::derivative(vv);
  135. return bilinear(Bu,matrix,Bv);
  136. }
  137. static __forceinline Vertex_t normal(const Vertex matrix[4][4], const float uu, const float vv)
  138. {
  139. const Vertex_t dPdu = eval_du(matrix,uu,vv);
  140. const Vertex_t dPdv = eval_dv(matrix,uu,vv);
  141. return cross(dPdu,dPdv);
  142. }
  143. __forceinline Vertex_t normal(const float uu, const float vv)
  144. {
  145. const Vertex_t dPdu = eval_du(matrix,uu,vv);
  146. const Vertex_t dPdv = eval_dv(matrix,uu,vv);
  147. return cross(dPdu,dPdv);
  148. }
  149. __forceinline Vertex_t eval(const float uu, const float vv) const {
  150. return eval(matrix,uu,vv);
  151. }
  152. __forceinline Vertex_t eval_du(const float uu, const float vv) const {
  153. return eval_du(matrix,uu,vv);
  154. }
  155. __forceinline Vertex_t eval_dv(const float uu, const float vv) const {
  156. return eval_dv(matrix,uu,vv);
  157. }
  158. __forceinline Vertex_t eval_dudu(const float uu, const float vv) const {
  159. return eval_dudu(matrix,uu,vv);
  160. }
  161. __forceinline Vertex_t eval_dvdv(const float uu, const float vv) const {
  162. return eval_dvdv(matrix,uu,vv);
  163. }
  164. __forceinline Vertex_t eval_dudv(const float uu, const float vv) const {
  165. return eval_dudv(matrix,uu,vv);
  166. }
  167. __forceinline void eval(const float u, const float v, Vertex* P, Vertex* dPdu, Vertex* dPdv, Vertex* ddPdudu, Vertex* ddPdvdv, Vertex* ddPdudv, const float dscale = 1.0f) const
  168. {
  169. if (P) {
  170. *P = eval(u,v);
  171. }
  172. if (dPdu) {
  173. assert(dPdu); *dPdu = eval_du(u,v)*dscale;
  174. assert(dPdv); *dPdv = eval_dv(u,v)*dscale;
  175. }
  176. if (ddPdudu) {
  177. assert(ddPdudu); *ddPdudu = eval_dudu(u,v)*sqr(dscale);
  178. assert(ddPdvdv); *ddPdvdv = eval_dvdv(u,v)*sqr(dscale);
  179. assert(ddPdudv); *ddPdudv = eval_dudv(u,v)*sqr(dscale);
  180. }
  181. }
  182. template<class vfloat>
  183. __forceinline vfloat eval(const size_t i, const vfloat& uu, const vfloat& vv, const Vec4<vfloat>& u_n, const Vec4<vfloat>& v_n) const
  184. {
  185. const vfloat curve0_x = v_n[0] * vfloat(matrix[0][0][i]) + v_n[1] * vfloat(matrix[1][0][i]) + v_n[2] * vfloat(matrix[2][0][i]) + v_n[3] * vfloat(matrix[3][0][i]);
  186. const vfloat curve1_x = v_n[0] * vfloat(matrix[0][1][i]) + v_n[1] * vfloat(matrix[1][1][i]) + v_n[2] * vfloat(matrix[2][1][i]) + v_n[3] * vfloat(matrix[3][1][i]);
  187. const vfloat curve2_x = v_n[0] * vfloat(matrix[0][2][i]) + v_n[1] * vfloat(matrix[1][2][i]) + v_n[2] * vfloat(matrix[2][2][i]) + v_n[3] * vfloat(matrix[3][2][i]);
  188. const vfloat curve3_x = v_n[0] * vfloat(matrix[0][3][i]) + v_n[1] * vfloat(matrix[1][3][i]) + v_n[2] * vfloat(matrix[2][3][i]) + v_n[3] * vfloat(matrix[3][3][i]);
  189. return u_n[0] * curve0_x + u_n[1] * curve1_x + u_n[2] * curve2_x + u_n[3] * curve3_x;
  190. }
  191. template<typename vbool, typename vfloat>
  192. __forceinline void eval(const vbool& valid, const vfloat& uu, const vfloat& vv,
  193. float* P, float* dPdu, float* dPdv, float* ddPdudu, float* ddPdvdv, float* ddPdudv,
  194. const float dscale, const size_t dstride, const size_t N) const
  195. {
  196. if (P) {
  197. const Vec4<vfloat> u_n = BezierBasis::eval(uu);
  198. const Vec4<vfloat> v_n = BezierBasis::eval(vv);
  199. for (size_t i=0; i<N; i++) vfloat::store(valid,P+i*dstride,eval(i,uu,vv,u_n,v_n));
  200. }
  201. if (dPdu)
  202. {
  203. {
  204. assert(dPdu);
  205. const Vec4<vfloat> u_n = BezierBasis::derivative(uu);
  206. const Vec4<vfloat> v_n = BezierBasis::eval(vv);
  207. for (size_t i=0; i<N; i++) vfloat::store(valid,dPdu+i*dstride,eval(i,uu,vv,u_n,v_n)*dscale);
  208. }
  209. {
  210. assert(dPdv);
  211. const Vec4<vfloat> u_n = BezierBasis::eval(uu);
  212. const Vec4<vfloat> v_n = BezierBasis::derivative(vv);
  213. for (size_t i=0; i<N; i++) vfloat::store(valid,dPdv+i*dstride,eval(i,uu,vv,u_n,v_n)*dscale);
  214. }
  215. }
  216. if (ddPdudu)
  217. {
  218. {
  219. assert(ddPdudu);
  220. const Vec4<vfloat> u_n = BezierBasis::derivative2(uu);
  221. const Vec4<vfloat> v_n = BezierBasis::eval(vv);
  222. for (size_t i=0; i<N; i++) vfloat::store(valid,ddPdudu+i*dstride,eval(i,uu,vv,u_n,v_n)*sqr(dscale));
  223. }
  224. {
  225. assert(ddPdvdv);
  226. const Vec4<vfloat> u_n = BezierBasis::eval(uu);
  227. const Vec4<vfloat> v_n = BezierBasis::derivative2(vv);
  228. for (size_t i=0; i<N; i++) vfloat::store(valid,ddPdvdv+i*dstride,eval(i,uu,vv,u_n,v_n)*sqr(dscale));
  229. }
  230. {
  231. assert(ddPdudv);
  232. const Vec4<vfloat> u_n = BezierBasis::derivative(uu);
  233. const Vec4<vfloat> v_n = BezierBasis::derivative(vv);
  234. for (size_t i=0; i<N; i++) vfloat::store(valid,ddPdudv+i*dstride,eval(i,uu,vv,u_n,v_n)*sqr(dscale));
  235. }
  236. }
  237. }
  238. template<typename T>
  239. static __forceinline Vec3<T> eval(const Vertex matrix[4][4], const T& uu, const T& vv)
  240. {
  241. const T one_minus_uu = 1.0f - uu;
  242. const T one_minus_vv = 1.0f - vv;
  243. const T B0_u = one_minus_uu * one_minus_uu * one_minus_uu;
  244. const T B0_v = one_minus_vv * one_minus_vv * one_minus_vv;
  245. const T B1_u = 3.0f * (one_minus_uu * uu * one_minus_uu);
  246. const T B1_v = 3.0f * (one_minus_vv * vv * one_minus_vv);
  247. const T B2_u = 3.0f * (uu * one_minus_uu * uu);
  248. const T B2_v = 3.0f * (vv * one_minus_vv * vv);
  249. const T B3_u = uu * uu * uu;
  250. const T B3_v = vv * vv * vv;
  251. const T x =
  252. madd(B0_v,madd(B0_u,matrix[0][0].x,madd(B1_u,matrix[0][1].x,madd(B2_u,matrix[0][2].x,B3_u*matrix[0][3].x))),
  253. madd(B1_v,madd(B0_u,matrix[1][0].x,madd(B1_u,matrix[1][1].x,madd(B2_u,matrix[1][2].x,B3_u*matrix[1][3].x))),
  254. madd(B2_v,madd(B0_u,matrix[2][0].x,madd(B1_u,matrix[2][1].x,madd(B2_u,matrix[2][2].x,B3_u*matrix[2][3].x))),
  255. B3_v*madd(B0_u,matrix[3][0].x,madd(B1_u,matrix[3][1].x,madd(B2_u,matrix[3][2].x,B3_u*matrix[3][3].x))))));
  256. const T y =
  257. madd(B0_v,madd(B0_u,matrix[0][0].y,madd(B1_u,matrix[0][1].y,madd(B2_u,matrix[0][2].y,B3_u*matrix[0][3].y))),
  258. madd(B1_v,madd(B0_u,matrix[1][0].y,madd(B1_u,matrix[1][1].y,madd(B2_u,matrix[1][2].y,B3_u*matrix[1][3].y))),
  259. madd(B2_v,madd(B0_u,matrix[2][0].y,madd(B1_u,matrix[2][1].y,madd(B2_u,matrix[2][2].y,B3_u*matrix[2][3].y))),
  260. B3_v*madd(B0_u,matrix[3][0].y,madd(B1_u,matrix[3][1].y,madd(B2_u,matrix[3][2].y,B3_u*matrix[3][3].y))))));
  261. const T z =
  262. madd(B0_v,madd(B0_u,matrix[0][0].z,madd(B1_u,matrix[0][1].z,madd(B2_u,matrix[0][2].z,B3_u*matrix[0][3].z))),
  263. madd(B1_v,madd(B0_u,matrix[1][0].z,madd(B1_u,matrix[1][1].z,madd(B2_u,matrix[1][2].z,B3_u*matrix[1][3].z))),
  264. madd(B2_v,madd(B0_u,matrix[2][0].z,madd(B1_u,matrix[2][1].z,madd(B2_u,matrix[2][2].z,B3_u*matrix[2][3].z))),
  265. B3_v*madd(B0_u,matrix[3][0].z,madd(B1_u,matrix[3][1].z,madd(B2_u,matrix[3][2].z,B3_u*matrix[3][3].z))))));
  266. return Vec3<T>(x,y,z);
  267. }
  268. template<typename vfloat>
  269. __forceinline Vec3<vfloat> eval(const vfloat& uu, const vfloat& vv) const {
  270. return eval(matrix,uu,vv);
  271. }
  272. template<class T>
  273. static __forceinline Vec3<T> normal(const Vertex matrix[4][4], const T& uu, const T& vv)
  274. {
  275. const Vec3<T> matrix_00 = Vec3<T>(matrix[0][0].x,matrix[0][0].y,matrix[0][0].z);
  276. const Vec3<T> matrix_01 = Vec3<T>(matrix[0][1].x,matrix[0][1].y,matrix[0][1].z);
  277. const Vec3<T> matrix_02 = Vec3<T>(matrix[0][2].x,matrix[0][2].y,matrix[0][2].z);
  278. const Vec3<T> matrix_03 = Vec3<T>(matrix[0][3].x,matrix[0][3].y,matrix[0][3].z);
  279. const Vec3<T> matrix_10 = Vec3<T>(matrix[1][0].x,matrix[1][0].y,matrix[1][0].z);
  280. const Vec3<T> matrix_11 = Vec3<T>(matrix[1][1].x,matrix[1][1].y,matrix[1][1].z);
  281. const Vec3<T> matrix_12 = Vec3<T>(matrix[1][2].x,matrix[1][2].y,matrix[1][2].z);
  282. const Vec3<T> matrix_13 = Vec3<T>(matrix[1][3].x,matrix[1][3].y,matrix[1][3].z);
  283. const Vec3<T> matrix_20 = Vec3<T>(matrix[2][0].x,matrix[2][0].y,matrix[2][0].z);
  284. const Vec3<T> matrix_21 = Vec3<T>(matrix[2][1].x,matrix[2][1].y,matrix[2][1].z);
  285. const Vec3<T> matrix_22 = Vec3<T>(matrix[2][2].x,matrix[2][2].y,matrix[2][2].z);
  286. const Vec3<T> matrix_23 = Vec3<T>(matrix[2][3].x,matrix[2][3].y,matrix[2][3].z);
  287. const Vec3<T> matrix_30 = Vec3<T>(matrix[3][0].x,matrix[3][0].y,matrix[3][0].z);
  288. const Vec3<T> matrix_31 = Vec3<T>(matrix[3][1].x,matrix[3][1].y,matrix[3][1].z);
  289. const Vec3<T> matrix_32 = Vec3<T>(matrix[3][2].x,matrix[3][2].y,matrix[3][2].z);
  290. const Vec3<T> matrix_33 = Vec3<T>(matrix[3][3].x,matrix[3][3].y,matrix[3][3].z);
  291. /* tangentU */
  292. const Vec3<T> col0 = deCasteljau(vv, matrix_00, matrix_10, matrix_20, matrix_30);
  293. const Vec3<T> col1 = deCasteljau(vv, matrix_01, matrix_11, matrix_21, matrix_31);
  294. const Vec3<T> col2 = deCasteljau(vv, matrix_02, matrix_12, matrix_22, matrix_32);
  295. const Vec3<T> col3 = deCasteljau(vv, matrix_03, matrix_13, matrix_23, matrix_33);
  296. const Vec3<T> tangentU = deCasteljau_tangent(uu, col0, col1, col2, col3);
  297. /* tangentV */
  298. const Vec3<T> row0 = deCasteljau(uu, matrix_00, matrix_01, matrix_02, matrix_03);
  299. const Vec3<T> row1 = deCasteljau(uu, matrix_10, matrix_11, matrix_12, matrix_13);
  300. const Vec3<T> row2 = deCasteljau(uu, matrix_20, matrix_21, matrix_22, matrix_23);
  301. const Vec3<T> row3 = deCasteljau(uu, matrix_30, matrix_31, matrix_32, matrix_33);
  302. const Vec3<T> tangentV = deCasteljau_tangent(vv, row0, row1, row2, row3);
  303. /* normal = tangentU x tangentV */
  304. const Vec3<T> n = cross(tangentU,tangentV);
  305. return n;
  306. }
  307. template<typename vfloat>
  308. __forceinline Vec3<vfloat> normal(const vfloat& uu, const vfloat& vv) const {
  309. return normal(matrix,uu,vv);
  310. }
  311. };
  312. typedef BezierPatchT<Vec3fa,Vec3fa_t> BezierPatch3fa;
  313. }