feature_adaptive_eval.h 11 KB

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