feature_adaptive_eval.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // Copyright 2009-2021 Intel Corporation
  2. // SPDX-License-Identifier: Apache-2.0
  3. #pragma once
  4. #include "patch.h"
  5. namespace embree
  6. {
  7. namespace isa
  8. {
  9. template<typename Vertex, typename Vertex_t = Vertex>
  10. struct FeatureAdaptiveEval
  11. {
  12. public:
  13. typedef PatchT<Vertex,Vertex_t> Patch;
  14. typedef typename Patch::Ref Ref;
  15. typedef GeneralCatmullClarkPatchT<Vertex,Vertex_t> GeneralCatmullClarkPatch;
  16. typedef CatmullClark1RingT<Vertex,Vertex_t> CatmullClarkRing;
  17. typedef CatmullClarkPatchT<Vertex,Vertex_t> CatmullClarkPatch;
  18. typedef BSplinePatchT<Vertex,Vertex_t> BSplinePatch;
  19. typedef BezierPatchT<Vertex,Vertex_t> BezierPatch;
  20. typedef GregoryPatchT<Vertex,Vertex_t> GregoryPatch;
  21. typedef BilinearPatchT<Vertex,Vertex_t> BilinearPatch;
  22. typedef BezierCurveT<Vertex> BezierCurve;
  23. public:
  24. FeatureAdaptiveEval (const HalfEdge* edge, const char* vertices, size_t stride, const float u, const float v,
  25. Vertex* P, Vertex* dPdu, Vertex* dPdv, Vertex* ddPdudu, Vertex* ddPdvdv, Vertex* ddPdudv)
  26. : P(P), dPdu(dPdu), dPdv(dPdv), ddPdudu(ddPdudu), ddPdvdv(ddPdvdv), ddPdudv(ddPdudv)
  27. {
  28. switch (edge->patch_type) {
  29. case HalfEdge::BILINEAR_PATCH: BilinearPatch(edge,vertices,stride).eval(u,v,P,dPdu,dPdv,ddPdudu,ddPdvdv,ddPdudv,1.0f); break;
  30. case HalfEdge::REGULAR_QUAD_PATCH: RegularPatchT(edge,vertices,stride).eval(u,v,P,dPdu,dPdv,ddPdudu,ddPdvdv,ddPdudv,1.0f); break;
  31. #if PATCH_USE_GREGORY == 2
  32. case HalfEdge::IRREGULAR_QUAD_PATCH: GregoryPatch(edge,vertices,stride).eval(u,v,P,dPdu,dPdv,ddPdudu,ddPdvdv,ddPdudv,1.0f); break;
  33. #endif
  34. default: {
  35. GeneralCatmullClarkPatch patch(edge,vertices,stride);
  36. eval(patch,Vec2f(u,v),0);
  37. break;
  38. }
  39. }
  40. }
  41. FeatureAdaptiveEval (CatmullClarkPatch& patch, const float u, const float v, float dscale, size_t depth,
  42. Vertex* P, Vertex* dPdu, Vertex* dPdv, Vertex* ddPdudu, Vertex* ddPdvdv, Vertex* ddPdudv)
  43. : P(P), dPdu(dPdu), dPdv(dPdv), ddPdudu(ddPdudu), ddPdvdv(ddPdvdv), ddPdudv(ddPdudv)
  44. {
  45. eval(patch,Vec2f(u,v),dscale,depth);
  46. }
  47. void eval_general_quad(const GeneralCatmullClarkPatch& patch, array_t<CatmullClarkPatch,GeneralCatmullClarkPatch::SIZE>& patches, const Vec2f& uv, size_t depth)
  48. {
  49. float u = uv.x, v = uv.y;
  50. if (v < 0.5f) {
  51. if (u < 0.5f) {
  52. #if PATCH_USE_GREGORY == 2
  53. BezierCurve borders[2]; patch.getLimitBorder(borders,0);
  54. BezierCurve border0l,border0r; borders[0].subdivide(border0l,border0r);
  55. BezierCurve border2l,border2r; borders[1].subdivide(border2l,border2r);
  56. eval(patches[0],Vec2f(2.0f*u,2.0f*v),2.0f,depth+1, &border0l, nullptr, nullptr, &border2r);
  57. #else
  58. eval(patches[0],Vec2f(2.0f*u,2.0f*v),2.0f,depth+1);
  59. #endif
  60. if (dPdu && dPdv) {
  61. const Vertex dpdx = *dPdu, dpdy = *dPdv;
  62. *dPdu = dpdx; *dPdv = dpdy;
  63. }
  64. }
  65. else {
  66. #if PATCH_USE_GREGORY == 2
  67. BezierCurve borders[2]; patch.getLimitBorder(borders,1);
  68. BezierCurve border0l,border0r; borders[0].subdivide(border0l,border0r);
  69. BezierCurve border2l,border2r; borders[1].subdivide(border2l,border2r);
  70. eval(patches[1],Vec2f(2.0f*v,2.0f-2.0f*u),2.0f,depth+1, &border0l, nullptr, nullptr, &border2r);
  71. #else
  72. eval(patches[1],Vec2f(2.0f*v,2.0f-2.0f*u),2.0f,depth+1);
  73. #endif
  74. if (dPdu && dPdv) {
  75. const Vertex dpdx = *dPdu, dpdy = *dPdv;
  76. *dPdu = -dpdy; *dPdv = dpdx;
  77. }
  78. }
  79. } else {
  80. if (u > 0.5f) {
  81. #if PATCH_USE_GREGORY == 2
  82. BezierCurve borders[2]; patch.getLimitBorder(borders,2);
  83. BezierCurve border0l,border0r; borders[0].subdivide(border0l,border0r);
  84. BezierCurve border2l,border2r; borders[1].subdivide(border2l,border2r);
  85. eval(patches[2],Vec2f(2.0f-2.0f*u,2.0f-2.0f*v),2.0f,depth+1, &border0l, nullptr, nullptr, &border2r);
  86. #else
  87. eval(patches[2],Vec2f(2.0f-2.0f*u,2.0f-2.0f*v),2.0f,depth+1);
  88. #endif
  89. if (dPdu && dPdv) {
  90. const Vertex dpdx = *dPdu, dpdy = *dPdv;
  91. *dPdu = -dpdx; *dPdv = -dpdy;
  92. }
  93. }
  94. else {
  95. #if PATCH_USE_GREGORY == 2
  96. BezierCurve borders[2]; patch.getLimitBorder(borders,3);
  97. BezierCurve border0l,border0r; borders[0].subdivide(border0l,border0r);
  98. BezierCurve border2l,border2r; borders[1].subdivide(border2l,border2r);
  99. eval(patches[3],Vec2f(2.0f-2.0f*v,2.0f*u),2.0f,depth+1, &border0l, nullptr, nullptr, &border2r);
  100. #else
  101. eval(patches[3],Vec2f(2.0f-2.0f*v,2.0f*u),2.0f,depth+1);
  102. #endif
  103. if (dPdu && dPdv) {
  104. const Vertex dpdx = *dPdu, dpdy = *dPdv;
  105. *dPdu = dpdy; *dPdv = -dpdx;
  106. }
  107. }
  108. }
  109. }
  110. __forceinline bool final(const CatmullClarkPatch& patch, const typename CatmullClarkRing::Type type, size_t depth)
  111. {
  112. const int max_eval_depth = (type & CatmullClarkRing::TYPE_CREASES) ? PATCH_MAX_EVAL_DEPTH_CREASE : PATCH_MAX_EVAL_DEPTH_IRREGULAR;
  113. //#if PATCH_MIN_RESOLUTION
  114. // return patch.isFinalResolution(PATCH_MIN_RESOLUTION) || depth>=(size_t)max_eval_depth;
  115. //#else
  116. return depth>=(size_t)max_eval_depth;
  117. //#endif
  118. }
  119. void eval(CatmullClarkPatch& patch, Vec2f uv, float dscale, size_t depth,
  120. BezierCurve* border0 = nullptr, BezierCurve* border1 = nullptr, BezierCurve* border2 = nullptr, BezierCurve* border3 = nullptr)
  121. {
  122. while (true)
  123. {
  124. typename CatmullClarkPatch::Type ty = patch.type();
  125. if (unlikely(final(patch,ty,depth)))
  126. {
  127. if (ty & CatmullClarkRing::TYPE_REGULAR) {
  128. RegularPatch(patch,border0,border1,border2,border3).eval(uv.x,uv.y,P,dPdu,dPdv,ddPdudu,ddPdvdv,ddPdudv,dscale);
  129. PATCH_DEBUG_SUBDIVISION(234423,c,c,-1);
  130. return;
  131. } else {
  132. IrregularFillPatch(patch,border0,border1,border2,border3).eval(uv.x,uv.y,P,dPdu,dPdv,ddPdudu,ddPdvdv,ddPdudv,dscale);
  133. PATCH_DEBUG_SUBDIVISION(34534,c,-1,c);
  134. return;
  135. }
  136. }
  137. else if (ty & CatmullClarkRing::TYPE_REGULAR_CREASES) {
  138. assert(depth > 0);
  139. RegularPatch(patch,border0,border1,border2,border3).eval(uv.x,uv.y,P,dPdu,dPdv,ddPdudu,ddPdvdv,ddPdudv,dscale);
  140. PATCH_DEBUG_SUBDIVISION(43524,c,c,-1);
  141. return;
  142. }
  143. #if PATCH_USE_GREGORY == 2
  144. else if (ty & CatmullClarkRing::TYPE_GREGORY_CREASES) {
  145. assert(depth > 0);
  146. GregoryPatch(patch,border0,border1,border2,border3).eval(uv.x,uv.y,P,dPdu,dPdv,ddPdudu,ddPdvdv,ddPdudv,dscale);
  147. PATCH_DEBUG_SUBDIVISION(23498,c,-1,c);
  148. return;
  149. }
  150. #endif
  151. else
  152. {
  153. array_t<CatmullClarkPatch,4> patches;
  154. patch.subdivide(patches); // FIXME: only have to generate one of the patches
  155. const float u = uv.x, v = uv.y;
  156. if (v < 0.5f) {
  157. if (u < 0.5f) { patch = patches[0]; uv = Vec2f(2.0f*u,2.0f*v); dscale *= 2.0f; }
  158. else { patch = patches[1]; uv = Vec2f(2.0f*u-1.0f,2.0f*v); dscale *= 2.0f; }
  159. } else {
  160. if (u > 0.5f) { patch = patches[2]; uv = Vec2f(2.0f*u-1.0f,2.0f*v-1.0f); dscale *= 2.0f; }
  161. else { patch = patches[3]; uv = Vec2f(2.0f*u,2.0f*v-1.0f); dscale *= 2.0f; }
  162. }
  163. depth++;
  164. }
  165. }
  166. }
  167. void eval(const GeneralCatmullClarkPatch& patch, const Vec2f& uv, const size_t depth)
  168. {
  169. /* convert into standard quad patch if possible */
  170. if (likely(patch.isQuadPatch()))
  171. {
  172. CatmullClarkPatch qpatch; patch.init(qpatch);
  173. return eval(qpatch,uv,1.0f,depth);
  174. }
  175. /* subdivide patch */
  176. unsigned N;
  177. array_t<CatmullClarkPatch,GeneralCatmullClarkPatch::SIZE> patches;
  178. patch.subdivide(patches,N); // FIXME: only have to generate one of the patches
  179. /* parametrization for quads */
  180. if (N == 4)
  181. eval_general_quad(patch,patches,uv,depth);
  182. /* parametrization for arbitrary polygons */
  183. else
  184. {
  185. const unsigned l = (unsigned) floor(0.5f*uv.x); const float u = 2.0f*frac(0.5f*uv.x)-0.5f;
  186. const unsigned h = (unsigned) floor(0.5f*uv.y); const float v = 2.0f*frac(0.5f*uv.y)-0.5f;
  187. const unsigned i = 4*h+l; assert(i<N);
  188. if (i >= N) return;
  189. #if PATCH_USE_GREGORY == 2
  190. BezierCurve borders[2]; patch.getLimitBorder(borders,i);
  191. BezierCurve border0l,border0r; borders[0].subdivide(border0l,border0r);
  192. BezierCurve border2l,border2r; borders[1].subdivide(border2l,border2r);
  193. eval(patches[i],Vec2f(u,v),1.0f,depth+1, &border0l, nullptr, nullptr, &border2r);
  194. #else
  195. eval(patches[i],Vec2f(u,v),1.0f,depth+1);
  196. #endif
  197. }
  198. }
  199. private:
  200. Vertex* const P;
  201. Vertex* const dPdu;
  202. Vertex* const dPdv;
  203. Vertex* const ddPdudu;
  204. Vertex* const ddPdvdv;
  205. Vertex* const ddPdudv;
  206. };
  207. }
  208. }