gregory_patch.h 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  1. // Copyright 2009-2021 Intel Corporation
  2. // SPDX-License-Identifier: Apache-2.0
  3. #pragma once
  4. #include "catmullclark_patch.h"
  5. #include "bezier_patch.h"
  6. #include "bezier_curve.h"
  7. #include "catmullclark_coefficients.h"
  8. namespace embree
  9. {
  10. template<typename Vertex, typename Vertex_t = Vertex>
  11. class __aligned(64) GregoryPatchT
  12. {
  13. typedef CatmullClarkPatchT<Vertex,Vertex_t> CatmullClarkPatch;
  14. typedef GeneralCatmullClarkPatchT<Vertex,Vertex_t> GeneralCatmullClarkPatch;
  15. typedef CatmullClark1RingT<Vertex,Vertex_t> CatmullClark1Ring;
  16. typedef BezierCurveT<Vertex> BezierCurve;
  17. public:
  18. Vertex v[4][4];
  19. Vertex f[2][2];
  20. __forceinline GregoryPatchT() {}
  21. __forceinline GregoryPatchT(const CatmullClarkPatch& patch) {
  22. init(patch);
  23. }
  24. __forceinline GregoryPatchT(const CatmullClarkPatch& patch,
  25. const BezierCurve* border0, const BezierCurve* border1, const BezierCurve* border2, const BezierCurve* border3)
  26. {
  27. init_crackfix(patch,border0,border1,border2,border3);
  28. }
  29. __forceinline GregoryPatchT (const HalfEdge* edge, const char* vertices, size_t stride) {
  30. init(CatmullClarkPatch(edge,vertices,stride));
  31. }
  32. __forceinline Vertex& p0() { return v[0][0]; }
  33. __forceinline Vertex& p1() { return v[0][3]; }
  34. __forceinline Vertex& p2() { return v[3][3]; }
  35. __forceinline Vertex& p3() { return v[3][0]; }
  36. __forceinline Vertex& e0_p() { return v[0][1]; }
  37. __forceinline Vertex& e0_m() { return v[1][0]; }
  38. __forceinline Vertex& e1_p() { return v[1][3]; }
  39. __forceinline Vertex& e1_m() { return v[0][2]; }
  40. __forceinline Vertex& e2_p() { return v[3][2]; }
  41. __forceinline Vertex& e2_m() { return v[2][3]; }
  42. __forceinline Vertex& e3_p() { return v[2][0]; }
  43. __forceinline Vertex& e3_m() { return v[3][1]; }
  44. __forceinline Vertex& f0_p() { return v[1][1]; }
  45. __forceinline Vertex& f1_p() { return v[1][2]; }
  46. __forceinline Vertex& f2_p() { return v[2][2]; }
  47. __forceinline Vertex& f3_p() { return v[2][1]; }
  48. __forceinline Vertex& f0_m() { return f[0][0]; }
  49. __forceinline Vertex& f1_m() { return f[0][1]; }
  50. __forceinline Vertex& f2_m() { return f[1][1]; }
  51. __forceinline Vertex& f3_m() { return f[1][0]; }
  52. __forceinline const Vertex& p0() const { return v[0][0]; }
  53. __forceinline const Vertex& p1() const { return v[0][3]; }
  54. __forceinline const Vertex& p2() const { return v[3][3]; }
  55. __forceinline const Vertex& p3() const { return v[3][0]; }
  56. __forceinline const Vertex& e0_p() const { return v[0][1]; }
  57. __forceinline const Vertex& e0_m() const { return v[1][0]; }
  58. __forceinline const Vertex& e1_p() const { return v[1][3]; }
  59. __forceinline const Vertex& e1_m() const { return v[0][2]; }
  60. __forceinline const Vertex& e2_p() const { return v[3][2]; }
  61. __forceinline const Vertex& e2_m() const { return v[2][3]; }
  62. __forceinline const Vertex& e3_p() const { return v[2][0]; }
  63. __forceinline const Vertex& e3_m() const { return v[3][1]; }
  64. __forceinline const Vertex& f0_p() const { return v[1][1]; }
  65. __forceinline const Vertex& f1_p() const { return v[1][2]; }
  66. __forceinline const Vertex& f2_p() const { return v[2][2]; }
  67. __forceinline const Vertex& f3_p() const { return v[2][1]; }
  68. __forceinline const Vertex& f0_m() const { return f[0][0]; }
  69. __forceinline const Vertex& f1_m() const { return f[0][1]; }
  70. __forceinline const Vertex& f2_m() const { return f[1][1]; }
  71. __forceinline const Vertex& f3_m() const { return f[1][0]; }
  72. __forceinline Vertex initCornerVertex(const CatmullClarkPatch& irreg_patch, const size_t index) {
  73. return irreg_patch.ring[index].getLimitVertex();
  74. }
  75. __forceinline Vertex initPositiveEdgeVertex(const CatmullClarkPatch& irreg_patch, const size_t index, const Vertex& p_vtx) {
  76. return madd(1.0f/3.0f,irreg_patch.ring[index].getLimitTangent(),p_vtx);
  77. }
  78. __forceinline Vertex initNegativeEdgeVertex(const CatmullClarkPatch& irreg_patch, const size_t index, const Vertex& p_vtx) {
  79. return madd(1.0f/3.0f,irreg_patch.ring[index].getSecondLimitTangent(),p_vtx);
  80. }
  81. __forceinline Vertex initPositiveEdgeVertex2(const CatmullClarkPatch& irreg_patch, const size_t index, const Vertex& p_vtx)
  82. {
  83. CatmullClark1Ring3fa r0,r1,r2;
  84. irreg_patch.ring[index].subdivide(r0);
  85. r0.subdivide(r1);
  86. r1.subdivide(r2);
  87. return madd(8.0f/3.0f,r2.getLimitTangent(),p_vtx);
  88. }
  89. __forceinline Vertex initNegativeEdgeVertex2(const CatmullClarkPatch& irreg_patch, const size_t index, const Vertex& p_vtx)
  90. {
  91. CatmullClark1Ring3fa r0,r1,r2;
  92. irreg_patch.ring[index].subdivide(r0);
  93. r0.subdivide(r1);
  94. r1.subdivide(r2);
  95. return madd(8.0f/3.0f,r2.getSecondLimitTangent(),p_vtx);
  96. }
  97. void initFaceVertex(const CatmullClarkPatch& irreg_patch,
  98. const size_t index,
  99. const Vertex& p_vtx,
  100. const Vertex& e0_p_vtx,
  101. const Vertex& e1_m_vtx,
  102. const unsigned int face_valence_p1,
  103. const Vertex& e0_m_vtx,
  104. const Vertex& e3_p_vtx,
  105. const unsigned int face_valence_p3,
  106. Vertex& f_p_vtx,
  107. Vertex& f_m_vtx)
  108. {
  109. const unsigned int face_valence = irreg_patch.ring[index].face_valence;
  110. const unsigned int edge_valence = irreg_patch.ring[index].edge_valence;
  111. const unsigned int border_index = irreg_patch.ring[index].border_index;
  112. const Vertex& vtx = irreg_patch.ring[index].vtx;
  113. const Vertex e_i = irreg_patch.ring[index].getEdgeCenter(0);
  114. const Vertex c_i_m_1 = irreg_patch.ring[index].getQuadCenter(0);
  115. const Vertex e_i_m_1 = irreg_patch.ring[index].getEdgeCenter(1);
  116. Vertex c_i, e_i_p_1;
  117. const bool hasHardEdge0 =
  118. std::isinf(irreg_patch.ring[index].vertex_crease_weight) &&
  119. std::isinf(irreg_patch.ring[index].crease_weight[0]);
  120. if (unlikely((border_index == edge_valence-2) || hasHardEdge0))
  121. {
  122. /* mirror quad center and edge mid-point */
  123. c_i = madd(2.0f, e_i - c_i_m_1, c_i_m_1);
  124. e_i_p_1 = madd(2.0f, vtx - e_i_m_1, e_i_m_1);
  125. }
  126. else
  127. {
  128. c_i = irreg_patch.ring[index].getQuadCenter( face_valence-1 );
  129. e_i_p_1 = irreg_patch.ring[index].getEdgeCenter( face_valence-1 );
  130. }
  131. Vertex c_i_m_2, e_i_m_2;
  132. const bool hasHardEdge1 =
  133. std::isinf(irreg_patch.ring[index].vertex_crease_weight) &&
  134. std::isinf(irreg_patch.ring[index].crease_weight[1]);
  135. if (unlikely(border_index == 2 || hasHardEdge1))
  136. {
  137. /* mirror quad center and edge mid-point */
  138. c_i_m_2 = madd(2.0f, e_i_m_1 - c_i_m_1, c_i_m_1);
  139. e_i_m_2 = madd(2.0f, vtx - e_i, + e_i);
  140. }
  141. else
  142. {
  143. c_i_m_2 = irreg_patch.ring[index].getQuadCenter( 1 );
  144. e_i_m_2 = irreg_patch.ring[index].getEdgeCenter( 2 );
  145. }
  146. const float d = 3.0f;
  147. //const float c = cosf(2.0f*M_PI/(float)face_valence);
  148. //const float c_e_p = cosf(2.0f*M_PI/(float)face_valence_p1);
  149. //const float c_e_m = cosf(2.0f*M_PI/(float)face_valence_p3);
  150. const float c = CatmullClarkPrecomputedCoefficients::table.cos_2PI_div_n(face_valence);
  151. const float c_e_p = CatmullClarkPrecomputedCoefficients::table.cos_2PI_div_n(face_valence_p1);
  152. const float c_e_m = CatmullClarkPrecomputedCoefficients::table.cos_2PI_div_n(face_valence_p3);
  153. const Vertex r_e_p = 1.0f/3.0f * (e_i_m_1 - e_i_p_1) + 2.0f/3.0f * (c_i_m_1 - c_i);
  154. const Vertex r_e_m = 1.0f/3.0f * (e_i - e_i_m_2) + 2.0f/3.0f * (c_i_m_1 - c_i_m_2);
  155. f_p_vtx = 1.0f / d * (c_e_p * p_vtx + (d - 2.0f*c - c_e_p) * e0_p_vtx + 2.0f*c* e1_m_vtx + r_e_p);
  156. f_m_vtx = 1.0f / d * (c_e_m * p_vtx + (d - 2.0f*c - c_e_m) * e0_m_vtx + 2.0f*c* e3_p_vtx + r_e_m);
  157. }
  158. __noinline void init(const CatmullClarkPatch& patch)
  159. {
  160. assert( patch.ring[0].hasValidPositions() );
  161. assert( patch.ring[1].hasValidPositions() );
  162. assert( patch.ring[2].hasValidPositions() );
  163. assert( patch.ring[3].hasValidPositions() );
  164. p0() = initCornerVertex(patch,0);
  165. p1() = initCornerVertex(patch,1);
  166. p2() = initCornerVertex(patch,2);
  167. p3() = initCornerVertex(patch,3);
  168. e0_p() = initPositiveEdgeVertex(patch,0, p0());
  169. e1_p() = initPositiveEdgeVertex(patch,1, p1());
  170. e2_p() = initPositiveEdgeVertex(patch,2, p2());
  171. e3_p() = initPositiveEdgeVertex(patch,3, p3());
  172. e0_m() = initNegativeEdgeVertex(patch,0, p0());
  173. e1_m() = initNegativeEdgeVertex(patch,1, p1());
  174. e2_m() = initNegativeEdgeVertex(patch,2, p2());
  175. e3_m() = initNegativeEdgeVertex(patch,3, p3());
  176. const unsigned int face_valence_p0 = patch.ring[0].face_valence;
  177. const unsigned int face_valence_p1 = patch.ring[1].face_valence;
  178. const unsigned int face_valence_p2 = patch.ring[2].face_valence;
  179. const unsigned int face_valence_p3 = patch.ring[3].face_valence;
  180. initFaceVertex(patch,0,p0(),e0_p(),e1_m(),face_valence_p1,e0_m(),e3_p(),face_valence_p3,f0_p(),f0_m() );
  181. initFaceVertex(patch,1,p1(),e1_p(),e2_m(),face_valence_p2,e1_m(),e0_p(),face_valence_p0,f1_p(),f1_m() );
  182. initFaceVertex(patch,2,p2(),e2_p(),e3_m(),face_valence_p3,e2_m(),e1_p(),face_valence_p1,f2_p(),f2_m() );
  183. initFaceVertex(patch,3,p3(),e3_p(),e0_m(),face_valence_p0,e3_m(),e2_p(),face_valence_p3,f3_p(),f3_m() );
  184. }
  185. __noinline void init_crackfix(const CatmullClarkPatch& patch,
  186. const BezierCurve* border0,
  187. const BezierCurve* border1,
  188. const BezierCurve* border2,
  189. const BezierCurve* border3)
  190. {
  191. assert( patch.ring[0].hasValidPositions() );
  192. assert( patch.ring[1].hasValidPositions() );
  193. assert( patch.ring[2].hasValidPositions() );
  194. assert( patch.ring[3].hasValidPositions() );
  195. p0() = initCornerVertex(patch,0);
  196. p1() = initCornerVertex(patch,1);
  197. p2() = initCornerVertex(patch,2);
  198. p3() = initCornerVertex(patch,3);
  199. e0_p() = initPositiveEdgeVertex(patch,0, p0());
  200. e1_p() = initPositiveEdgeVertex(patch,1, p1());
  201. e2_p() = initPositiveEdgeVertex(patch,2, p2());
  202. e3_p() = initPositiveEdgeVertex(patch,3, p3());
  203. e0_m() = initNegativeEdgeVertex(patch,0, p0());
  204. e1_m() = initNegativeEdgeVertex(patch,1, p1());
  205. e2_m() = initNegativeEdgeVertex(patch,2, p2());
  206. e3_m() = initNegativeEdgeVertex(patch,3, p3());
  207. if (unlikely(border0 != nullptr))
  208. {
  209. p0() = border0->v0;
  210. e0_p() = border0->v1;
  211. e1_m() = border0->v2;
  212. p1() = border0->v3;
  213. }
  214. if (unlikely(border1 != nullptr))
  215. {
  216. p1() = border1->v0;
  217. e1_p() = border1->v1;
  218. e2_m() = border1->v2;
  219. p2() = border1->v3;
  220. }
  221. if (unlikely(border2 != nullptr))
  222. {
  223. p2() = border2->v0;
  224. e2_p() = border2->v1;
  225. e3_m() = border2->v2;
  226. p3() = border2->v3;
  227. }
  228. if (unlikely(border3 != nullptr))
  229. {
  230. p3() = border3->v0;
  231. e3_p() = border3->v1;
  232. e0_m() = border3->v2;
  233. p0() = border3->v3;
  234. }
  235. const unsigned int face_valence_p0 = patch.ring[0].face_valence;
  236. const unsigned int face_valence_p1 = patch.ring[1].face_valence;
  237. const unsigned int face_valence_p2 = patch.ring[2].face_valence;
  238. const unsigned int face_valence_p3 = patch.ring[3].face_valence;
  239. initFaceVertex(patch,0,p0(),e0_p(),e1_m(),face_valence_p1,e0_m(),e3_p(),face_valence_p3,f0_p(),f0_m() );
  240. initFaceVertex(patch,1,p1(),e1_p(),e2_m(),face_valence_p2,e1_m(),e0_p(),face_valence_p0,f1_p(),f1_m() );
  241. initFaceVertex(patch,2,p2(),e2_p(),e3_m(),face_valence_p3,e2_m(),e1_p(),face_valence_p1,f2_p(),f2_m() );
  242. initFaceVertex(patch,3,p3(),e3_p(),e0_m(),face_valence_p0,e3_m(),e2_p(),face_valence_p3,f3_p(),f3_m() );
  243. }
  244. void computeGregoryPatchFacePoints(const unsigned int face_valence,
  245. const Vertex& r_e_p,
  246. const Vertex& r_e_m,
  247. const Vertex& p_vtx,
  248. const Vertex& e0_p_vtx,
  249. const Vertex& e1_m_vtx,
  250. const unsigned int face_valence_p1,
  251. const Vertex& e0_m_vtx,
  252. const Vertex& e3_p_vtx,
  253. const unsigned int face_valence_p3,
  254. Vertex& f_p_vtx,
  255. Vertex& f_m_vtx,
  256. const float d = 3.0f)
  257. {
  258. //const float c = cosf(2.0*M_PI/(float)face_valence);
  259. //const float c_e_p = cosf(2.0*M_PI/(float)face_valence_p1);
  260. //const float c_e_m = cosf(2.0*M_PI/(float)face_valence_p3);
  261. const float c = CatmullClarkPrecomputedCoefficients::table.cos_2PI_div_n(face_valence);
  262. const float c_e_p = CatmullClarkPrecomputedCoefficients::table.cos_2PI_div_n(face_valence_p1);
  263. const float c_e_m = CatmullClarkPrecomputedCoefficients::table.cos_2PI_div_n(face_valence_p3);
  264. f_p_vtx = 1.0f / d * (c_e_p * p_vtx + (d - 2.0f*c - c_e_p) * e0_p_vtx + 2.0f*c* e1_m_vtx + r_e_p);
  265. f_m_vtx = 1.0f / d * (c_e_m * p_vtx + (d - 2.0f*c - c_e_m) * e0_m_vtx + 2.0f*c* e3_p_vtx + r_e_m);
  266. f_p_vtx = 1.0f / d * (c_e_p * p_vtx + (d - 2.0f*c - c_e_p) * e0_p_vtx + 2.0f*c* e1_m_vtx + r_e_p);
  267. f_m_vtx = 1.0f / d * (c_e_m * p_vtx + (d - 2.0f*c - c_e_m) * e0_m_vtx + 2.0f*c* e3_p_vtx + r_e_m);
  268. }
  269. __noinline void init(const GeneralCatmullClarkPatch& patch)
  270. {
  271. assert(patch.size() == 4);
  272. #if 0
  273. CatmullClarkPatch qpatch; patch.init(qpatch);
  274. init(qpatch);
  275. #else
  276. const float face_valence_p0 = patch.ring[0].face_valence;
  277. const float face_valence_p1 = patch.ring[1].face_valence;
  278. const float face_valence_p2 = patch.ring[2].face_valence;
  279. const float face_valence_p3 = patch.ring[3].face_valence;
  280. Vertex p0_r_p, p0_r_m;
  281. patch.ring[0].computeGregoryPatchEdgePoints( p0(), e0_p(), e0_m(), p0_r_p, p0_r_m );
  282. Vertex p1_r_p, p1_r_m;
  283. patch.ring[1].computeGregoryPatchEdgePoints( p1(), e1_p(), e1_m(), p1_r_p, p1_r_m );
  284. Vertex p2_r_p, p2_r_m;
  285. patch.ring[2].computeGregoryPatchEdgePoints( p2(), e2_p(), e2_m(), p2_r_p, p2_r_m );
  286. Vertex p3_r_p, p3_r_m;
  287. patch.ring[3].computeGregoryPatchEdgePoints( p3(), e3_p(), e3_m(), p3_r_p, p3_r_m );
  288. computeGregoryPatchFacePoints(face_valence_p0, p0_r_p, p0_r_m, p0(), e0_p(), e1_m(), face_valence_p1, e0_m(), e3_p(), face_valence_p3, f0_p(), f0_m() );
  289. computeGregoryPatchFacePoints(face_valence_p1, p1_r_p, p1_r_m, p1(), e1_p(), e2_m(), face_valence_p2, e1_m(), e0_p(), face_valence_p0, f1_p(), f1_m() );
  290. computeGregoryPatchFacePoints(face_valence_p2, p2_r_p, p2_r_m, p2(), e2_p(), e3_m(), face_valence_p3, e2_m(), e1_p(), face_valence_p1, f2_p(), f2_m() );
  291. computeGregoryPatchFacePoints(face_valence_p3, p3_r_p, p3_r_m, p3(), e3_p(), e0_m(), face_valence_p0, e3_m(), e2_p(), face_valence_p3, f3_p(), f3_m() );
  292. #endif
  293. }
  294. __forceinline void convert_to_bezier()
  295. {
  296. f0_p() = (f0_p() + f0_m()) * 0.5f;
  297. f1_p() = (f1_p() + f1_m()) * 0.5f;
  298. f2_p() = (f2_p() + f2_m()) * 0.5f;
  299. f3_p() = (f3_p() + f3_m()) * 0.5f;
  300. f0_m() = Vertex( zero );
  301. f1_m() = Vertex( zero );
  302. f2_m() = Vertex( zero );
  303. f3_m() = Vertex( zero );
  304. }
  305. static __forceinline void computeInnerVertices(const Vertex matrix[4][4], const Vertex f_m[2][2], const float uu, const float vv,
  306. Vertex_t& matrix_11, Vertex_t& matrix_12, Vertex_t& matrix_22, Vertex_t& matrix_21)
  307. {
  308. if (unlikely(uu == 0.0f || uu == 1.0f || vv == 0.0f || vv == 1.0f))
  309. {
  310. matrix_11 = matrix[1][1];
  311. matrix_12 = matrix[1][2];
  312. matrix_22 = matrix[2][2];
  313. matrix_21 = matrix[2][1];
  314. }
  315. else
  316. {
  317. const Vertex_t f0_p = matrix[1][1];
  318. const Vertex_t f1_p = matrix[1][2];
  319. const Vertex_t f2_p = matrix[2][2];
  320. const Vertex_t f3_p = matrix[2][1];
  321. const Vertex_t f0_m = f_m[0][0];
  322. const Vertex_t f1_m = f_m[0][1];
  323. const Vertex_t f2_m = f_m[1][1];
  324. const Vertex_t f3_m = f_m[1][0];
  325. matrix_11 = ( uu * f0_p + vv * f0_m)*rcp(uu+vv);
  326. matrix_12 = ((1.0f-uu) * f1_m + vv * f1_p)*rcp(1.0f-uu+vv);
  327. matrix_22 = ((1.0f-uu) * f2_p + (1.0f-vv) * f2_m)*rcp(2.0f-uu-vv);
  328. matrix_21 = ( uu * f3_m + (1.0f-vv) * f3_p)*rcp(1.0f+uu-vv);
  329. }
  330. }
  331. template<typename vfloat>
  332. static __forceinline void computeInnerVertices(const Vertex v[4][4], const Vertex f[2][2],
  333. size_t i, const vfloat& uu, const vfloat& vv, vfloat& matrix_11, vfloat& matrix_12, vfloat& matrix_22, vfloat& matrix_21)
  334. {
  335. const auto m_border = (uu == 0.0f) | (uu == 1.0f) | (vv == 0.0f) | (vv == 1.0f);
  336. const vfloat f0_p = v[1][1][i];
  337. const vfloat f1_p = v[1][2][i];
  338. const vfloat f2_p = v[2][2][i];
  339. const vfloat f3_p = v[2][1][i];
  340. const vfloat f0_m = f[0][0][i];
  341. const vfloat f1_m = f[0][1][i];
  342. const vfloat f2_m = f[1][1][i];
  343. const vfloat f3_m = f[1][0][i];
  344. const vfloat one_minus_uu = vfloat(1.0f) - uu;
  345. const vfloat one_minus_vv = vfloat(1.0f) - vv;
  346. const vfloat f0_i = ( uu * f0_p + vv * f0_m) * rcp(uu+vv);
  347. const vfloat f1_i = (one_minus_uu * f1_m + vv * f1_p) * rcp(one_minus_uu+vv);
  348. const vfloat f2_i = (one_minus_uu * f2_p + one_minus_vv * f2_m) * rcp(one_minus_uu+one_minus_vv);
  349. const vfloat f3_i = ( uu * f3_m + one_minus_vv * f3_p) * rcp(uu+one_minus_vv);
  350. matrix_11 = select(m_border,f0_p,f0_i);
  351. matrix_12 = select(m_border,f1_p,f1_i);
  352. matrix_22 = select(m_border,f2_p,f2_i);
  353. matrix_21 = select(m_border,f3_p,f3_i);
  354. }
  355. static __forceinline Vertex eval(const Vertex matrix[4][4], const Vertex f[2][2], const float& uu, const float& vv)
  356. {
  357. Vertex_t v_11, v_12, v_22, v_21;
  358. computeInnerVertices(matrix,f,uu,vv,v_11, v_12, v_22, v_21);
  359. const Vec4<float> Bu = BezierBasis::eval(uu);
  360. const Vec4<float> Bv = BezierBasis::eval(vv);
  361. return madd(Bv.x,madd(Bu.x,matrix[0][0],madd(Bu.y,matrix[0][1],madd(Bu.z,matrix[0][2],Bu.w * matrix[0][3]))),
  362. madd(Bv.y,madd(Bu.x,matrix[1][0],madd(Bu.y,v_11 ,madd(Bu.z,v_12 ,Bu.w * matrix[1][3]))),
  363. madd(Bv.z,madd(Bu.x,matrix[2][0],madd(Bu.y,v_21 ,madd(Bu.z,v_22 ,Bu.w * matrix[2][3]))),
  364. Bv.w*madd(Bu.x,matrix[3][0],madd(Bu.y,matrix[3][1],madd(Bu.z,matrix[3][2],Bu.w * matrix[3][3]))))));
  365. }
  366. static __forceinline Vertex eval_du(const Vertex matrix[4][4], const Vertex f[2][2], const float uu, const float vv) // approximative derivative
  367. {
  368. Vertex_t v_11, v_12, v_22, v_21;
  369. computeInnerVertices(matrix,f,uu,vv,v_11, v_12, v_22, v_21);
  370. const Vec4<float> Bu = BezierBasis::derivative(uu);
  371. const Vec4<float> Bv = BezierBasis::eval(vv);
  372. return madd(Bv.x,madd(Bu.x,matrix[0][0],madd(Bu.y,matrix[0][1],madd(Bu.z,matrix[0][2],Bu.w * matrix[0][3]))),
  373. madd(Bv.y,madd(Bu.x,matrix[1][0],madd(Bu.y,v_11 ,madd(Bu.z,v_12 ,Bu.w * matrix[1][3]))),
  374. madd(Bv.z,madd(Bu.x,matrix[2][0],madd(Bu.y,v_21 ,madd(Bu.z,v_22 ,Bu.w * matrix[2][3]))),
  375. Bv.w*madd(Bu.x,matrix[3][0],madd(Bu.y,matrix[3][1],madd(Bu.z,matrix[3][2],Bu.w * matrix[3][3]))))));
  376. }
  377. static __forceinline Vertex eval_dv(const Vertex matrix[4][4], const Vertex f[2][2], const float uu, const float vv) // approximative derivative
  378. {
  379. Vertex_t v_11, v_12, v_22, v_21;
  380. computeInnerVertices(matrix,f,uu,vv,v_11, v_12, v_22, v_21);
  381. const Vec4<float> Bu = BezierBasis::eval(uu);
  382. const Vec4<float> Bv = BezierBasis::derivative(vv);
  383. return madd(Bv.x,madd(Bu.x,matrix[0][0],madd(Bu.y,matrix[0][1],madd(Bu.z,matrix[0][2],Bu.w * matrix[0][3]))),
  384. madd(Bv.y,madd(Bu.x,matrix[1][0],madd(Bu.y,v_11 ,madd(Bu.z,v_12 ,Bu.w * matrix[1][3]))),
  385. madd(Bv.z,madd(Bu.x,matrix[2][0],madd(Bu.y,v_21 ,madd(Bu.z,v_22 ,Bu.w * matrix[2][3]))),
  386. Bv.w*madd(Bu.x,matrix[3][0],madd(Bu.y,matrix[3][1],madd(Bu.z,matrix[3][2],Bu.w * matrix[3][3]))))));
  387. }
  388. static __forceinline Vertex eval_dudu(const Vertex matrix[4][4], const Vertex f[2][2], const float uu, const float vv) // approximative derivative
  389. {
  390. Vertex_t v_11, v_12, v_22, v_21;
  391. computeInnerVertices(matrix,f,uu,vv,v_11, v_12, v_22, v_21);
  392. const Vec4<float> Bu = BezierBasis::derivative2(uu);
  393. const Vec4<float> Bv = BezierBasis::eval(vv);
  394. return madd(Bv.x,madd(Bu.x,matrix[0][0],madd(Bu.y,matrix[0][1],madd(Bu.z,matrix[0][2],Bu.w * matrix[0][3]))),
  395. madd(Bv.y,madd(Bu.x,matrix[1][0],madd(Bu.y,v_11 ,madd(Bu.z,v_12 ,Bu.w * matrix[1][3]))),
  396. madd(Bv.z,madd(Bu.x,matrix[2][0],madd(Bu.y,v_21 ,madd(Bu.z,v_22 ,Bu.w * matrix[2][3]))),
  397. Bv.w*madd(Bu.x,matrix[3][0],madd(Bu.y,matrix[3][1],madd(Bu.z,matrix[3][2],Bu.w * matrix[3][3]))))));
  398. }
  399. static __forceinline Vertex eval_dvdv(const Vertex matrix[4][4], const Vertex f[2][2], const float uu, const float vv) // approximative derivative
  400. {
  401. Vertex_t v_11, v_12, v_22, v_21;
  402. computeInnerVertices(matrix,f,uu,vv,v_11, v_12, v_22, v_21);
  403. const Vec4<float> Bu = BezierBasis::eval(uu);
  404. const Vec4<float> Bv = BezierBasis::derivative2(vv);
  405. return madd(Bv.x,madd(Bu.x,matrix[0][0],madd(Bu.y,matrix[0][1],madd(Bu.z,matrix[0][2],Bu.w * matrix[0][3]))),
  406. madd(Bv.y,madd(Bu.x,matrix[1][0],madd(Bu.y,v_11 ,madd(Bu.z,v_12 ,Bu.w * matrix[1][3]))),
  407. madd(Bv.z,madd(Bu.x,matrix[2][0],madd(Bu.y,v_21 ,madd(Bu.z,v_22 ,Bu.w * matrix[2][3]))),
  408. Bv.w*madd(Bu.x,matrix[3][0],madd(Bu.y,matrix[3][1],madd(Bu.z,matrix[3][2],Bu.w * matrix[3][3]))))));
  409. }
  410. static __forceinline Vertex eval_dudv(const Vertex matrix[4][4], const Vertex f[2][2], const float uu, const float vv) // approximative derivative
  411. {
  412. Vertex_t v_11, v_12, v_22, v_21;
  413. computeInnerVertices(matrix,f,uu,vv,v_11, v_12, v_22, v_21);
  414. const Vec4<float> Bu = BezierBasis::derivative(uu);
  415. const Vec4<float> Bv = BezierBasis::derivative(vv);
  416. return madd(Bv.x,madd(Bu.x,matrix[0][0],madd(Bu.y,matrix[0][1],madd(Bu.z,matrix[0][2],Bu.w * matrix[0][3]))),
  417. madd(Bv.y,madd(Bu.x,matrix[1][0],madd(Bu.y,v_11 ,madd(Bu.z,v_12 ,Bu.w * matrix[1][3]))),
  418. madd(Bv.z,madd(Bu.x,matrix[2][0],madd(Bu.y,v_21 ,madd(Bu.z,v_22 ,Bu.w * matrix[2][3]))),
  419. Bv.w*madd(Bu.x,matrix[3][0],madd(Bu.y,matrix[3][1],madd(Bu.z,matrix[3][2],Bu.w * matrix[3][3]))))));
  420. }
  421. __forceinline Vertex eval(const float uu, const float vv) const {
  422. return eval(v,f,uu,vv);
  423. }
  424. __forceinline Vertex eval_du( const float uu, const float vv) const {
  425. return eval_du(v,f,uu,vv);
  426. }
  427. __forceinline Vertex eval_dv( const float uu, const float vv) const {
  428. return eval_dv(v,f,uu,vv);
  429. }
  430. __forceinline Vertex eval_dudu( const float uu, const float vv) const {
  431. return eval_dudu(v,f,uu,vv);
  432. }
  433. __forceinline Vertex eval_dvdv( const float uu, const float vv) const {
  434. return eval_dvdv(v,f,uu,vv);
  435. }
  436. __forceinline Vertex eval_dudv( const float uu, const float vv) const {
  437. return eval_dudv(v,f,uu,vv);
  438. }
  439. static __forceinline Vertex normal(const Vertex matrix[4][4], const Vertex f_m[2][2], const float uu, const float vv) // FIXME: why not using basis functions
  440. {
  441. /* interpolate inner vertices */
  442. Vertex_t matrix_11, matrix_12, matrix_22, matrix_21;
  443. computeInnerVertices(matrix,f_m,uu,vv,matrix_11, matrix_12, matrix_22, matrix_21);
  444. /* tangentU */
  445. const Vertex_t col0 = deCasteljau(vv, (Vertex_t)matrix[0][0], (Vertex_t)matrix[1][0], (Vertex_t)matrix[2][0], (Vertex_t)matrix[3][0]);
  446. const Vertex_t col1 = deCasteljau(vv, (Vertex_t)matrix[0][1], (Vertex_t)matrix_11 , (Vertex_t)matrix_21 , (Vertex_t)matrix[3][1]);
  447. const Vertex_t col2 = deCasteljau(vv, (Vertex_t)matrix[0][2], (Vertex_t)matrix_12 , (Vertex_t)matrix_22 , (Vertex_t)matrix[3][2]);
  448. const Vertex_t col3 = deCasteljau(vv, (Vertex_t)matrix[0][3], (Vertex_t)matrix[1][3], (Vertex_t)matrix[2][3], (Vertex_t)matrix[3][3]);
  449. const Vertex_t tangentU = deCasteljau_tangent(uu, col0, col1, col2, col3);
  450. /* tangentV */
  451. const Vertex_t row0 = deCasteljau(uu, (Vertex_t)matrix[0][0], (Vertex_t)matrix[0][1], (Vertex_t)matrix[0][2], (Vertex_t)matrix[0][3]);
  452. const Vertex_t row1 = deCasteljau(uu, (Vertex_t)matrix[1][0], (Vertex_t)matrix_11 , (Vertex_t)matrix_12 , (Vertex_t)matrix[1][3]);
  453. const Vertex_t row2 = deCasteljau(uu, (Vertex_t)matrix[2][0], (Vertex_t)matrix_21 , (Vertex_t)matrix_22 , (Vertex_t)matrix[2][3]);
  454. const Vertex_t row3 = deCasteljau(uu, (Vertex_t)matrix[3][0], (Vertex_t)matrix[3][1], (Vertex_t)matrix[3][2], (Vertex_t)matrix[3][3]);
  455. const Vertex_t tangentV = deCasteljau_tangent(vv, row0, row1, row2, row3);
  456. /* normal = tangentU x tangentV */
  457. const Vertex_t n = cross(tangentU,tangentV);
  458. return n;
  459. }
  460. __forceinline Vertex normal( const float uu, const float vv) const {
  461. return normal(v,f,uu,vv);
  462. }
  463. __forceinline void eval(const float u, const float v,
  464. Vertex* P, Vertex* dPdu, Vertex* dPdv,
  465. Vertex* ddPdudu, Vertex* ddPdvdv, Vertex* ddPdudv,
  466. const float dscale = 1.0f) const
  467. {
  468. if (P) {
  469. *P = eval(u,v);
  470. }
  471. if (dPdu) {
  472. assert(dPdu); *dPdu = eval_du(u,v)*dscale;
  473. assert(dPdv); *dPdv = eval_dv(u,v)*dscale;
  474. }
  475. if (ddPdudu) {
  476. assert(ddPdudu); *ddPdudu = eval_dudu(u,v)*sqr(dscale);
  477. assert(ddPdvdv); *ddPdvdv = eval_dvdv(u,v)*sqr(dscale);
  478. assert(ddPdudv); *ddPdudv = eval_dudv(u,v)*sqr(dscale);
  479. }
  480. }
  481. template<class vfloat>
  482. static __forceinline vfloat eval(const Vertex v[4][4], const Vertex f[2][2],
  483. const size_t i, const vfloat& uu, const vfloat& vv, const Vec4<vfloat>& u_n, const Vec4<vfloat>& v_n,
  484. vfloat& matrix_11, vfloat& matrix_12, vfloat& matrix_22, vfloat& matrix_21)
  485. {
  486. const vfloat curve0_x = madd(v_n[0],vfloat(v[0][0][i]),madd(v_n[1],vfloat(v[1][0][i]),madd(v_n[2],vfloat(v[2][0][i]),v_n[3] * vfloat(v[3][0][i]))));
  487. const vfloat curve1_x = madd(v_n[0],vfloat(v[0][1][i]),madd(v_n[1],vfloat(matrix_11 ),madd(v_n[2],vfloat(matrix_21 ),v_n[3] * vfloat(v[3][1][i]))));
  488. const vfloat curve2_x = madd(v_n[0],vfloat(v[0][2][i]),madd(v_n[1],vfloat(matrix_12 ),madd(v_n[2],vfloat(matrix_22 ),v_n[3] * vfloat(v[3][2][i]))));
  489. const vfloat curve3_x = madd(v_n[0],vfloat(v[0][3][i]),madd(v_n[1],vfloat(v[1][3][i]),madd(v_n[2],vfloat(v[2][3][i]),v_n[3] * vfloat(v[3][3][i]))));
  490. return madd(u_n[0],curve0_x,madd(u_n[1],curve1_x,madd(u_n[2],curve2_x,u_n[3] * curve3_x)));
  491. }
  492. template<typename vbool, typename vfloat>
  493. static __forceinline void eval(const Vertex v[4][4], const Vertex f[2][2],
  494. const vbool& valid, const vfloat& uu, const vfloat& vv,
  495. float* P, float* dPdu, float* dPdv, float* ddPdudu, float* ddPdvdv, float* ddPdudv,
  496. const float dscale, const size_t dstride, const size_t N)
  497. {
  498. if (P) {
  499. const Vec4<vfloat> u_n = BezierBasis::eval(uu);
  500. const Vec4<vfloat> v_n = BezierBasis::eval(vv);
  501. for (size_t i=0; i<N; i++) {
  502. vfloat matrix_11, matrix_12, matrix_22, matrix_21;
  503. computeInnerVertices(v,f,i,uu,vv,matrix_11,matrix_12,matrix_22,matrix_21); // FIXME: calculated multiple times
  504. vfloat::store(valid,P+i*dstride,eval(v,f,i,uu,vv,u_n,v_n,matrix_11,matrix_12,matrix_22,matrix_21));
  505. }
  506. }
  507. if (dPdu)
  508. {
  509. {
  510. assert(dPdu);
  511. const Vec4<vfloat> u_n = BezierBasis::derivative(uu);
  512. const Vec4<vfloat> v_n = BezierBasis::eval(vv);
  513. for (size_t i=0; i<N; i++) {
  514. vfloat matrix_11, matrix_12, matrix_22, matrix_21;
  515. computeInnerVertices(v,f,i,uu,vv,matrix_11,matrix_12,matrix_22,matrix_21); // FIXME: calculated multiple times
  516. vfloat::store(valid,dPdu+i*dstride,eval(v,f,i,uu,vv,u_n,v_n,matrix_11,matrix_12,matrix_22,matrix_21)*dscale);
  517. }
  518. }
  519. {
  520. assert(dPdv);
  521. const Vec4<vfloat> u_n = BezierBasis::eval(uu);
  522. const Vec4<vfloat> v_n = BezierBasis::derivative(vv);
  523. for (size_t i=0; i<N; i++) {
  524. vfloat matrix_11, matrix_12, matrix_22, matrix_21;
  525. computeInnerVertices(v,f,i,uu,vv,matrix_11,matrix_12,matrix_22,matrix_21); // FIXME: calculated multiple times
  526. vfloat::store(valid,dPdv+i*dstride,eval(v,f,i,uu,vv,u_n,v_n,matrix_11,matrix_12,matrix_22,matrix_21)*dscale);
  527. }
  528. }
  529. }
  530. if (ddPdudu)
  531. {
  532. {
  533. assert(ddPdudu);
  534. const Vec4<vfloat> u_n = BezierBasis::derivative2(uu);
  535. const Vec4<vfloat> v_n = BezierBasis::eval(vv);
  536. for (size_t i=0; i<N; i++) {
  537. vfloat matrix_11, matrix_12, matrix_22, matrix_21;
  538. computeInnerVertices(v,f,i,uu,vv,matrix_11,matrix_12,matrix_22,matrix_21); // FIXME: calculated multiple times
  539. vfloat::store(valid,ddPdudu+i*dstride,eval(v,f,i,uu,vv,u_n,v_n,matrix_11,matrix_12,matrix_22,matrix_21)*sqr(dscale));
  540. }
  541. }
  542. {
  543. assert(ddPdvdv);
  544. const Vec4<vfloat> u_n = BezierBasis::eval(uu);
  545. const Vec4<vfloat> v_n = BezierBasis::derivative2(vv);
  546. for (size_t i=0; i<N; i++) {
  547. vfloat matrix_11, matrix_12, matrix_22, matrix_21;
  548. computeInnerVertices(v,f,i,uu,vv,matrix_11,matrix_12,matrix_22,matrix_21); // FIXME: calculated multiple times
  549. vfloat::store(valid,ddPdvdv+i*dstride,eval(v,f,i,uu,vv,u_n,v_n,matrix_11,matrix_12,matrix_22,matrix_21)*sqr(dscale));
  550. }
  551. }
  552. {
  553. assert(ddPdudv);
  554. const Vec4<vfloat> u_n = BezierBasis::derivative(uu);
  555. const Vec4<vfloat> v_n = BezierBasis::derivative(vv);
  556. for (size_t i=0; i<N; i++) {
  557. vfloat matrix_11, matrix_12, matrix_22, matrix_21;
  558. computeInnerVertices(v,f,i,uu,vv,matrix_11,matrix_12,matrix_22,matrix_21); // FIXME: calculated multiple times
  559. vfloat::store(valid,ddPdudv+i*dstride,eval(v,f,i,uu,vv,u_n,v_n,matrix_11,matrix_12,matrix_22,matrix_21)*sqr(dscale));
  560. }
  561. }
  562. }
  563. }
  564. template<typename vbool, typename vfloat>
  565. __forceinline void eval(const vbool& valid, const vfloat& uu, const vfloat& vv,
  566. float* P, float* dPdu, float* dPdv, float* ddPdudu, float* ddPdvdv, float* ddPdudv,
  567. const float dscale, const size_t dstride, const size_t N) const {
  568. eval(v,f,valid,uu,vv,P,dPdu,dPdv,ddPdudu,ddPdvdv,ddPdudv,dscale,dstride,N);
  569. }
  570. template<class T>
  571. static __forceinline Vec3<T> eval_t(const Vertex matrix[4][4], const Vec3<T> f[2][2], const T& uu, const T& vv)
  572. {
  573. typedef typename T::Bool M;
  574. const M m_border = (uu == 0.0f) | (uu == 1.0f) | (vv == 0.0f) | (vv == 1.0f);
  575. const Vec3<T> f0_p = Vec3<T>(matrix[1][1].x,matrix[1][1].y,matrix[1][1].z);
  576. const Vec3<T> f1_p = Vec3<T>(matrix[1][2].x,matrix[1][2].y,matrix[1][2].z);
  577. const Vec3<T> f2_p = Vec3<T>(matrix[2][2].x,matrix[2][2].y,matrix[2][2].z);
  578. const Vec3<T> f3_p = Vec3<T>(matrix[2][1].x,matrix[2][1].y,matrix[2][1].z);
  579. const Vec3<T> f0_m = f[0][0];
  580. const Vec3<T> f1_m = f[0][1];
  581. const Vec3<T> f2_m = f[1][1];
  582. const Vec3<T> f3_m = f[1][0];
  583. const T one_minus_uu = T(1.0f) - uu;
  584. const T one_minus_vv = T(1.0f) - vv;
  585. const Vec3<T> f0_i = ( uu * f0_p + vv * f0_m) * rcp(uu+vv);
  586. const Vec3<T> f1_i = (one_minus_uu * f1_m + vv * f1_p) * rcp(one_minus_uu+vv);
  587. const Vec3<T> f2_i = (one_minus_uu * f2_p + one_minus_vv * f2_m) * rcp(one_minus_uu+one_minus_vv);
  588. const Vec3<T> f3_i = ( uu * f3_m + one_minus_vv * f3_p) * rcp(uu+one_minus_vv);
  589. const Vec3<T> F0( select(m_border,f0_p.x,f0_i.x), select(m_border,f0_p.y,f0_i.y), select(m_border,f0_p.z,f0_i.z) );
  590. const Vec3<T> F1( select(m_border,f1_p.x,f1_i.x), select(m_border,f1_p.y,f1_i.y), select(m_border,f1_p.z,f1_i.z) );
  591. const Vec3<T> F2( select(m_border,f2_p.x,f2_i.x), select(m_border,f2_p.y,f2_i.y), select(m_border,f2_p.z,f2_i.z) );
  592. const Vec3<T> F3( select(m_border,f3_p.x,f3_i.x), select(m_border,f3_p.y,f3_i.y), select(m_border,f3_p.z,f3_i.z) );
  593. const T B0_u = one_minus_uu * one_minus_uu * one_minus_uu;
  594. const T B0_v = one_minus_vv * one_minus_vv * one_minus_vv;
  595. const T B1_u = 3.0f * (one_minus_uu * uu * one_minus_uu);
  596. const T B1_v = 3.0f * (one_minus_vv * vv * one_minus_vv);
  597. const T B2_u = 3.0f * (uu * one_minus_uu * uu);
  598. const T B2_v = 3.0f * (vv * one_minus_vv * vv);
  599. const T B3_u = uu * uu * uu;
  600. const T B3_v = vv * vv * vv;
  601. const T x = 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))),
  602. madd(B1_v,madd(B0_u,matrix[1][0].x,madd(B1_u,F0.x ,madd(B2_u,F1.x ,B3_u * matrix[1][3].x))),
  603. madd(B2_v,madd(B0_u,matrix[2][0].x,madd(B1_u,F3.x ,madd(B2_u,F2.x ,B3_u * matrix[2][3].x))),
  604. 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))))));
  605. const T y = 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))),
  606. madd(B1_v,madd(B0_u,matrix[1][0].y,madd(B1_u,F0.y ,madd(B2_u,F1.y ,B3_u * matrix[1][3].y))),
  607. madd(B2_v,madd(B0_u,matrix[2][0].y,madd(B1_u,F3.y ,madd(B2_u,F2.y ,B3_u * matrix[2][3].y))),
  608. 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))))));
  609. const T z = 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))),
  610. madd(B1_v,madd(B0_u,matrix[1][0].z,madd(B1_u,F0.z ,madd(B2_u,F1.z ,B3_u * matrix[1][3].z))),
  611. madd(B2_v,madd(B0_u,matrix[2][0].z,madd(B1_u,F3.z ,madd(B2_u,F2.z ,B3_u * matrix[2][3].z))),
  612. 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))))));
  613. return Vec3<T>(x,y,z);
  614. }
  615. template<class T>
  616. __forceinline Vec3<T> eval(const T& uu, const T& vv) const
  617. {
  618. Vec3<T> ff[2][2];
  619. ff[0][0] = Vec3<T>(f[0][0]);
  620. ff[0][1] = Vec3<T>(f[0][1]);
  621. ff[1][1] = Vec3<T>(f[1][1]);
  622. ff[1][0] = Vec3<T>(f[1][0]);
  623. return eval_t(v,ff,uu,vv);
  624. }
  625. template<class T>
  626. static __forceinline Vec3<T> normal_t(const Vertex matrix[4][4], const Vec3<T> f[2][2], const T& uu, const T& vv)
  627. {
  628. typedef typename T::Bool M;
  629. const Vec3<T> f0_p = Vec3<T>(matrix[1][1].x,matrix[1][1].y,matrix[1][1].z);
  630. const Vec3<T> f1_p = Vec3<T>(matrix[1][2].x,matrix[1][2].y,matrix[1][2].z);
  631. const Vec3<T> f2_p = Vec3<T>(matrix[2][2].x,matrix[2][2].y,matrix[2][2].z);
  632. const Vec3<T> f3_p = Vec3<T>(matrix[2][1].x,matrix[2][1].y,matrix[2][1].z);
  633. const Vec3<T> f0_m = f[0][0];
  634. const Vec3<T> f1_m = f[0][1];
  635. const Vec3<T> f2_m = f[1][1];
  636. const Vec3<T> f3_m = f[1][0];
  637. const T one_minus_uu = T(1.0f) - uu;
  638. const T one_minus_vv = T(1.0f) - vv;
  639. const Vec3<T> f0_i = ( uu * f0_p + vv * f0_m) * rcp(uu+vv);
  640. const Vec3<T> f1_i = (one_minus_uu * f1_m + vv * f1_p) * rcp(one_minus_uu+vv);
  641. const Vec3<T> f2_i = (one_minus_uu * f2_p + one_minus_vv * f2_m) * rcp(one_minus_uu+one_minus_vv);
  642. const Vec3<T> f3_i = ( uu * f3_m + one_minus_vv * f3_p) * rcp(uu+one_minus_vv);
  643. #if 1
  644. const M m_corner0 = (uu == 0.0f) & (vv == 0.0f);
  645. const M m_corner1 = (uu == 1.0f) & (vv == 0.0f);
  646. const M m_corner2 = (uu == 1.0f) & (vv == 1.0f);
  647. const M m_corner3 = (uu == 0.0f) & (vv == 1.0f);
  648. const Vec3<T> matrix_11( select(m_corner0,f0_p.x,f0_i.x), select(m_corner0,f0_p.y,f0_i.y), select(m_corner0,f0_p.z,f0_i.z) );
  649. const Vec3<T> matrix_12( select(m_corner1,f1_p.x,f1_i.x), select(m_corner1,f1_p.y,f1_i.y), select(m_corner1,f1_p.z,f1_i.z) );
  650. const Vec3<T> matrix_22( select(m_corner2,f2_p.x,f2_i.x), select(m_corner2,f2_p.y,f2_i.y), select(m_corner2,f2_p.z,f2_i.z) );
  651. const Vec3<T> matrix_21( select(m_corner3,f3_p.x,f3_i.x), select(m_corner3,f3_p.y,f3_i.y), select(m_corner3,f3_p.z,f3_i.z) );
  652. #else
  653. const M m_border = (uu == 0.0f) | (uu == 1.0f) | (vv == 0.0f) | (vv == 1.0f);
  654. const Vec3<T> matrix_11( select(m_border,f0_p.x,f0_i.x), select(m_border,f0_p.y,f0_i.y), select(m_border,f0_p.z,f0_i.z) );
  655. const Vec3<T> matrix_12( select(m_border,f1_p.x,f1_i.x), select(m_border,f1_p.y,f1_i.y), select(m_border,f1_p.z,f1_i.z) );
  656. const Vec3<T> matrix_22( select(m_border,f2_p.x,f2_i.x), select(m_border,f2_p.y,f2_i.y), select(m_border,f2_p.z,f2_i.z) );
  657. const Vec3<T> matrix_21( select(m_border,f3_p.x,f3_i.x), select(m_border,f3_p.y,f3_i.y), select(m_border,f3_p.z,f3_i.z) );
  658. #endif
  659. const Vec3<T> matrix_00 = Vec3<T>(matrix[0][0].x,matrix[0][0].y,matrix[0][0].z);
  660. const Vec3<T> matrix_10 = Vec3<T>(matrix[1][0].x,matrix[1][0].y,matrix[1][0].z);
  661. const Vec3<T> matrix_20 = Vec3<T>(matrix[2][0].x,matrix[2][0].y,matrix[2][0].z);
  662. const Vec3<T> matrix_30 = Vec3<T>(matrix[3][0].x,matrix[3][0].y,matrix[3][0].z);
  663. const Vec3<T> matrix_01 = Vec3<T>(matrix[0][1].x,matrix[0][1].y,matrix[0][1].z);
  664. const Vec3<T> matrix_02 = Vec3<T>(matrix[0][2].x,matrix[0][2].y,matrix[0][2].z);
  665. const Vec3<T> matrix_03 = Vec3<T>(matrix[0][3].x,matrix[0][3].y,matrix[0][3].z);
  666. const Vec3<T> matrix_31 = Vec3<T>(matrix[3][1].x,matrix[3][1].y,matrix[3][1].z);
  667. const Vec3<T> matrix_32 = Vec3<T>(matrix[3][2].x,matrix[3][2].y,matrix[3][2].z);
  668. const Vec3<T> matrix_33 = Vec3<T>(matrix[3][3].x,matrix[3][3].y,matrix[3][3].z);
  669. const Vec3<T> matrix_13 = Vec3<T>(matrix[1][3].x,matrix[1][3].y,matrix[1][3].z);
  670. const Vec3<T> matrix_23 = Vec3<T>(matrix[2][3].x,matrix[2][3].y,matrix[2][3].z);
  671. /* tangentU */
  672. const Vec3<T> col0 = deCasteljau(vv, matrix_00, matrix_10, matrix_20, matrix_30);
  673. const Vec3<T> col1 = deCasteljau(vv, matrix_01, matrix_11, matrix_21, matrix_31);
  674. const Vec3<T> col2 = deCasteljau(vv, matrix_02, matrix_12, matrix_22, matrix_32);
  675. const Vec3<T> col3 = deCasteljau(vv, matrix_03, matrix_13, matrix_23, matrix_33);
  676. const Vec3<T> tangentU = deCasteljau_tangent(uu, col0, col1, col2, col3);
  677. /* tangentV */
  678. const Vec3<T> row0 = deCasteljau(uu, matrix_00, matrix_01, matrix_02, matrix_03);
  679. const Vec3<T> row1 = deCasteljau(uu, matrix_10, matrix_11, matrix_12, matrix_13);
  680. const Vec3<T> row2 = deCasteljau(uu, matrix_20, matrix_21, matrix_22, matrix_23);
  681. const Vec3<T> row3 = deCasteljau(uu, matrix_30, matrix_31, matrix_32, matrix_33);
  682. const Vec3<T> tangentV = deCasteljau_tangent(vv, row0, row1, row2, row3);
  683. /* normal = tangentU x tangentV */
  684. const Vec3<T> n = cross(tangentU,tangentV);
  685. return n;
  686. }
  687. template<class T>
  688. __forceinline Vec3<T> normal(const T& uu, const T& vv) const
  689. {
  690. Vec3<T> ff[2][2];
  691. ff[0][0] = Vec3<T>(f[0][0]);
  692. ff[0][1] = Vec3<T>(f[0][1]);
  693. ff[1][1] = Vec3<T>(f[1][1]);
  694. ff[1][0] = Vec3<T>(f[1][0]);
  695. return normal_t(v,ff,uu,vv);
  696. }
  697. __forceinline BBox<Vertex> bounds() const
  698. {
  699. const Vertex *const cv = &v[0][0];
  700. BBox<Vertex> bounds (cv[0]);
  701. for (size_t i=1; i<16; i++)
  702. bounds.extend( cv[i] );
  703. bounds.extend(f[0][0]);
  704. bounds.extend(f[1][0]);
  705. bounds.extend(f[1][1]);
  706. bounds.extend(f[1][1]);
  707. return bounds;
  708. }
  709. friend embree_ostream operator<<(embree_ostream o, const GregoryPatchT& p)
  710. {
  711. for (size_t y=0; y<4; y++)
  712. for (size_t x=0; x<4; x++)
  713. o << "v[" << y << "][" << x << "] " << p.v[y][x] << embree_endl;
  714. for (size_t y=0; y<2; y++)
  715. for (size_t x=0; x<2; x++)
  716. o << "f[" << y << "][" << x << "] " << p.f[y][x] << embree_endl;
  717. return o;
  718. }
  719. };
  720. typedef GregoryPatchT<Vec3fa,Vec3fa_t> GregoryPatch3fa;
  721. template<typename Vertex, typename Vertex_t>
  722. __forceinline BezierPatchT<Vertex,Vertex_t>::BezierPatchT (const HalfEdge* edge, const char* vertices, size_t stride)
  723. {
  724. CatmullClarkPatchT<Vertex,Vertex_t> patch(edge,vertices,stride);
  725. GregoryPatchT<Vertex,Vertex_t> gpatch(patch);
  726. gpatch.convert_to_bezier();
  727. for (size_t y=0; y<4; y++)
  728. for (size_t x=0; x<4; x++)
  729. matrix[y][x] = (Vertex_t)gpatch.v[y][x];
  730. }
  731. template<typename Vertex, typename Vertex_t>
  732. __forceinline BezierPatchT<Vertex,Vertex_t>::BezierPatchT(const CatmullClarkPatchT<Vertex,Vertex_t>& patch)
  733. {
  734. GregoryPatchT<Vertex,Vertex_t> gpatch(patch);
  735. gpatch.convert_to_bezier();
  736. for (size_t y=0; y<4; y++)
  737. for (size_t x=0; x<4; x++)
  738. matrix[y][x] = (Vertex_t)gpatch.v[y][x];
  739. }
  740. template<typename Vertex, typename Vertex_t>
  741. __forceinline BezierPatchT<Vertex,Vertex_t>::BezierPatchT(const CatmullClarkPatchT<Vertex,Vertex_t>& patch,
  742. const BezierCurveT<Vertex>* border0,
  743. const BezierCurveT<Vertex>* border1,
  744. const BezierCurveT<Vertex>* border2,
  745. const BezierCurveT<Vertex>* border3)
  746. {
  747. GregoryPatchT<Vertex,Vertex_t> gpatch(patch,border0,border1,border2,border3);
  748. gpatch.convert_to_bezier();
  749. for (size_t y=0; y<4; y++)
  750. for (size_t x=0; x<4; x++)
  751. matrix[y][x] = (Vertex_t)gpatch.v[y][x];
  752. }
  753. }